diff options
| author | Matthias Melcher <fltk@matthiasm.com> | 2008-09-10 23:56:49 +0000 |
|---|---|---|
| committer | Matthias Melcher <fltk@matthiasm.com> | 2008-09-10 23:56:49 +0000 |
| commit | b6bde2e4569aa617c8a6af64947c688c624ed7f8 (patch) | |
| tree | 010d15843eb7d4faf7cd1b0cd44d5b9c00462a83 /src/scandir_win32.c | |
| parent | dfb50e85292687561927610e689eb5ab30d0ba26 (diff) | |
Merging the UTF8 patch, consisting of O'ksi'd s original 1.1.6 patch and additions by Ian. PLEASE BE AWARE that the patch in its current incarnation is a regression in many aspects and further work is required before we can announce Unicode support.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@6212 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src/scandir_win32.c')
| -rw-r--r-- | src/scandir_win32.c | 66 |
1 files changed, 42 insertions, 24 deletions
diff --git a/src/scandir_win32.c b/src/scandir_win32.c index 00d0b8e6a..804fadc0c 100644 --- a/src/scandir_win32.c +++ b/src/scandir_win32.c @@ -27,7 +27,7 @@ #ifndef __CYGWIN__ /* Emulation of posix scandir() call */ - +#include <FL/fl_utf8.H> #include <FL/filename.H> #include "flstring.h" #include <windows.h> @@ -38,18 +38,18 @@ int fl_scandir(const char *dirname, struct dirent ***namelist, int (*compar)(struct dirent **, struct dirent **)) { int len; char *findIn, *d, is_dir = 0; - WIN32_FIND_DATA find; + WIN32_FIND_DATAW findw; HANDLE h; int nDir = 0, NDir = 0; struct dirent **dir = 0, *selectDir; unsigned long ret; len = strlen(dirname); - findIn = (char *)malloc((size_t)(len+5)); - + findIn = (char *)malloc((size_t)(len+10)); if (!findIn) return -1; - strcpy(findIn, dirname); + + //#warning FIXME This probably needs to be MORE UTF8 aware now for (d = findIn; *d; d++) if (*d=='/') *d='\\'; if ((len==0)) { strcpy(findIn, ".\\*"); } if ((len==2)&&findIn[1]==':'&&isalpha(findIn[0])) { *d++ = '\\'; *d = 0; } @@ -58,10 +58,22 @@ int fl_scandir(const char *dirname, struct dirent ***namelist, if ((len>1) && (d[-1]=='.') && (d[-2]=='\\')) { d[-1] = '*'; is_dir = 1; } if (!is_dir) { /* this file may still be a directory that we need to list */ DWORD attr = GetFileAttributes(findIn); - if (attr&FILE_ATTRIBUTE_DIRECTORY) + if (attr&FILE_ATTRIBUTE_DIRECTORY) strcpy(d, "\\*"); } - if ((h=FindFirstFile(findIn, &find))==INVALID_HANDLE_VALUE) { + { /* Create a block to limit the scope while we find the initial "wide" filename */ +// unsigned short * wbuf = (unsigned short*)malloc(sizeof(short) *(len + 10)); +// wbuf[fl_utf2unicode(findIn, strlen(findIn), wbuf)] = 0; + unsigned short *wbuf = NULL; + unsigned wlen = fl_utf8toUtf16(findIn, strlen(findIn), NULL, 0); /* Pass NULL to query length */ + wlen++; /* add a little extra for termination etc. */ + wbuf = (unsigned short*)malloc(sizeof(unsigned short)*wlen); + wlen = fl_utf8toUtf16(findIn, strlen(findIn), wbuf, wlen); /* actually convert the filename */ + wbuf[wlen] = 0; /* NULL terminate the resultant string */ + h = FindFirstFileW(wbuf, &findw); /* get a handle to the first filename in the search */ + free(wbuf); /* release the "wide" buffer before the pointer goes out of scope */ + } + if (h==INVALID_HANDLE_VALUE) { free(findIn); ret = GetLastError(); if (ret != ERROR_NO_MORE_FILES) { @@ -71,31 +83,37 @@ int fl_scandir(const char *dirname, struct dirent ***namelist, return nDir; } do { - selectDir=(struct dirent*)malloc(sizeof(struct dirent)+strlen(find.cFileName)+2); - strcpy(selectDir->d_name, find.cFileName); - if (find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { - /* Append a trailing slash to directory names... */ - strcat(selectDir->d_name, "/"); - } - if (!select || (*select)(selectDir)) { - if (nDir==NDir) { + int l = wcslen(findw.cFileName); + int dstlen = l * 5 + 1; + selectDir=(struct dirent*)malloc(sizeof(struct dirent)+dstlen); + +// l = fl_unicode2utf(findw.cFileName, l, selectDir->d_name); + l = fl_utf8fromwc(selectDir->d_name, dstlen, findw.cFileName, l); + + selectDir->d_name[l] = 0; + if (findw.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + /* Append a trailing slash to directory names... */ + strcat(selectDir->d_name, "/"); + } + if (!select || (*select)(selectDir)) { + if (nDir==NDir) { struct dirent **tempDir = (struct dirent **)calloc(sizeof(struct dirent*), (size_t)(NDir+33)); if (NDir) memcpy(tempDir, dir, sizeof(struct dirent*)*NDir); if (dir) free(dir); dir = tempDir; NDir += 32; - } - dir[nDir] = selectDir; - nDir++; - dir[nDir] = 0; - } else { - free(selectDir); - } - } while (FindNextFile(h, &find)); + } + dir[nDir] = selectDir; + nDir++; + dir[nDir] = 0; + } else { + free(selectDir); + } + } while (FindNextFileW(h, &findw)); ret = GetLastError(); if (ret != ERROR_NO_MORE_FILES) { /* don't return an error code, because the dir list may still be valid - up to this point */ + up to this point */ } FindClose(h); |
