|
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: filename_list.cxx 8192 2011-01-05 16:50:10Z manolo $" 00003 // 00004 // Filename list routines 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 // Wrapper for scandir with const-correct function prototypes. 00029 00030 #include <FL/filename.H> 00031 #include <FL/fl_utf8.h> 00032 #include "flstring.h" 00033 #include <stdlib.h> 00034 00035 00036 extern "C" { 00037 #ifndef HAVE_SCANDIR 00038 int fl_scandir (const char *dir, dirent ***namelist, 00039 int (*select)(dirent *), 00040 int (*compar)(dirent **, dirent **)); 00041 #endif 00042 } 00043 00044 int fl_alphasort(struct dirent **a, struct dirent **b) { 00045 return strcmp((*a)->d_name, (*b)->d_name); 00046 } 00047 00048 int fl_casealphasort(struct dirent **a, struct dirent **b) { 00049 return strcasecmp((*a)->d_name, (*b)->d_name); 00050 } 00051 00052 00082 int fl_filename_list(const char *d, dirent ***list, 00083 Fl_File_Sort_F *sort) { 00084 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(HAVE_SCANDIR) 00085 // For Windows we have a special scandir implementation that uses 00086 // the Win32 "wide" functions for lookup, avoiding the code page mess 00087 // entirely. It also fixes up the trailing '/'. 00088 return fl_scandir(d, list, 0, sort); 00089 00090 #else // WIN32 00091 00092 int dirlen; 00093 char *dirloc; 00094 00095 // Assume that locale encoding is no less dense than UTF-8 00096 dirlen = strlen(d); 00097 #ifdef __APPLE__ 00098 dirloc = (char *)d; 00099 #else 00100 dirloc = (char *)malloc(dirlen + 1); 00101 fl_utf8to_mb(d, dirlen, dirloc, dirlen + 1); 00102 #endif 00103 00104 #ifndef HAVE_SCANDIR 00105 // This version is when we define our own scandir 00106 int n = fl_scandir(dirloc, list, 0, sort); 00107 #elif defined(HAVE_SCANDIR_POSIX) && !defined(__APPLE__) 00108 // POSIX (2008) defines the comparison function like this: 00109 int n = scandir(dirloc, list, 0, (int(*)(const dirent **, const dirent **))sort); 00110 #elif defined(__osf__) 00111 // OSF, DU 4.0x 00112 int n = scandir(dirloc, list, 0, (int(*)(dirent **, dirent **))sort); 00113 #elif defined(_AIX) 00114 // AIX is almost standard... 00115 int n = scandir(dirloc, list, 0, (int(*)(void*, void*))sort); 00116 #elif defined(__sgi) 00117 int n = scandir(dirloc, list, 0, sort); 00118 #else 00119 // The vast majority of UNIX systems want the sort function to have this 00120 // prototype, most likely so that it can be passed to qsort without any 00121 // changes: 00122 int n = scandir(dirloc, list, 0, (int(*)(const void*,const void*))sort); 00123 #endif 00124 00125 #ifndef __APPLE__ 00126 free(dirloc); 00127 #endif 00128 00129 // convert every filename to utf-8, and append a '/' to all 00130 // filenames that are directories 00131 int i; 00132 char *fullname = (char*)malloc(dirlen+FL_PATH_MAX+3); // Add enough extra for two /'s and a nul 00133 // Use memcpy for speed since we already know the length of the string... 00134 memcpy(fullname, d, dirlen+1); 00135 00136 char *name = fullname + dirlen; 00137 if (name!=fullname && name[-1]!='/') 00138 *name++ = '/'; 00139 00140 for (i=0; i<n; i++) { 00141 int newlen; 00142 dirent *de = (*list)[i]; 00143 int len = strlen(de->d_name); 00144 #ifdef __APPLE__ 00145 newlen = len; 00146 #else 00147 newlen = fl_utf8from_mb(NULL, 0, de->d_name, len); 00148 #endif 00149 dirent *newde = (dirent*)malloc(de->d_name - (char*)de + newlen + 2); // Add space for a / and a nul 00150 00151 // Conversion to UTF-8 00152 memcpy(newde, de, de->d_name - (char*)de); 00153 #ifdef __APPLE__ 00154 strcpy(newde->d_name, de->d_name); 00155 #else 00156 fl_utf8from_mb(newde->d_name, newlen + 1, de->d_name, len); 00157 #endif 00158 00159 // Check if dir (checks done on "old" name as we need to interact with 00160 // the underlying OS) 00161 if (de->d_name[len-1]!='/' && len<=FL_PATH_MAX) { 00162 // Use memcpy for speed since we already know the length of the string... 00163 memcpy(name, de->d_name, len+1); 00164 if (fl_filename_isdir(fullname)) { 00165 char *dst = newde->d_name + newlen; 00166 *dst++ = '/'; 00167 *dst = 0; 00168 } 00169 } 00170 00171 free(de); 00172 (*list)[i] = newde; 00173 } 00174 free(fullname); 00175 00176 return n; 00177 00178 #endif // WIN32 00179 } 00180 00190 void fl_filename_free_list(struct dirent ***list, int n) 00191 { 00192 if (n<0) return; 00193 00194 int i; 00195 for (i = 0; i < n; i ++) { 00196 if ((*list)[i]) 00197 free((*list)[i]); 00198 } 00199 free(*list); 00200 *list = 0; 00201 } 00202 00203 00204 // 00205 // End of "$Id: filename_list.cxx 8192 2011-01-05 16:50:10Z manolo $". 00206 //