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  * Conversion parameters:
26  * inFile  = glib-File-Utilities.html
27  * outPack = glib
28  * outFile = FileUtils
29  * strct   = 
30  * realStrct=
31  * ctorStrct=
32  * clss    = FileUtils
33  * interf  = 
34  * class Code: No
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_
41  * omit structs:
42  * omit prefixes:
43  * 	- g_dir_
44  * 	- g_mapped_file_
45  * omit code:
46  * omit signals:
47  * imports:
48  * 	- std.c.stdio
49  * 	- glib.Str
50  * 	- glib.ErrorG
51  * 	- glib.GException
52  * structWrap:
53  * module aliases:
54  * local aliases:
55  * overrides:
56  */
57 
58 module glib.FileUtils;
59 
60 public  import gtkc.glibtypes;
61 
62 private import gtkc.glib;
63 private import glib.ConstructionException;
64 
65 
66 private import glib.Str;
67 private import glib.ErrorG;
68 private import glib.GException;
69 
70 
71 version(Tango) {
72 	private import tango.stdc.stdio;
73 } else {
74 	private import std.c.stdio;
75 }
76 
77 
78 
79 /**
80  * Description
81  * There is a group of functions which wrap the common POSIX functions
82  * dealing with filenames (g_open(), g_rename(), g_mkdir(), g_stat(),
83  * g_unlink(), g_remove(), g_fopen(), g_freopen()). The point of these
84  * wrappers is to make it possible to handle file names with any Unicode
85  * characters in them on Windows without having to use ifdefs and the
86  * wide character API in the application code.
87  * The pathname argument should be in the GLib file name encoding. On
88  * POSIX this is the actual on-disk encoding which might correspond to
89  * the locale settings of the process (or the
90  * G_FILENAME_ENCODING environment variable), or not.
91  * On Windows the GLib file name encoding is UTF-8. Note that the
92  * Microsoft C library does not use UTF-8, but has separate APIs for
93  * current system code page and wide characters (UTF-16). The GLib
94  * wrappers call the wide character API if present (on modern Windows
95  * systems), otherwise convert to/from the system code page.
96  * Another group of functions allows to open and read directories
97  * in the GLib file name encoding. These are g_dir_open(),
98  * g_dir_read_name(), g_dir_rewind(), g_dir_close().
99  */
100 public class FileUtils
101 {
102 	
103 	/**
104 	 */
105 	
106 	/**
107 	 * Gets a GFileError constant based on the passed-in errno.
108 	 * For example, if you pass in EEXIST this function returns
109 	 * G_FILE_ERROR_EXIST. Unlike errno values, you can portably
110 	 * assume that all GFileError values will exist.
111 	 * Normally a GFileError value goes into a GError returned
112 	 * from a function that manipulates files. So you would use
113 	 * g_file_error_from_errno() when constructing a GError.
114 	 * Params:
115 	 * errNo = an "errno" value
116 	 * Returns: GFileError corresponding to the given errno
117 	 */
118 	public static GFileError fileErrorFromErrno(int errNo)
119 	{
120 		// GFileError g_file_error_from_errno (gint err_no);
121 		return g_file_error_from_errno(errNo);
122 	}
123 	
124 	/**
125 	 * Writes all of contents to a file named filename, with good error checking.
126 	 * If a file called filename already exists it will be overwritten.
127 	 * This write is atomic in the sense that it is first written to a temporary
128 	 * Since 2.8
129 	 * Params:
130 	 * filename = name of a file to write contents to, in the GLib file name
131 	 * encoding
132 	 * contents = string to write to the file
133 	 * length = length of contents, or -1 if contents is a nul-terminated string
134 	 * Returns: TRUE on success, FALSE if an error occurred
135 	 * Throws: GException on failure.
136 	 */
137 	public static int fileSetContents(string filename, string contents, gssize length)
138 	{
139 		// gboolean g_file_set_contents (const gchar *filename,  const gchar *contents,  gssize length,  GError **error);
140 		GError* err = null;
141 		
142 		auto p = g_file_set_contents(Str.toStringz(filename), Str.toStringz(contents), length, &err);
143 		
144 		if (err !is null)
145 		{
146 			throw new GException( new ErrorG(err) );
147 		}
148 		
149 		return p;
150 	}
151 	
152 	/**
153 	 * Opens a temporary file. See the mkstemp() documentation
154 	 * on most UNIX-like systems.
155 	 * The parameter is a string that should follow the rules for
156 	 * mkstemp() templates, i.e. contain the string "XXXXXX".
157 	 * g_mkstemp_full() is slightly more flexible than mkstemp()
158 	 * in that the sequence does not have to occur at the very end of the
159 	 * template and you can pass a mode and additional flags. The X
160 	 * string will be modified to form the name of a file that didn't exist.
161 	 * The string should be in the GLib file name encoding. Most importantly,
162 	 * on Windows it should be in UTF-8.
163 	 * Since 2.22
164 	 * Params:
165 	 * tmpl = template filename
166 	 * flags = flags to pass to an open() call in addition to O_EXCL and
167 	 * O_CREAT, which are passed automatically
168 	 * mode = permissios to create the temporary file with
169 	 * Returns: A file handle (as from open()) to the file opened for reading and writing. The file handle should be closed with close(). In case of errors, -1 is returned.
170 	 */
171 	public static int mkstempFull(string tmpl, int flags, int mode)
172 	{
173 		// gint g_mkstemp_full (gchar *tmpl,  int flags,  int mode);
174 		return g_mkstemp_full(Str.toStringz(tmpl), flags, mode);
175 	}
176 	
177 	/**
178 	 * Reads the contents of the symbolic link filename like the POSIX
179 	 * readlink() function. The returned string is in the encoding used
180 	 * for filenames. Use g_filename_to_utf8() to convert it to UTF-8.
181 	 * Since 2.4
182 	 * Params:
183 	 * filename = the symbolic link
184 	 * Returns: A newly-allocated string with the contents of the symbolic link, or NULL if an error occurred.
185 	 * Throws: GException on failure.
186 	 */
187 	public static string fileReadLink(string filename)
188 	{
189 		// gchar * g_file_read_link (const gchar *filename,  GError **error);
190 		GError* err = null;
191 		
192 		auto p = g_file_read_link(Str.toStringz(filename), &err);
193 		
194 		if (err !is null)
195 		{
196 			throw new GException( new ErrorG(err) );
197 		}
198 		
199 		return Str.toString(p);
200 	}
201 	
202 	/**
203 	 * Create a directory if it doesn't already exist. Create intermediate
204 	 * parent directories as needed, too.
205 	 * Since 2.8
206 	 * Params:
207 	 * pathname = a pathname in the GLib file name encoding
208 	 * mode = permissions to use for newly created directories
209 	 * Returns: 0 if the directory already exists, or was successfully created. Returns -1 if an error occurred, with errno set.
210 	 */
211 	public static int mkdirWithParents(string pathname, int mode)
212 	{
213 		// int g_mkdir_with_parents (const gchar *pathname,  int mode);
214 		return g_mkdir_with_parents(Str.toStringz(pathname), mode);
215 	}
216 	
217 	/**
218 	 * A wrapper for the POSIX unlink() function. The unlink() function
219 	 * deletes a name from the filesystem. If this was the last link to the
220 	 * file and no processes have it opened, the diskspace occupied by the
221 	 * file is freed.
222 	 * See your C library manual for more details about unlink(). Note
223 	 * that on Windows, it is in general not possible to delete files that
224 	 * are open to some process, or mapped into memory.
225 	 * Since 2.6
226 	 * Params:
227 	 * filename = a pathname in the GLib file name encoding (UTF-8 on Windows)
228 	 * Returns: 0 if the name was successfully deleted, -1 if an error occurred
229 	 */
230 	public static int unlink(string filename)
231 	{
232 		// int g_unlink (const gchar *filename);
233 		return g_unlink(Str.toStringz(filename));
234 	}
235 	
236 	/**
237 	 * A wrapper for the POSIX rmdir() function. The rmdir() function
238 	 * deletes a directory from the filesystem.
239 	 * See your C library manual for more details about how rmdir() works
240 	 * on your system.
241 	 * Since 2.6
242 	 * Params:
243 	 * filename = a pathname in the GLib file name encoding (UTF-8 on Windows)
244 	 * Returns: 0 if the directory was successfully removed, -1 if an error occurred
245 	 */
246 	public static int rmdir(string filename)
247 	{
248 		// int g_rmdir (const gchar *filename);
249 		return g_rmdir(Str.toStringz(filename));
250 	}
251 	
252 	/**
253 	 * A wrapper for the POSIX access() function. This function is used to
254 	 * test a pathname for one or several of read, write or execute
255 	 * permissions, or just existence.
256 	 * On Windows, the file protection mechanism is not at all POSIX-like,
257 	 * and the underlying function in the C library only checks the
258 	 * FAT-style READONLY attribute, and does not look at the ACL of a
259 	 * file at all. This function is this in practise almost useless on
260 	 * Windows. Software that needs to handle file permissions on Windows
261 	 * more exactly should use the Win32 API.
262 	 * See your C library manual for more details about access().
263 	 * Since 2.8
264 	 * Params:
265 	 * filename = a pathname in the GLib file name encoding (UTF-8 on Windows)
266 	 * mode = as in access()
267 	 * Returns: zero if the pathname refers to an existing file system object that has all the tested permissions, or -1 otherwise or on error.
268 	 */
269 	public static int access(string filename, int mode)
270 	{
271 		// int g_access (const gchar *filename,  int mode);
272 		return g_access(Str.toStringz(filename), mode);
273 	}
274 	
275 	/**
276 	 * A wrapper for the POSIX chdir() function. The function changes the
277 	 * current directory of the process to path.
278 	 * See your C library manual for more details about chdir().
279 	 * Since 2.8
280 	 * Params:
281 	 * path = a pathname in the GLib file name encoding (UTF-8 on Windows)
282 	 * Returns: 0 on success, -1 if an error occurred.
283 	 */
284 	public static int chdir(string path)
285 	{
286 		// int g_chdir (const gchar *path);
287 		return g_chdir(Str.toStringz(path));
288 	}
289 }