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