Improve the g_dir_read_name documentation
[platform/upstream/glib.git] / glib / gdir.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * gdir.c: Simplified wrapper around the DIRENT functions.
5  *
6  * Copyright 2001 Hans Breuer
7  * Copyright 2004 Tor Lillqvist
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 #include "config.h"
26
27 #include <errno.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <sys/stat.h>
31
32 #ifdef HAVE_DIRENT_H
33 #include <sys/types.h>
34 #include <dirent.h>
35 #endif
36
37 #include "gdir.h"
38
39 #include "gconvert.h"
40 #include "gfileutils.h"
41 #include "gstrfuncs.h"
42 #include "gtestutils.h"
43 #include "glibintl.h"
44
45
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
50
51 /**
52  * GDir:
53  *
54  * An opaque structure representing an opened directory.
55  */
56
57 struct _GDir
58 {
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
67 };
68
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.
77  *
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.
81  *
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)
90 {
91   GDir *dir;
92   int errsv;
93 #ifdef G_OS_WIN32
94   wchar_t *wpath;
95 #else
96   gchar *utf8_path;
97 #endif
98
99   g_return_val_if_fail (path != NULL, NULL);
100
101 #ifdef G_OS_WIN32
102   wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
103
104   if (wpath == NULL)
105     return NULL;
106
107   dir = g_new (GDir, 1);
108
109   dir->wdirp = _wopendir (wpath);
110   g_free (wpath);
111
112   if (dir->wdirp)
113     return dir;
114
115   /* error case */
116   errsv = errno;
117
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));
123   
124   g_free (dir);
125       
126   return NULL;
127 #else
128   dir = g_new (GDir, 1);
129
130   dir->dirp = opendir (path);
131
132   if (dir->dirp)
133     return dir;
134
135   /* error case */
136   errsv = errno;
137
138   utf8_path = g_filename_to_utf8 (path, -1,
139                                   NULL, NULL, NULL);
140
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));
146
147   g_free (utf8_path);
148   g_free (dir);
149
150   return NULL;
151 #endif
152 }
153
154 #if defined (G_OS_WIN32) && !defined (_WIN64)
155
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.
159  */
160
161 #undef g_dir_open
162
163 /* Binary compatibility version. Not for newly compiled code. */
164
165 GDir *
166 g_dir_open (const gchar  *path,
167             guint         flags,
168             GError      **error)
169 {
170   gchar *utf8_path = g_locale_to_utf8 (path, -1, NULL, NULL, error);
171   GDir *retval;
172
173   if (utf8_path == NULL)
174     return NULL;
175
176   retval = g_dir_open_utf8 (utf8_path, flags, error);
177
178   g_free (utf8_path);
179
180   return retval;
181 }
182 #endif
183
184 /**
185  * g_dir_read_name:
186  * @dir: a #GDir* created by g_dir_open()
187  *
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.
192  *
193  * %NULL may also be returned in case of errors. On Unix, you can
194  * check <literal>errno</literal> to find out if %NULL was returned
195  * because of an error.
196  *
197  * On Unix, the '.' and '..' entries are omitted, and the returned
198  * name is in the on-disk encoding.
199  *
200  * On Windows, as is true of all GLib functions which operate on
201  * filenames, the returned name is in UTF-8.
202  *
203  * Return value: The entry's name or %NULL if there are no
204  *   more entries. The return value is owned by GLib and
205  *   must not be modified or freed.
206  **/
207 const gchar *
208 g_dir_read_name (GDir *dir)
209 {
210 #ifdef G_OS_WIN32
211   gchar *utf8_name;
212   struct _wdirent *wentry;
213 #else
214   struct dirent *entry;
215 #endif
216
217   g_return_val_if_fail (dir != NULL, NULL);
218
219 #ifdef G_OS_WIN32
220   while (1)
221     {
222       wentry = _wreaddir (dir->wdirp);
223       while (wentry 
224              && (0 == wcscmp (wentry->d_name, L".") ||
225                  0 == wcscmp (wentry->d_name, L"..")))
226         wentry = _wreaddir (dir->wdirp);
227
228       if (wentry == NULL)
229         return NULL;
230
231       utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);
232
233       if (utf8_name == NULL)
234         continue;               /* Huh, impossible? Skip it anyway */
235
236       strcpy (dir->utf8_buf, utf8_name);
237       g_free (utf8_name);
238
239       return dir->utf8_buf;
240     }
241 #else
242   entry = readdir (dir->dirp);
243   while (entry 
244          && (0 == strcmp (entry->d_name, ".") ||
245              0 == strcmp (entry->d_name, "..")))
246     entry = readdir (dir->dirp);
247
248   if (entry)
249     return entry->d_name;
250   else
251     return NULL;
252 #endif
253 }
254
255 #if defined (G_OS_WIN32) && !defined (_WIN64)
256
257 /* Ditto for g_dir_read_name */
258
259 #undef g_dir_read_name
260
261 /* Binary compatibility version. Not for newly compiled code. */
262
263 const gchar *
264 g_dir_read_name (GDir *dir)
265 {
266   while (1)
267     {
268       const gchar *utf8_name = g_dir_read_name_utf8 (dir);
269       gchar *retval;
270       
271       if (utf8_name == NULL)
272         return NULL;
273
274       retval = g_locale_from_utf8 (utf8_name, -1, NULL, NULL, NULL);
275
276       if (retval != NULL)
277         {
278           strcpy (dir->utf8_buf, retval);
279           g_free (retval);
280
281           return dir->utf8_buf;
282         }
283     }
284 }
285
286 #endif
287
288 /**
289  * g_dir_rewind:
290  * @dir: a #GDir* created by g_dir_open()
291  *
292  * Resets the given directory. The next call to g_dir_read_name()
293  * will return the first entry again.
294  **/
295 void
296 g_dir_rewind (GDir *dir)
297 {
298   g_return_if_fail (dir != NULL);
299   
300 #ifdef G_OS_WIN32
301   _wrewinddir (dir->wdirp);
302 #else
303   rewinddir (dir->dirp);
304 #endif
305 }
306
307 /**
308  * g_dir_close:
309  * @dir: a #GDir* created by g_dir_open()
310  *
311  * Closes the directory and deallocates all related resources.
312  **/
313 void
314 g_dir_close (GDir *dir)
315 {
316   g_return_if_fail (dir != NULL);
317
318 #ifdef G_OS_WIN32
319   _wclosedir (dir->wdirp);
320 #else
321   closedir (dir->dirp);
322 #endif
323   g_free (dir);
324 }