Debian Codegraph @

src/win32/dirent.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 * Dec 15, 2009, John Cunningham
26 * Added rewinddir member function
27 *
28 * Jan 18, 2008, Toni Ronkko
29 * Using FindFirstFileA and WIN32_FIND_DATAA to avoid converting string
30 * between multi-byte and unicode representations. This makes the
31 * code simpler and also allows the code to be compiled under MingW. Thanks
32 * to Azriel Fasten for the suggestion.
33 *
34 * Mar 4, 2007, Toni Ronkko
35 * Bug fix: due to the strncpy_s() function this file only compiled in
36 * Visual Studio 2005. Using the new string functions only when the
37 * compiler version allows.
38 *
39 * Nov 2, 2006, Toni Ronkko
40 * Major update: removed support for Watcom C, MS-DOS and Turbo C to
41 * simplify the file, updated the code to compile cleanly on Visual
42 * Studio 2005 with both unicode and multi-byte character strings,
43 * removed rewinddir() as it had a bug.
44 *
45 * Aug 20, 2006, Toni Ronkko
46 * Removed all remarks about MSVC 1.0, which is antiqued now. Simplified
47 * comments by removing SGML tags.
48 *
49 * May 14 2002, Toni Ronkko
50 * Embedded the function definitions directly to the header so that no
51 * source modules need to be included in the Visual Studio project. Removed
52 * all the dependencies to other projects so that this very header can be
53 * used independently.
54 *
55 * May 28 1998, Toni Ronkko
56 * First version.
57 *****************************************************************************/
58 #ifndef DIRENT_H
59 #define DIRENT_H
60
61 #include <windows.h>
62 #include <string.h>
63 #include <assert.h>
64
65
66 typedef struct dirent
67 {
68 char d_name[MAX_PATH + 1]; /* current dir entry (multi-byte char string) */
69 WIN32_FIND_DATAA data; /* file attributes */
70 } dirent;
71
72
73 typedef struct DIR
74 {
75 dirent current; /* Current directory entry */
76 int cached; /* Indicates un-processed entry in memory */
77 HANDLE search_handle; /* File search handle */
78 char patt[MAX_PATH + 3]; /* search pattern (3 = pattern + "\\*\0") */
79 } DIR;
80
81
82 /* Forward declarations */
83 static DIR *opendir(const char *dirname);
84 static struct dirent *readdir(DIR *dirp);
85 static int closedir(DIR *dirp);
86 static void rewinddir(DIR* dirp);
87
88
89 /* Use the new safe string functions introduced in Visual Studio 2005 */
90 #if defined(_MSC_VER) && _MSC_VER >= 1400
91 # define STRNCPY(dest,src,size) strncpy_s((dest),(size),(src),_TRUNCATE)
92 #else
93 # define STRNCPY(dest,src,size) strncpy((dest),(src),(size))
94 #endif
95
96
97 /*****************************************************************************
98 * Open directory stream DIRNAME for read and return a pointer to the
99 * internal working area that is used to retrieve individual directory
100 * entries.
101 */
102 static DIR *opendir(const char *dirname)
103 {
104 DIR *dirp;
105 assert(dirname != NULL);
106 assert(strlen(dirname) < MAX_PATH);
107
108 /* construct new DIR structure */
109 dirp = (DIR*)malloc(sizeof (struct DIR));
110 if (dirp != NULL) {
111 char *p;
112
113 /* take directory name... */
114 STRNCPY(dirp->patt, dirname, sizeof(dirp->patt));
115 dirp->patt[MAX_PATH] = '\0';
116
117 /* ... and append search pattern to it */
118 p = strchr(dirp->patt, '\0');
119 if (dirp->patt < p && *(p - 1) != '\\' && *(p - 1) != ':') {
120 *p++ = '\\';
121 }
122 *p++ = '*';
123 *p = '\0';
124
125 /* open stream and retrieve first file */
126 dirp->search_handle = FindFirstFileA(dirp->patt, &dirp->current.data);
127 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
128 /* invalid search pattern? */
129 free(dirp);
130 return NULL;
131 }
132
133 /* there is an un-processed directory entry in memory now */
134 dirp->cached = 1;
135 }
136
137 return dirp;
138 }
139
140
141 /*****************************************************************************
142 * Read a directory entry, and return a pointer to a dirent structure
143 * containing the name of the entry in d_name field. Individual directory
144 * entries returned by this very function include regular files,
145 * sub-directories, pseudo-directories "." and "..", but also volume labels,
146 * hidden files and system files may be returned.
147 */
148 static struct dirent *readdir(DIR *dirp)
149 {
150 assert(dirp != NULL);
151
152 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
153 /* directory stream was opened/rewound incorrectly or ended normally */
154 return NULL;
155 }
156
157 /* get next directory entry */
158 if (dirp->cached != 0) {
159 /* a valid directory entry already in memory */
160 dirp->cached = 0;
161 }
162 else {
163 /* read next directory entry from disk */
164 if (FindNextFileA(dirp->search_handle, &dirp->current.data) == FALSE) {
165 /* the very last file has been processed or an error occured */
166 FindClose(dirp->search_handle);
167 dirp->search_handle = INVALID_HANDLE_VALUE;
168 return NULL;
169 }
170 }
171
172 /* copy as a multibyte character string */
173 STRNCPY(dirp->current.d_name,
174 dirp->current.data.cFileName,
175 sizeof(dirp->current.d_name));
176 dirp->current.d_name[MAX_PATH] = '\0';
177
178 return &dirp->current;
179 }
180
181
182 /*****************************************************************************
183 * Close directory stream opened by opendir() function. Close of the
184 * directory stream invalidates the DIR structure as well as any previously
185 * read directory entry.
186 */
187 static int closedir(DIR *dirp)
188 {
189 assert(dirp != NULL);
190
191 /* release search handle */
192 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
193 FindClose(dirp->search_handle);
194 dirp->search_handle = INVALID_HANDLE_VALUE;
195 }
196
197 /* release directory handle */
198 free(dirp);
199 return 0;
200 }
201
202
203 /*****************************************************************************
204 * Resets the position of the directory stream to which dirp refers to the
205 * beginning of the directory. It also causes the directory stream to refer
206 * to the current state of the corresponding directory, as a call to opendir()
207 * would have done. If dirp does not refer to a directory stream, the effect
208 * is undefined.
209 */
210 static void rewinddir(DIR* dirp)
211 {
212 /* release search handle */
213 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
214 FindClose(dirp->search_handle);
215 dirp->search_handle = INVALID_HANDLE_VALUE;
216 }
217
218 /* open new search handle and retrieve first file */
219 dirp->search_handle = FindFirstFileA(dirp->patt, &dirp->current.data);
220 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
221 /* invalid search pattern? */
222 free(dirp);
223 return;
224 }
225
226 /* there is an un-processed directory entry in memory now */
227 dirp->cached = 1;
228 }
229
230
231 #endif /*DIRENT_H*/