More include cleanups
[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 "gtestutils.h"
42 #include "glibintl.h"
43
44
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
49
50 struct _GDir
51 {
52 #ifdef G_OS_WIN32
53   _WDIR *wdirp;
54 #else
55   DIR *dirp;
56 #endif
57 #ifdef G_OS_WIN32
58   gchar utf8_buf[FILENAME_MAX*4];
59 #endif
60 };
61
62 /**
63  * g_dir_open:
64  * @path: the path to the directory you are interested in. On Unix
65  *         in the on-disk encoding. On Windows in UTF-8
66  * @flags: Currently must be set to 0. Reserved for future use.
67  * @error: return location for a #GError, or %NULL.
68  *         If non-%NULL, an error will be set if and only if
69  *         g_dir_open() fails.
70  *
71  * Opens a directory for reading. The names of the files in the
72  * directory can then be retrieved using g_dir_read_name().
73  *
74  * Return value: a newly allocated #GDir on success, %NULL on failure.
75  *   If non-%NULL, you must free the result with g_dir_close()
76  *   when you are finished with it.
77  **/
78 GDir *
79 g_dir_open (const gchar  *path,
80             guint         flags,
81             GError      **error)
82 {
83   GDir *dir;
84   int errsv;
85 #ifdef G_OS_WIN32
86   wchar_t *wpath;
87 #else
88   gchar *utf8_path;
89 #endif
90
91   g_return_val_if_fail (path != NULL, NULL);
92
93 #ifdef G_OS_WIN32
94   wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
95
96   if (wpath == NULL)
97     return NULL;
98
99   dir = g_new (GDir, 1);
100
101   dir->wdirp = _wopendir (wpath);
102   g_free (wpath);
103
104   if (dir->wdirp)
105     return dir;
106
107   /* error case */
108   errsv = errno;
109
110   g_set_error (error,
111                G_FILE_ERROR,
112                g_file_error_from_errno (errsv),
113                _("Error opening directory '%s': %s"),
114                path, g_strerror (errsv));
115   
116   g_free (dir);
117       
118   return NULL;
119 #else
120   dir = g_new (GDir, 1);
121
122   dir->dirp = opendir (path);
123
124   if (dir->dirp)
125     return dir;
126
127   /* error case */
128   errsv = errno;
129
130   utf8_path = g_filename_to_utf8 (path, -1,
131                                   NULL, NULL, NULL);
132
133   g_set_error (error,
134                G_FILE_ERROR,
135                g_file_error_from_errno (errsv),
136                _("Error opening directory '%s': %s"),
137                utf8_path, g_strerror (errsv));
138
139   g_free (utf8_path);
140   g_free (dir);
141
142   return NULL;
143 #endif
144 }
145
146 #if defined (G_OS_WIN32) && !defined (_WIN64)
147
148 /* The above function actually is called g_dir_open_utf8, and it's
149  * that what applications compiled with this GLib version will
150  * use.
151  */
152
153 #undef g_dir_open
154
155 /* Binary compatibility version. Not for newly compiled code. */
156
157 GDir *
158 g_dir_open (const gchar  *path,
159             guint         flags,
160             GError      **error)
161 {
162   gchar *utf8_path = g_locale_to_utf8 (path, -1, NULL, NULL, error);
163   GDir *retval;
164
165   if (utf8_path == NULL)
166     return NULL;
167
168   retval = g_dir_open_utf8 (utf8_path, flags, error);
169
170   g_free (utf8_path);
171
172   return retval;
173 }
174 #endif
175
176 /**
177  * g_dir_read_name:
178  * @dir: a #GDir* created by g_dir_open()
179  *
180  * Retrieves the name of the next entry in the directory.  The '.' and
181  * '..' entries are omitted. On Windows, the returned name is in
182  * UTF-8. On Unix, it is in the on-disk encoding.
183  *
184  * Return value: The entry's name or %NULL if there are no 
185  *   more entries. The return value is owned by GLib and
186  *   must not be modified or freed.
187  **/
188 G_CONST_RETURN gchar*
189 g_dir_read_name (GDir *dir)
190 {
191 #ifdef G_OS_WIN32
192   gchar *utf8_name;
193   struct _wdirent *wentry;
194 #else
195   struct dirent *entry;
196 #endif
197
198   g_return_val_if_fail (dir != NULL, NULL);
199
200 #ifdef G_OS_WIN32
201   while (1)
202     {
203       wentry = _wreaddir (dir->wdirp);
204       while (wentry 
205              && (0 == wcscmp (wentry->d_name, L".") ||
206                  0 == wcscmp (wentry->d_name, L"..")))
207         wentry = _wreaddir (dir->wdirp);
208
209       if (wentry == NULL)
210         return NULL;
211
212       utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);
213
214       if (utf8_name == NULL)
215         continue;               /* Huh, impossible? Skip it anyway */
216
217       strcpy (dir->utf8_buf, utf8_name);
218       g_free (utf8_name);
219
220       return dir->utf8_buf;
221     }
222 #else
223   entry = readdir (dir->dirp);
224   while (entry 
225          && (0 == strcmp (entry->d_name, ".") ||
226              0 == strcmp (entry->d_name, "..")))
227     entry = readdir (dir->dirp);
228
229   if (entry)
230     return entry->d_name;
231   else
232     return NULL;
233 #endif
234 }
235
236 #if defined (G_OS_WIN32) && !defined (_WIN64)
237
238 /* Ditto for g_dir_read_name */
239
240 #undef g_dir_read_name
241
242 /* Binary compatibility version. Not for newly compiled code. */
243
244 G_CONST_RETURN gchar*
245 g_dir_read_name (GDir *dir)
246 {
247   while (1)
248     {
249       const gchar *utf8_name = g_dir_read_name_utf8 (dir);
250       gchar *retval;
251       
252       if (utf8_name == NULL)
253         return NULL;
254
255       retval = g_locale_from_utf8 (utf8_name, -1, NULL, NULL, NULL);
256
257       if (retval != NULL)
258         {
259           strcpy (dir->utf8_buf, retval);
260           g_free (retval);
261
262           return dir->utf8_buf;
263         }
264     }
265 }
266
267 #endif
268
269 /**
270  * g_dir_rewind:
271  * @dir: a #GDir* created by g_dir_open()
272  *
273  * Resets the given directory. The next call to g_dir_read_name()
274  * will return the first entry again.
275  **/
276 void
277 g_dir_rewind (GDir *dir)
278 {
279   g_return_if_fail (dir != NULL);
280   
281 #ifdef G_OS_WIN32
282   _wrewinddir (dir->wdirp);
283 #else
284   rewinddir (dir->dirp);
285 #endif
286 }
287
288 /**
289  * g_dir_close:
290  * @dir: a #GDir* created by g_dir_open()
291  *
292  * Closes the directory and deallocates all related resources.
293  **/
294 void
295 g_dir_close (GDir *dir)
296 {
297   g_return_if_fail (dir != NULL);
298
299 #ifdef G_OS_WIN32
300   _wclosedir (dir->wdirp);
301 #else
302   closedir (dir->dirp);
303 #endif
304   g_free (dir);
305 }