Rewrite filename_isdir.cxx for the driver model.

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3-porting@11555 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Manolo Gouy 2016-04-08 15:48:28 +00:00
parent 62952ea295
commit 90682dbd48
7 changed files with 69 additions and 57 deletions

View File

@ -106,6 +106,10 @@ public:
virtual int filename_relative(char *to, int tolen, const char *from, const char *base);
// the default implementation of filename_absolute() is in src/filename_absolute.cxx and may be enough
virtual int filename_absolute(char *to, int tolen, const char *from);
// the default implementation of filename_isdir() is in src/filename_isdir.cxx and may be enough
virtual int filename_isdir(const char* n);
// the default implementation of filename_isdir_quick() is in src/filename_isdir.cxx and may be enough
virtual int filename_isdir_quick(const char* n);
};
#endif // FL_SYSTEM_DRIVER_H

View File

@ -100,17 +100,6 @@ FL_EXPORT int fl_open_uri(const char *uri, char *msg = (char *)0,
FL_EXPORT void fl_decode_uri(char *uri);
# ifndef FL_DOXYGEN
/*
* _fl_filename_isdir_quick() is a private function that checks for a
* trailing slash and assumes that the passed name is a directory if
* it finds one. This function is used by Fl_File_Browser and
* Fl_File_Chooser to avoid extra stat() calls, but is not supported
* outside of FLTK...
*/
int _fl_filename_isdir_quick(const char *name);
# endif
# endif /* __cplusplus */
/*

View File

@ -37,6 +37,8 @@
//
#include <FL/Fl_File_Browser.H>
#include <FL/Fl.H>
#include <FL/Fl_System_Driver.H>
#include <FL/fl_draw.H>
#include <FL/filename.H>
#include <FL/Fl_Image.H> // icon
@ -700,7 +702,7 @@ Fl_File_Browser::load(const char *directory,// I - Directory to load
icon = Fl_File_Icon::find(filename);
if ((icon && icon->type() == Fl_File_Icon::DIRECTORY) ||
_fl_filename_isdir_quick(filename)) {
Fl::system_driver()->filename_isdir_quick(filename)) {
num_dirs ++;
insert(num_dirs, files[i]->d_name, icon);
} else if (filetype_ == FILES &&

View File

@ -344,7 +344,8 @@
//
#include <FL/Fl_File_Chooser.H>
#include <FL/Fl_System_Driver.H> // for struct stat
#include <FL/Fl_System_Driver.H>
#include <FL/Fl.H>
#include <FL/filename.H>
#include <FL/fl_ask.H>
#include <FL/x.H>
@ -724,9 +725,9 @@ Fl_File_Chooser::fileListCB()
if (Fl::event_clicks()) {
#if (defined(WIN32) && ! defined(__CYGWIN__)) || defined(__EMX__)
if ((strlen(pathname) == 2 && pathname[1] == ':') ||
_fl_filename_isdir_quick(pathname))
Fl::system_driver()->filename_isdir_quick(pathname))
#else
if (_fl_filename_isdir_quick(pathname))
if (Fl::system_driver()->filename_isdir_quick(pathname))
#endif /* WIN32 || __EMX__ */
{
// Change directories...
@ -790,7 +791,7 @@ Fl_File_Chooser::fileListCB()
if (callback_) (*callback_)(this, data_);
// Activate the OK button as needed...
if (!_fl_filename_isdir_quick(pathname) || (type_ & DIRECTORY))
if (!Fl::system_driver()->filename_isdir_quick(pathname) || (type_ & DIRECTORY))
okButton->activate();
else
okButton->deactivate();
@ -857,15 +858,15 @@ Fl_File_Chooser::fileNameCB()
// Enter pressed - select or change directory...
#if (defined(WIN32) && ! defined(__CYGWIN__)) || defined(__EMX__)
if ((isalpha(pathname[0] & 255) && pathname[1] == ':' && !pathname[2]) ||
(_fl_filename_isdir_quick(pathname) &&
(Fl::system_driver()->filename_isdir_quick(pathname) &&
compare_dirnames(pathname, directory_))) {
#else
if (_fl_filename_isdir_quick(pathname) &&
if (Fl::system_driver()->filename_isdir_quick(pathname) &&
compare_dirnames(pathname, directory_)) {
#endif /* WIN32 || __EMX__ */
directory(pathname);
} else if ((type_ & CREATE) || access(pathname, 0) == 0) {
if (!_fl_filename_isdir_quick(pathname) || (type_ & DIRECTORY)) {
if (!Fl::system_driver()->filename_isdir_quick(pathname) || (type_ & DIRECTORY)) {
// Update the preview box...
update_preview();

View File

@ -72,6 +72,8 @@ public:
virtual int filename_expand(char *to,int tolen, const char *from);
virtual int filename_relative(char *to, int tolen, const char *from, const char *base);
virtual int filename_absolute(char *to, int tolen, const char *from);
virtual int filename_isdir(const char* n);
virtual int filename_isdir_quick(const char* n);
};
#endif // FL_WINAPI_SYSTEM_DRIVER_H

View File

@ -21,6 +21,7 @@
#include "Fl_WinAPI_System_Driver.H"
#include <FL/Fl.H>
#include <FL/fl_utf8.h>
#include <FL/filename.H>
#include <stdio.h>
#include <stdarg.h>
#include <windows.h>
@ -614,6 +615,39 @@ int Fl_WinAPI_System_Driver::filename_absolute(char *to, int tolen, const char *
return 1;
}
int Fl_WinAPI_System_Driver::filename_isdir(const char* n)
{
struct stat s;
char fn[FL_PATH_MAX];
int length;
length = (int) strlen(n);
// This workaround brought to you by the fine folks at Microsoft!
// (read lots of sarcasm in that...)
if (length < (int)(sizeof(fn) - 1)) {
if (length < 4 && isalpha(n[0]) && n[1] == ':' &&
(isdirsep(n[2]) || !n[2])) {
// Always use D:/ for drive letters
fn[0] = n[0];
strcpy(fn + 1, ":/");
n = fn;
} else if (length > 0 && isdirsep(n[length - 1])) {
// Strip trailing slash from name...
length --;
memcpy(fn, n, length);
fn[length] = '\0';
n = fn;
}
}
return !stat(n, &s) && (s.st_mode & S_IFMT) == S_IFDIR;
}
int Fl_WinAPI_System_Driver::filename_isdir_quick(const char* n)
{
// Do a quick optimization for filenames with a trailing slash...
if (*n && isdirsep(n[strlen(n) - 1])) return 1;
return filename_isdir(n);
}
//
// End of "$Id$".
//

View File

@ -19,22 +19,21 @@
// Used by fl_file_chooser
#include "flstring.h"
#include <sys/types.h>
#include <FL/Fl_System_Driver.H> // for struct stat
#include <ctype.h>
#include <FL/Fl_System_Driver.H>
#include <FL/filename.H>
#include <FL/fl_utf8.h>
#include <FL/Fl.H>
#if defined(WIN32) || defined(__EMX__) && !defined(__CYGWIN__)
static inline int isdirsep(char c) {return c=='/' || c=='\\';}
#else
#define isdirsep(c) ((c)=='/')
#endif
int _fl_filename_isdir_quick(const char* n) {
/*
* filename_isdir_quick() is a private function that checks for a
* trailing slash and assumes that the passed name is a directory if
* it finds one. This function is used by Fl_File_Browser and
* Fl_File_Chooser to avoid extra stat() calls, but is not supported
* outside of FLTK...
*/
int Fl_System_Driver::filename_isdir_quick(const char* n) {
// Do a quick optimization for filenames with a trailing slash...
if (*n && isdirsep(n[strlen(n) - 1])) return 1;
return fl_filename_isdir(n);
if (*n && n[strlen(n) - 1] == '/') return 1;
return filename_isdir(n);
}
/**
@ -49,43 +48,24 @@ int _fl_filename_isdir_quick(const char* n) {
\return non zero if file exists and is a directory, zero otherwise
*/
int fl_filename_isdir(const char* n) {
return Fl::system_driver()->filename_isdir(n);
}
int Fl_System_Driver::filename_isdir(const char* n) {
struct stat s;
char fn[FL_PATH_MAX];
int length;
length = (int) strlen(n);
#ifdef WIN32
// This workaround brought to you by the fine folks at Microsoft!
// (read lots of sarcasm in that...)
if (length < (int)(sizeof(fn) - 1)) {
if (length < 4 && isalpha(n[0]) && n[1] == ':' &&
(isdirsep(n[2]) || !n[2])) {
// Always use D:/ for drive letters
fn[0] = n[0];
strcpy(fn + 1, ":/");
n = fn;
} else if (length > 0 && isdirsep(n[length - 1])) {
// Strip trailing slash from name...
length --;
memcpy(fn, n, length);
fn[length] = '\0';
n = fn;
}
}
#else
// Matt: Just in case, we strip the slash for other operating
// systems as well, avoid bugs by sloppy implementations
// of "stat".
if (length > 1 && isdirsep(n[length - 1])) {
if (length > 1 && n[length - 1] == '/') {
length --;
memcpy(fn, n, length);
fn[length] = '\0';
n = fn;
}
#endif
return !fl_stat(n, &s) && (s.st_mode & S_IFMT) == S_IFDIR;
return !stat(n, &s) && (s.st_mode & S_IFMT) == S_IFDIR;
}
//