fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

filename_list.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // Filename list routines for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18 
19 // Wrapper for scandir with const-correct function prototypes.
20 
21 #include <FL/filename.H>
22 #include <FL/fl_utf8.h>
23 #include "flstring.h"
24 #include <stdlib.h>
25 #ifdef __APPLE__
26 #include <FL/x.H>
27 #endif
28 
29 extern "C" {
30 #ifndef HAVE_SCANDIR
31  int fl_scandir (const char *dir, dirent ***namelist,
32  int (*select)(dirent *),
33  int (*compar)(dirent **, dirent **));
34 #endif
35 }
36 
37 int fl_alphasort(struct dirent **a, struct dirent **b) {
38  return strcmp((*a)->d_name, (*b)->d_name);
39 }
40 
41 int fl_casealphasort(struct dirent **a, struct dirent **b) {
42  return strcasecmp((*a)->d_name, (*b)->d_name);
43 }
44 
45 
75 int fl_filename_list(const char *d, dirent ***list,
77 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(HAVE_SCANDIR)
78  // For Windows we have a special scandir implementation that uses
79  // the Win32 "wide" functions for lookup, avoiding the code page mess
80  // entirely. It also fixes up the trailing '/'.
81  return fl_scandir(d, list, 0, sort);
82 
83 #else // WIN32
84 
85  int dirlen;
86  char *dirloc;
87 
88  // Assume that locale encoding is no less dense than UTF-8
89  dirlen = strlen(d);
90 #ifdef __APPLE__
91  dirloc = (char *)d;
92 #else
93  dirloc = (char *)malloc(dirlen + 1);
94  fl_utf8to_mb(d, dirlen, dirloc, dirlen + 1);
95 #endif
96 
97 #ifndef HAVE_SCANDIR
98  // This version is when we define our own scandir
99  int n = fl_scandir(dirloc, list, 0, sort);
100 #elif defined(HAVE_SCANDIR_POSIX) && !defined(__APPLE__)
101  // POSIX (2008) defines the comparison function like this:
102  int n = scandir(dirloc, list, 0, (int(*)(const dirent **, const dirent **))sort);
103 #elif defined(__osf__)
104  // OSF, DU 4.0x
105  int n = scandir(dirloc, list, 0, (int(*)(dirent **, dirent **))sort);
106 #elif defined(_AIX)
107  // AIX is almost standard...
108  int n = scandir(dirloc, list, 0, (int(*)(void*, void*))sort);
109 #elif defined(__sgi)
110  int n = scandir(dirloc, list, 0, sort);
111 #elif defined(__APPLE__)
112 # if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
113  int n = scandir(dirloc, list, 0, (int(*)(const struct dirent**,const struct dirent**))sort);
114 # else
115  int n = scandir(dirloc, list, 0, (int(*)(const void*,const void*))sort);
116 # endif
117 #else
118  // The vast majority of UNIX systems want the sort function to have this
119  // prototype, most likely so that it can be passed to qsort without any
120  // changes:
121  int n = scandir(dirloc, list, 0, (int(*)(const void*,const void*))sort);
122 #endif
123 
124 #ifndef __APPLE__
125  free(dirloc);
126 #endif
127 
128  // convert every filename to utf-8, and append a '/' to all
129  // filenames that are directories
130  int i;
131  char *fullname = (char*)malloc(dirlen+FL_PATH_MAX+3); // Add enough extra for two /'s and a nul
132  // Use memcpy for speed since we already know the length of the string...
133  memcpy(fullname, d, dirlen+1);
134 
135  char *name = fullname + dirlen;
136  if (name!=fullname && name[-1]!='/')
137  *name++ = '/';
138 
139  for (i=0; i<n; i++) {
140  int newlen;
141  dirent *de = (*list)[i];
142  int len = strlen(de->d_name);
143 #ifdef __APPLE__
144  newlen = len;
145 #else
146  newlen = fl_utf8from_mb(NULL, 0, de->d_name, len);
147 #endif
148  dirent *newde = (dirent*)malloc(de->d_name - (char*)de + newlen + 2); // Add space for a / and a nul
149 
150  // Conversion to UTF-8
151  memcpy(newde, de, de->d_name - (char*)de);
152 #ifdef __APPLE__
153  strcpy(newde->d_name, de->d_name);
154 #else
155  fl_utf8from_mb(newde->d_name, newlen + 1, de->d_name, len);
156 #endif
157 
158  // Check if dir (checks done on "old" name as we need to interact with
159  // the underlying OS)
160  if (de->d_name[len-1]!='/' && len<=FL_PATH_MAX) {
161  // Use memcpy for speed since we already know the length of the string...
162  memcpy(name, de->d_name, len+1);
163  if (fl_filename_isdir(fullname)) {
164  char *dst = newde->d_name + newlen;
165  *dst++ = '/';
166  *dst = 0;
167  }
168  }
169 
170  free(de);
171  (*list)[i] = newde;
172  }
173  free(fullname);
174 
175  return n;
176 
177 #endif // WIN32
178 }
179 
189 void fl_filename_free_list(struct dirent ***list, int n)
190 {
191  if (n<0) return;
192 
193  int i;
194  for (i = 0; i < n; i ++) {
195  if ((*list)[i])
196  free((*list)[i]);
197  }
198  free(*list);
199  *list = 0;
200 }
201 
202 
203 //
204 // End of "$Id$".
205 //
fl_casealphasort
int fl_casealphasort(struct dirent **a, struct dirent **b)
Definition: filename_list.cxx:41
x.H
filename.H
free
void free()
NULL
#define NULL
Definition: forms.H:34
fl_filename_isdir
int fl_filename_isdir(const char *name)
Definition: filename_isdir.cxx:52
b
long b
Definition: jpegint.h:397
fl_scandir
int fl_scandir(const char *dir, dirent ***namelist, int(*select)(dirent *), int(*compar)(dirent **, dirent **))
fl_filename_free_list
void fl_filename_free_list(struct dirent ***list, int n)
Free the list of filenames that is generated by fl_filename_list().
Definition: filename_list.cxx:189
dir
static int dir
Definition: fl_draw_image.cxx:67
fl_utf8.h
header for Unicode and UTF-8 character handling
FL_PATH_MAX
#define FL_PATH_MAX
Definition: filename.H:38
fl_filename_list
int fl_filename_list(const char *d, dirent ***list, Fl_File_Sort_F *sort)
Definition: filename_list.cxx:75
fl_utf8from_mb
unsigned fl_utf8from_mb(char *dst, unsigned dstlen, const char *src, unsigned srclen)
Definition: fl_utf.c:858
sort
Fl_Type * sort(Fl_Type *parent)
Definition: Fl_Widget_Type.cxx:270
select
void select(Fl_Type *, int)
Definition: Fl_Type.cxx:240
fl_alphasort
int fl_alphasort(struct dirent **a, struct dirent **b)
Definition: filename_list.cxx:37
Fl_File_Sort_F
int() Fl_File_Sort_F(struct dirent **, struct dirent **)
Definition: filename.H:116
malloc
voidp malloc()
flstring.h
name
static const char * name
Definition: Fl_arg.cxx:53
fl_utf8to_mb
unsigned fl_utf8to_mb(const char *src, unsigned srclen, char *dst, unsigned dstlen)
Definition: fl_utf.c:784
dirent
#define dirent
Definition: numericsort.c:31