Debian Codegraph @

src/core/dirent_win32.h

raw symbols SCIP
1 /*
2 * dirent.h - dirent API for Microsoft Visual Studio
3 *
4 * Copyright (C) 2006 Toni Ronkko
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * ``Software''), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 *
25 *
26 * Jan 18, 2008, Toni Ronkko
27 * Using FindFirstFileA and WIN32_FIND_DATAA to avoid converting string
28 * between multi-byte and unicode representations. This makes the
29 * code simpler and also allows the code to be compiled under MingW. Thanks
30 * to Azriel Fasten for the suggestion.
31 *
32 * Mar 4, 2007, Toni Ronkko
33 * Bug fix: due to the strncpy_s() function this file only compiled in
34 * Visual Studio 2005. Using the new string functions only when the
35 * compiler version allows.
36 *
37 * Nov 2, 2006, Toni Ronkko
38 * Major update: removed support for Watcom C, MS-DOS and Turbo C to
39 * simplify the file, updated the code to compile cleanly on Visual
40 * Studio 2005 with both unicode and multi-byte character strings,
41 * removed rewinddir() as it had a bug.
42 *
43 * Aug 20, 2006, Toni Ronkko
44 * Removed all remarks about MSVC 1.0, which is antiqued now. Simplified
45 * comments by removing SGML tags.
46 *
47 * May 14 2002, Toni Ronkko
48 * Embedded the function definitions directly to the header so that no
49 * source modules need to be included in the Visual Studio project. Removed
50 * all the dependencies to other projects so that this very header can be
51 * used independently.
52 *
53 * May 28 1998, Toni Ronkko
54 * First version.
55 */
56 #ifndef DIRENT_H
57 #define DIRENT_H
58
59
60 // assume windows...
61 #ifndef __CHUNREAL_ENGINE__
62 #include <windows.h>
63 #else
64 // 1.5.0.0 (ge) | #chunreal
65 // unreal engine on windows disallows including windows.h
66 #include "Windows/MinWindows.h"
67 #endif // #ifndef __CHUNREAL_ENGINE__
68
69 #include <string.h>
70 #include <assert.h>
71
72
73 typedef struct dirent {
74 /* name of current directory entry (a multi-byte character string) */
75 char d_name[MAX_PATH + 1];
76
77 /* file attributes */
78 WIN32_FIND_DATAA data;
79 } dirent;
80
81
82 typedef struct DIR {
83 /* current directory entry */
84 dirent current;
85
86 /* is there an un-processed entry in current? */
87 int cached;
88
89 /* file search handle */
90 HANDLE search_handle;
91
92 /* search pattern (3 = zero terminator + pattern "\\*") */
93 char patt[MAX_PATH + 3];
94 } DIR;
95
96
97 static DIR *opendir (const char *dirname);
98 static struct dirent *readdir (DIR *dirp);
99 static int closedir (DIR *dirp);
100
101
102 /* use the new safe string functions introduced in Visual Studio 2005 */
103 #if defined(_MSC_VER) && _MSC_VER >= 1400
104 # define STRNCPY(dest,src,size) strncpy_s((dest),(size),(src),_TRUNCATE)
105 #else
106 # define STRNCPY(dest,src,size) strncpy((dest),(src),(size))
107 #endif
108
109
110 /*
111 * Open directory stream DIRNAME for read and return a pointer to the
112 * internal working area that is used to retrieve individual directory
113 * entries.
114 */
115 static DIR*
116 opendir(
117 const char *dirname)
118 {
119 DIR *dirp;
120 assert (dirname != NULL);
121 assert (strlen (dirname) < MAX_PATH);
122
123 /* construct new DIR structure */
124 dirp = (DIR*) malloc (sizeof (struct DIR));
125 if (dirp != NULL) {
126 char *p;
127
128 /* take directory name... */
129 STRNCPY (dirp->patt, dirname, sizeof(dirp->patt));
130 dirp->patt[MAX_PATH] = '\0';
131
132 /* ... and append search pattern to it */
133 p = strchr (dirp->patt, '\0');
134 if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
135 *p++ = '\\';
136 }
137 *p++ = '*';
138 *p = '\0';
139
140 /* open stream and retrieve first file */
141 dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->current.data);
142 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
143 /* invalid search pattern? */
144 free (dirp);
145 return NULL;
146 }
147
148 /* there is an un-processed directory entry in memory now */
149 dirp->cached = 1;
150
151 }
152 return dirp;
153 }
154
155
156 /*
157 * ge: not a good idea
158 */
159 static void
160 rewinddir(
161 DIR *dirp)
162 {
163 assert( dirp != NULL );
164
165 /* no cache */
166 dirp->cached = 0;
167
168 /* open stream and retrieve first file */
169 dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->current.data);
170 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
171 /* invalid search pattern? */
172 return;
173 }
174
175 /* there is an un-processed directory entry in memory now */
176 dirp->cached = 1;
177 }
178
179
180 /*
181 * Read a directory entry, and return a pointer to a dirent structure
182 * containing the name of the entry in d_name field. Individual directory
183 * entries returned by this very function include regular files,
184 * sub-directories, pseudo-directories "." and "..", but also volume labels,
185 * hidden files and system files may be returned.
186 */
187 static struct dirent *
188 readdir(
189 DIR *dirp)
190 {
191 assert (dirp != NULL);
192
193 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
194 /* directory stream was opened/rewound incorrectly or it ended normally */
195 return NULL;
196 }
197
198 /* get next directory entry */
199 if (dirp->cached != 0) {
200 /* a valid directory entry already in memory */
201 dirp->cached = 0;
202 } else {
203 /* read next directory entry from disk */
204 if (FindNextFileA (dirp->search_handle, &dirp->current.data) == FALSE) {
205 /* the very last file has been processed or an error occurred */
206 FindClose (dirp->search_handle);
207 dirp->search_handle = INVALID_HANDLE_VALUE;
208 return NULL;
209 }
210 }
211
212 /* copy as a multibyte character string */
213 STRNCPY (dirp->current.d_name, dirp->current.data.cFileName, sizeof(dirp->current.d_name));
214 dirp->current.d_name[MAX_PATH] = '\0';
215
216 return &dirp->current;
217 }
218
219
220 /*
221 * Close directory stream opened by opendir() function. Close of the
222 * directory stream invalidates the DIR structure as well as any previously
223 * read directory entry.
224 */
225 static int
226 closedir(
227 DIR *dirp)
228 {
229 assert (dirp != NULL);
230
231 /* release search handle */
232 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
233 FindClose (dirp->search_handle);
234 dirp->search_handle = INVALID_HANDLE_VALUE;
235 }
236
237 /* release directory handle */
238 free (dirp);
239 return 0;
240 }
241
242
243 #endif /*DIRENT_H*/