Fossil SCM

simplify switching between widechar and unicode variant of dirent

jan.nijtmans 2012-09-12 22:01 UTC restore-win95
Commit a8f7a8ac24ea067db5af3cd3a7087ac167bee3a8
+7 -16
--- src/file.c
+++ src/file.c
@@ -1005,33 +1005,24 @@
10051005
blob_reset(&onDisk);
10061006
return rc==0;
10071007
}
10081008
10091009
/*
1010
-** Portable unicode implementation of opendir()
1010
+** Portable unicode implementation of opendir() for win32
10111011
*/
10121012
#if INTERFACE
10131013
1014
+#include <dirent.h>
10141015
#if defined(_WIN32) && defined(UNICODE)
1015
-# include <dirent.h>
1016
-# define FOSSIL_DIR _WDIR
1017
-# define fossil_dirent _wdirent
1018
-# define fossil_opendir _wopendir
1019
-# define fossil_readdir _wreaddir
1020
-# define fossil_closedir _wclosedir
1021
-#else
1022
-# include <dirent.h>
1023
-# define FOSSIL_DIR DIR
1024
-# define fossil_dirent dirent
1025
-# define fossil_opendir opendir
1026
-# define fossil_readdir readdir
1027
-# define fossil_closedir closedir
1016
+# define dirent _wdirent
1017
+# define opendir _wopendir
1018
+# define readdir _wreaddir
1019
+# define closedir _wclosedir
1020
+# define DIR _WDIR
10281021
#endif
10291022
10301023
#endif /* INTERFACE */
1031
-
1032
-
10331024
10341025
/**************************************************************************
10351026
** The following routines translate between MBCS and UTF8 on windows.
10361027
** Since everything is always UTF8 on unix, these routines are no-ops
10371028
** there.
10381029
--- src/file.c
+++ src/file.c
@@ -1005,33 +1005,24 @@
1005 blob_reset(&onDisk);
1006 return rc==0;
1007 }
1008
1009 /*
1010 ** Portable unicode implementation of opendir()
1011 */
1012 #if INTERFACE
1013
 
1014 #if defined(_WIN32) && defined(UNICODE)
1015 # include <dirent.h>
1016 # define FOSSIL_DIR _WDIR
1017 # define fossil_dirent _wdirent
1018 # define fossil_opendir _wopendir
1019 # define fossil_readdir _wreaddir
1020 # define fossil_closedir _wclosedir
1021 #else
1022 # include <dirent.h>
1023 # define FOSSIL_DIR DIR
1024 # define fossil_dirent dirent
1025 # define fossil_opendir opendir
1026 # define fossil_readdir readdir
1027 # define fossil_closedir closedir
1028 #endif
1029
1030 #endif /* INTERFACE */
1031
1032
1033
1034 /**************************************************************************
1035 ** The following routines translate between MBCS and UTF8 on windows.
1036 ** Since everything is always UTF8 on unix, these routines are no-ops
1037 ** there.
1038
--- src/file.c
+++ src/file.c
@@ -1005,33 +1005,24 @@
1005 blob_reset(&onDisk);
1006 return rc==0;
1007 }
1008
1009 /*
1010 ** Portable unicode implementation of opendir() for win32
1011 */
1012 #if INTERFACE
1013
1014 #include <dirent.h>
1015 #if defined(_WIN32) && defined(UNICODE)
1016 # define dirent _wdirent
1017 # define opendir _wopendir
1018 # define readdir _wreaddir
1019 # define closedir _wclosedir
1020 # define DIR _WDIR
 
 
 
 
 
 
 
 
1021 #endif
1022
1023 #endif /* INTERFACE */
 
 
1024
1025 /**************************************************************************
1026 ** The following routines translate between MBCS and UTF8 on windows.
1027 ** Since everything is always UTF8 on unix, these routines are no-ops
1028 ** there.
1029
+8 -8
--- src/rebuild.c
+++ src/rebuild.c
@@ -817,21 +817,21 @@
817817
/*
818818
** Recursively read all files from the directory zPath and install
819819
** every file read as a new artifact in the repository.
820820
*/
821821
void recon_read_dir(char *zPath){
822
- FOSSIL_DIR *d;
823
- struct fossil_dirent *pEntry;
822
+ DIR *d;
823
+ struct dirent *pEntry;
824824
Blob aContent; /* content of the just read artifact */
825825
static int nFileRead = 0;
826
- void *zUnicodePath;
826
+ void *zMbcsPath;
827827
char *zUtf8Name;
828828
829
- zUnicodePath = fossil_utf8_to_mbcs(zPath);
830
- d = fossil_opendir(zUnicodePath);
829
+ zMbcsPath = fossil_utf8_to_mbcs(zPath);
830
+ d = opendir(zMbcsPath);
831831
if( d ){
832
- while( (pEntry=fossil_readdir(d))!=0 ){
832
+ while( (pEntry=readdir(d))!=0 ){
833833
Blob path;
834834
char *zSubpath;
835835
836836
if( pEntry->d_name[0]=='.' ){
837837
continue;
@@ -853,16 +853,16 @@
853853
blob_reset(&aContent);
854854
free(zSubpath);
855855
fossil_print("\r%d", ++nFileRead);
856856
fflush(stdout);
857857
}
858
- fossil_closedir(d);
858
+ closedir(d);
859859
}else {
860860
fossil_panic("encountered error %d while trying to open \"%s\".",
861861
errno, g.argv[3]);
862862
}
863
- fossil_mbcs_free(zUnicodePath);
863
+ fossil_mbcs_free(zMbcsPath);
864864
}
865865
866866
/*
867867
** COMMAND: reconstruct*
868868
**
869869
--- src/rebuild.c
+++ src/rebuild.c
@@ -817,21 +817,21 @@
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_mbcs(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;
@@ -853,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
--- src/rebuild.c
+++ src/rebuild.c
@@ -817,21 +817,21 @@
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 DIR *d;
823 struct dirent *pEntry;
824 Blob aContent; /* content of the just read artifact */
825 static int nFileRead = 0;
826 void *zMbcsPath;
827 char *zUtf8Name;
828
829 zMbcsPath = fossil_utf8_to_mbcs(zPath);
830 d = opendir(zMbcsPath);
831 if( d ){
832 while( (pEntry=readdir(d))!=0 ){
833 Blob path;
834 char *zSubpath;
835
836 if( pEntry->d_name[0]=='.' ){
837 continue;
@@ -853,16 +853,16 @@
853 blob_reset(&aContent);
854 free(zSubpath);
855 fossil_print("\r%d", ++nFileRead);
856 fflush(stdout);
857 }
858 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(zMbcsPath);
864 }
865
866 /*
867 ** COMMAND: reconstruct*
868 **
869
+5 -5
--- src/vfile.c
+++ src/vfile.c
@@ -376,14 +376,14 @@
376376
** Any files or directories that match the glob pattern pIgnore are
377377
** excluded from the scan. Name matching occurs after the first
378378
** nPrefix characters are elided from the filename.
379379
*/
380380
void vfile_scan(Blob *pPath, int nPrefix, int allFlag, Glob *pIgnore){
381
- FOSSIL_DIR *d;
381
+ DIR *d;
382382
int origSize;
383383
const char *zDir;
384
- struct fossil_dirent *pEntry;
384
+ struct dirent *pEntry;
385385
int skipAll = 0;
386386
static Stmt ins;
387387
static int depth = 0;
388388
void *zMbcs;
389389
@@ -403,13 +403,13 @@
403403
}
404404
depth++;
405405
406406
zDir = blob_str(pPath);
407407
zMbcs = fossil_utf8_to_mbcs(zDir);
408
- d = fossil_opendir(zMbcs);
408
+ d = opendir(zMbcs);
409409
if( d ){
410
- while( (pEntry=fossil_readdir(d))!=0 ){
410
+ while( (pEntry=readdir(d))!=0 ){
411411
char *zPath;
412412
char *zUtf8;
413413
if( pEntry->d_name[0]=='.' ){
414414
if( !allFlag ) continue;
415415
if( pEntry->d_name[1]==0 ) continue;
@@ -430,11 +430,11 @@
430430
db_step(&ins);
431431
db_reset(&ins);
432432
}
433433
blob_resize(pPath, origSize);
434434
}
435
- fossil_closedir(d);
435
+ closedir(d);
436436
}
437437
fossil_mbcs_free(zMbcs);
438438
439439
depth--;
440440
if( depth==0 ){
441441
--- src/vfile.c
+++ src/vfile.c
@@ -376,14 +376,14 @@
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
@@ -403,13 +403,13 @@
403 }
404 depth++;
405
406 zDir = blob_str(pPath);
407 zMbcs = fossil_utf8_to_mbcs(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;
@@ -430,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
--- src/vfile.c
+++ src/vfile.c
@@ -376,14 +376,14 @@
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 DIR *d;
382 int origSize;
383 const char *zDir;
384 struct dirent *pEntry;
385 int skipAll = 0;
386 static Stmt ins;
387 static int depth = 0;
388 void *zMbcs;
389
@@ -403,13 +403,13 @@
403 }
404 depth++;
405
406 zDir = blob_str(pPath);
407 zMbcs = fossil_utf8_to_mbcs(zDir);
408 d = opendir(zMbcs);
409 if( d ){
410 while( (pEntry=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;
@@ -430,11 +430,11 @@
430 db_step(&ins);
431 db_reset(&ins);
432 }
433 blob_resize(pPath, origSize);
434 }
435 closedir(d);
436 }
437 fossil_mbcs_free(zMbcs);
438
439 depth--;
440 if( depth==0 ){
441
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -20,13 +20,12 @@
2020
* IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR
2121
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2222
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2323
* OTHER DEALINGS IN THE SOFTWARE.
2424
*
25
- * Aug 30, 2012, Jan Nijtmans
26
- * Remove rewinddir() (not necessary for fossil)
27
- * Replace everything with its wide-character variant.
25
+ * Sept 12, 2012, Jan Nijtmans
26
+ * Switchable wide-character variant.
2827
*
2928
* Mar 15, 2011, Toni Ronkko
3029
* Defined FILE_ATTRIBUTE_DEVICE for MSVC 6.0.
3130
*
3231
* Aug 11, 2010, Toni Ronkko
@@ -149,27 +148,26 @@
149148
#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
150149
#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
151150
#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
152151
#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
153152
154
-#ifdef __cplusplus
155
-extern "C" {
156
-#endif
157
-
158153
#ifdef UNICODE
159154
# define dirent _wdirent
160155
# define opendir _wopendir
161156
# define readdir _wreaddir
162157
# define closedir _wclosedir
158
+# define rewinddir _wrewinddir
163159
# define DIR _WDIR
164
-#else
165
-# define wchar_t char
160
+#endif
161
+
162
+#ifdef __cplusplus
163
+extern "C" {
166164
#endif
167165
168166
typedef struct dirent
169167
{
170
- wchar_t d_name[MAX_PATH + 1]; /* File name */
168
+ TCHAR d_name[MAX_PATH + 1]; /* File name */
171169
size_t d_namlen; /* Length of name without \0 */
172170
int d_type; /* File type */
173171
} dirent;
174172
175173
@@ -177,18 +175,19 @@
177175
{
178176
dirent curentry; /* Current directory entry */
179177
WIN32_FIND_DATA find_data; /* Private file data */
180178
int cached; /* True if data is valid */
181179
HANDLE search_handle; /* Win32 search handle */
182
- wchar_t patt[MAX_PATH + 3]; /* Initial directory name */
180
+ TCHAR patt[MAX_PATH + 3]; /* Initial directory name */
183181
} DIR;
184182
185183
186184
/* Forward declarations */
187
-static DIR *opendir(const wchar_t *dirname);
185
+static DIR *opendir(const TCHAR *dirname);
188186
static struct dirent *readdir(DIR *dirp);
189187
static int closedir(DIR *dirp);
188
+static void rewinddir(DIR* dirp);
190189
191190
192191
/* Use the new safe string functions introduced in Visual Studio 2005 */
193192
#if defined(_MSC_VER) && _MSC_VER >= 1400
194193
# define DIRENT_STRNCPY(dest,src,size) _tcsncpy_s((dest),(size),(src),_TRUNCATE)
@@ -207,11 +206,11 @@
207206
/*****************************************************************************
208207
* Open directory stream DIRNAME for read and return a pointer to the
209208
* internal working area that is used to retrieve individual directory
210209
* entries.
211210
*/
212
-static DIR *opendir(const wchar_t *dirname)
211
+static DIR *opendir(const TCHAR *dirname)
213212
{
214213
DIR *dirp;
215214
216215
/* ensure that the resulting search pattern will be a valid file name */
217216
if (dirname == NULL) {
@@ -232,11 +231,11 @@
232231
* Convert relative directory name to an absolute one. This
233232
* allows rewinddir() to function correctly when the current working
234233
* directory is changed between opendir() and rewinddir().
235234
*/
236235
if (GetFullPathName (dirname, MAX_PATH, dirp->patt, NULL)) {
237
- wchar_t *p;
236
+ TCHAR *p;
238237
239238
/* append the search pattern "\\*\0" to the directory name */
240239
p = _tcschr (dirp->patt, '\0');
241240
if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
242241
*p++ = '\\';
@@ -302,14 +301,14 @@
302301
dirp->search_handle = INVALID_HANDLE_VALUE;
303302
return NULL;
304303
}
305304
}
306305
307
- /* copy as a unicode character string */
306
+ /* copy as a multibyte/unicode character string */
308307
DIRENT_STRNCPY ( dirp->curentry.d_name,
309308
dirp->find_data.cFileName,
310
- sizeof(dirp->curentry.d_name)/sizeof(dirp->curentry.d_name[0]) );
309
+ sizeof(dirp->curentry.d_name)/sizeof(TCHAR) );
311310
dirp->curentry.d_name[MAX_PATH] = '\0';
312311
313312
/* compute the length of name */
314313
dirp->curentry.d_namlen = _tcslen (dirp->curentry.d_name);
315314
@@ -347,20 +346,45 @@
347346
348347
/* release directory structure */
349348
free (dirp);
350349
return 0;
351350
}
351
+
352
+/*****************************************************************************
353
+ * Resets the position of the directory stream to which dirp refers to the
354
+ * beginning of the directory. It also causes the directory stream to refer
355
+ * to the current state of the corresponding directory, as a call to opendir()
356
+ * would have done. If dirp does not refer to a directory stream, the effect
357
+ * is undefined.
358
+ */
359
+static void rewinddir(DIR* dirp)
360
+{
361
+ if (dirp != NULL) {
362
+ /* release search handle */
363
+ if (dirp->search_handle != INVALID_HANDLE_VALUE) {
364
+ FindClose (dirp->search_handle);
365
+ }
366
+
367
+ /* open new search handle and retrieve the first entry */
368
+ dirp->search_handle = FindFirstFile (dirp->patt, &dirp->find_data);
369
+ if (dirp->search_handle != INVALID_HANDLE_VALUE) {
370
+ /* a directory entry is now waiting in memory */
371
+ dirp->cached = 1;
372
+ } else {
373
+ /* failed to re-open directory: no directory entry in memory */
374
+ dirp->cached = 0;
375
+ }
376
+ }
377
+}
352378
353379
#ifdef UNICODE
354380
# undef dirent
355381
# undef opendir
356382
# undef readdir
357383
# undef closedir
358384
# undef DIR
359
-#else
360
-# undef wchar_t
361385
#endif
362386
363387
#ifdef __cplusplus
364388
}
365389
#endif
366390
#endif /*DIRENT_H*/
367391
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -20,13 +20,12 @@
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 * Aug 30, 2012, Jan Nijtmans
26 * Remove rewinddir() (not necessary for fossil)
27 * Replace everything with its wide-character variant.
28 *
29 * Mar 15, 2011, Toni Ronkko
30 * Defined FILE_ATTRIBUTE_DEVICE for MSVC 6.0.
31 *
32 * Aug 11, 2010, Toni Ronkko
@@ -149,27 +148,26 @@
149 #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
150 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
151 #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
152 #define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
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
@@ -177,18 +175,19 @@
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)
@@ -207,11 +206,11 @@
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) {
@@ -232,11 +231,11 @@
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++ = '\\';
@@ -302,14 +301,14 @@
302 dirp->search_handle = INVALID_HANDLE_VALUE;
303 return NULL;
304 }
305 }
306
307 /* copy as a unicode character string */
308 DIRENT_STRNCPY ( dirp->curentry.d_name,
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
@@ -347,20 +346,45 @@
347
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
--- win/include/dirent.h
+++ win/include/dirent.h
@@ -20,13 +20,12 @@
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 * Sept 12, 2012, Jan Nijtmans
26 * Switchable wide-character variant.
 
27 *
28 * Mar 15, 2011, Toni Ronkko
29 * Defined FILE_ATTRIBUTE_DEVICE for MSVC 6.0.
30 *
31 * Aug 11, 2010, Toni Ronkko
@@ -149,27 +148,26 @@
148 #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
149 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
150 #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
151 #define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
152
 
 
 
 
153 #ifdef UNICODE
154 # define dirent _wdirent
155 # define opendir _wopendir
156 # define readdir _wreaddir
157 # define closedir _wclosedir
158 # define rewinddir _wrewinddir
159 # define DIR _WDIR
160 #endif
161
162 #ifdef __cplusplus
163 extern "C" {
164 #endif
165
166 typedef struct dirent
167 {
168 TCHAR d_name[MAX_PATH + 1]; /* File name */
169 size_t d_namlen; /* Length of name without \0 */
170 int d_type; /* File type */
171 } dirent;
172
173
@@ -177,18 +175,19 @@
175 {
176 dirent curentry; /* Current directory entry */
177 WIN32_FIND_DATA find_data; /* Private file data */
178 int cached; /* True if data is valid */
179 HANDLE search_handle; /* Win32 search handle */
180 TCHAR patt[MAX_PATH + 3]; /* Initial directory name */
181 } DIR;
182
183
184 /* Forward declarations */
185 static DIR *opendir(const TCHAR *dirname);
186 static struct dirent *readdir(DIR *dirp);
187 static int closedir(DIR *dirp);
188 static void rewinddir(DIR* dirp);
189
190
191 /* Use the new safe string functions introduced in Visual Studio 2005 */
192 #if defined(_MSC_VER) && _MSC_VER >= 1400
193 # define DIRENT_STRNCPY(dest,src,size) _tcsncpy_s((dest),(size),(src),_TRUNCATE)
@@ -207,11 +206,11 @@
206 /*****************************************************************************
207 * Open directory stream DIRNAME for read and return a pointer to the
208 * internal working area that is used to retrieve individual directory
209 * entries.
210 */
211 static DIR *opendir(const TCHAR *dirname)
212 {
213 DIR *dirp;
214
215 /* ensure that the resulting search pattern will be a valid file name */
216 if (dirname == NULL) {
@@ -232,11 +231,11 @@
231 * Convert relative directory name to an absolute one. This
232 * allows rewinddir() to function correctly when the current working
233 * directory is changed between opendir() and rewinddir().
234 */
235 if (GetFullPathName (dirname, MAX_PATH, dirp->patt, NULL)) {
236 TCHAR *p;
237
238 /* append the search pattern "\\*\0" to the directory name */
239 p = _tcschr (dirp->patt, '\0');
240 if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
241 *p++ = '\\';
@@ -302,14 +301,14 @@
301 dirp->search_handle = INVALID_HANDLE_VALUE;
302 return NULL;
303 }
304 }
305
306 /* copy as a multibyte/unicode character string */
307 DIRENT_STRNCPY ( dirp->curentry.d_name,
308 dirp->find_data.cFileName,
309 sizeof(dirp->curentry.d_name)/sizeof(TCHAR) );
310 dirp->curentry.d_name[MAX_PATH] = '\0';
311
312 /* compute the length of name */
313 dirp->curentry.d_namlen = _tcslen (dirp->curentry.d_name);
314
@@ -347,20 +346,45 @@
346
347 /* release directory structure */
348 free (dirp);
349 return 0;
350 }
351
352 /*****************************************************************************
353 * Resets the position of the directory stream to which dirp refers to the
354 * beginning of the directory. It also causes the directory stream to refer
355 * to the current state of the corresponding directory, as a call to opendir()
356 * would have done. If dirp does not refer to a directory stream, the effect
357 * is undefined.
358 */
359 static void rewinddir(DIR* dirp)
360 {
361 if (dirp != NULL) {
362 /* release search handle */
363 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
364 FindClose (dirp->search_handle);
365 }
366
367 /* open new search handle and retrieve the first entry */
368 dirp->search_handle = FindFirstFile (dirp->patt, &dirp->find_data);
369 if (dirp->search_handle != INVALID_HANDLE_VALUE) {
370 /* a directory entry is now waiting in memory */
371 dirp->cached = 1;
372 } else {
373 /* failed to re-open directory: no directory entry in memory */
374 dirp->cached = 0;
375 }
376 }
377 }
378
379 #ifdef UNICODE
380 # undef dirent
381 # undef opendir
382 # undef readdir
383 # undef closedir
384 # undef DIR
 
 
385 #endif
386
387 #ifdef __cplusplus
388 }
389 #endif
390 #endif /*DIRENT_H*/
391

Keyboard Shortcuts

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