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