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