source: appstream-generator/build/girepo/glib/MappedFile.d @ 4841

Last change on this file since 4841 was 4841, checked in by Juanma, 2 years ago

Initial release

File size: 6.6 KB
Line 
1/*
2 * Licensed under the GNU Lesser General Public License Version 3
3 *
4 * This library is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the license, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18// generated automatically - do not change
19
20
21module glib.MappedFile;
22
23private import gi.glib;
24public  import gi.glibtypes;
25private import glib.Bytes;
26private import glib.ConstructionException;
27private import glib.ErrorG;
28private import glib.GException;
29private import glib.Str;
30
31
32/**
33 * The #GMappedFile represents a file mapping created with
34 * g_mapped_file_new(). It has only private members and should
35 * not be accessed directly.
36 */
37public class MappedFile
38{
39        /** the main Gtk struct */
40        protected GMappedFile* gMappedFile;
41        protected bool ownedRef;
42
43        /** Get the main Gtk struct */
44        public GMappedFile* getMappedFileStruct()
45        {
46                return gMappedFile;
47        }
48
49        /** the main Gtk struct as a void* */
50        protected void* getStruct()
51        {
52                return cast(void*)gMappedFile;
53        }
54
55        /**
56         * Sets our main struct and passes it to the parent class.
57         */
58        public this (GMappedFile* gMappedFile, bool ownedRef = false)
59        {
60                this.gMappedFile = gMappedFile;
61                this.ownedRef = ownedRef;
62        }
63
64
65        /**
66         * Maps a file into memory. On UNIX, this is using the mmap() function.
67         *
68         * If @writable is %TRUE, the mapped buffer may be modified, otherwise
69         * it is an error to modify the mapped buffer. Modifications to the buffer
70         * are not visible to other processes mapping the same file, and are not
71         * written back to the file.
72         *
73         * Note that modifications of the underlying file might affect the contents
74         * of the #GMappedFile. Therefore, mapping should only be used if the file
75         * will not be modified, or if all modifications of the file are done
76         * atomically (e.g. using g_file_set_contents()).
77         *
78         * If @filename is the name of an empty, regular file, the function
79         * will successfully return an empty #GMappedFile. In other cases of
80         * size 0 (e.g. device files such as /dev/null), @error will be set
81         * to the #GFileError value #G_FILE_ERROR_INVAL.
82         *
83         * Params:
84         *     filename = The path of the file to load, in the GLib filename encoding
85         *     writable = whether the mapping should be writable
86         *
87         * Returns: a newly allocated #GMappedFile which must be unref'd
88         *     with g_mapped_file_unref(), or %NULL if the mapping failed.
89         *
90         * Since: 2.8
91         *
92         * Throws: GException on failure.
93         * Throws: ConstructionException GTK+ fails to create the object.
94         */
95        public this(string filename, bool writable)
96        {
97                GError* err = null;
98               
99                auto p = g_mapped_file_new(Str.toStringz(filename), writable, &err);
100               
101                if (err !is null)
102                {
103                        throw new GException( new ErrorG(err) );
104                }
105               
106                if(p is null)
107                {
108                        throw new ConstructionException("null returned by new");
109                }
110               
111                this(cast(GMappedFile*) p);
112        }
113
114        /**
115         * Maps a file into memory. On UNIX, this is using the mmap() function.
116         *
117         * If @writable is %TRUE, the mapped buffer may be modified, otherwise
118         * it is an error to modify the mapped buffer. Modifications to the buffer
119         * are not visible to other processes mapping the same file, and are not
120         * written back to the file.
121         *
122         * Note that modifications of the underlying file might affect the contents
123         * of the #GMappedFile. Therefore, mapping should only be used if the file
124         * will not be modified, or if all modifications of the file are done
125         * atomically (e.g. using g_file_set_contents()).
126         *
127         * Params:
128         *     fd = The file descriptor of the file to load
129         *     writable = whether the mapping should be writable
130         *
131         * Returns: a newly allocated #GMappedFile which must be unref'd
132         *     with g_mapped_file_unref(), or %NULL if the mapping failed.
133         *
134         * Since: 2.32
135         *
136         * Throws: GException on failure.
137         * Throws: ConstructionException GTK+ fails to create the object.
138         */
139        public this(int fd, bool writable)
140        {
141                GError* err = null;
142               
143                auto p = g_mapped_file_new_from_fd(fd, writable, &err);
144               
145                if (err !is null)
146                {
147                        throw new GException( new ErrorG(err) );
148                }
149               
150                if(p is null)
151                {
152                        throw new ConstructionException("null returned by new_from_fd");
153                }
154               
155                this(cast(GMappedFile*) p);
156        }
157
158        /**
159         * This call existed before #GMappedFile had refcounting and is currently
160         * exactly the same as g_mapped_file_unref().
161         *
162         * Deprecated: Use g_mapped_file_unref() instead.
163         *
164         * Since: 2.8
165         */
166        public void free()
167        {
168                g_mapped_file_free(gMappedFile);
169        }
170
171        /**
172         * Creates a new #GBytes which references the data mapped from @file.
173         * The mapped contents of the file must not be modified after creating this
174         * bytes object, because a #GBytes should be immutable.
175         *
176         * Returns: A newly allocated #GBytes referencing data
177         *     from @file
178         *
179         * Since: 2.34
180         */
181        public Bytes getBytes()
182        {
183                auto p = g_mapped_file_get_bytes(gMappedFile);
184               
185                if(p is null)
186                {
187                        return null;
188                }
189               
190                return new Bytes(cast(GBytes*) p, true);
191        }
192
193        /**
194         * Returns the contents of a #GMappedFile.
195         *
196         * Note that the contents may not be zero-terminated,
197         * even if the #GMappedFile is backed by a text file.
198         *
199         * If the file is empty then %NULL is returned.
200         *
201         * Returns: the contents of @file, or %NULL.
202         *
203         * Since: 2.8
204         */
205        public string getContents()
206        {
207                auto retStr = g_mapped_file_get_contents(gMappedFile);
208               
209                scope(exit) Str.freeString(retStr);
210                return Str.toString(retStr);
211        }
212
213        /**
214         * Returns the length of the contents of a #GMappedFile.
215         *
216         * Returns: the length of the contents of @file.
217         *
218         * Since: 2.8
219         */
220        public size_t getLength()
221        {
222                return g_mapped_file_get_length(gMappedFile);
223        }
224
225        /**
226         * Increments the reference count of @file by one.  It is safe to call
227         * this function from any thread.
228         *
229         * Returns: the passed in #GMappedFile.
230         *
231         * Since: 2.22
232         */
233        public MappedFile doref()
234        {
235                auto p = g_mapped_file_ref(gMappedFile);
236               
237                if(p is null)
238                {
239                        return null;
240                }
241               
242                return new MappedFile(cast(GMappedFile*) p, true);
243        }
244
245        /**
246         * Decrements the reference count of @file by one.  If the reference count
247         * drops to 0, unmaps the buffer of @file and frees it.
248         *
249         * It is safe to call this function from any thread.
250         *
251         * Since 2.22
252         */
253        public void unref()
254        {
255                g_mapped_file_unref(gMappedFile);
256        }
257}
Note: See TracBrowser for help on using the repository browser.