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