2007-04-27 Jonathan Chambers <joncham@gmail.com>
[mono.git] / eglib / src / gdir.c
1 /*
2  * Directory utility functions.
3  *
4  * Author:
5  *   Gonzalo Paniagua Javier (gonzalo@novell.com)
6  *
7  * (C) 2006 Novell, Inc.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28 #include <glib.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #ifndef _MSC_VER
35 #include <unistd.h>
36 #include <dirent.h>
37 #else
38 #include <io.h>
39 #endif
40
41 #ifdef G_OS_WIN32
42 #include <winsock2.h>
43 #endif
44
45 struct _GDir {
46 #ifdef G_OS_WIN32
47         HANDLE handle;
48         gchar* current;
49         gchar* next;
50 #else
51         DIR *dir;
52 #endif
53 };
54
55 GDir *
56 g_dir_open (const gchar *path, guint flags, GError **error)
57 {
58 #ifdef G_OS_WIN32
59         GDir *dir;
60         gunichar2* path_utf16;
61         gunichar2* path_utf16_search;
62         WIN32_FIND_DATA find_data;
63
64         g_return_val_if_fail (path != NULL, NULL);
65         g_return_val_if_fail (error == NULL || *error == NULL, NULL);
66         dir = g_new0 (GDir, 1);
67
68         path_utf16 = u8to16 (path);
69
70         dir->handle = FindFirstFile (path_utf16, &find_data);
71         if (dir->handle == INVALID_HANDLE_VALUE) {
72                 if (error) {
73                         gint err = errno;
74                         *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), strerror (err));
75                 }
76                 g_free (dir);
77                 g_free (path_utf16);
78                 return NULL;
79         }
80
81         /* now get files */
82         FindClose (dir->handle);
83         path_utf16_search = g_malloc ((wcslen(path_utf16) + 3)*sizeof(gunichar2));
84         wcscpy (path_utf16_search, path_utf16);
85         wcscat (path_utf16_search, L"\\*");
86
87         dir->handle = FindFirstFile (path_utf16_search, &find_data);
88         g_free (path_utf16_search);
89
90         while ((wcscmp (find_data.cFileName, L".") == 0) || (wcscmp (find_data.cFileName, L"..") == 0)) {
91                 if (!FindNextFile (dir->handle, &find_data)) {
92                         if (error) {
93                                 gint err = errno;
94                                 *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), strerror (err));
95                         }
96                         g_free (dir);
97                         g_free (path_utf16);
98                         return NULL;
99                 }
100         }
101
102         dir->current = NULL;
103         dir->next = u16to8 (find_data.cFileName);
104
105         g_free (path_utf16);
106         return dir;
107 #else
108         GDir *dir;
109
110         g_return_val_if_fail (path != NULL, NULL);
111         g_return_val_if_fail (error == NULL || *error == NULL, NULL);
112
113         (void) flags; /* this is not used */
114         dir = g_new (GDir, 1);
115         dir->dir = opendir (path);
116         if (dir->dir == NULL) {
117                 if (error) {
118                         gint err = errno;
119                         *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), strerror (err));
120                 }
121                 g_free (dir);
122                 return NULL;
123         }
124         return dir;
125 #endif
126 }
127
128 const gchar *
129 g_dir_read_name (GDir *dir)
130 {
131 #ifdef G_OS_WIN32
132         WIN32_FIND_DATA find_data;
133
134         g_return_val_if_fail (dir != NULL && dir->handle != 0, NULL);
135
136         if (dir->current)
137                 g_free (dir->current);
138         dir->current = NULL;
139
140         dir->current = dir->next;
141
142         if (!dir->current)
143                 return NULL;
144
145         dir->next = NULL;
146
147         do {
148                 if (!FindNextFile (dir->handle, &find_data)) {
149                         dir->next = NULL;
150                         return dir->current;
151                 }
152         } while ((wcscmp (find_data.cFileName, L".") == 0) || (wcscmp (find_data.cFileName, L"..") == 0));
153
154         dir->next = u16to8 (find_data.cFileName);
155         return dir->current;
156 #else
157         struct dirent *entry;
158
159         g_return_val_if_fail (dir != NULL && dir->dir != NULL, NULL);
160         do {
161                 entry = readdir (dir->dir);
162                 if (entry == NULL)
163                         return NULL;
164         } while ((strcmp (entry->d_name, ".") == 0) || (strcmp (entry->d_name, "..") == 0));
165
166         return entry->d_name;
167 #endif
168 }
169
170 void
171 g_dir_rewind (GDir *dir)
172 {
173 #ifdef G_OS_WIN32
174 #else
175         g_return_if_fail (dir != NULL && dir->dir != NULL);
176         rewinddir (dir->dir);
177 #endif
178 }
179
180 void
181 g_dir_close (GDir *dir)
182 {
183 #ifdef G_OS_WIN32
184         g_return_if_fail (dir != NULL && dir->handle != 0);
185         
186         if (dir->current)
187                 g_free (dir->current);
188         dir->current = NULL;
189         if (dir->next)
190                 g_free (dir->next);
191         dir->next = NULL;
192         FindClose (dir->handle);
193         dir->handle = 0;
194         g_free (dir);
195 #else
196         g_return_if_fail (dir != NULL && dir->dir != 0);
197         closedir (dir->dir);
198         dir->dir = NULL;
199         g_free (dir);
200 #endif
201 }
202
203