diff options
| author | Michael R Sweet <michael.r.sweet@gmail.com> | 1998-10-06 18:46:47 +0000 |
|---|---|---|
| committer | Michael R Sweet <michael.r.sweet@gmail.com> | 1998-10-06 18:46:47 +0000 |
| commit | e7d805a88ca71a0e6499fe72830e58fe594ef05b (patch) | |
| tree | 8c0cd05b0f4098bba4b90b229c23ef8d96a80d8a /src/scandir_win32.c | |
| parent | f9039b2ae21988783feae9b362818e7923e82d14 (diff) | |
Commited Gustavo Hime's NT patches/fixes.
git-svn-id: file:///fltk/svn/fltk/trunk@7 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src/scandir_win32.c')
| -rw-r--r-- | src/scandir_win32.c | 161 |
1 files changed, 82 insertions, 79 deletions
diff --git a/src/scandir_win32.c b/src/scandir_win32.c index ea3d18723..525b289d4 100644 --- a/src/scandir_win32.c +++ b/src/scandir_win32.c @@ -1,79 +1,82 @@ -// scandir_win32.C - -// Emulation of posix scandir() call - -#include <config.h> -#include <FL/filename.H> -#include <string.h> -#include <windows.h> - -int scandir(const char *dirname, struct dirent ***namelist, - int (*select)(const struct dirent *), - int (*compar)(const struct dirent **, const struct dirent **)) { - - int len = strlen(dirname); - char *findIn = new char[len+5]; strcpy(findIn, dirname); - for (char *d = findIn; *d; d++) if (*d=='/') *d='\\'; - if ((len==0)) { strcpy(findIn, ".\\*"); } - if ((len==1)&& (d[-1]=='.')) { strcpy(findIn, ".\\*"); } - if ((len>0) && (d[-1]=='\\')) { *d++ = '*'; *d = 0; } - if ((len>1) && (d[-1]=='.') && (d[-2]=='\\')) { d[-1] = '*'; } - - WIN32_FIND_DATA find; - HANDLE h; - int nDir = 0, NDir = 0; - struct dirent **dir = 0, *selectDir; - /* - selectDir = (struct dirent*)new char[sizeof(dirent)+1]; - strcpy(selectDir->d_name, "."); - dir[0] = selectDir; - selectDir = (struct dirent*)new char[sizeof(dirent)+2]; - strcpy(selectDir->d_name, ".."); - dir[1] = selectDir; - */ - unsigned long ret; - - if ((h=FindFirstFile(findIn, &find))==INVALID_HANDLE_VALUE) { - ret = GetLastError(); - if (ret != ERROR_NO_MORE_FILES) { - // TODO: return some error code - } - *namelist = dir; - return nDir; - } - do { - selectDir=(struct dirent*)new char[sizeof(dirent)+strlen(find.cFileName)]; - strcpy(selectDir->d_name, find.cFileName); - if (!select || (*select)(selectDir)) { - if (nDir==NDir) { - struct dirent **tempDir = new struct dirent*[NDir+33]; - if (NDir) memcpy(tempDir, dir, sizeof(struct dirent*)*NDir); - if (dir) delete dir; - dir = tempDir; - NDir += 32; - } - dir[nDir] = selectDir; - nDir++; - dir[nDir] = 0; - } else { - delete selectDir; - } - } while (FindNextFile(h, &find)); - ret = GetLastError(); - if (ret != ERROR_NO_MORE_FILES) { - // TODO: return some error code - } - FindClose(h); - - delete findIn; - - if (compar) qsort (dir, nDir, sizeof(*dir), - (int(*)(const void*, const void*))compar); - - *namelist = dir; - return nDir; -} - -int alphasort (const struct dirent **a, const struct dirent **b) { - return strcmp ((*a)->d_name, (*b)->d_name); -} +// scandir_win32.C
+
+// Emulation of posix scandir() call
+
+#include <config.h>
+#include <FL/filename.H>
+#include <string.h>
+#include <windows.h>
+
+#ifdef __cplusplus
+extern "C"
+#endif
+int scandir(const char *dirname, struct dirent ***namelist,
+ int (*select)(const struct dirent *),
+ int (*compar)(const struct dirent **, const struct dirent **)) {
+
+ int len = strlen(dirname);
+ char *findIn = new char[len+5]; strcpy(findIn, dirname);
+ for (char *d = findIn; *d; d++) if (*d=='/') *d='\\';
+ if ((len==0)) { strcpy(findIn, ".\\*"); }
+ if ((len==1)&& (d[-1]=='.')) { strcpy(findIn, ".\\*"); }
+ if ((len>0) && (d[-1]=='\\')) { *d++ = '*'; *d = 0; }
+ if ((len>1) && (d[-1]=='.') && (d[-2]=='\\')) { d[-1] = '*'; }
+
+ WIN32_FIND_DATA find;
+ HANDLE h;
+ int nDir = 0, NDir = 0;
+ struct dirent **dir = 0, *selectDir;
+ /*
+ selectDir = (struct dirent*)new char[sizeof(dirent)+1];
+ strcpy(selectDir->d_name, ".");
+ dir[0] = selectDir;
+ selectDir = (struct dirent*)new char[sizeof(dirent)+2];
+ strcpy(selectDir->d_name, "..");
+ dir[1] = selectDir;
+ */
+ unsigned long ret;
+
+ if ((h=FindFirstFile(findIn, &find))==INVALID_HANDLE_VALUE) {
+ ret = GetLastError();
+ if (ret != ERROR_NO_MORE_FILES) {
+ // TODO: return some error code
+ }
+ *namelist = dir;
+ return nDir;
+ }
+ do {
+ selectDir=(struct dirent*)new char[sizeof(dirent)+strlen(find.cFileName)];
+ strcpy(selectDir->d_name, find.cFileName);
+ if (!select || (*select)(selectDir)) {
+ if (nDir==NDir) {
+ struct dirent **tempDir = new struct dirent*[NDir+33];
+ if (NDir) memcpy(tempDir, dir, sizeof(struct dirent*)*NDir);
+ if (dir) delete dir;
+ dir = tempDir;
+ NDir += 32;
+ }
+ dir[nDir] = selectDir;
+ nDir++;
+ dir[nDir] = 0;
+ } else {
+ delete selectDir;
+ }
+ } while (FindNextFile(h, &find));
+ ret = GetLastError();
+ if (ret != ERROR_NO_MORE_FILES) {
+ // TODO: return some error code
+ }
+ FindClose(h);
+
+ delete findIn;
+
+ if (compar) qsort (dir, nDir, sizeof(*dir),
+ (int(*)(const void*, const void*))compar);
+
+ *namelist = dir;
+ return nDir;
+}
+
+int alphasort (const struct dirent **a, const struct dirent **b) {
+ return strcmp ((*a)->d_name, (*b)->d_name);
+}
|
