|
fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation) ![]() |
00001 /* 00002 * "$Id: scandir_win32.c 7913 2010-11-29 18:18:27Z greg.ercolano $" 00003 * 00004 * WIN32 scandir function for the Fast Light Tool Kit (FLTK). 00005 * 00006 * Copyright 1998-2010 by Bill Spitzak and others. 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Library General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Library General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Library General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00021 * USA. 00022 * 00023 * Please report all bugs and problems on the following page: 00024 * 00025 * http://www.fltk.org/str.php 00026 */ 00027 00028 #ifndef __CYGWIN__ 00029 /* Emulation of posix scandir() call */ 00030 #include <FL/fl_utf8.h> 00031 #include <FL/filename.H> 00032 #include "flstring.h" 00033 #include <windows.h> 00034 #include <stdlib.h> 00035 00036 int fl_scandir(const char *dirname, struct dirent ***namelist, 00037 int (*select)(struct dirent *), 00038 int (*compar)(struct dirent **, struct dirent **)) { 00039 int len; 00040 char *findIn, *d, is_dir = 0; 00041 WIN32_FIND_DATAW findw; 00042 HANDLE h; 00043 int nDir = 0, NDir = 0; 00044 struct dirent **dir = 0, *selectDir; 00045 unsigned long ret; 00046 00047 len = strlen(dirname); 00048 findIn = (char *)malloc((size_t)(len+10)); 00049 if (!findIn) return -1; 00050 strcpy(findIn, dirname); 00051 00052 //#if defined(__GNUC__) 00053 //#warning FIXME This probably needs to be MORE UTF8 aware now 00054 //#endif /*__GNUC__*/ 00055 for (d = findIn; *d; d++) if (*d=='/') *d='\\'; 00056 if ((len==0)) { strcpy(findIn, ".\\*"); } 00057 if ((len==2)&&findIn[1]==':'&&isalpha(findIn[0])) { *d++ = '\\'; *d = 0; } 00058 if ((len==1)&& (d[-1]=='.')) { strcpy(findIn, ".\\*"); is_dir = 1; } 00059 if ((len>0) && (d[-1]=='\\')) { *d++ = '*'; *d = 0; is_dir = 1; } 00060 if ((len>1) && (d[-1]=='.') && (d[-2]=='\\')) { d[-1] = '*'; is_dir = 1; } 00061 if (!is_dir) { /* this file may still be a directory that we need to list */ 00062 DWORD attr = GetFileAttributes(findIn); 00063 if (attr&FILE_ATTRIBUTE_DIRECTORY) 00064 strcpy(d, "\\*"); 00065 } 00066 { /* Create a block to limit the scope while we find the initial "wide" filename */ 00067 // unsigned short * wbuf = (unsigned short*)malloc(sizeof(short) *(len + 10)); 00068 // wbuf[fl_utf2unicode(findIn, strlen(findIn), wbuf)] = 0; 00069 unsigned short *wbuf = NULL; 00070 unsigned wlen = fl_utf8toUtf16(findIn, strlen(findIn), NULL, 0); /* Pass NULL to query length */ 00071 wlen++; /* add a little extra for termination etc. */ 00072 wbuf = (unsigned short*)malloc(sizeof(unsigned short)*wlen); 00073 wlen = fl_utf8toUtf16(findIn, strlen(findIn), wbuf, wlen); /* actually convert the filename */ 00074 wbuf[wlen] = 0; /* NULL terminate the resultant string */ 00075 h = FindFirstFileW(wbuf, &findw); /* get a handle to the first filename in the search */ 00076 free(wbuf); /* release the "wide" buffer before the pointer goes out of scope */ 00077 } 00078 if (h==INVALID_HANDLE_VALUE) { 00079 free(findIn); 00080 ret = GetLastError(); 00081 if (ret != ERROR_NO_MORE_FILES) { 00082 nDir = -1; 00083 } 00084 *namelist = dir; 00085 return nDir; 00086 } 00087 do { 00088 int l = wcslen(findw.cFileName); 00089 int dstlen = l * 5 + 1; 00090 selectDir=(struct dirent*)malloc(sizeof(struct dirent)+dstlen); 00091 00092 // l = fl_unicode2utf(findw.cFileName, l, selectDir->d_name); 00093 l = fl_utf8fromwc(selectDir->d_name, dstlen, findw.cFileName, l); 00094 00095 selectDir->d_name[l] = 0; 00096 if (findw.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { 00097 /* Append a trailing slash to directory names... */ 00098 strcat(selectDir->d_name, "/"); 00099 } 00100 if (!select || (*select)(selectDir)) { 00101 if (nDir==NDir) { 00102 struct dirent **tempDir = (struct dirent **)calloc(sizeof(struct dirent*), (size_t)(NDir+33)); 00103 if (NDir) memcpy(tempDir, dir, sizeof(struct dirent*)*NDir); 00104 if (dir) free(dir); 00105 dir = tempDir; 00106 NDir += 32; 00107 } 00108 dir[nDir] = selectDir; 00109 nDir++; 00110 dir[nDir] = 0; 00111 } else { 00112 free(selectDir); 00113 } 00114 } while (FindNextFileW(h, &findw)); 00115 ret = GetLastError(); 00116 if (ret != ERROR_NO_MORE_FILES) { 00117 /* don't return an error code, because the dir list may still be valid 00118 up to this point */ 00119 } 00120 FindClose(h); 00121 00122 free (findIn); 00123 00124 if (compar) qsort(dir, (size_t)nDir, sizeof(*dir), 00125 (int(*)(const void*, const void*))compar); 00126 00127 *namelist = dir; 00128 return nDir; 00129 } 00130 00131 #endif 00132 00133 /* 00134 * End of "$Id: scandir_win32.c 7913 2010-11-29 18:18:27Z greg.ercolano $". 00135 */