[master] edfe684 - Windows: fix fl_filename_isdir()

GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[master] edfe684 - Windows: fix fl_filename_isdir() "Albrecht Schlosser" 11:47 Mar 21 top right image
 
commit edfe6844194a821c2be3312abaff129b57a54601
Author:     Albrecht Schlosser <albrechts.fltk@online.de>
AuthorDate: Sun Mar 21 19:36:27 2021 +0100
Commit:     Albrecht Schlosser <albrechts.fltk@online.de>
CommitDate: Sun Mar 21 19:36:27 2021 +0100

    Windows: fix fl_filename_isdir()
    
    - convert filename from UTF-8 to "Windows wide chars"
    - use GetFileAttributesW() instead of _stat()

 src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx | 40 ++++++++++++++------------
 1 file changed, 21 insertions(+), 19 deletions(-)

diff --git src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx
index 4a7a028..7e295fb 100644
--- src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx
+++ src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx
@@ -671,28 +671,30 @@ int Fl_WinAPI_System_Driver::filename_absolute(char *to, int tolen, const char *
 
 int Fl_WinAPI_System_Driver::filename_isdir(const char *n)
 {
-  struct _stat  s;
-  char          fn[FL_PATH_MAX];
-  int           length;
-  length = (int) strlen(n);
+  char fn[4]; // used for drive letter only: "X:/"
+  int length = (int)strlen(n);
+  // Strip trailing slash from name...
+  if (length > 0 && isdirsep(n[length - 1]))
+    length --;
+  if (length < 1)
+    return 0;
+
   // 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;
-    }
+
+  if (length == 2 && isalpha(n[0]) && n[1] == ':') { // trailing '/' already "removed"
+    // Always use "X:/" for drive letters
+    fn[0] = n[0];
+    strcpy(fn + 1, ":/");
+    n = fn;
+    length = 3;
   }
-  return !_stat(n, &s) && (s.st_mode & _S_IFDIR);
+
+  // convert filename to wide chars using *length*
+  utf8_to_wchar(n, wbuf, length);
+
+  DWORD fa = GetFileAttributesW(wbuf);
+  return (fa != INVALID_FILE_ATTRIBUTES) && (fa & FILE_ATTRIBUTE_DIRECTORY);
 }
 
 int Fl_WinAPI_System_Driver::filename_isdir_quick(const char *n)
Direct Link to Message ]
 
bottom left image   bottom right image
Previous Message ]Next Message ]
 
 

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