Fossil SCM

allow MSVC build without -DUNICODE as well

jan.nijtmans 2012-09-12 20:55 UTC restore-win95
Commit eede5db7c37727a04c222d321c970383c4284a9e
1 file changed +41 -22
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -85,10 +85,11 @@
8585
#include <string.h>
8686
#include <stdlib.h>
8787
#include <sys/types.h>
8888
#include <sys/stat.h>
8989
#include <errno.h>
90
+#include <tchar.h>
9091
9192
/* Entries missing from MSVC 6.0 */
9293
#if !defined(FILE_ATTRIBUTE_DEVICE)
9394
# define FILE_ATTRIBUTE_DEVICE 0x40
9495
#endif
@@ -152,40 +153,49 @@
152153
153154
#ifdef __cplusplus
154155
extern "C" {
155156
#endif
156157
158
+#ifdef UNICODE
159
+# define dirent _wdirent
160
+# define opendir _wopendir
161
+# define readdir _wreaddir
162
+# define closedir _wclosedir
163
+# define DIR _WDIR
164
+#else
165
+# define wchar_t char
166
+#endif
157167
158
-typedef struct _wdirent
168
+typedef struct dirent
159169
{
160170
wchar_t d_name[MAX_PATH + 1]; /* File name */
161171
size_t d_namlen; /* Length of name without \0 */
162172
int d_type; /* File type */
163
-} _wdirent;
173
+} dirent;
164174
165175
166
-typedef struct _WDIR
176
+typedef struct DIR
167177
{
168
- _wdirent curentry; /* Current directory entry */
169
- WIN32_FIND_DATAW find_data; /* Private file data */
178
+ dirent curentry; /* Current directory entry */
179
+ WIN32_FIND_DATA find_data; /* Private file data */
170180
int cached; /* True if data is valid */
171181
HANDLE search_handle; /* Win32 search handle */
172182
wchar_t patt[MAX_PATH + 3]; /* Initial directory name */
173
-} _WDIR;
183
+} DIR;
174184
175185
176186
/* Forward declarations */
177
-static _WDIR *_wopendir(const wchar_t *dirname);
178
-static struct _wdirent *_wreaddir(_WDIR *dirp);
179
-static int _wclosedir(_WDIR *dirp);
187
+static DIR *opendir(const wchar_t *dirname);
188
+static struct dirent *readdir(DIR *dirp);
189
+static int closedir(DIR *dirp);
180190
181191
182192
/* Use the new safe string functions introduced in Visual Studio 2005 */
183193
#if defined(_MSC_VER) && _MSC_VER >= 1400
184
-# define DIRENT_STRNCPY(dest,src,size) wcsncpy_s((dest),(size),(src),_TRUNCATE)
194
+# define DIRENT_STRNCPY(dest,src,size) _tcsncpy_s((dest),(size),(src),_TRUNCATE)
185195
#else
186
-# define DIRENT_STRNCPY(dest,src,size) wcsncpy((dest),(src),(size))
196
+# define DIRENT_STRNCPY(dest,src,size) _tcsncpy((dest),(src),(size))
187197
#endif
188198
189199
/* Set errno variable */
190200
#if defined(_MSC_VER)
191201
#define DIRENT_SET_ERRNO(x) _set_errno (x)
@@ -197,47 +207,47 @@
197207
/*****************************************************************************
198208
* Open directory stream DIRNAME for read and return a pointer to the
199209
* internal working area that is used to retrieve individual directory
200210
* entries.
201211
*/
202
-static _WDIR *_wopendir(const wchar_t *dirname)
212
+static DIR *opendir(const wchar_t *dirname)
203213
{
204
- _WDIR *dirp;
214
+ DIR *dirp;
205215
206216
/* ensure that the resulting search pattern will be a valid file name */
207217
if (dirname == NULL) {
208218
DIRENT_SET_ERRNO (ENOENT);
209219
return NULL;
210220
}
211
- if (wcslen (dirname) + 3 >= MAX_PATH) {
221
+ if (_tcslen (dirname) + 3 >= MAX_PATH) {
212222
DIRENT_SET_ERRNO (ENAMETOOLONG);
213223
return NULL;
214224
}
215225
216226
/* construct new DIR structure */
217
- dirp = (_WDIR*) malloc (sizeof (struct _WDIR));
227
+ dirp = (DIR*) malloc (sizeof (struct DIR));
218228
if (dirp != NULL) {
219229
int error;
220230
221231
/*
222232
* Convert relative directory name to an absolute one. This
223233
* allows rewinddir() to function correctly when the current working
224234
* directory is changed between opendir() and rewinddir().
225235
*/
226
- if (GetFullPathNameW (dirname, MAX_PATH, dirp->patt, NULL)) {
236
+ if (GetFullPathName (dirname, MAX_PATH, dirp->patt, NULL)) {
227237
wchar_t *p;
228238
229239
/* append the search pattern "\\*\0" to the directory name */
230
- p = wcschr (dirp->patt, '\0');
240
+ p = _tcschr (dirp->patt, '\0');
231241
if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
232242
*p++ = '\\';
233243
}
234244
*p++ = '*';
235245
*p = '\0';
236246
237247
/* open directory stream and retrieve the first entry */
238
- dirp->search_handle = FindFirstFileW (dirp->patt, &dirp->find_data);
248
+ dirp->search_handle = FindFirstFile (dirp->patt, &dirp->find_data);
239249
if (dirp->search_handle != INVALID_HANDLE_VALUE) {
240250
/* a directory entry is now waiting in memory */
241251
dirp->cached = 1;
242252
error = 0;
243253
} else {
@@ -266,11 +276,11 @@
266276
* containing the name of the entry in d_name field. Individual directory
267277
* entries returned by this very function include regular files,
268278
* sub-directories, pseudo-directories "." and "..", but also volume labels,
269279
* hidden files and system files may be returned.
270280
*/
271
-static struct _wdirent *_wreaddir(_WDIR *dirp)
281
+static struct dirent *readdir(DIR *dirp)
272282
{
273283
DWORD attr;
274284
if (dirp == NULL) {
275285
/* directory stream did not open */
276286
DIRENT_SET_ERRNO (EBADF);
@@ -284,11 +294,11 @@
284294
} else {
285295
/* get the next directory entry from stream */
286296
if (dirp->search_handle == INVALID_HANDLE_VALUE) {
287297
return NULL;
288298
}
289
- if (FindNextFileW (dirp->search_handle, &dirp->find_data) == FALSE) {
299
+ if (FindNextFile (dirp->search_handle, &dirp->find_data) == FALSE) {
290300
/* the very last entry has been processed or an error occured */
291301
FindClose (dirp->search_handle);
292302
dirp->search_handle = INVALID_HANDLE_VALUE;
293303
return NULL;
294304
}
@@ -299,11 +309,11 @@
299309
dirp->find_data.cFileName,
300310
sizeof(dirp->curentry.d_name)/sizeof(dirp->curentry.d_name[0]) );
301311
dirp->curentry.d_name[MAX_PATH] = '\0';
302312
303313
/* compute the length of name */
304
- dirp->curentry.d_namlen = wcslen (dirp->curentry.d_name);
314
+ dirp->curentry.d_namlen = _tcslen (dirp->curentry.d_name);
305315
306316
/* determine file type */
307317
attr = dirp->find_data.dwFileAttributes;
308318
if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
309319
dirp->curentry.d_type = DT_CHR;
@@ -319,11 +329,11 @@
319329
/*****************************************************************************
320330
* Close directory stream opened by opendir() function. Close of the
321331
* directory stream invalidates the DIR structure as well as any previously
322332
* read directory entry.
323333
*/
324
-static int _wclosedir(_WDIR *dirp)
334
+static int closedir(DIR *dirp)
325335
{
326336
if (dirp == NULL) {
327337
/* invalid directory stream */
328338
DIRENT_SET_ERRNO (EBADF);
329339
return -1;
@@ -338,10 +348,19 @@
338348
/* release directory structure */
339349
free (dirp);
340350
return 0;
341351
}
342352
353
+#ifdef UNICODE
354
+# undef dirent
355
+# undef opendir
356
+# undef readdir
357
+# undef closedir
358
+# undef DIR
359
+#else
360
+# undef wchar_t
361
+#endif
343362
344363
#ifdef __cplusplus
345364
}
346365
#endif
347366
#endif /*DIRENT_H*/
348367
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -85,10 +85,11 @@
85 #include <string.h>
86 #include <stdlib.h>
87 #include <sys/types.h>
88 #include <sys/stat.h>
89 #include <errno.h>
 
90
91 /* Entries missing from MSVC 6.0 */
92 #if !defined(FILE_ATTRIBUTE_DEVICE)
93 # define FILE_ATTRIBUTE_DEVICE 0x40
94 #endif
@@ -152,40 +153,49 @@
152
153 #ifdef __cplusplus
154 extern "C" {
155 #endif
156
 
 
 
 
 
 
 
 
 
157
158 typedef struct _wdirent
159 {
160 wchar_t d_name[MAX_PATH + 1]; /* File name */
161 size_t d_namlen; /* Length of name without \0 */
162 int d_type; /* File type */
163 } _wdirent;
164
165
166 typedef struct _WDIR
167 {
168 _wdirent curentry; /* Current directory entry */
169 WIN32_FIND_DATAW find_data; /* Private file data */
170 int cached; /* True if data is valid */
171 HANDLE search_handle; /* Win32 search handle */
172 wchar_t patt[MAX_PATH + 3]; /* Initial directory name */
173 } _WDIR;
174
175
176 /* Forward declarations */
177 static _WDIR *_wopendir(const wchar_t *dirname);
178 static struct _wdirent *_wreaddir(_WDIR *dirp);
179 static int _wclosedir(_WDIR *dirp);
180
181
182 /* Use the new safe string functions introduced in Visual Studio 2005 */
183 #if defined(_MSC_VER) && _MSC_VER >= 1400
184 # define DIRENT_STRNCPY(dest,src,size) wcsncpy_s((dest),(size),(src),_TRUNCATE)
185 #else
186 # define DIRENT_STRNCPY(dest,src,size) wcsncpy((dest),(src),(size))
187 #endif
188
189 /* Set errno variable */
190 #if defined(_MSC_VER)
191 #define DIRENT_SET_ERRNO(x) _set_errno (x)
@@ -197,47 +207,47 @@
197 /*****************************************************************************
198 * Open directory stream DIRNAME for read and return a pointer to the
199 * internal working area that is used to retrieve individual directory
200 * entries.
201 */
202 static _WDIR *_wopendir(const wchar_t *dirname)
203 {
204 _WDIR *dirp;
205
206 /* ensure that the resulting search pattern will be a valid file name */
207 if (dirname == NULL) {
208 DIRENT_SET_ERRNO (ENOENT);
209 return NULL;
210 }
211 if (wcslen (dirname) + 3 >= MAX_PATH) {
212 DIRENT_SET_ERRNO (ENAMETOOLONG);
213 return NULL;
214 }
215
216 /* construct new DIR structure */
217 dirp = (_WDIR*) malloc (sizeof (struct _WDIR));
218 if (dirp != NULL) {
219 int error;
220
221 /*
222 * Convert relative directory name to an absolute one. This
223 * allows rewinddir() to function correctly when the current working
224 * directory is changed between opendir() and rewinddir().
225 */
226 if (GetFullPathNameW (dirname, MAX_PATH, dirp->patt, NULL)) {
227 wchar_t *p;
228
229 /* append the search pattern "\\*\0" to the directory name */
230 p = wcschr (dirp->patt, '\0');
231 if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
232 *p++ = '\\';
233 }
234 *p++ = '*';
235 *p = '\0';
236
237 /* open directory stream and retrieve the first entry */
238 dirp->search_handle = FindFirstFileW (dirp->patt, &dirp->find_data);
239 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
240 /* a directory entry is now waiting in memory */
241 dirp->cached = 1;
242 error = 0;
243 } else {
@@ -266,11 +276,11 @@
266 * containing the name of the entry in d_name field. Individual directory
267 * entries returned by this very function include regular files,
268 * sub-directories, pseudo-directories "." and "..", but also volume labels,
269 * hidden files and system files may be returned.
270 */
271 static struct _wdirent *_wreaddir(_WDIR *dirp)
272 {
273 DWORD attr;
274 if (dirp == NULL) {
275 /* directory stream did not open */
276 DIRENT_SET_ERRNO (EBADF);
@@ -284,11 +294,11 @@
284 } else {
285 /* get the next directory entry from stream */
286 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
287 return NULL;
288 }
289 if (FindNextFileW (dirp->search_handle, &dirp->find_data) == FALSE) {
290 /* the very last entry has been processed or an error occured */
291 FindClose (dirp->search_handle);
292 dirp->search_handle = INVALID_HANDLE_VALUE;
293 return NULL;
294 }
@@ -299,11 +309,11 @@
299 dirp->find_data.cFileName,
300 sizeof(dirp->curentry.d_name)/sizeof(dirp->curentry.d_name[0]) );
301 dirp->curentry.d_name[MAX_PATH] = '\0';
302
303 /* compute the length of name */
304 dirp->curentry.d_namlen = wcslen (dirp->curentry.d_name);
305
306 /* determine file type */
307 attr = dirp->find_data.dwFileAttributes;
308 if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
309 dirp->curentry.d_type = DT_CHR;
@@ -319,11 +329,11 @@
319 /*****************************************************************************
320 * Close directory stream opened by opendir() function. Close of the
321 * directory stream invalidates the DIR structure as well as any previously
322 * read directory entry.
323 */
324 static int _wclosedir(_WDIR *dirp)
325 {
326 if (dirp == NULL) {
327 /* invalid directory stream */
328 DIRENT_SET_ERRNO (EBADF);
329 return -1;
@@ -338,10 +348,19 @@
338 /* release directory structure */
339 free (dirp);
340 return 0;
341 }
342
 
 
 
 
 
 
 
 
 
343
344 #ifdef __cplusplus
345 }
346 #endif
347 #endif /*DIRENT_H*/
348
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -85,10 +85,11 @@
85 #include <string.h>
86 #include <stdlib.h>
87 #include <sys/types.h>
88 #include <sys/stat.h>
89 #include <errno.h>
90 #include <tchar.h>
91
92 /* Entries missing from MSVC 6.0 */
93 #if !defined(FILE_ATTRIBUTE_DEVICE)
94 # define FILE_ATTRIBUTE_DEVICE 0x40
95 #endif
@@ -152,40 +153,49 @@
153
154 #ifdef __cplusplus
155 extern "C" {
156 #endif
157
158 #ifdef UNICODE
159 # define dirent _wdirent
160 # define opendir _wopendir
161 # define readdir _wreaddir
162 # define closedir _wclosedir
163 # define DIR _WDIR
164 #else
165 # define wchar_t char
166 #endif
167
168 typedef struct dirent
169 {
170 wchar_t d_name[MAX_PATH + 1]; /* File name */
171 size_t d_namlen; /* Length of name without \0 */
172 int d_type; /* File type */
173 } dirent;
174
175
176 typedef struct DIR
177 {
178 dirent curentry; /* Current directory entry */
179 WIN32_FIND_DATA find_data; /* Private file data */
180 int cached; /* True if data is valid */
181 HANDLE search_handle; /* Win32 search handle */
182 wchar_t patt[MAX_PATH + 3]; /* Initial directory name */
183 } DIR;
184
185
186 /* Forward declarations */
187 static DIR *opendir(const wchar_t *dirname);
188 static struct dirent *readdir(DIR *dirp);
189 static int closedir(DIR *dirp);
190
191
192 /* Use the new safe string functions introduced in Visual Studio 2005 */
193 #if defined(_MSC_VER) && _MSC_VER >= 1400
194 # define DIRENT_STRNCPY(dest,src,size) _tcsncpy_s((dest),(size),(src),_TRUNCATE)
195 #else
196 # define DIRENT_STRNCPY(dest,src,size) _tcsncpy((dest),(src),(size))
197 #endif
198
199 /* Set errno variable */
200 #if defined(_MSC_VER)
201 #define DIRENT_SET_ERRNO(x) _set_errno (x)
@@ -197,47 +207,47 @@
207 /*****************************************************************************
208 * Open directory stream DIRNAME for read and return a pointer to the
209 * internal working area that is used to retrieve individual directory
210 * entries.
211 */
212 static DIR *opendir(const wchar_t *dirname)
213 {
214 DIR *dirp;
215
216 /* ensure that the resulting search pattern will be a valid file name */
217 if (dirname == NULL) {
218 DIRENT_SET_ERRNO (ENOENT);
219 return NULL;
220 }
221 if (_tcslen (dirname) + 3 >= MAX_PATH) {
222 DIRENT_SET_ERRNO (ENAMETOOLONG);
223 return NULL;
224 }
225
226 /* construct new DIR structure */
227 dirp = (DIR*) malloc (sizeof (struct DIR));
228 if (dirp != NULL) {
229 int error;
230
231 /*
232 * Convert relative directory name to an absolute one. This
233 * allows rewinddir() to function correctly when the current working
234 * directory is changed between opendir() and rewinddir().
235 */
236 if (GetFullPathName (dirname, MAX_PATH, dirp->patt, NULL)) {
237 wchar_t *p;
238
239 /* append the search pattern "\\*\0" to the directory name */
240 p = _tcschr (dirp->patt, '\0');
241 if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
242 *p++ = '\\';
243 }
244 *p++ = '*';
245 *p = '\0';
246
247 /* open directory stream and retrieve the first entry */
248 dirp->search_handle = FindFirstFile (dirp->patt, &dirp->find_data);
249 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
250 /* a directory entry is now waiting in memory */
251 dirp->cached = 1;
252 error = 0;
253 } else {
@@ -266,11 +276,11 @@
276 * containing the name of the entry in d_name field. Individual directory
277 * entries returned by this very function include regular files,
278 * sub-directories, pseudo-directories "." and "..", but also volume labels,
279 * hidden files and system files may be returned.
280 */
281 static struct dirent *readdir(DIR *dirp)
282 {
283 DWORD attr;
284 if (dirp == NULL) {
285 /* directory stream did not open */
286 DIRENT_SET_ERRNO (EBADF);
@@ -284,11 +294,11 @@
294 } else {
295 /* get the next directory entry from stream */
296 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
297 return NULL;
298 }
299 if (FindNextFile (dirp->search_handle, &dirp->find_data) == FALSE) {
300 /* the very last entry has been processed or an error occured */
301 FindClose (dirp->search_handle);
302 dirp->search_handle = INVALID_HANDLE_VALUE;
303 return NULL;
304 }
@@ -299,11 +309,11 @@
309 dirp->find_data.cFileName,
310 sizeof(dirp->curentry.d_name)/sizeof(dirp->curentry.d_name[0]) );
311 dirp->curentry.d_name[MAX_PATH] = '\0';
312
313 /* compute the length of name */
314 dirp->curentry.d_namlen = _tcslen (dirp->curentry.d_name);
315
316 /* determine file type */
317 attr = dirp->find_data.dwFileAttributes;
318 if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
319 dirp->curentry.d_type = DT_CHR;
@@ -319,11 +329,11 @@
329 /*****************************************************************************
330 * Close directory stream opened by opendir() function. Close of the
331 * directory stream invalidates the DIR structure as well as any previously
332 * read directory entry.
333 */
334 static int closedir(DIR *dirp)
335 {
336 if (dirp == NULL) {
337 /* invalid directory stream */
338 DIRENT_SET_ERRNO (EBADF);
339 return -1;
@@ -338,10 +348,19 @@
348 /* release directory structure */
349 free (dirp);
350 return 0;
351 }
352
353 #ifdef UNICODE
354 # undef dirent
355 # undef opendir
356 # undef readdir
357 # undef closedir
358 # undef DIR
359 #else
360 # undef wchar_t
361 #endif
362
363 #ifdef __cplusplus
364 }
365 #endif
366 #endif /*DIRENT_H*/
367

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button