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