diff liboctave/util/oct-glob.cc @ 28869:4e64416772f1

Use Windows wide character API for __wglob__ (bug #59231). * liboctave/util/oct-glob.cc (windows_glob): Use wide character WinAPI functions (instead of gnulib functions) on Windows. * libinterp/corefcn/dirfns.cc (Fglob): Document that function won't work with non-ASCII characters on Windows.
author Markus Mützel <markus.muetzel@gmx.de>
date Thu, 08 Oct 2020 16:13:20 +0200
parents 43ad651cf5a0
children caf577e7ef43
line wrap: on
line diff
--- a/liboctave/util/oct-glob.cc	Thu Oct 08 16:07:39 2020 +0200
+++ b/liboctave/util/oct-glob.cc	Thu Oct 08 16:13:20 2020 +0200
@@ -36,6 +36,13 @@
 #include "file-stat.h"
 #include "unwind-prot.h"
 
+#if defined (OCTAVE_USE_WINDOWS_API)
+#  include <windows.h>
+#  include <wchar.h>
+
+#  include "lo-sysdep.h"
+#endif
+
 // These functions are defined here and not in glob_match.cc so that we
 // can include the glob.h file from gnulib, which defines glob to
 // be rpl_glob.  If we include glob.h in glob_match.cc, then it
@@ -151,6 +158,36 @@
 
       int npat = pat.numel ();
 
+#if defined (OCTAVE_USE_WINDOWS_API)
+
+      _WIN32_FIND_DATAW ffd;
+      std::list<std::string> dirlist_str;
+
+      for (int i = 0; i < npat; i++)
+        {
+          std::string xpat = pat(i);
+          if (xpat.empty ())
+            continue;
+
+          // find first file in directory that matches pattern
+          std::wstring wxpat = u8_to_wstring (xpat);
+          HANDLE h_find = FindFirstFileW (wxpat.c_str (), &ffd);
+          // ignore any error
+          if (h_find == INVALID_HANDLE_VALUE)
+            continue;
+
+          unwind_action close_h_find ([=] () { FindClose (h_find); });
+
+          // find all other files that match pattern
+          do
+            dirlist_str.push_back (u8_from_wstring (ffd.cFileName));
+          while (FindNextFileW (h_find, &ffd) != 0);
+        }
+
+      retval = string_vector (dirlist_str);
+
+#else
+
       int k = 0;
 
       void *glob_info = octave_create_glob_info_struct ();
@@ -169,12 +206,12 @@
 
               for (size_t j = 0; j < xpat.length (); j++)
                 {
-#if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)           \
-     && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
+#  if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)           \
+       && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
                   if (xpat[j] == '\\')
                     escaped += '/';
                   else
-#endif
+#  endif
                     {
                       if (xpat[j] == ']' || xpat[j] == '[')
                         escaped += '\\';
@@ -219,12 +256,12 @@
 
                           for (size_t m = 0; m < tmp.length (); m++)
                             {
-#if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)           \
+#  if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)           \
      && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
                               if (tmp[m] == '/')
                                 unescaped += '\\';
                               else
-#endif
+#  endif
                                 {
                                   if (tmp[m] == '\\'
                                       && ++m == tmp.length ())
@@ -242,6 +279,7 @@
                 }
             }
         }
+#endif
 
       return retval.sort ();
     }