Rewrite filename_list.cxx under the driver model.

One more platform-dependent type is necessary: struct dirent
Create a new header file, FL/platform_types.h, to define all
types whose definition is platform-dependent.
This file is for C because it is included by scandir_XXX.c

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3-porting@11550 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Manolo Gouy 2016-04-07 15:10:30 +00:00
parent 406fcaf305
commit 23a60edb44
17 changed files with 260 additions and 302 deletions

View File

@ -25,56 +25,10 @@
#define FL_SYSTEM_DRIVER_H #define FL_SYSTEM_DRIVER_H
#include <FL/Fl_Export.H> #include <FL/Fl_Export.H>
#include <FL/platform_types.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
// platform-dependent types are declared here
#ifdef __APPLE__
typedef struct CGContext* Fl_Offscreen;
typedef struct CGImage* Fl_Bitmask;
typedef struct flCocoaRegion* Fl_Region;
typedef int FL_SOCKET;
#include <sys/stat.h>
#elif defined(WIN32)
typedef struct HBITMAP__ *HBITMAP;
typedef HBITMAP Fl_Offscreen;
typedef HBITMAP Fl_Bitmask;
typedef struct HRGN__ *Fl_Region;
# if defined(_WIN64)
typedef unsigned __int64 FL_SOCKET;
# else
typedef int FL_SOCKET;
# endif
#include <sys/stat.h>
#elif defined(FL_PORTING)
# pragma message "FL_PORTING: define OS-dependent types"
typedef void* Fl_Offscreen;
typedef void* Fl_Bitmask;
typedef void *Fl_Region;
typedef int FL_SOCKET;
# pragma message "FL_PORTING: define struct stat and implement stat() for the platform"
struct stat { // the FLTK source code uses part of the stat() API
unsigned st_mode;
off_t st_size;
};
#define S_IFMT 0170000 /* type of file */
#define S_IFIFO 0010000 /* named pipe (fifo) */
#define S_IFCHR 0020000 /* character special */
#define S_IFDIR 0040000 /* directory */
#define S_IFBLK 0060000 /* block special */
#define S_IFREG 0100000 /* regular */
#define S_IFLNK 0120000 /* symbolic link */
#else
typedef unsigned long Fl_Offscreen;
typedef unsigned long Fl_Bitmask;
typedef struct _XRegion *Fl_Region;
typedef int FL_SOCKET;
#include <sys/stat.h>
#endif // __APPLE__
/** /**
\brief A base class for platform-specific system operations. \brief A base class for platform-specific system operations.
@ -142,6 +96,8 @@ public:
// implement functions telling whether a key is pressed // implement functions telling whether a key is pressed
virtual int event_key(int k) {return 0;} virtual int event_key(int k) {return 0;}
virtual int get_key(int k) {return 0;} virtual int get_key(int k) {return 0;}
// implement scandir-like function
virtual int filename_list(const char *d, dirent ***list, int (*sort)(struct dirent **, struct dirent **) ) {return -1;}
}; };
#endif // FL_SYSTEM_DRIVER_H #endif // FL_SYSTEM_DRIVER_H

View File

@ -1,25 +0,0 @@
//
// "$Id$"
//
// Directory header file for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2011 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// http://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
// http://www.fltk.org/str.php
//
// this file is for back-compatibility only
#include "filename.H"
//
// End of "$Id$".
//

View File

@ -19,17 +19,11 @@
File names and URI utility functions. File names and URI utility functions.
*/ */
/* Xcode on OS X includes files by recursing down into directories.
* This code catches the cycle and directly includes the required file.
*/
#ifdef fl_dirent_h_cyclic_include
# include "/usr/include/dirent.h"
#endif
#ifndef FL_FILENAME_H #ifndef FL_FILENAME_H
# define FL_FILENAME_H # define FL_FILENAME_H
# include "Fl_Export.H" #include "Fl_Export.H"
#include <FL/platform_types.h>
/** \addtogroup filenames File names and URI utility functions /** \addtogroup filenames File names and URI utility functions
File names and URI functions defined in <FL/filename.H> File names and URI functions defined in <FL/filename.H>
@ -72,39 +66,6 @@ FL_EXPORT int fl_filename_relative(char *to, int tolen, const char *from, const
inline int fl_filename_relative(char *to, const char *from) { return fl_filename_relative(to, FL_PATH_MAX, from); } inline int fl_filename_relative(char *to, const char *from) { return fl_filename_relative(to, FL_PATH_MAX, from); }
# endif /* __cplusplus */ # endif /* __cplusplus */
# if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(__WATCOMC__)
struct dirent {char d_name[1];};
# elif defined(__WATCOMC__)
# include <sys/types.h>
# include <direct.h>
# elif defined(FL_PORTING)
# pragma message "FL_PORTING: insert the equivalent to dirent.h on your system"
# include <dirent.h>
# else
/*
* WARNING: on some systems (very few nowadays?) <dirent.h> may not exist.
* The correct information is in one of these files:
*
* #include <sys/ndir.h>
* #include <sys/dir.h>
* #include <ndir.h>
*
* plus you must do the following #define:
*
* #define dirent direct
*
* It would be best to create a <dirent.h> file that does this...
*/
# include <sys/types.h>
# define fl_dirent_h_cyclic_include
# include <dirent.h>
# undef fl_dirent_h_cyclic_include
# endif
# if defined (__cplusplus) # if defined (__cplusplus)
extern "C" { extern "C" {
# endif /* __cplusplus */ # endif /* __cplusplus */
@ -116,7 +77,7 @@ FL_EXPORT int fl_casenumericsort(struct dirent **, struct dirent **);
FL_EXPORT int fl_numericsort(struct dirent **, struct dirent **); FL_EXPORT int fl_numericsort(struct dirent **, struct dirent **);
# endif # endif
typedef int (Fl_File_Sort_F)(struct dirent **, struct dirent **); /**< File sorting function. \see fl_filename_list() */ typedef int (Fl_File_Sort_F)(struct dirent **, struct dirent **); /**< File sorting function. \see fl_filename_list() */
# if defined(__cplusplus) # if defined(__cplusplus)
} }

83
FL/platform_types.h Normal file
View File

@ -0,0 +1,83 @@
/*
* "$Id: platform_types.h 11467 2016-03-29 19:41:14Z manolo $"
*
* Copyright 2016 by Bill Spitzak and others.
*
* This library is free software. Distribution and use rights are outlined in
* the file "COPYING" which should have been included with this file. If this
* file is missing or damaged, see the license at:
*
* http://www.fltk.org/COPYING.php
*
* Please report all bugs and problems on the following page:
*
* http://www.fltk.org/str.php
*/
#ifndef PLATFORM_TYPES_H
#define PLATFORM_TYPES_H
/* Platform-dependent types are defined here
These types must be defined by any platform:
Fl_Offscreen, Fl_Bitmask, Fl_Region, FL_SOCKET, struct dirent, struct stat
*/
#ifdef __APPLE__
typedef struct CGContext* Fl_Offscreen;
typedef struct CGImage* Fl_Bitmask;
typedef struct flCocoaRegion* Fl_Region;
typedef int FL_SOCKET;
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#elif defined(WIN32)
typedef struct HBITMAP__ *HBITMAP;
typedef HBITMAP Fl_Offscreen;
typedef HBITMAP Fl_Bitmask;
typedef struct HRGN__ *Fl_Region;
# if defined(_WIN64)
typedef unsigned __int64 FL_SOCKET;
# else
typedef int FL_SOCKET;
# endif
#include <sys/stat.h>
struct dirent {char d_name[1];};
#elif defined(FL_PORTING)
# pragma message "FL_PORTING: define OS-dependent types"
typedef void* Fl_Offscreen;
typedef void* Fl_Bitmask;
typedef void *Fl_Region;
typedef int FL_SOCKET;
# pragma message "FL_PORTING: define struct dirent and implement scandir() for the platform"
struct dirent {char d_name[1];};
# pragma message "FL_PORTING: define struct stat and implement stat() for the platform"
struct stat { /* the FLTK source code uses part of the stat() API */
unsigned st_mode;
off_t st_size;
};
#define S_IFMT 0170000 /* type of file */
#define S_IFIFO 0010000 /* named pipe (fifo) */
#define S_IFCHR 0020000 /* character special */
#define S_IFDIR 0040000 /* directory */
#define S_IFBLK 0060000 /* block special */
#define S_IFREG 0100000 /* regular */
#define S_IFLNK 0120000 /* symbolic link */
#else
typedef unsigned long Fl_Offscreen;
typedef unsigned long Fl_Bitmask;
typedef struct _XRegion *Fl_Region;
typedef int FL_SOCKET;
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#endif // __APPLE__
#endif /* PLATFORM_TYPES_H */
/*
* End of "$Id: platform_types.h 11467 2016-03-29 19:41:14Z manolo $".
*/

View File

@ -366,7 +366,6 @@ set (IMGCPPFILES
set (CFILES set (CFILES
flstring.c flstring.c
scandir.c
numericsort.c numericsort.c
vsnprintf.c vsnprintf.c
xutf8/is_right2left.c xutf8/is_right2left.c
@ -377,6 +376,7 @@ set (CFILES
if (USE_X11) if (USE_X11)
list (APPEND CFILES list (APPEND CFILES
xutf8/keysym2Ucs.c xutf8/keysym2Ucs.c
scandir_posix.c
) )
if (NOT USE_XFT) if (NOT USE_XFT)
list (APPEND CFILES list (APPEND CFILES
@ -387,6 +387,12 @@ if (USE_X11)
endif (NOT USE_XFT) endif (NOT USE_XFT)
endif (USE_X11) endif (USE_X11)
if (WIN32)
list (APPEND CFILES
scandir_win32.c
)
endif (WIN32)
if (APPLE AND (NOT OPTION_APPLE_X11) AND (NOT OPTION_APPLE_SDL)) if (APPLE AND (NOT OPTION_APPLE_X11) AND (NOT OPTION_APPLE_SDL))
set (MMFILES set (MMFILES
Fl_cocoa.mm Fl_cocoa.mm

View File

@ -209,7 +209,7 @@ IMGCPPFILES = \
Fl_PNM_Image.cxx Fl_PNM_Image.cxx
CFILES = fl_call_main.c flstring.c scandir.c numericsort.c vsnprintf.c CFILES = fl_call_main.c flstring.c numericsort.c vsnprintf.c
UTF8CFILES = \ UTF8CFILES = \
xutf8/case.c \ xutf8/case.c \
@ -257,7 +257,8 @@ XLIBCPPFILES = \
# This C file is used under condition: BUILD_X11 # This C file is used under condition: BUILD_X11
XLIBCFILES = \ XLIBCFILES = \
xutf8/keysym2Ucs.c xutf8/keysym2Ucs.c \
scandir_posix.c
# These C files are used under condition: BUILD_X11 AND NOT BUILD_XFT # These C files are used under condition: BUILD_X11 AND NOT BUILD_XFT
XLIBXCFILES = \ XLIBXCFILES = \
@ -294,6 +295,10 @@ GDICPPFILES = \
Fl_Native_File_Chooser_WIN32.cxx \ Fl_Native_File_Chooser_WIN32.cxx \
Fl_get_key_win32.cxx Fl_get_key_win32.cxx
# These C files are used under condition: BUILD_GDI
GDICFILES = \
scandir_win32.c
PSCPPFILES = \ PSCPPFILES = \
drivers/PostScript/Fl_PostScript.cxx \ drivers/PostScript/Fl_PostScript.cxx \
drivers/PostScript/Fl_PostScript_image.cxx drivers/PostScript/Fl_PostScript_image.cxx
@ -315,6 +320,8 @@ CPPFILES += $(shell if test $(BUILD_GDI) = Yes; then echo $(GDICPPFILES); fi)
CFILES += $(shell if test $(BUILD_X11) = Yes; then echo $(XLIBCFILES); fi) CFILES += $(shell if test $(BUILD_X11) = Yes; then echo $(XLIBCFILES); fi)
CFILES += $(shell if test $(BUILD_X11) = Yes -a $(BUILD_XFT) != Yes; then echo $(XLIBXCFILES); fi) CFILES += $(shell if test $(BUILD_X11) = Yes -a $(BUILD_XFT) != Yes; then echo $(XLIBXCFILES); fi)
CFILES += $(shell if test $(BUILD_GDI) = Yes; then echo $(GDICFILES); fi)
OBJECTS = $(MMFILES:.mm=.o) $(CPPFILES:.cxx=.o) $(CFILES:.c=.o) $(UTF8CFILES:.c=.o) OBJECTS = $(MMFILES:.mm=.o) $(CPPFILES:.cxx=.o) $(CFILES:.c=.o) $(UTF8CFILES:.c=.o)
GLOBJECTS = $(GLCPPFILES:.cxx=.o) GLOBJECTS = $(GLCPPFILES:.cxx=.o)
FLOBJECTS = $(FLCPPFILES:.cxx=.o) FLOBJECTS = $(FLCPPFILES:.cxx=.o)
@ -595,6 +602,7 @@ include makedepend
# (like "*xft*") here: # (like "*xft*") here:
Fl_get_key_mac.o: Fl_get_key_mac.cxx Fl_get_key_mac.o: Fl_get_key_mac.cxx
Fl_get_key_win32.o: Fl_get_key_win32.cxx Fl_get_key_win32.o: Fl_get_key_win32.cxx
scandir_win32.o scandir_win32.c
Fl_Native_File_Chooser_WIN32.o : Fl_Native_File_Chooser_WIN32.cxx Fl_Native_File_Chooser_WIN32.o : Fl_Native_File_Chooser_WIN32.cxx
Fl_Native_File_Chooser_MAC.o: Fl_Native_File_Chooser_MAC.mm Fl_Native_File_Chooser_MAC.o: Fl_Native_File_Chooser_MAC.mm
Fl_Native_File_Chooser_FLTK.o: Fl_Native_File_Chooser_FLTK.cxx Fl_Native_File_Chooser_FLTK.o: Fl_Native_File_Chooser_FLTK.cxx

View File

@ -68,6 +68,7 @@ public:
// these 2 are in Fl_get_key_mac.cxx // these 2 are in Fl_get_key_mac.cxx
virtual int event_key(int k); virtual int event_key(int k);
virtual int get_key(int k); virtual int get_key(int k);
virtual int filename_list(const char *d, dirent ***list, int (*sort)(struct dirent **, struct dirent **) );
}; };
#endif // FL_DARWIN_SYSTEM_DRIVER_H #endif // FL_DARWIN_SYSTEM_DRIVER_H

View File

@ -20,6 +20,7 @@
#include "../../config_lib.h" #include "../../config_lib.h"
#include "Fl_Darwin_System_Driver.H" #include "Fl_Darwin_System_Driver.H"
#include <FL/Fl.H> #include <FL/Fl.H>
#include <FL/filename.H>
#include <string.h> #include <string.h>
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
#include <xlocale.h> #include <xlocale.h>
@ -88,6 +89,52 @@ void *Fl_Darwin_System_Driver::get_carbon_function(const char *function_name) {
return (carbon ? dlsym(carbon, function_name) : NULL); return (carbon ? dlsym(carbon, function_name) : NULL);
} }
int Fl_Darwin_System_Driver::filename_list(const char *d, dirent ***list, int (*sort)(struct dirent **, struct dirent **) ) {
int dirlen;
char *dirloc;
// Assume that locale encoding is no less dense than UTF-8
dirlen = strlen(d);
dirloc = (char *)d;
# if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
int n = scandir(dirloc, list, 0, (int(*)(const struct dirent**,const struct dirent**))sort);
# else
int n = scandir(dirloc, list, 0, (int(*)(const void*,const void*))sort);
# endif
// convert every filename to utf-8, and append a '/' to all
// filenames that are directories
int i;
char *fullname = (char*)malloc(dirlen+FL_PATH_MAX+3); // Add enough extra for two /'s and a nul
// Use memcpy for speed since we already know the length of the string...
memcpy(fullname, d, dirlen+1);
char *name = fullname + dirlen;
if (name!=fullname && name[-1]!='/') *name++ = '/';
for (i=0; i<n; i++) {
int newlen;
dirent *de = (*list)[i];
int len = strlen(de->d_name);
newlen = len;
dirent *newde = (dirent*)malloc(de->d_name - (char*)de + newlen + 2); // Add space for a / and a nul
// Conversion to UTF-8
memcpy(newde, de, de->d_name - (char*)de);
strcpy(newde->d_name, de->d_name);
// Check if dir (checks done on "old" name as we need to interact with
// the underlying OS)
if (de->d_name[len-1]!='/' && len<=FL_PATH_MAX) {
// Use memcpy for speed since we already know the length of the string...
memcpy(name, de->d_name, len+1);
if (fl_filename_isdir(fullname)) {
char *dst = newde->d_name + newlen;
*dst++ = '/';
*dst = 0;
}
}
free(de);
(*list)[i] = newde;
}
free(fullname);
return n;
}
// //
// End of "$Id$". // End of "$Id$".
// //

View File

@ -65,6 +65,7 @@ public:
// these 2 are in Fl_get_key.cxx // these 2 are in Fl_get_key.cxx
virtual int event_key(int k); virtual int event_key(int k);
virtual int get_key(int k); virtual int get_key(int k);
virtual int filename_list(const char *d, dirent ***list, int (*sort)(struct dirent **, struct dirent **) );
}; };
#endif // FL_POSIX_SYSTEM_DRIVER_H #endif // FL_POSIX_SYSTEM_DRIVER_H

View File

@ -16,8 +16,9 @@
// http://www.fltk.org/str.php // http://www.fltk.org/str.php
// //
#include <config.h>
#include "Fl_Posix_System_Driver.H" #include "Fl_Posix_System_Driver.H"
#include <FL/filename.H>
#include <FL/Fl.H> #include <FL/Fl.H>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <locale.h> #include <locale.h>
@ -30,6 +31,14 @@ const char* fl_local_ctrl = "Ctrl";
const char* fl_local_meta = "Meta"; const char* fl_local_meta = "Meta";
const char* fl_local_shift = "Shift"; const char* fl_local_shift = "Shift";
#ifndef HAVE_SCANDIR
extern "C" {
int fl_scandir(const char *dirname, struct dirent ***namelist,
int (*select)(struct dirent *),
int (*compar)(struct dirent **, struct dirent **));
}
#endif
/** /**
Creates a driver that manages all screen and display related calls. Creates a driver that manages all screen and display related calls.
@ -57,6 +66,80 @@ int Fl_Posix_System_Driver::clocale_printf(FILE *output, const char *format, va_
return retval; return retval;
} }
int Fl_Posix_System_Driver::filename_list(const char *d, dirent ***list, int (*sort)(struct dirent **, struct dirent **) ) {
int dirlen;
char *dirloc;
// Assume that locale encoding is no less dense than UTF-8
dirlen = strlen(d);
dirloc = (char *)malloc(dirlen + 1);
fl_utf8to_mb(d, dirlen, dirloc, dirlen + 1);
#ifndef HAVE_SCANDIR
// This version is when we define our own scandir
int n = fl_scandir(dirloc, list, 0, sort);
#elif defined(HAVE_SCANDIR_POSIX)
// POSIX (2008) defines the comparison function like this:
int n = scandir(dirloc, list, 0, (int(*)(const dirent **, const dirent **))sort);
#elif defined(__osf__)
// OSF, DU 4.0x
int n = scandir(dirloc, list, 0, (int(*)(dirent **, dirent **))sort);
#elif defined(_AIX)
// AIX is almost standard...
int n = scandir(dirloc, list, 0, (int(*)(void*, void*))sort);
#elif defined(__sgi)
int n = scandir(dirloc, list, 0, sort);
#else
// The vast majority of UNIX systems want the sort function to have this
// prototype, most likely so that it can be passed to qsort without any
// changes:
int n = scandir(dirloc, list, 0, (int(*)(const void*,const void*))sort);
#endif
free(dirloc);
// convert every filename to utf-8, and append a '/' to all
// filenames that are directories
int i;
char *fullname = (char*)malloc(dirlen+FL_PATH_MAX+3); // Add enough extra for two /'s and a nul
// Use memcpy for speed since we already know the length of the string...
memcpy(fullname, d, dirlen+1);
char *name = fullname + dirlen;
if (name!=fullname && name[-1]!='/')
*name++ = '/';
for (i=0; i<n; i++) {
int newlen;
dirent *de = (*list)[i];
int len = strlen(de->d_name);
newlen = fl_utf8from_mb(NULL, 0, de->d_name, len);
dirent *newde = (dirent*)malloc(de->d_name - (char*)de + newlen + 2); // Add space for a / and a nul
// Conversion to UTF-8
memcpy(newde, de, de->d_name - (char*)de);
fl_utf8from_mb(newde->d_name, newlen + 1, de->d_name, len);
// Check if dir (checks done on "old" name as we need to interact with
// the underlying OS)
if (de->d_name[len-1]!='/' && len<=FL_PATH_MAX) {
// Use memcpy for speed since we already know the length of the string...
memcpy(name, de->d_name, len+1);
if (fl_filename_isdir(fullname)) {
char *dst = newde->d_name + newlen;
*dst++ = '/';
*dst = 0;
}
}
free(de);
(*list)[i] = newde;
}
free(fullname);
return n;
}
// //
// End of "$Id$". // End of "$Id$".
// //

View File

@ -68,6 +68,7 @@ public:
// these 2 are in Fl_get_key_win32.cxx // these 2 are in Fl_get_key_win32.cxx
virtual int event_key(int k); virtual int event_key(int k);
virtual int get_key(int k); virtual int get_key(int k);
virtual int filename_list(const char *d, dirent ***list, int (*sort)(struct dirent **, struct dirent **) );
}; };
#endif // FL_WINAPI_SYSTEM_DRIVER_H #endif // FL_WINAPI_SYSTEM_DRIVER_H

View File

@ -39,6 +39,12 @@ static wchar_t *mbwbuf = NULL;
static wchar_t *wbuf = NULL; static wchar_t *wbuf = NULL;
static wchar_t *wbuf1 = NULL; static wchar_t *wbuf1 = NULL;
extern "C" {
int fl_scandir(const char *dirname, struct dirent ***namelist,
int (*select)(struct dirent *),
int (*compar)(struct dirent **, struct dirent **));
}
/** /**
Creates a driver that manages all screen and display related calls. Creates a driver that manages all screen and display related calls.
@ -429,6 +435,13 @@ int Fl_WinAPI_System_Driver::clocale_printf(FILE *output, const char *format, va
return retval; return retval;
} }
int Fl_WinAPI_System_Driver::filename_list(const char *d, dirent ***list, int (*sort)(struct dirent **, struct dirent **) ) {
// For Windows we have a special scandir implementation that uses
// the Win32 "wide" functions for lookup, avoiding the code page mess
// entirely. It also fixes up the trailing '/'.
return fl_scandir(d, list, 0, sort);
}
// //
// End of "$Id$". // End of "$Id$".
// //

View File

@ -19,27 +19,12 @@
// Wrapper for scandir with const-correct function prototypes. // Wrapper for scandir with const-correct function prototypes.
#include <FL/filename.H> #include <FL/filename.H>
#include <FL/Fl.H>
#include <FL/platform_types.h>
#include <FL/fl_utf8.h> #include <FL/fl_utf8.h>
#include "flstring.h" #include "flstring.h"
#include <stdlib.h> #include <stdlib.h>
#ifdef __APPLE__ // PORTME: Fl_System_Driver - directory stuff
#include <FL/x.H>
#endif
#ifdef WIN32
#elif defined(__APPLE__) // PORTME: Fl_System_Driver - directory stuff
#elif defined(FL_PORTING)
# pragma message "FL_PORTING: implement directory and filename handling for your platform if needed"
#else // X11
#endif
extern "C" {
#ifndef HAVE_SCANDIR
int fl_scandir (const char *dir, dirent ***namelist,
int (*select)(dirent *),
int (*compar)(dirent **, dirent **));
#endif
}
int fl_alphasort(struct dirent **a, struct dirent **b) { int fl_alphasort(struct dirent **a, struct dirent **b) {
return strcmp((*a)->d_name, (*b)->d_name); return strcmp((*a)->d_name, (*b)->d_name);
@ -79,112 +64,8 @@ int fl_casealphasort(struct dirent **a, struct dirent **b) {
according to their ASCII ordering - uppercase before lowercase. according to their ASCII ordering - uppercase before lowercase.
\return the number of entries if no error, a negative value otherwise. \return the number of entries if no error, a negative value otherwise.
*/ */
int fl_filename_list(const char *d, dirent ***list, int fl_filename_list(const char *d, dirent ***list, Fl_File_Sort_F *sort) {
Fl_File_Sort_F *sort) { return Fl::system_driver()->filename_list(d, list, sort);
#if defined(WIN32) && !defined(__CYGWIN__) && !defined(HAVE_SCANDIR)
// For Windows we have a special scandir implementation that uses
// the Win32 "wide" functions for lookup, avoiding the code page mess
// entirely. It also fixes up the trailing '/'.
return fl_scandir(d, list, 0, sort);
#else // WIN32
int dirlen;
char *dirloc;
// Assume that locale encoding is no less dense than UTF-8
dirlen = strlen(d);
#ifdef __APPLE__ // PORTME: Fl_System_Driver - directory stuff
dirloc = (char *)d;
#else
dirloc = (char *)malloc(dirlen + 1);
fl_utf8to_mb(d, dirlen, dirloc, dirlen + 1);
#endif
#ifndef HAVE_SCANDIR
// This version is when we define our own scandir
int n = fl_scandir(dirloc, list, 0, sort);
#elif defined(HAVE_SCANDIR_POSIX) && !defined(__APPLE__) // PORTME: Fl_System_Driver - directory stuff
// POSIX (2008) defines the comparison function like this:
int n = scandir(dirloc, list, 0, (int(*)(const dirent **, const dirent **))sort);
#elif defined(__osf__)
// OSF, DU 4.0x
int n = scandir(dirloc, list, 0, (int(*)(dirent **, dirent **))sort);
#elif defined(_AIX)
// AIX is almost standard...
int n = scandir(dirloc, list, 0, (int(*)(void*, void*))sort);
#elif defined(__sgi)
int n = scandir(dirloc, list, 0, sort);
#elif defined(__APPLE__) // PORTME: Fl_System_Driver - directory stuff
# if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8
int n = scandir(dirloc, list, 0, (int(*)(const struct dirent**,const struct dirent**))sort);
# else
int n = scandir(dirloc, list, 0, (int(*)(const void*,const void*))sort);
# endif
#elif defined(FL_PORTING)
# pragma message "FL_PORTING: defien scandir"
int n = 0;
#else
// The vast majority of UNIX systems want the sort function to have this
// prototype, most likely so that it can be passed to qsort without any
// changes:
int n = scandir(dirloc, list, 0, (int(*)(const void*,const void*))sort);
#endif
#ifndef __APPLE__ // PORTME: Fl_System_Driver - directory stuff
free(dirloc);
#endif
// convert every filename to utf-8, and append a '/' to all
// filenames that are directories
int i;
char *fullname = (char*)malloc(dirlen+FL_PATH_MAX+3); // Add enough extra for two /'s and a nul
// Use memcpy for speed since we already know the length of the string...
memcpy(fullname, d, dirlen+1);
char *name = fullname + dirlen;
if (name!=fullname && name[-1]!='/')
*name++ = '/';
for (i=0; i<n; i++) {
int newlen;
dirent *de = (*list)[i];
int len = strlen(de->d_name);
#ifdef __APPLE__ // PORTME: Fl_System_Driver - directory stuff
newlen = len;
#else
newlen = fl_utf8from_mb(NULL, 0, de->d_name, len);
#endif
dirent *newde = (dirent*)malloc(de->d_name - (char*)de + newlen + 2); // Add space for a / and a nul
// Conversion to UTF-8
memcpy(newde, de, de->d_name - (char*)de);
#ifdef __APPLE__ // PORTME: Fl_System_Driver - directory stuff
strcpy(newde->d_name, de->d_name);
#else
fl_utf8from_mb(newde->d_name, newlen + 1, de->d_name, len);
#endif
// Check if dir (checks done on "old" name as we need to interact with
// the underlying OS)
if (de->d_name[len-1]!='/' && len<=FL_PATH_MAX) {
// Use memcpy for speed since we already know the length of the string...
memcpy(name, de->d_name, len+1);
if (fl_filename_isdir(fullname)) {
char *dst = newde->d_name + newlen;
*dst++ = '/';
*dst = 0;
}
}
free(de);
(*list)[i] = newde;
}
free(fullname);
return n;
#endif // WIN32
} }
/** /**
@ -209,7 +90,6 @@ void fl_filename_free_list(struct dirent ***list, int n)
*list = 0; *list = 0;
} }
// //
// End of "$Id$". // End of "$Id$".
// //

View File

@ -16,32 +16,9 @@
* http://www.fltk.org/str.php * http://www.fltk.org/str.php
*/ */
/* My own scandir sorting function, useful for the film industry where
we have many files with numbers in their names: */
#include <config.h>
#include <ctype.h> #include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <FL/platform_types.h>
#if !defined(WIN32) || defined(__CYGWIN__)
# ifdef HAVE_DIRENT_H
# include <dirent.h>
# else
# define dirent direct
# ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif /* HAVE_SYS_NDIR_H */
# ifdef HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif /* HAVE_SYS_DIR_H */
# ifdef HAVE_NDIR_H
# include <ndir.h>
# endif /* HAVE_NDIR_H */
# endif /* HAVE_DIRENT_H */
#endif /* !WIN32 || __CYGWIN__ */
#include <FL/filename.H>
/* /*
* 'numericsort()' - Compare two directory entries, possibly with * 'numericsort()' - Compare two directory entries, possibly with

View File

@ -1,35 +0,0 @@
/*
* "$Id$"
*
* This is a placekeeper stub that pulls in scandir implementations for host
* systems that do not provide a compatible one natively
*
* Copyright 1998-2013 by Bill Spitzak and others.
*
* This library is free software. Distribution and use rights are outlined in
* the file "COPYING" which should have been included with this file. If this
* file is missing or damaged, see the license at:
*
* http://www.fltk.org/COPYING.php
*
* Please report all bugs and problems on the following page:
*
* http://www.fltk.org/str.php
*/
#if defined(WIN32) && !defined(__CYGWIN__)
# include "scandir_win32.c"
#else
# include <config.h>
# ifndef HAVE_SCANDIR
# include "scandir_posix.c"
# endif /* HAVE_SCANDIR */
#endif
/* Avoid "ISO C forbids an empty translation unit" warning */
typedef int dummy;
/*
* End of "$Id$".
*/

View File

@ -32,6 +32,7 @@
* http://womble.decadent.org.uk/readdir_r-advisory.html * http://womble.decadent.org.uk/readdir_r-advisory.html
*/ */
#include <config.h>
#ifndef HAVE_PTHREAD #ifndef HAVE_PTHREAD
/* Switch system headers into POSIX.1-1990 mode */ /* Switch system headers into POSIX.1-1990 mode */
# define _POSIX_SOURCE # define _POSIX_SOURCE

View File

@ -19,7 +19,7 @@
#ifndef __CYGWIN__ #ifndef __CYGWIN__
/* Emulation of posix scandir() call */ /* Emulation of posix scandir() call */
#include <FL/fl_utf8.h> #include <FL/fl_utf8.h>
#include <FL/filename.H> #include <FL/platform_types.h>
#include "flstring.h" #include "flstring.h"
#include <windows.h> #include <windows.h>
#include <stdlib.h> #include <stdlib.h>