Fossil SCM

remaining part of [01a2f3a346] factor out the opendir() family of routines and provide portable alternatives, fossil_opendir()

jan.nijtmans 2012-08-29 22:43 trunk merge
Commit 4f9f0570db8c8dd91baec5f398ea4756517e85f7
+25
--- src/file.c
+++ src/file.c
@@ -1002,10 +1002,35 @@
10021002
}
10031003
rc = blob_compare(&onDisk, pContent);
10041004
blob_reset(&onDisk);
10051005
return rc==0;
10061006
}
1007
+
1008
+/*
1009
+** Portable unicode implementation of opendir()
1010
+*/
1011
+#if INTERFACE
1012
+
1013
+#if defined(_WIN32)
1014
+# include <dirent.h>
1015
+# define FOSSIL_DIR _WDIR
1016
+# define fossil_dirent _wdirent
1017
+# define fossil_opendir _wopendir
1018
+# define fossil_readdir _wreaddir
1019
+# define fossil_closedir _wclosedir
1020
+#else
1021
+# include <dirent.h>
1022
+# define FOSSIL_DIR DIR
1023
+# define fossil_dirent dirent
1024
+# define fossil_opendir opendir
1025
+# define fossil_readdir readdir
1026
+# define fossil_closedir closedir
1027
+#endif
1028
+
1029
+#endif /* INTERFACE */
1030
+
1031
+
10071032
10081033
/**************************************************************************
10091034
** The following routines translate between MBCS and UTF8 on windows.
10101035
** Since everything is always UTF8 on unix, these routines are no-ops
10111036
** there.
10121037
--- src/file.c
+++ src/file.c
@@ -1002,10 +1002,35 @@
1002 }
1003 rc = blob_compare(&onDisk, pContent);
1004 blob_reset(&onDisk);
1005 return rc==0;
1006 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1007
1008 /**************************************************************************
1009 ** The following routines translate between MBCS and UTF8 on windows.
1010 ** Since everything is always UTF8 on unix, these routines are no-ops
1011 ** there.
1012
--- src/file.c
+++ src/file.c
@@ -1002,10 +1002,35 @@
1002 }
1003 rc = blob_compare(&onDisk, pContent);
1004 blob_reset(&onDisk);
1005 return rc==0;
1006 }
1007
1008 /*
1009 ** Portable unicode implementation of opendir()
1010 */
1011 #if INTERFACE
1012
1013 #if defined(_WIN32)
1014 # include <dirent.h>
1015 # define FOSSIL_DIR _WDIR
1016 # define fossil_dirent _wdirent
1017 # define fossil_opendir _wopendir
1018 # define fossil_readdir _wreaddir
1019 # define fossil_closedir _wclosedir
1020 #else
1021 # include <dirent.h>
1022 # define FOSSIL_DIR DIR
1023 # define fossil_dirent dirent
1024 # define fossil_opendir opendir
1025 # define fossil_readdir readdir
1026 # define fossil_closedir closedir
1027 #endif
1028
1029 #endif /* INTERFACE */
1030
1031
1032
1033 /**************************************************************************
1034 ** The following routines translate between MBCS and UTF8 on windows.
1035 ** Since everything is always UTF8 on unix, these routines are no-ops
1036 ** there.
1037
+10 -11
--- src/rebuild.c
+++ src/rebuild.c
@@ -1,7 +1,7 @@
11
/*
2
-** Copyright (c) 2007 D. Richard Hipp
2
+** Copyright © 2007 D. Richard Hipp
33
**
44
** This program is free software; you can redistribute it and/or
55
** modify it under the terms of the Simplified BSD License (also
66
** known as the "2-Clause License" or "FreeBSD License".)
77
@@ -18,11 +18,10 @@
1818
** This file contains code used to rebuild the database.
1919
*/
2020
#include "config.h"
2121
#include "rebuild.h"
2222
#include <assert.h>
23
-#include <dirent.h>
2423
#include <errno.h>
2524
2625
/*
2726
** Make changes to the stable part of the schema (the part that is not
2827
** simply deleted and reconstructed on a rebuild) to bring the schema
@@ -818,28 +817,28 @@
818817
/*
819818
** Recursively read all files from the directory zPath and install
820819
** every file read as a new artifact in the repository.
821820
*/
822821
void recon_read_dir(char *zPath){
823
- DIR *d;
824
- struct dirent *pEntry;
822
+ FOSSIL_DIR *d;
823
+ struct fossil_dirent *pEntry;
825824
Blob aContent; /* content of the just read artifact */
826825
static int nFileRead = 0;
827
- char *zMbcsPath;
826
+ void *zUnicodePath;
828827
char *zUtf8Name;
829828
830
- zMbcsPath = fossil_utf8_to_mbcs(zPath);
831
- d = opendir(zMbcsPath);
829
+ zUnicodePath = fossil_utf8_to_unicode(zPath);
830
+ d = fossil_opendir(zUnicodePath);
832831
if( d ){
833
- while( (pEntry=readdir(d))!=0 ){
832
+ while( (pEntry=fossil_readdir(d))!=0 ){
834833
Blob path;
835834
char *zSubpath;
836835
837836
if( pEntry->d_name[0]=='.' ){
838837
continue;
839838
}
840
- zUtf8Name = fossil_mbcs_to_utf8(pEntry->d_name);
839
+ zUtf8Name = fossil_unicode_to_utf8(pEntry->d_name);
841840
zSubpath = mprintf("%s/%s", zPath, zUtf8Name);
842841
fossil_mbcs_free(zUtf8Name);
843842
if( file_isdir(zSubpath)==1 ){
844843
recon_read_dir(zSubpath);
845844
}
@@ -854,16 +853,16 @@
854853
blob_reset(&aContent);
855854
free(zSubpath);
856855
fossil_print("\r%d", ++nFileRead);
857856
fflush(stdout);
858857
}
859
- closedir(d);
858
+ fossil_closedir(d);
860859
}else {
861860
fossil_panic("encountered error %d while trying to open \"%s\".",
862861
errno, g.argv[3]);
863862
}
864
- fossil_mbcs_free(zMbcsPath);
863
+ fossil_mbcs_free(zUnicodePath);
865864
}
866865
867866
/*
868867
** COMMAND: reconstruct*
869868
**
870869
--- src/rebuild.c
+++ src/rebuild.c
@@ -1,7 +1,7 @@
1 /*
2 ** Copyright (c) 2007 D. Richard Hipp
3 **
4 ** This program is free software; you can redistribute it and/or
5 ** modify it under the terms of the Simplified BSD License (also
6 ** known as the "2-Clause License" or "FreeBSD License".)
7
@@ -18,11 +18,10 @@
18 ** This file contains code used to rebuild the database.
19 */
20 #include "config.h"
21 #include "rebuild.h"
22 #include <assert.h>
23 #include <dirent.h>
24 #include <errno.h>
25
26 /*
27 ** Make changes to the stable part of the schema (the part that is not
28 ** simply deleted and reconstructed on a rebuild) to bring the schema
@@ -818,28 +817,28 @@
818 /*
819 ** Recursively read all files from the directory zPath and install
820 ** every file read as a new artifact in the repository.
821 */
822 void recon_read_dir(char *zPath){
823 DIR *d;
824 struct dirent *pEntry;
825 Blob aContent; /* content of the just read artifact */
826 static int nFileRead = 0;
827 char *zMbcsPath;
828 char *zUtf8Name;
829
830 zMbcsPath = fossil_utf8_to_mbcs(zPath);
831 d = opendir(zMbcsPath);
832 if( d ){
833 while( (pEntry=readdir(d))!=0 ){
834 Blob path;
835 char *zSubpath;
836
837 if( pEntry->d_name[0]=='.' ){
838 continue;
839 }
840 zUtf8Name = fossil_mbcs_to_utf8(pEntry->d_name);
841 zSubpath = mprintf("%s/%s", zPath, zUtf8Name);
842 fossil_mbcs_free(zUtf8Name);
843 if( file_isdir(zSubpath)==1 ){
844 recon_read_dir(zSubpath);
845 }
@@ -854,16 +853,16 @@
854 blob_reset(&aContent);
855 free(zSubpath);
856 fossil_print("\r%d", ++nFileRead);
857 fflush(stdout);
858 }
859 closedir(d);
860 }else {
861 fossil_panic("encountered error %d while trying to open \"%s\".",
862 errno, g.argv[3]);
863 }
864 fossil_mbcs_free(zMbcsPath);
865 }
866
867 /*
868 ** COMMAND: reconstruct*
869 **
870
--- src/rebuild.c
+++ src/rebuild.c
@@ -1,7 +1,7 @@
1 /*
2 ** Copyright © 2007 D. Richard Hipp
3 **
4 ** This program is free software; you can redistribute it and/or
5 ** modify it under the terms of the Simplified BSD License (also
6 ** known as the "2-Clause License" or "FreeBSD License".)
7
@@ -18,11 +18,10 @@
18 ** This file contains code used to rebuild the database.
19 */
20 #include "config.h"
21 #include "rebuild.h"
22 #include <assert.h>
 
23 #include <errno.h>
24
25 /*
26 ** Make changes to the stable part of the schema (the part that is not
27 ** simply deleted and reconstructed on a rebuild) to bring the schema
@@ -818,28 +817,28 @@
817 /*
818 ** Recursively read all files from the directory zPath and install
819 ** every file read as a new artifact in the repository.
820 */
821 void recon_read_dir(char *zPath){
822 FOSSIL_DIR *d;
823 struct fossil_dirent *pEntry;
824 Blob aContent; /* content of the just read artifact */
825 static int nFileRead = 0;
826 void *zUnicodePath;
827 char *zUtf8Name;
828
829 zUnicodePath = fossil_utf8_to_unicode(zPath);
830 d = fossil_opendir(zUnicodePath);
831 if( d ){
832 while( (pEntry=fossil_readdir(d))!=0 ){
833 Blob path;
834 char *zSubpath;
835
836 if( pEntry->d_name[0]=='.' ){
837 continue;
838 }
839 zUtf8Name = fossil_unicode_to_utf8(pEntry->d_name);
840 zSubpath = mprintf("%s/%s", zPath, zUtf8Name);
841 fossil_mbcs_free(zUtf8Name);
842 if( file_isdir(zSubpath)==1 ){
843 recon_read_dir(zSubpath);
844 }
@@ -854,16 +853,16 @@
853 blob_reset(&aContent);
854 free(zSubpath);
855 fossil_print("\r%d", ++nFileRead);
856 fflush(stdout);
857 }
858 fossil_closedir(d);
859 }else {
860 fossil_panic("encountered error %d while trying to open \"%s\".",
861 errno, g.argv[3]);
862 }
863 fossil_mbcs_free(zUnicodePath);
864 }
865
866 /*
867 ** COMMAND: reconstruct*
868 **
869
+9 -14
--- src/vfile.c
+++ src/vfile.c
@@ -1,7 +1,7 @@
11
/*
2
-** Copyright (c) 2007 D. Richard Hipp
2
+** Copyright © 2007 D. Richard Hipp
33
**
44
** This program is free software; you can redistribute it and/or
55
** modify it under the terms of the Simplified BSD License (also
66
** known as the "2-Clause License" or "FreeBSD License".)
77
@@ -19,15 +19,10 @@
1919
*/
2020
#include "config.h"
2121
#include "vfile.h"
2222
#include <assert.h>
2323
#include <sys/types.h>
24
-#if defined(__DMC__)
25
-#include "dirent.h"
26
-#else
27
-#include <dirent.h>
28
-#endif
2924
3025
/*
3126
** The input is guaranteed to be a 40-character well-formed UUID.
3227
** Find its rid.
3328
*/
@@ -381,18 +376,18 @@
381376
** Any files or directories that match the glob pattern pIgnore are
382377
** excluded from the scan. Name matching occurs after the first
383378
** nPrefix characters are elided from the filename.
384379
*/
385380
void vfile_scan(Blob *pPath, int nPrefix, int allFlag, Glob *pIgnore){
386
- DIR *d;
381
+ FOSSIL_DIR *d;
387382
int origSize;
388383
const char *zDir;
389
- struct dirent *pEntry;
384
+ struct fossil_dirent *pEntry;
390385
int skipAll = 0;
391386
static Stmt ins;
392387
static int depth = 0;
393
- char *zMbcs;
388
+ void *zMbcs;
394389
395390
origSize = blob_size(pPath);
396391
if( pIgnore ){
397392
blob_appendf(pPath, "/");
398393
if( glob_match(pIgnore, &blob_str(pPath)[nPrefix+1]) ) skipAll = 1;
@@ -407,22 +402,22 @@
407402
);
408403
}
409404
depth++;
410405
411406
zDir = blob_str(pPath);
412
- zMbcs = fossil_utf8_to_mbcs(zDir);
413
- d = opendir(zMbcs);
407
+ zMbcs = fossil_utf8_to_unicode(zDir);
408
+ d = fossil_opendir(zMbcs);
414409
if( d ){
415
- while( (pEntry=readdir(d))!=0 ){
410
+ while( (pEntry=fossil_readdir(d))!=0 ){
416411
char *zPath;
417412
char *zUtf8;
418413
if( pEntry->d_name[0]=='.' ){
419414
if( !allFlag ) continue;
420415
if( pEntry->d_name[1]==0 ) continue;
421416
if( pEntry->d_name[1]=='.' && pEntry->d_name[2]==0 ) continue;
422417
}
423
- zUtf8 = fossil_mbcs_to_utf8(pEntry->d_name);
418
+ zUtf8 = fossil_unicode_to_utf8(pEntry->d_name);
424419
blob_appendf(pPath, "/%s", zUtf8);
425420
fossil_mbcs_free(zUtf8);
426421
zPath = blob_str(pPath);
427422
if( glob_match(pIgnore, &zPath[nPrefix+1]) ){
428423
/* do nothing */
@@ -435,11 +430,11 @@
435430
db_step(&ins);
436431
db_reset(&ins);
437432
}
438433
blob_resize(pPath, origSize);
439434
}
440
- closedir(d);
435
+ fossil_closedir(d);
441436
}
442437
fossil_mbcs_free(zMbcs);
443438
444439
depth--;
445440
if( depth==0 ){
446441
--- src/vfile.c
+++ src/vfile.c
@@ -1,7 +1,7 @@
1 /*
2 ** Copyright (c) 2007 D. Richard Hipp
3 **
4 ** This program is free software; you can redistribute it and/or
5 ** modify it under the terms of the Simplified BSD License (also
6 ** known as the "2-Clause License" or "FreeBSD License".)
7
@@ -19,15 +19,10 @@
19 */
20 #include "config.h"
21 #include "vfile.h"
22 #include <assert.h>
23 #include <sys/types.h>
24 #if defined(__DMC__)
25 #include "dirent.h"
26 #else
27 #include <dirent.h>
28 #endif
29
30 /*
31 ** The input is guaranteed to be a 40-character well-formed UUID.
32 ** Find its rid.
33 */
@@ -381,18 +376,18 @@
381 ** Any files or directories that match the glob pattern pIgnore are
382 ** excluded from the scan. Name matching occurs after the first
383 ** nPrefix characters are elided from the filename.
384 */
385 void vfile_scan(Blob *pPath, int nPrefix, int allFlag, Glob *pIgnore){
386 DIR *d;
387 int origSize;
388 const char *zDir;
389 struct dirent *pEntry;
390 int skipAll = 0;
391 static Stmt ins;
392 static int depth = 0;
393 char *zMbcs;
394
395 origSize = blob_size(pPath);
396 if( pIgnore ){
397 blob_appendf(pPath, "/");
398 if( glob_match(pIgnore, &blob_str(pPath)[nPrefix+1]) ) skipAll = 1;
@@ -407,22 +402,22 @@
407 );
408 }
409 depth++;
410
411 zDir = blob_str(pPath);
412 zMbcs = fossil_utf8_to_mbcs(zDir);
413 d = opendir(zMbcs);
414 if( d ){
415 while( (pEntry=readdir(d))!=0 ){
416 char *zPath;
417 char *zUtf8;
418 if( pEntry->d_name[0]=='.' ){
419 if( !allFlag ) continue;
420 if( pEntry->d_name[1]==0 ) continue;
421 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]==0 ) continue;
422 }
423 zUtf8 = fossil_mbcs_to_utf8(pEntry->d_name);
424 blob_appendf(pPath, "/%s", zUtf8);
425 fossil_mbcs_free(zUtf8);
426 zPath = blob_str(pPath);
427 if( glob_match(pIgnore, &zPath[nPrefix+1]) ){
428 /* do nothing */
@@ -435,11 +430,11 @@
435 db_step(&ins);
436 db_reset(&ins);
437 }
438 blob_resize(pPath, origSize);
439 }
440 closedir(d);
441 }
442 fossil_mbcs_free(zMbcs);
443
444 depth--;
445 if( depth==0 ){
446
--- src/vfile.c
+++ src/vfile.c
@@ -1,7 +1,7 @@
1 /*
2 ** Copyright © 2007 D. Richard Hipp
3 **
4 ** This program is free software; you can redistribute it and/or
5 ** modify it under the terms of the Simplified BSD License (also
6 ** known as the "2-Clause License" or "FreeBSD License".)
7
@@ -19,15 +19,10 @@
19 */
20 #include "config.h"
21 #include "vfile.h"
22 #include <assert.h>
23 #include <sys/types.h>
 
 
 
 
 
24
25 /*
26 ** The input is guaranteed to be a 40-character well-formed UUID.
27 ** Find its rid.
28 */
@@ -381,18 +376,18 @@
376 ** Any files or directories that match the glob pattern pIgnore are
377 ** excluded from the scan. Name matching occurs after the first
378 ** nPrefix characters are elided from the filename.
379 */
380 void vfile_scan(Blob *pPath, int nPrefix, int allFlag, Glob *pIgnore){
381 FOSSIL_DIR *d;
382 int origSize;
383 const char *zDir;
384 struct fossil_dirent *pEntry;
385 int skipAll = 0;
386 static Stmt ins;
387 static int depth = 0;
388 void *zMbcs;
389
390 origSize = blob_size(pPath);
391 if( pIgnore ){
392 blob_appendf(pPath, "/");
393 if( glob_match(pIgnore, &blob_str(pPath)[nPrefix+1]) ) skipAll = 1;
@@ -407,22 +402,22 @@
402 );
403 }
404 depth++;
405
406 zDir = blob_str(pPath);
407 zMbcs = fossil_utf8_to_unicode(zDir);
408 d = fossil_opendir(zMbcs);
409 if( d ){
410 while( (pEntry=fossil_readdir(d))!=0 ){
411 char *zPath;
412 char *zUtf8;
413 if( pEntry->d_name[0]=='.' ){
414 if( !allFlag ) continue;
415 if( pEntry->d_name[1]==0 ) continue;
416 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]==0 ) continue;
417 }
418 zUtf8 = fossil_unicode_to_utf8(pEntry->d_name);
419 blob_appendf(pPath, "/%s", zUtf8);
420 fossil_mbcs_free(zUtf8);
421 zPath = blob_str(pPath);
422 if( glob_match(pIgnore, &zPath[nPrefix+1]) ){
423 /* do nothing */
@@ -435,11 +430,11 @@
430 db_step(&ins);
431 db_reset(&ins);
432 }
433 blob_resize(pPath, origSize);
434 }
435 fossil_closedir(d);
436 }
437 fossil_mbcs_free(zMbcs);
438
439 depth--;
440 if( depth==0 ){
441
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -61,71 +61,70 @@
6161
#include <windows.h>
6262
#include <string.h>
6363
#include <assert.h>
6464
6565
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 */
66
+typedef struct _wdirent
67
+{
68
+ wchar_t d_name[MAX_PATH + 1]; /* current dir entry (unicode char string) */
69
+ WIN32_FIND_DATAW data; /* file attributes */
70
+} _wdirent;
71
+
72
+
73
+typedef struct _WDIR
74
+{
75
+ _wdirent current; /* Current directory entry */
7676
int cached; /* Indicates un-processed entry in memory */
7777
HANDLE search_handle; /* File search handle */
78
- char patt[MAX_PATH + 3]; /* search pattern (3 = pattern + "\\*\0") */
79
-} DIR;
78
+ wchar_t patt[MAX_PATH + 3]; /* search pattern (3 = pattern + "\\*\0") */
79
+} _WDIR;
8080
8181
8282
/* 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);
83
+static _WDIR *_wopendir (const wchar_t *dirname);
84
+static struct _wdirent *_wreaddir (_WDIR *dirp);
85
+static int _wclosedir (_WDIR *dirp);
8786
8887
8988
/* Use the new safe string functions introduced in Visual Studio 2005 */
9089
#if defined(_MSC_VER) && _MSC_VER >= 1400
91
-# define STRNCPY(dest,src,size) strncpy_s((dest),(size),(src),_TRUNCATE)
90
+# define STRNCPY(dest,src,size) wcsncpy_s((dest),(size),(src),_TRUNCATE)
9291
#else
93
-# define STRNCPY(dest,src,size) strncpy((dest),(src),(size))
92
+# define STRNCPY(dest,src,size) wcsncpy((dest),(src),(size))
9493
#endif
9594
9695
9796
/*****************************************************************************
9897
* Open directory stream DIRNAME for read and return a pointer to the
9998
* internal working area that is used to retrieve individual directory
10099
* entries.
101100
*/
102
-static DIR *opendir(const char *dirname)
101
+static _WDIR *_wopendir(const wchar_t *dirname)
103102
{
104
- DIR *dirp;
103
+ _WDIR *dirp;
105104
assert (dirname != NULL);
106
- assert (strlen (dirname) < MAX_PATH);
105
+ assert (wcslen (dirname) < MAX_PATH);
107106
108
- /* construct new DIR structure */
109
- dirp = (DIR*) malloc (sizeof (struct DIR));
107
+ /* construct new _WDIR structure */
108
+ dirp = (_WDIR*) malloc (sizeof (struct _WDIR));
110109
if (dirp != NULL) {
111
- char *p;
110
+ wchar_t *p;
112111
113112
/* take directory name... */
114113
STRNCPY (dirp->patt, dirname, sizeof(dirp->patt));
115114
dirp->patt[MAX_PATH] = '\0';
116115
117116
/* ... and append search pattern to it */
118
- p = strchr (dirp->patt, '\0');
117
+ p = wcschr (dirp->patt, '\0');
119118
if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
120119
*p++ = '\\';
121120
}
122121
*p++ = '*';
123122
*p = '\0';
124123
125124
/* open stream and retrieve first file */
126
- dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->current.data);
125
+ dirp->search_handle = FindFirstFileW (dirp->patt, &dirp->current.data);
127126
if (dirp->search_handle == INVALID_HANDLE_VALUE) {
128127
/* invalid search pattern? */
129128
free (dirp);
130129
return NULL;
131130
}
@@ -143,11 +142,11 @@
143142
* containing the name of the entry in d_name field. Individual directory
144143
* entries returned by this very function include regular files,
145144
* sub-directories, pseudo-directories "." and "..", but also volume labels,
146145
* hidden files and system files may be returned.
147146
*/
148
-static struct dirent *readdir(DIR *dirp)
147
+static struct _wdirent *_wreaddir(_WDIR *dirp)
149148
{
150149
assert (dirp != NULL);
151150
152151
if (dirp->search_handle == INVALID_HANDLE_VALUE) {
153152
/* directory stream was opened/rewound incorrectly or ended normally */
@@ -158,11 +157,11 @@
158157
if (dirp->cached != 0) {
159158
/* a valid directory entry already in memory */
160159
dirp->cached = 0;
161160
} else {
162161
/* read next directory entry from disk */
163
- if (FindNextFileA (dirp->search_handle, &dirp->current.data) == FALSE) {
162
+ if (FindNextFileW (dirp->search_handle, &dirp->current.data) == FALSE) {
164163
/* the very last file has been processed or an error occured */
165164
FindClose (dirp->search_handle);
166165
dirp->search_handle = INVALID_HANDLE_VALUE;
167166
return NULL;
168167
}
@@ -181,11 +180,11 @@
181180
/*****************************************************************************
182181
* Close directory stream opened by opendir() function. Close of the
183182
* directory stream invalidates the DIR structure as well as any previously
184183
* read directory entry.
185184
*/
186
-static int closedir(DIR *dirp)
185
+static int _wclosedir(_WDIR *dirp)
187186
{
188187
assert (dirp != NULL);
189188
190189
/* release search handle */
191190
if (dirp->search_handle != INVALID_HANDLE_VALUE) {
@@ -195,36 +194,7 @@
195194
196195
/* release directory handle */
197196
free (dirp);
198197
return 0;
199198
}
200
-
201
-
202
-/*****************************************************************************
203
- * Resets the position of the directory stream to which dirp refers to the
204
- * beginning of the directory. It also causes the directory stream to refer
205
- * to the current state of the corresponding directory, as a call to opendir()
206
- * would have done. If dirp does not refer to a directory stream, the effect
207
- * is undefined.
208
- */
209
-static void rewinddir(DIR* dirp)
210
-{
211
- /* release search handle */
212
- if (dirp->search_handle != INVALID_HANDLE_VALUE) {
213
- FindClose (dirp->search_handle);
214
- dirp->search_handle = INVALID_HANDLE_VALUE;
215
- }
216
-
217
- /* open new search handle and retrieve first file */
218
- dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->current.data);
219
- if (dirp->search_handle == INVALID_HANDLE_VALUE) {
220
- /* invalid search pattern? */
221
- free (dirp);
222
- return;
223
- }
224
-
225
- /* there is an un-processed directory entry in memory now */
226
- dirp->cached = 1;
227
-}
228
-
229199
230200
#endif /*DIRENT_H*/
231201
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -61,71 +61,70 @@
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 }
@@ -143,11 +142,11 @@
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 */
@@ -158,11 +157,11 @@
158 if (dirp->cached != 0) {
159 /* a valid directory entry already in memory */
160 dirp->cached = 0;
161 } else {
162 /* read next directory entry from disk */
163 if (FindNextFileA (dirp->search_handle, &dirp->current.data) == FALSE) {
164 /* the very last file has been processed or an error occured */
165 FindClose (dirp->search_handle);
166 dirp->search_handle = INVALID_HANDLE_VALUE;
167 return NULL;
168 }
@@ -181,11 +180,11 @@
181 /*****************************************************************************
182 * Close directory stream opened by opendir() function. Close of the
183 * directory stream invalidates the DIR structure as well as any previously
184 * read directory entry.
185 */
186 static int closedir(DIR *dirp)
187 {
188 assert (dirp != NULL);
189
190 /* release search handle */
191 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
@@ -195,36 +194,7 @@
195
196 /* release directory handle */
197 free (dirp);
198 return 0;
199 }
200
201
202 /*****************************************************************************
203 * Resets the position of the directory stream to which dirp refers to the
204 * beginning of the directory. It also causes the directory stream to refer
205 * to the current state of the corresponding directory, as a call to opendir()
206 * would have done. If dirp does not refer to a directory stream, the effect
207 * is undefined.
208 */
209 static void rewinddir(DIR* dirp)
210 {
211 /* release search handle */
212 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
213 FindClose (dirp->search_handle);
214 dirp->search_handle = INVALID_HANDLE_VALUE;
215 }
216
217 /* open new search handle and retrieve first file */
218 dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->current.data);
219 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
220 /* invalid search pattern? */
221 free (dirp);
222 return;
223 }
224
225 /* there is an un-processed directory entry in memory now */
226 dirp->cached = 1;
227 }
228
229
230 #endif /*DIRENT_H*/
231
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -61,71 +61,70 @@
61 #include <windows.h>
62 #include <string.h>
63 #include <assert.h>
64
65
66 typedef struct _wdirent
67 {
68 wchar_t d_name[MAX_PATH + 1]; /* current dir entry (unicode char string) */
69 WIN32_FIND_DATAW data; /* file attributes */
70 } _wdirent;
71
72
73 typedef struct _WDIR
74 {
75 _wdirent current; /* Current directory entry */
76 int cached; /* Indicates un-processed entry in memory */
77 HANDLE search_handle; /* File search handle */
78 wchar_t patt[MAX_PATH + 3]; /* search pattern (3 = pattern + "\\*\0") */
79 } _WDIR;
80
81
82 /* Forward declarations */
83 static _WDIR *_wopendir (const wchar_t *dirname);
84 static struct _wdirent *_wreaddir (_WDIR *dirp);
85 static int _wclosedir (_WDIR *dirp);
 
86
87
88 /* Use the new safe string functions introduced in Visual Studio 2005 */
89 #if defined(_MSC_VER) && _MSC_VER >= 1400
90 # define STRNCPY(dest,src,size) wcsncpy_s((dest),(size),(src),_TRUNCATE)
91 #else
92 # define STRNCPY(dest,src,size) wcsncpy((dest),(src),(size))
93 #endif
94
95
96 /*****************************************************************************
97 * Open directory stream DIRNAME for read and return a pointer to the
98 * internal working area that is used to retrieve individual directory
99 * entries.
100 */
101 static _WDIR *_wopendir(const wchar_t *dirname)
102 {
103 _WDIR *dirp;
104 assert (dirname != NULL);
105 assert (wcslen (dirname) < MAX_PATH);
106
107 /* construct new _WDIR structure */
108 dirp = (_WDIR*) malloc (sizeof (struct _WDIR));
109 if (dirp != NULL) {
110 wchar_t *p;
111
112 /* take directory name... */
113 STRNCPY (dirp->patt, dirname, sizeof(dirp->patt));
114 dirp->patt[MAX_PATH] = '\0';
115
116 /* ... and append search pattern to it */
117 p = wcschr (dirp->patt, '\0');
118 if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
119 *p++ = '\\';
120 }
121 *p++ = '*';
122 *p = '\0';
123
124 /* open stream and retrieve first file */
125 dirp->search_handle = FindFirstFileW (dirp->patt, &dirp->current.data);
126 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
127 /* invalid search pattern? */
128 free (dirp);
129 return NULL;
130 }
@@ -143,11 +142,11 @@
142 * containing the name of the entry in d_name field. Individual directory
143 * entries returned by this very function include regular files,
144 * sub-directories, pseudo-directories "." and "..", but also volume labels,
145 * hidden files and system files may be returned.
146 */
147 static struct _wdirent *_wreaddir(_WDIR *dirp)
148 {
149 assert (dirp != NULL);
150
151 if (dirp->search_handle == INVALID_HANDLE_VALUE) {
152 /* directory stream was opened/rewound incorrectly or ended normally */
@@ -158,11 +157,11 @@
157 if (dirp->cached != 0) {
158 /* a valid directory entry already in memory */
159 dirp->cached = 0;
160 } else {
161 /* read next directory entry from disk */
162 if (FindNextFileW (dirp->search_handle, &dirp->current.data) == FALSE) {
163 /* the very last file has been processed or an error occured */
164 FindClose (dirp->search_handle);
165 dirp->search_handle = INVALID_HANDLE_VALUE;
166 return NULL;
167 }
@@ -181,11 +180,11 @@
180 /*****************************************************************************
181 * Close directory stream opened by opendir() function. Close of the
182 * directory stream invalidates the DIR structure as well as any previously
183 * read directory entry.
184 */
185 static int _wclosedir(_WDIR *dirp)
186 {
187 assert (dirp != NULL);
188
189 /* release search handle */
190 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
@@ -195,36 +194,7 @@
194
195 /* release directory handle */
196 free (dirp);
197 return 0;
198 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
200 #endif /*DIRENT_H*/
201

Keyboard Shortcuts

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