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.
33 #include <sys/types.h>
44 #if defined (_MSC_VER) && !defined (HAVE_DIRENT_H)
45 #include "../build/win32/dirent/dirent.h"
46 #include "../build/win32/dirent/wdirent.c"
57 gchar utf8_buf[FILENAME_MAX*4];
63 * @path: the path to the directory you are interested in. On Unix
64 * in the on-disk encoding. On Windows in UTF-8
65 * @flags: Currently must be set to 0. Reserved for future use.
66 * @error: return location for a #GError, or %NULL.
67 * If non-%NULL, an error will be set if and only if
70 * Opens a directory for reading. The names of the files in the
71 * directory can then be retrieved using g_dir_read_name().
73 * Return value: a newly allocated #GDir on success, %NULL on failure.
74 * If non-%NULL, you must free the result with g_dir_close()
75 * when you are finished with it.
78 g_dir_open (const gchar *path,
89 g_return_val_if_fail (path != NULL, NULL);
92 wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
97 dir = g_new (GDir, 1);
99 dir->wdirp = _wopendir (wpath);
109 g_file_error_from_errno (errno),
110 _("Error opening directory '%s': %s"),
111 path, g_strerror (errno));
117 dir = g_new (GDir, 1);
119 dir->dirp = opendir (path);
125 utf8_path = g_filename_to_utf8 (path, -1,
129 g_file_error_from_errno (errno),
130 _("Error opening directory '%s': %s"),
131 utf8_path, g_strerror (errno));
142 /* The above function actually is called g_dir_open_utf8, and it's
143 * that what applications compiled with this GLib version will
149 /* Binary compatibility version. Not for newly compiled code. */
152 g_dir_open (const gchar *path,
156 gchar *utf8_path = g_locale_to_utf8 (path, -1, NULL, NULL, error);
159 if (utf8_path == NULL)
162 retval = g_dir_open_utf8 (utf8_path, flags, error);
172 * @dir: a #GDir* created by g_dir_open()
174 * Retrieves the name of the next entry in the directory. The '.' and
175 * '..' entries are omitted. On Windows, the returned name is in
176 * UTF-8. On Unix, it is in the on-disk encoding.
178 * Return value: The entry's name or %NULL if there are no
179 * more entries. The return value is owned by GLib and
180 * must not be modified or freed.
182 G_CONST_RETURN gchar*
183 g_dir_read_name (GDir *dir)
187 struct _wdirent *wentry;
189 struct dirent *entry;
192 g_return_val_if_fail (dir != NULL, NULL);
197 wentry = _wreaddir (dir->wdirp);
199 && (0 == wcscmp (wentry->d_name, L".") ||
200 0 == wcscmp (wentry->d_name, L"..")))
201 wentry = _wreaddir (dir->wdirp);
206 utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);
208 if (utf8_name == NULL)
209 continue; /* Huh, impossible? Skip it anyway */
211 strcpy (dir->utf8_buf, utf8_name);
214 return dir->utf8_buf;
217 entry = readdir (dir->dirp);
219 && (0 == strcmp (entry->d_name, ".") ||
220 0 == strcmp (entry->d_name, "..")))
221 entry = readdir (dir->dirp);
224 return entry->d_name;
232 /* Ditto for g_dir_read_name */
234 #undef g_dir_read_name
236 /* Binary compatibility version. Not for newly compiled code. */
238 G_CONST_RETURN gchar*
239 g_dir_read_name (GDir *dir)
243 const gchar *utf8_name = g_dir_read_name_utf8 (dir);
246 if (utf8_name == NULL)
249 retval = g_locale_from_utf8 (utf8_name, -1, NULL, NULL, NULL);
253 strcpy (dir->utf8_buf, retval);
256 return dir->utf8_buf;
265 * @dir: a #GDir* created by g_dir_open()
267 * Resets the given directory. The next call to g_dir_read_name()
268 * will return the first entry again.
271 g_dir_rewind (GDir *dir)
273 g_return_if_fail (dir != NULL);
276 _wrewinddir (dir->wdirp);
278 rewinddir (dir->dirp);
284 * @dir: a #GDir* created by g_dir_open()
286 * Closes the directory and deallocates all related resources.
289 g_dir_close (GDir *dir)
291 g_return_if_fail (dir != NULL);
294 _wclosedir (dir->wdirp);
296 closedir (dir->dirp);
302 #include "galiasdef.c"