2010-05-04 Jonathan Chambers <joncham@gmail.com>
[mono.git] / eglib / src / gdir-win32.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 #include <io.h>
35
36 #include <winsock2.h>
37
38 struct _GDir {
39         HANDLE handle;
40         gchar* current;
41         gchar* next;
42 };
43
44 GDir *
45 g_dir_open (const gchar *path, guint flags, GError **error)
46 {
47         GDir *dir;
48         gunichar2* path_utf16;
49         gunichar2* path_utf16_search;
50         WIN32_FIND_DATA find_data;
51
52         g_return_val_if_fail (path != NULL, NULL);
53         g_return_val_if_fail (error == NULL || *error == NULL, NULL);
54         dir = g_new0 (GDir, 1);
55
56         path_utf16 = u8to16 (path);
57
58         dir->handle = FindFirstFile (path_utf16, &find_data);
59         if (dir->handle == INVALID_HANDLE_VALUE) {
60                 if (error) {
61                         gint err = errno;
62                         *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), strerror (err));
63                 }
64                 g_free (dir);
65                 g_free (path_utf16);
66                 return NULL;
67         }
68
69         /* now get files */
70         FindClose (dir->handle);
71         path_utf16_search = g_malloc ((wcslen(path_utf16) + 3)*sizeof(gunichar2));
72         wcscpy (path_utf16_search, path_utf16);
73         wcscat (path_utf16_search, L"\\*");
74
75         dir->handle = FindFirstFile (path_utf16_search, &find_data);
76         g_free (path_utf16_search);
77
78         while ((wcscmp (find_data.cFileName, L".") == 0) || (wcscmp (find_data.cFileName, L"..") == 0)) {
79                 if (!FindNextFile (dir->handle, &find_data)) {
80                         if (error) {
81                                 gint err = errno;
82                                 *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), strerror (err));
83                         }
84                         g_free (dir);
85                         g_free (path_utf16);
86                         return NULL;
87                 }
88         }
89
90         dir->current = NULL;
91         dir->next = u16to8 (find_data.cFileName);
92
93         g_free (path_utf16);
94         return dir;
95 }
96
97 const gchar *
98 g_dir_read_name (GDir *dir)
99 {
100         WIN32_FIND_DATA find_data;
101
102         g_return_val_if_fail (dir != NULL && dir->handle != 0, NULL);
103
104         if (dir->current)
105                 g_free (dir->current);
106         dir->current = NULL;
107
108         dir->current = dir->next;
109
110         if (!dir->current)
111                 return NULL;
112
113         dir->next = NULL;
114
115         do {
116                 if (!FindNextFile (dir->handle, &find_data)) {
117                         dir->next = NULL;
118                         return dir->current;
119                 }
120         } while ((wcscmp (find_data.cFileName, L".") == 0) || (wcscmp (find_data.cFileName, L"..") == 0));
121
122         dir->next = u16to8 (find_data.cFileName);
123         return dir->current;
124 }
125
126 void
127 g_dir_rewind (GDir *dir)
128 {
129 }
130
131 void
132 g_dir_close (GDir *dir)
133 {
134         g_return_if_fail (dir != NULL && dir->handle != 0);
135         
136         if (dir->current)
137                 g_free (dir->current);
138         dir->current = NULL;
139         if (dir->next)
140                 g_free (dir->next);
141         dir->next = NULL;
142         FindClose (dir->handle);
143         dir->handle = 0;
144         g_free (dir);
145 }
146
147