GObject: substantially rework g_object_new()
[glib.git] / glib / gdir.c
blob0c304f6be1c81aea559bd379def465c795653148
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gdir.c: Simplified wrapper around the DIRENT functions.
6 * Copyright 2001 Hans Breuer
7 * Copyright 2004 Tor Lillqvist
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
25 #include "config.h"
27 #include <errno.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <sys/stat.h>
32 #ifdef HAVE_DIRENT_H
33 #include <sys/types.h>
34 #include <dirent.h>
35 #endif
37 #include "gdir.h"
39 #include "gconvert.h"
40 #include "gfileutils.h"
41 #include "gstrfuncs.h"
42 #include "gtestutils.h"
43 #include "glibintl.h"
45 #if defined (_MSC_VER) && !defined (HAVE_DIRENT_H)
46 #include "../build/win32/dirent/dirent.h"
47 #include "../build/win32/dirent/wdirent.c"
48 #endif
50 /**
51 * GDir:
53 * An opaque structure representing an opened directory.
56 struct _GDir
58 #ifdef G_OS_WIN32
59 _WDIR *wdirp;
60 #else
61 DIR *dirp;
62 #endif
63 #ifdef G_OS_WIN32
64 gchar utf8_buf[FILENAME_MAX*4];
65 #endif
68 /**
69 * g_dir_open:
70 * @path: the path to the directory you are interested in. On Unix
71 * in the on-disk encoding. On Windows in UTF-8
72 * @flags: Currently must be set to 0. Reserved for future use.
73 * @error: return location for a #GError, or %NULL.
74 * If non-%NULL, an error will be set if and only if
75 * g_dir_open() fails.
77 * Opens a directory for reading. The names of the files in the
78 * directory can then be retrieved using g_dir_read_name(). Note
79 * that the ordering is not defined.
81 * Return value: a newly allocated #GDir on success, %NULL on failure.
82 * If non-%NULL, you must free the result with g_dir_close()
83 * when you are finished with it.
84 **/
85 GDir *
86 g_dir_open (const gchar *path,
87 guint flags,
88 GError **error)
90 GDir *dir;
91 int errsv;
92 #ifdef G_OS_WIN32
93 wchar_t *wpath;
94 #else
95 gchar *utf8_path;
96 #endif
98 g_return_val_if_fail (path != NULL, NULL);
100 #ifdef G_OS_WIN32
101 wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
103 if (wpath == NULL)
104 return NULL;
106 dir = g_new (GDir, 1);
108 dir->wdirp = _wopendir (wpath);
109 g_free (wpath);
111 if (dir->wdirp)
112 return dir;
114 /* error case */
115 errsv = errno;
117 g_set_error (error,
118 G_FILE_ERROR,
119 g_file_error_from_errno (errsv),
120 _("Error opening directory '%s': %s"),
121 path, g_strerror (errsv));
123 g_free (dir);
125 return NULL;
126 #else
127 dir = g_new (GDir, 1);
129 dir->dirp = opendir (path);
131 if (dir->dirp)
132 return dir;
134 /* error case */
135 errsv = errno;
137 utf8_path = g_filename_to_utf8 (path, -1,
138 NULL, NULL, NULL);
140 g_set_error (error,
141 G_FILE_ERROR,
142 g_file_error_from_errno (errsv),
143 _("Error opening directory '%s': %s"),
144 utf8_path, g_strerror (errsv));
146 g_free (utf8_path);
147 g_free (dir);
149 return NULL;
150 #endif
153 #if defined (G_OS_WIN32) && !defined (_WIN64)
155 /* The above function actually is called g_dir_open_utf8, and it's
156 * that what applications compiled with this GLib version will
157 * use.
160 #undef g_dir_open
162 /* Binary compatibility version. Not for newly compiled code. */
164 GDir *
165 g_dir_open (const gchar *path,
166 guint flags,
167 GError **error)
169 gchar *utf8_path = g_locale_to_utf8 (path, -1, NULL, NULL, error);
170 GDir *retval;
172 if (utf8_path == NULL)
173 return NULL;
175 retval = g_dir_open_utf8 (utf8_path, flags, error);
177 g_free (utf8_path);
179 return retval;
181 #endif
184 * g_dir_read_name:
185 * @dir: a #GDir* created by g_dir_open()
187 * Retrieves the name of another entry in the directory, or %NULL.
188 * The order of entries returned from this function is not defined,
189 * and may vary by file system or other operating-system dependent
190 * factors.
192 * %NULL may also be returned in case of errors. On Unix, you can
193 * check <literal>errno</literal> to find out if %NULL was returned
194 * because of an error.
196 * On Unix, the '.' and '..' entries are omitted, and the returned
197 * name is in the on-disk encoding.
199 * On Windows, as is true of all GLib functions which operate on
200 * filenames, the returned name is in UTF-8.
202 * Return value: The entry's name or %NULL if there are no
203 * more entries. The return value is owned by GLib and
204 * must not be modified or freed.
206 const gchar *
207 g_dir_read_name (GDir *dir)
209 #ifdef G_OS_WIN32
210 gchar *utf8_name;
211 struct _wdirent *wentry;
212 #else
213 struct dirent *entry;
214 #endif
216 g_return_val_if_fail (dir != NULL, NULL);
218 #ifdef G_OS_WIN32
219 while (1)
221 wentry = _wreaddir (dir->wdirp);
222 while (wentry
223 && (0 == wcscmp (wentry->d_name, L".") ||
224 0 == wcscmp (wentry->d_name, L"..")))
225 wentry = _wreaddir (dir->wdirp);
227 if (wentry == NULL)
228 return NULL;
230 utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);
232 if (utf8_name == NULL)
233 continue; /* Huh, impossible? Skip it anyway */
235 strcpy (dir->utf8_buf, utf8_name);
236 g_free (utf8_name);
238 return dir->utf8_buf;
240 #else
241 entry = readdir (dir->dirp);
242 while (entry
243 && (0 == strcmp (entry->d_name, ".") ||
244 0 == strcmp (entry->d_name, "..")))
245 entry = readdir (dir->dirp);
247 if (entry)
248 return entry->d_name;
249 else
250 return NULL;
251 #endif
254 #if defined (G_OS_WIN32) && !defined (_WIN64)
256 /* Ditto for g_dir_read_name */
258 #undef g_dir_read_name
260 /* Binary compatibility version. Not for newly compiled code. */
262 const gchar *
263 g_dir_read_name (GDir *dir)
265 while (1)
267 const gchar *utf8_name = g_dir_read_name_utf8 (dir);
268 gchar *retval;
270 if (utf8_name == NULL)
271 return NULL;
273 retval = g_locale_from_utf8 (utf8_name, -1, NULL, NULL, NULL);
275 if (retval != NULL)
277 strcpy (dir->utf8_buf, retval);
278 g_free (retval);
280 return dir->utf8_buf;
285 #endif
288 * g_dir_rewind:
289 * @dir: a #GDir* created by g_dir_open()
291 * Resets the given directory. The next call to g_dir_read_name()
292 * will return the first entry again.
294 void
295 g_dir_rewind (GDir *dir)
297 g_return_if_fail (dir != NULL);
299 #ifdef G_OS_WIN32
300 _wrewinddir (dir->wdirp);
301 #else
302 rewinddir (dir->dirp);
303 #endif
307 * g_dir_close:
308 * @dir: a #GDir* created by g_dir_open()
310 * Closes the directory and deallocates all related resources.
312 void
313 g_dir_close (GDir *dir)
315 g_return_if_fail (dir != NULL);
317 #ifdef G_OS_WIN32
318 _wclosedir (dir->wdirp);
319 #else
320 closedir (dir->dirp);
321 #endif
322 g_free (dir);