summaryrefslogtreecommitdiff
path: root/src/drivers/Darwin/Fl_Darwin_System_Driver.cxx
diff options
context:
space:
mode:
authorManolo Gouy <Manolo>2016-04-07 15:10:30 +0000
committerManolo Gouy <Manolo>2016-04-07 15:10:30 +0000
commit23a60edb44d264887a398eef1b6346f8f24737e2 (patch)
treeae6920a8b002dd26240f560d61df84888ba3b4a5 /src/drivers/Darwin/Fl_Darwin_System_Driver.cxx
parent406fcaf3053b2d8d9c8ea677c59667a7ec43a556 (diff)
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
Diffstat (limited to 'src/drivers/Darwin/Fl_Darwin_System_Driver.cxx')
-rw-r--r--src/drivers/Darwin/Fl_Darwin_System_Driver.cxx47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/drivers/Darwin/Fl_Darwin_System_Driver.cxx b/src/drivers/Darwin/Fl_Darwin_System_Driver.cxx
index 2f6d52fc6..617e8b19d 100644
--- a/src/drivers/Darwin/Fl_Darwin_System_Driver.cxx
+++ b/src/drivers/Darwin/Fl_Darwin_System_Driver.cxx
@@ -20,6 +20,7 @@
#include "../../config_lib.h"
#include "Fl_Darwin_System_Driver.H"
#include <FL/Fl.H>
+#include <FL/filename.H>
#include <string.h>
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
#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);
}
+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$".
//