2009-02-09 Jeffrey Stedfast <fejj@novell.com>
[mono-project/dkf.git] / eglib / src / gdir-win32.c
blob839f0b131bc4113a84c34cd0417b70e181122532
1 /*
2 * Directory utility functions.
4 * Author:
5 * Gonzalo Paniagua Javier (gonzalo@novell.com)
7 * (C) 2006 Novell, Inc.
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:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
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.
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>
36 #include <winsock2.h>
38 struct _GDir {
39 HANDLE handle;
40 gchar* current;
41 gchar* next;
44 GDir *
45 g_dir_open (const gchar *path, guint flags, GError **error)
47 GDir *dir;
48 gunichar2* path_utf16;
49 gunichar2* path_utf16_search;
50 WIN32_FIND_DATA find_data;
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);
56 path_utf16 = u8to16 (path);
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));
64 g_free (dir);
65 g_free (path_utf16);
66 return NULL;
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"\\*");
75 dir->handle = FindFirstFile (path_utf16_search, &find_data);
76 g_free (path_utf16_search);
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));
84 g_free (dir);
85 g_free (path_utf16);
86 return NULL;
90 dir->current = NULL;
91 dir->next = u16to8 (find_data.cFileName);
93 g_free (path_utf16);
94 return dir;
97 const gchar *
98 g_dir_read_name (GDir *dir)
100 WIN32_FIND_DATA find_data;
102 g_return_val_if_fail (dir != NULL && dir->handle != 0, NULL);
104 if (dir->current)
105 g_free (dir->current);
106 dir->current = NULL;
108 dir->current = dir->next;
110 if (!dir->current)
111 return NULL;
113 dir->next = NULL;
115 do {
116 if (!FindNextFile (dir->handle, &find_data)) {
117 dir->next = NULL;
118 return dir->current;
120 } while ((wcscmp (find_data.cFileName, L".") == 0) || (wcscmp (find_data.cFileName, L"..") == 0));
122 dir->next = u16to8 (find_data.cFileName);
123 return dir->current;
126 void
127 g_dir_rewind (GDir *dir)
131 void
132 g_dir_close (GDir *dir)
134 g_return_if_fail (dir != NULL && dir->handle != 0);
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);