FLTK logo

[Library] r4526 - branches/branch-1.1/src

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[Library] r4526 - branches/branch-1.1/src fltk-dev Aug 18, 2005  
 
Author: mike
Date: 2005-08-18 10:08:17 -0400 (Thu, 18 Aug 2005)
New Revision: 4526

Modified:
   branches/branch-1.1/src/Fl_File_Browser.cxx
   branches/branch-1.1/src/filename_isdir.cxx
   branches/branch-1.1/src/filename_list.cxx
Log:
Fix new trailing slash usage, and restore correct OSX filesystem listing
(added filter to hide /dev and /.vol)

src/Fl_File_Browser.cxx:
    - Fl_File_Browser::load(): use getfsstat() API on OSX, and don't
      bother adding a trailing slash to directories since we already
      have them!

src/filename_list.cxx:
    - fl_filename_list(): Add 3 bytes (two possible slashes + nul byte)
      to temporary buffer, and document why we are using memcpy() instead
      of strcpy().

src/filename_isdir.cxx:
    - fl_filename_isdir(): Add check for trailing directory separator
      before wasting time doing a stat() call.



Modified: branches/branch-1.1/src/Fl_File_Browser.cxx
===================================================================
--- branches/branch-1.1/src/Fl_File_Browser.cxx	2005-08-17 21:56:22 UTC (rev 4525)
+++ branches/branch-1.1/src/Fl_File_Browser.cxx	2005-08-18 14:08:17 UTC (rev 4526)
@@ -67,7 +67,6 @@
 // CodeWarrior (__MWERKS__) gets its include paths confused, so we
 // temporarily disable this...
 #if defined(__APPLE__) && !defined(__MWERKS__)
-#  include <Carbon/Carbon.h>
 #  include <sys/param.h>
 #  include <sys/ucred.h>
 #  include <sys/mount.h>
@@ -496,20 +495,34 @@
 	num_files ++;
       }
 #elif defined(__APPLE__) && !defined(__MWERKS__)
-    // All mounted volumes are in a directory called '/Volumes/'
-    // This seems to be the case on international installations, too.
+    // MacOS X and Darwin use getfsstat() system call...
+    int			numfs;	// Number of file systems
+    struct statfs	*fs;	// Buffer for file system info
+
+
+    // We always have the root filesystem.
     add("/", icon);
-    dirent **dir;
-    int n = fl_filename_list("/Volumes/", &dir, 0);
-    if (n>=0) {
-      int i;
-      for (i=0; i<n; i++) {
-        if (dir[i]->d_name[0]=='.') continue;
-        sprintf(filename, "/Volumes/%s", dir[i]->d_name);
-        add(filename, icon);
-        free(dir[i]);
+
+    // Get the mounted filesystems...
+    numfs = getfsstat(NULL, 0, MNT_NOWAIT);
+    if (numfs > 0) {
+      // We have file systems, get them...
+      fs = new struct statfs[numfs];
+      getfsstat(fs, sizeof(struct statfs) * numfs, MNT_NOWAIT);
+
+      // Add filesystems to the list...
+      for (i = 0; i < numfs; i ++) {
+	// Ignore "/", "/dev", and "/.vol"...
+        if (fs[i].f_mntonname[1] && strcmp(fs[i].f_mntonname, "/dev") &&
+	    strcmp(fs[i].f_mntonname, "/.vol")) {
+          snprintf(filename, sizeof(filename), "%s/", fs[i].f_mntonname);
+          add(filename, icon);
+        }
+        num_files ++;
       }
-      free(dir);
+
+      // Free the memory used for the file system info array...
+      delete[] fs;
     }
 #else
     //
@@ -578,8 +591,7 @@
       return (0);
 
     for (i = 0, num_dirs = 0; i < num_files; i ++) {
-      if (strcmp(files[i]->d_name, ".") &&
-          strcmp(files[i]->d_name, "./")) {
+      if (strcmp(files[i]->d_name, "./")) {
 	snprintf(filename, sizeof(filename), "%s/%s", directory_,
 	         files[i]->d_name);
 

Modified: branches/branch-1.1/src/filename_isdir.cxx
===================================================================
--- branches/branch-1.1/src/filename_isdir.cxx	2005-08-17 21:56:22 UTC (rev 4525)
+++ branches/branch-1.1/src/filename_isdir.cxx	2005-08-18 14:08:17 UTC (rev 4526)
@@ -43,6 +43,9 @@
 int fl_filename_isdir(const char* n) {
   struct stat	s;
 
+  // Do a quick optimization for filenames with a trailing slash...
+  if (*n && isdirsep(n[strlen(n) - 1])) return 1;
+
 #ifdef WIN32
   char		fn[1024];
   int		length;

Modified: branches/branch-1.1/src/filename_list.cxx
===================================================================
--- branches/branch-1.1/src/filename_list.cxx	2005-08-17 21:56:22 UTC (rev 4525)
+++ branches/branch-1.1/src/filename_list.cxx	2005-08-18 14:08:17 UTC (rev 4526)
@@ -79,7 +79,8 @@
 #else
   // append a '/' to all filenames that are directories
   int i, dirlen = strlen(d);
-  char *fullname = (char*)malloc(dirlen+FL_PATH_MAX+2);
+  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++ = '/';
@@ -87,6 +88,7 @@
     dirent *de = (*list)[i];
     int len = strlen(de->d_name);
     if (de->d_name[len-1]=='/' || len>FL_PATH_MAX) continue;
+    // 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)) {
       if (len<FL_PATH_MAX) {

Direct Link to Message ]
 
     
Previous Message ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.