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