1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19  
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 /*
25  * Conversion parameters:
26  * inFile  = 
27  * outPack = glib
28  * outFile = MappedFile
29  * strct   = GMappedFile
30  * realStrct=
31  * ctorStrct=
32  * clss    = MappedFile
33  * interf  = 
34  * class Code: No
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_mapped_file_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * omit signals:
45  * imports:
46  * 	- glib.Str
47  * 	- glib.Bytes
48  * 	- glib.ErrorG
49  * 	- glib.GException
50  * structWrap:
51  * 	- GBytes* -> Bytes
52  * 	- GMappedFile* -> MappedFile
53  * module aliases:
54  * local aliases:
55  * overrides:
56  */
57 
58 module glib.MappedFile;
59 
60 public  import gtkc.glibtypes;
61 
62 private import gtkc.glib;
63 private import glib.ConstructionException;
64 
65 private import glib.Str;
66 private import glib.Bytes;
67 private import glib.ErrorG;
68 private import glib.GException;
69 
70 
71 
72 /**
73  * There is a group of functions which wrap the common POSIX functions
74  * dealing with filenames (g_open(), g_rename(), g_mkdir(), g_stat(),
75  * g_unlink(), g_remove(), g_fopen(), g_freopen()). The point of these
76  * wrappers is to make it possible to handle file names with any Unicode
77  * characters in them on Windows without having to use ifdefs and the
78  * wide character API in the application code.
79  *
80  * The pathname argument should be in the GLib file name encoding.
81  * On POSIX this is the actual on-disk encoding which might correspond
82  * to the locale settings of the process (or the
83  * G_FILENAME_ENCODING environment variable), or not.
84  *
85  * On Windows the GLib file name encoding is UTF-8. Note that the
86  * Microsoft C library does not use UTF-8, but has separate APIs for
87  * current system code page and wide characters (UTF-16). The GLib
88  * wrappers call the wide character API if present (on modern Windows
89  * systems), otherwise convert to/from the system code page.
90  *
91  * Another group of functions allows to open and read directories
92  * in the GLib file name encoding. These are g_dir_open(),
93  * g_dir_read_name(), g_dir_rewind(), g_dir_close().
94  */
95 public class MappedFile
96 {
97 	
98 	/** the main Gtk struct */
99 	protected GMappedFile* gMappedFile;
100 	
101 	
102 	/** Get the main Gtk struct */
103 	public GMappedFile* getMappedFileStruct()
104 	{
105 		return gMappedFile;
106 	}
107 	
108 	
109 	/** the main Gtk struct as a void* */
110 	protected void* getStruct()
111 	{
112 		return cast(void*)gMappedFile;
113 	}
114 	
115 	/**
116 	 * Sets our main struct and passes it to the parent class
117 	 */
118 	public this (GMappedFile* gMappedFile)
119 	{
120 		this.gMappedFile = gMappedFile;
121 	}
122 	
123 	/**
124 	 */
125 	
126 	/**
127 	 * Maps a file into memory. On UNIX, this is using the mmap() function.
128 	 * If writable is TRUE, the mapped buffer may be modified, otherwise
129 	 * it is an error to modify the mapped buffer. Modifications to the buffer
130 	 * are not visible to other processes mapping the same file, and are not
131 	 * written back to the file.
132 	 * Note that modifications of the underlying file might affect the contents
133 	 * of the GMappedFile. Therefore, mapping should only be used if the file
134 	 * will not be modified, or if all modifications of the file are done
135 	 * atomically (e.g. using g_file_set_contents()).
136 	 * If filename is the name of an empty, regular file, the function
137 	 * will successfully return an empty GMappedFile. In other cases of
138 	 * size 0 (e.g. device files such as /dev/null), error will be set
139 	 * to the GFileError value G_FILE_ERROR_INVAL.
140 	 * Since 2.8
141 	 * Params:
142 	 * filename = The path of the file to load, in the GLib filename encoding
143 	 * writable = whether the mapping should be writable
144 	 * Throws: GException on failure.
145 	 * Throws: ConstructionException GTK+ fails to create the object.
146 	 */
147 	public this (string filename, int writable)
148 	{
149 		// GMappedFile * g_mapped_file_new (const gchar *filename,  gboolean writable,  GError **error);
150 		GError* err = null;
151 		
152 		auto p = g_mapped_file_new(Str.toStringz(filename), writable, &err);
153 		
154 		if (err !is null)
155 		{
156 			throw new GException( new ErrorG(err) );
157 		}
158 		
159 		if(p is null)
160 		{
161 			throw new ConstructionException("null returned by g_mapped_file_new(Str.toStringz(filename), writable, &err)");
162 		}
163 		this(cast(GMappedFile*) p);
164 	}
165 	
166 	/**
167 	 * Maps a file into memory. On UNIX, this is using the mmap() function.
168 	 * If writable is TRUE, the mapped buffer may be modified, otherwise
169 	 * it is an error to modify the mapped buffer. Modifications to the buffer
170 	 * are not visible to other processes mapping the same file, and are not
171 	 * written back to the file.
172 	 * Note that modifications of the underlying file might affect the contents
173 	 * of the GMappedFile. Therefore, mapping should only be used if the file
174 	 * will not be modified, or if all modifications of the file are done
175 	 * atomically (e.g. using g_file_set_contents()).
176 	 * Since 2.32
177 	 * Params:
178 	 * fd = The file descriptor of the file to load
179 	 * writable = whether the mapping should be writable
180 	 * Throws: GException on failure.
181 	 * Throws: ConstructionException GTK+ fails to create the object.
182 	 */
183 	public this (int fd, int writable)
184 	{
185 		// GMappedFile * g_mapped_file_new_from_fd (gint fd,  gboolean writable,  GError **error);
186 		GError* err = null;
187 		
188 		auto p = g_mapped_file_new_from_fd(fd, writable, &err);
189 		
190 		if (err !is null)
191 		{
192 			throw new GException( new ErrorG(err) );
193 		}
194 		
195 		if(p is null)
196 		{
197 			throw new ConstructionException("null returned by g_mapped_file_new_from_fd(fd, writable, &err)");
198 		}
199 		this(cast(GMappedFile*) p);
200 	}
201 	
202 	/**
203 	 * Increments the reference count of file by one. It is safe to call
204 	 * this function from any thread.
205 	 * Since 2.22
206 	 * Returns: the passed in GMappedFile.
207 	 */
208 	public MappedFile doref()
209 	{
210 		// GMappedFile * g_mapped_file_ref (GMappedFile *file);
211 		auto p = g_mapped_file_ref(gMappedFile);
212 		
213 		if(p is null)
214 		{
215 			return null;
216 		}
217 		
218 		return new MappedFile(cast(GMappedFile*) p);
219 	}
220 	
221 	/**
222 	 * Decrements the reference count of file by one. If the reference count
223 	 * drops to 0, unmaps the buffer of file and frees it.
224 	 * It is safe to call this function from any thread.
225 	 * Since 2.22
226 	 */
227 	public void unref()
228 	{
229 		// void g_mapped_file_unref (GMappedFile *file);
230 		g_mapped_file_unref(gMappedFile);
231 	}
232 	
233 	/**
234 	 * Warning
235 	 * g_mapped_file_free has been deprecated since version 2.22 and should not be used in newly-written code. Use g_mapped_file_unref() instead.
236 	 * This call existed before GMappedFile had refcounting and is currently
237 	 * exactly the same as g_mapped_file_unref().
238 	 * Since 2.8
239 	 */
240 	public void free()
241 	{
242 		// void g_mapped_file_free (GMappedFile *file);
243 		g_mapped_file_free(gMappedFile);
244 	}
245 	
246 	/**
247 	 * Returns the length of the contents of a GMappedFile.
248 	 * Since 2.8
249 	 * Returns: the length of the contents of file.
250 	 */
251 	public gsize getLength()
252 	{
253 		// gsize g_mapped_file_get_length (GMappedFile *file);
254 		return g_mapped_file_get_length(gMappedFile);
255 	}
256 	
257 	/**
258 	 * Returns the contents of a GMappedFile.
259 	 * Note that the contents may not be zero-terminated,
260 	 * even if the GMappedFile is backed by a text file.
261 	 * If the file is empty then NULL is returned.
262 	 * Since 2.8
263 	 * Returns: the contents of file, or NULL.
264 	 */
265 	public string getContents()
266 	{
267 		// gchar * g_mapped_file_get_contents (GMappedFile *file);
268 		return Str.toString(g_mapped_file_get_contents(gMappedFile));
269 	}
270 	
271 	/**
272 	 * Creates a new GBytes which references the data mapped from file.
273 	 * The mapped contents of the file must not be modified after creating this
274 	 * bytes object, because a GBytes should be immutable.
275 	 * Since 2.34
276 	 * Returns: A newly allocated GBytes referencing data from file. [transfer full]
277 	 */
278 	public Bytes getBytes()
279 	{
280 		// GBytes * g_mapped_file_get_bytes (GMappedFile *file);
281 		auto p = g_mapped_file_get_bytes(gMappedFile);
282 		
283 		if(p is null)
284 		{
285 			return null;
286 		}
287 		
288 		return new Bytes(cast(GBytes*) p);
289 	}
290 }