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 
66 private import glib.Str;
67 private import glib.Bytes;
68 private import glib.ErrorG;
69 private import glib.GException;
70 
71 
72 
73 
74 /**
75  * There is a group of functions which wrap the common POSIX functions
76  * dealing with filenames (g_open(), g_rename(), g_mkdir(), g_stat(),
77  * g_unlink(), g_remove(), g_fopen(), g_freopen()). The point of these
78  * wrappers is to make it possible to handle file names with any Unicode
79  * characters in them on Windows without having to use ifdefs and the
80  * wide character API in the application code.
81  *
82  * The pathname argument should be in the GLib file name encoding.
83  * On POSIX this is the actual on-disk encoding which might correspond
84  * to the locale settings of the process (or the
85  * G_FILENAME_ENCODING environment variable), or not.
86  *
87  * On Windows the GLib file name encoding is UTF-8. Note that the
88  * Microsoft C library does not use UTF-8, but has separate APIs for
89  * current system code page and wide characters (UTF-16). The GLib
90  * wrappers call the wide character API if present (on modern Windows
91  * systems), otherwise convert to/from the system code page.
92  *
93  * Another group of functions allows to open and read directories
94  * in the GLib file name encoding. These are g_dir_open(),
95  * g_dir_read_name(), g_dir_rewind(), g_dir_close().
96  */
97 public class MappedFile
98 {
99 	
100 	/** the main Gtk struct */
101 	protected GMappedFile* gMappedFile;
102 	
103 	
104 	public GMappedFile* getMappedFileStruct()
105 	{
106 		return gMappedFile;
107 	}
108 	
109 	
110 	/** the main Gtk struct as a void* */
111 	protected void* getStruct()
112 	{
113 		return cast(void*)gMappedFile;
114 	}
115 	
116 	/**
117 	 * Sets our main struct and passes it to the parent class
118 	 */
119 	public this (GMappedFile* gMappedFile)
120 	{
121 		this.gMappedFile = gMappedFile;
122 	}
123 	
124 	/**
125 	 */
126 	
127 	/**
128 	 * Maps a file into memory. On UNIX, this is using the mmap() function.
129 	 * If writable is TRUE, the mapped buffer may be modified, otherwise
130 	 * it is an error to modify the mapped buffer. Modifications to the buffer
131 	 * are not visible to other processes mapping the same file, and are not
132 	 * written back to the file.
133 	 * Note that modifications of the underlying file might affect the contents
134 	 * of the GMappedFile. Therefore, mapping should only be used if the file
135 	 * will not be modified, or if all modifications of the file are done
136 	 * atomically (e.g. using g_file_set_contents()).
137 	 * If filename is the name of an empty, regular file, the function
138 	 * will successfully return an empty GMappedFile. In other cases of
139 	 * size 0 (e.g. device files such as /dev/null), error will be set
140 	 * to the GFileError value G_FILE_ERROR_INVAL.
141 	 * Since 2.8
142 	 * Params:
143 	 * filename = The path of the file to load, in the GLib filename encoding
144 	 * writable = whether the mapping should be writable
145 	 * Throws: GException on failure.
146 	 * Throws: ConstructionException GTK+ fails to create the object.
147 	 */
148 	public this (string filename, int writable)
149 	{
150 		// GMappedFile * g_mapped_file_new (const gchar *filename,  gboolean writable,  GError **error);
151 		GError* err = null;
152 		
153 		auto p = g_mapped_file_new(Str.toStringz(filename), writable, &err);
154 		
155 		if (err !is null)
156 		{
157 			throw new GException( new ErrorG(err) );
158 		}
159 		
160 		if(p is null)
161 		{
162 			throw new ConstructionException("null returned by g_mapped_file_new(Str.toStringz(filename), writable, &err)");
163 		}
164 		this(cast(GMappedFile*) p);
165 	}
166 	
167 	/**
168 	 * Maps a file into memory. On UNIX, this is using the mmap() function.
169 	 * If writable is TRUE, the mapped buffer may be modified, otherwise
170 	 * it is an error to modify the mapped buffer. Modifications to the buffer
171 	 * are not visible to other processes mapping the same file, and are not
172 	 * written back to the file.
173 	 * Note that modifications of the underlying file might affect the contents
174 	 * of the GMappedFile. Therefore, mapping should only be used if the file
175 	 * will not be modified, or if all modifications of the file are done
176 	 * atomically (e.g. using g_file_set_contents()).
177 	 * Since 2.32
178 	 * Params:
179 	 * fd = The file descriptor of the file to load
180 	 * writable = whether the mapping should be writable
181 	 * Throws: GException on failure.
182 	 * Throws: ConstructionException GTK+ fails to create the object.
183 	 */
184 	public this (int fd, int writable)
185 	{
186 		// GMappedFile * g_mapped_file_new_from_fd (gint fd,  gboolean writable,  GError **error);
187 		GError* err = null;
188 		
189 		auto p = g_mapped_file_new_from_fd(fd, writable, &err);
190 		
191 		if (err !is null)
192 		{
193 			throw new GException( new ErrorG(err) );
194 		}
195 		
196 		if(p is null)
197 		{
198 			throw new ConstructionException("null returned by g_mapped_file_new_from_fd(fd, writable, &err)");
199 		}
200 		this(cast(GMappedFile*) p);
201 	}
202 	
203 	/**
204 	 * Increments the reference count of file by one. It is safe to call
205 	 * this function from any thread.
206 	 * Since 2.22
207 	 * Returns: the passed in GMappedFile.
208 	 */
209 	public MappedFile doref()
210 	{
211 		// GMappedFile * g_mapped_file_ref (GMappedFile *file);
212 		auto p = g_mapped_file_ref(gMappedFile);
213 		
214 		if(p is null)
215 		{
216 			return null;
217 		}
218 		
219 		return new MappedFile(cast(GMappedFile*) p);
220 	}
221 	
222 	/**
223 	 * Decrements the reference count of file by one. If the reference count
224 	 * drops to 0, unmaps the buffer of file and frees it.
225 	 * It is safe to call this function from any thread.
226 	 * Since 2.22
227 	 */
228 	public void unref()
229 	{
230 		// void g_mapped_file_unref (GMappedFile *file);
231 		g_mapped_file_unref(gMappedFile);
232 	}
233 	
234 	/**
235 	 * Warning
236 	 * 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.
237 	 * This call existed before GMappedFile had refcounting and is currently
238 	 * exactly the same as g_mapped_file_unref().
239 	 * Since 2.8
240 	 */
241 	public void free()
242 	{
243 		// void g_mapped_file_free (GMappedFile *file);
244 		g_mapped_file_free(gMappedFile);
245 	}
246 	
247 	/**
248 	 * Returns the length of the contents of a GMappedFile.
249 	 * Since 2.8
250 	 * Returns: the length of the contents of file.
251 	 */
252 	public gsize getLength()
253 	{
254 		// gsize g_mapped_file_get_length (GMappedFile *file);
255 		return g_mapped_file_get_length(gMappedFile);
256 	}
257 	
258 	/**
259 	 * Returns the contents of a GMappedFile.
260 	 * Note that the contents may not be zero-terminated,
261 	 * even if the GMappedFile is backed by a text file.
262 	 * If the file is empty then NULL is returned.
263 	 * Since 2.8
264 	 * Returns: the contents of file, or NULL.
265 	 */
266 	public string getContents()
267 	{
268 		// gchar * g_mapped_file_get_contents (GMappedFile *file);
269 		return Str.toString(g_mapped_file_get_contents(gMappedFile));
270 	}
271 	
272 	/**
273 	 * Creates a new GBytes which references the data mapped from file.
274 	 * The mapped contents of the file must not be modified after creating this
275 	 * bytes object, because a GBytes should be immutable.
276 	 * Since 2.34
277 	 * Returns: A newly allocated GBytes referencing data from file. [transfer full]
278 	 */
279 	public Bytes getBytes()
280 	{
281 		// GBytes * g_mapped_file_get_bytes (GMappedFile *file);
282 		auto p = g_mapped_file_get_bytes(gMappedFile);
283 		
284 		if(p is null)
285 		{
286 			return null;
287 		}
288 		
289 		return new Bytes(cast(GBytes*) p);
290 	}
291 }