Remove a bit of unused code
[glib.git] / glib / gdir.c
blob3173ae70e05b079eb65821906ed63aef0eda5dc3
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"
46 #if defined (_MSC_VER) && !defined (HAVE_DIRENT_H)
47 #include "../build/win32/dirent/dirent.h"
48 #include "../build/win32/dirent/wdirent.c"
49 #endif
51 /**
52 * GDir:
54 * An opaque structure representing an opened directory.
57 struct _GDir
59 #ifdef G_OS_WIN32
60 _WDIR *wdirp;
61 #else
62 DIR *dirp;
63 #endif
64 #ifdef G_OS_WIN32
65 gchar utf8_buf[FILENAME_MAX*4];
66 #endif
69 /**
70 * g_dir_open:
71 * @path: the path to the directory you are interested in. On Unix
72 * in the on-disk encoding. On Windows in UTF-8
73 * @flags: Currently must be set to 0. Reserved for future use.
74 * @error: return location for a #GError, or %NULL.
75 * If non-%NULL, an error will be set if and only if
76 * g_dir_open() fails.
78 * Opens a directory for reading. The names of the files in the
79 * directory can then be retrieved using g_dir_read_name(). Note
80 * that the ordering is not defined.
82 * Return value: a newly allocated #GDir on success, %NULL on failure.
83 * If non-%NULL, you must free the result with g_dir_close()
84 * when you are finished with it.
85 **/
86 GDir *
87 g_dir_open (const gchar *path,
88 guint flags,
89 GError **error)
91 GDir *dir;
92 int errsv;
93 #ifdef G_OS_WIN32
94 wchar_t *wpath;
95 #else
96 gchar *utf8_path;
97 #endif
99 g_return_val_if_fail (path != NULL, NULL);
101 #ifdef G_OS_WIN32
102 wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
104 if (wpath == NULL)
105 return NULL;
107 dir = g_new (GDir, 1);
109 dir->wdirp = _wopendir (wpath);
110 g_free (wpath);
112 if (dir->wdirp)
113 return dir;
115 /* error case */
116 errsv = errno;
118 g_set_error (error,
119 G_FILE_ERROR,
120 g_file_error_from_errno (errsv),
121 _("Error opening directory '%s': %s"),
122 path, g_strerror (errsv));
124 g_free (dir);
126 return NULL;
127 #else
128 dir = g_new (GDir, 1);
130 dir->dirp = opendir (path);
132 if (dir->dirp)
133 return dir;
135 /* error case */
136 errsv = errno;
138 utf8_path = g_filename_to_utf8 (path, -1,
139 NULL, NULL, NULL);
141 g_set_error (error,
142 G_FILE_ERROR,
143 g_file_error_from_errno (errsv),
144 _("Error opening directory '%s': %s"),
145 utf8_path, g_strerror (errsv));
147 g_free (utf8_path);
148 g_free (dir);
150 return NULL;
151 #endif
154 #if defined (G_OS_WIN32) && !defined (_WIN64)
156 /* The above function actually is called g_dir_open_utf8, and it's
157 * that what applications compiled with this GLib version will
158 * use.
161 #undef g_dir_open
163 /* Binary compatibility version. Not for newly compiled code. */
165 GDir *
166 g_dir_open (const gchar *path,
167 guint flags,
168 GError **error)
170 gchar *utf8_path = g_locale_to_utf8 (path, -1, NULL, NULL, error);
171 GDir *retval;
173 if (utf8_path == NULL)
174 return NULL;
176 retval = g_dir_open_utf8 (utf8_path, flags, error);
178 g_free (utf8_path);
180 return retval;
182 #endif
185 * g_dir_read_name:
186 * @dir: a #GDir* created by g_dir_open()
188 * Retrieves the name of another entry in the directory, or %NULL.
189 * The order of entries returned from this function is not defined,
190 * and may vary by file system or other operating-system dependent
191 * factors.
193 * On Unix, the '.' and '..' entries are omitted, and the returned
194 * name is in the on-disk encoding.
196 * On Windows, as is true of all GLib functions which operate on
197 * filenames, the returned name is in UTF-8.
199 * Return value: The entry's name or %NULL if there are no
200 * more entries. The return value is owned by GLib and
201 * must not be modified or freed.
203 const gchar *
204 g_dir_read_name (GDir *dir)
206 #ifdef G_OS_WIN32
207 gchar *utf8_name;
208 struct _wdirent *wentry;
209 #else
210 struct dirent *entry;
211 #endif
213 g_return_val_if_fail (dir != NULL, NULL);
215 #ifdef G_OS_WIN32
216 while (1)
218 wentry = _wreaddir (dir->wdirp);
219 while (wentry
220 && (0 == wcscmp (wentry->d_name, L".") ||
221 0 == wcscmp (wentry->d_name, L"..")))
222 wentry = _wreaddir (dir->wdirp);
224 if (wentry == NULL)
225 return NULL;
227 utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);
229 if (utf8_name == NULL)
230 continue; /* Huh, impossible? Skip it anyway */
232 strcpy (dir->utf8_buf, utf8_name);
233 g_free (utf8_name);
235 return dir->utf8_buf;
237 #else
238 entry = readdir (dir->dirp);
239 while (entry
240 && (0 == strcmp (entry->d_name, ".") ||
241 0 == strcmp (entry->d_name, "..")))
242 entry = readdir (dir->dirp);
244 if (entry)
245 return entry->d_name;
246 else
247 return NULL;
248 #endif
251 #if defined (G_OS_WIN32) && !defined (_WIN64)
253 /* Ditto for g_dir_read_name */
255 #undef g_dir_read_name
257 /* Binary compatibility version. Not for newly compiled code. */
259 const gchar *
260 g_dir_read_name (GDir *dir)
262 while (1)
264 const gchar *utf8_name = g_dir_read_name_utf8 (dir);
265 gchar *retval;
267 if (utf8_name == NULL)
268 return NULL;
270 retval = g_locale_from_utf8 (utf8_name, -1, NULL, NULL, NULL);
272 if (retval != NULL)
274 strcpy (dir->utf8_buf, retval);
275 g_free (retval);
277 return dir->utf8_buf;
282 #endif
285 * g_dir_rewind:
286 * @dir: a #GDir* created by g_dir_open()
288 * Resets the given directory. The next call to g_dir_read_name()
289 * will return the first entry again.
291 void
292 g_dir_rewind (GDir *dir)
294 g_return_if_fail (dir != NULL);
296 #ifdef G_OS_WIN32
297 _wrewinddir (dir->wdirp);
298 #else
299 rewinddir (dir->dirp);
300 #endif
304 * g_dir_close:
305 * @dir: a #GDir* created by g_dir_open()
307 * Closes the directory and deallocates all related resources.
309 void
310 g_dir_close (GDir *dir)
312 g_return_if_fail (dir != NULL);
314 #ifdef G_OS_WIN32
315 _wclosedir (dir->wdirp);
316 #else
317 closedir (dir->dirp);
318 #endif
319 g_free (dir);