GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-glob.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2010-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <algorithm>
31 #include <string>
32 
33 #include "glob-wrappers.h"
34 
35 #include "oct-glob.h"
36 #include "file-ops.h"
37 #include "file-stat.h"
38 #include "unwind-prot.h"
39 
40 #if defined (OCTAVE_USE_WINDOWS_API)
41 # include <windows.h>
42 # include <shlwapi.h>
43 # include <wchar.h>
44 
45 # include "lo-sysdep.h"
46 #endif
47 
48 // These functions are defined here and not in glob_match.cc so that we
49 // can include the glob.h file from gnulib, which defines glob to
50 // be rpl_glob. If we include glob.h in glob_match.cc, then it
51 // transforms the glob_match::glob function to be glob_match::rpl_glob,
52 // which is not what we want...
53 
55 
56 static bool
57 single_match_exists (const std::string& file)
58 {
59  sys::file_stat s (file);
60 
61  return s.exists ();
62 }
63 
65 
66 bool
67 fnmatch (const string_vector& pat, const std::string& str, int fnm_flags)
68 {
69  int npat = pat.numel ();
70 
71  const char *cstr = str.c_str ();
72 
73  for (int i = 0; i < npat; i++)
74  if (octave_fnmatch_wrapper (pat(i).c_str (), cstr, fnm_flags)
76  return true;
77 
78  return false;
79 }
80 
82 glob (const string_vector& pat)
83 {
84  string_vector retval;
85 
86  int npat = pat.numel ();
87 
88  int k = 0;
89 
90  void *glob_info = octave_create_glob_info_struct ();
91 
92  unwind_action cleanup_glob_info_struct
93  ([=] () { octave_destroy_glob_info_struct (glob_info); });
94 
95  for (int i = 0; i < npat; i++)
96  {
97  std::string xpat = pat(i);
98 
99  if (! xpat.empty ())
100  {
101 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
102  && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
103  std::replace (xpat.begin (), xpat.end (), '\\', '/');
104 #endif
105 
106  int err = octave_glob_wrapper (xpat.c_str (),
108  glob_info);
109 
110  if (! err)
111  {
112  int n = octave_glob_num_matches (glob_info);
113 
114  const char *const *matches
115  = octave_glob_match_list (glob_info);
116 
117  // FIXME: we shouldn't have to check to see if
118  // a single match exists, but it seems that glob() won't
119  // check for us unless the pattern contains globbing
120  // characters. Hmm.
121 
122  if (n > 1
123  || (n == 1
124  && single_match_exists (std::string (matches[0]))))
125  {
126  retval.resize (k+n);
127 
128  for (int j = 0; j < n; j++)
129  {
130  std::string tmp = matches[j];
131 
132 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
133  && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM)
134  std::replace (tmp.begin (), tmp.end (), '/', '\\');
135 #endif
136 
137  retval[k++] = tmp;
138  }
139  }
140 
141  octave_globfree_wrapper (glob_info);
142  }
143  }
144  }
145 
146  return retval.sort ();
147 }
148 
149 #if defined (OCTAVE_USE_WINDOWS_API)
150 
151 static void
152 find_files (std::list<std::string>& dirlist, const std::string& dir,
153  const std::string& pat, std::string& file)
154 {
155  // remove leading file separators
156  bool is_file_empty = file.empty ();
157  while (! file.empty () && sys::file_ops::is_dir_sep (file[0]))
158  file = file.substr (1, std::string::npos);
159 
160  bool is_trailing_file_sep = ! is_file_empty && file.empty ();
161 
162  if (! pat.compare (".") || ! pat.compare (".."))
163  {
164  // shortcut for trivial patterns that would expand to a folder name
165 
166  // get next component of path (or file name)
167  std::size_t sep_pos
168  = file.find_first_of (sys::file_ops::dir_sep_chars ());
169  std::string pat_str = file.substr (0, sep_pos);
170  std::string file_str = (sep_pos != std::string::npos)
171  ? file.substr (sep_pos) : "";
172 
173  // Original pattern ends with "." or "..". Take it as we have it.
174  if (pat_str.empty ())
175  {
176  if (is_trailing_file_sep)
177  pat_str = sys::file_ops::dir_sep_char ();
178  dirlist.push_back (sys::file_ops::concat (dir, pat) + pat_str);
179  return;
180  }
181 
182  // call this function recursively with next path component in PAT
183  find_files (dirlist, sys::file_ops::concat (dir, pat),
184  pat_str, file_str);
185  return;
186  }
187 
188  // find first file in directory that matches pattern in PAT
189  std::wstring wpat = u8_to_wstring (sys::file_ops::concat (dir, pat));
190  _WIN32_FIND_DATAW ffd;
191  HANDLE h_find = FindFirstFileW (wpat.c_str (), &ffd);
192  // ignore any error
193  if (h_find == INVALID_HANDLE_VALUE)
194  return;
195 
196  unwind_action close_h_find ([=] () { FindClose (h_find); });
197 
198  // find all files that match pattern
199  do
200  {
201  // must be directory if pattern continues
202  if (! (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
203  && (! file.empty () || is_trailing_file_sep))
204  continue;
205 
206  std::string found_dir = u8_from_wstring (ffd.cFileName);
207 
208  if (file.empty ())
209  {
210  // Don't include "." and ".." in matches.
211  if (found_dir.compare (".") && found_dir.compare (".."))
212  {
213  if (is_trailing_file_sep)
214  found_dir += sys::file_ops::dir_sep_char ();
215  dirlist.push_back (sys::file_ops::concat (dir, found_dir));
216  }
217  }
218  else
219  {
220  // get next component of path (or file name)
221  std::size_t sep_pos
222  = file.find_first_of (sys::file_ops::dir_sep_chars ());
223  std::string pat_str = file.substr (0, sep_pos);
224  std::string file_str = (sep_pos != std::string::npos)
225  ? file.substr (sep_pos) : "";
226 
227  // call this function recursively with next path component in PAT
228  find_files (dirlist, sys::file_ops::concat (dir, found_dir),
229  pat_str, file_str);
230  }
231  }
232  while (FindNextFileW (h_find, &ffd) != 0);
233 }
234 
235 #endif
236 
237 // Glob like Windows "dir". Treat only * and ? as wildcards,
238 // and "*.*" matches filenames even if they do not contain ".".
241 {
242  string_vector retval;
243 
244  int npat = pat.numel ();
245 
246 #if defined (OCTAVE_USE_WINDOWS_API)
247 
248  std::list<std::string> dirlist;
249 
250  for (int i = 0; i < npat; i++)
251  {
252  std::string xpat = pat(i);
253  if (xpat.empty ())
254  continue;
255 
256  std::string dir = "";
257 
258  // separate component until first file separator
259  std::size_t sep_pos
260  = xpat.find_first_of (sys::file_ops::dir_sep_chars ());
261 
262  // handle UNC paths
263  if (sep_pos == 0 && xpat.length () > 1
264  && sys::file_ops::is_dir_sep (xpat[1]))
265  {
266  // start pattern with a file, i.e., "\\SERVER\share\file"
267  sep_pos = xpat.find_first_of (sys::file_ops::dir_sep_chars (), 2);
268  if (sep_pos != std::string::npos)
269  sep_pos = xpat.find_first_of (sys::file_ops::dir_sep_chars (),
270  sep_pos + 1);
271  if (sep_pos != std::string::npos)
272  {
273  dir = xpat.substr(0, sep_pos);
274  xpat = xpat.substr (sep_pos+1);
275  sep_pos = xpat.find_first_of (sys::file_ops::dir_sep_chars ());
276  }
277  }
278 
279  std::string file = (sep_pos != std::string::npos)
280  ? xpat.substr (sep_pos) : "";
281  xpat = xpat.substr (0, sep_pos);
282 
283  if ((sep_pos == 2 || xpat.length () == 2) && xpat[1] == ':')
284  {
285  // include disc root with first file or folder
286 
287  // remove leading file separators in path without disc root
288  while (file.length () > 1 && sys::file_ops::is_dir_sep (file[0]))
289  file = file.substr (1, std::string::npos);
290 
291  sep_pos = file.find_first_of (sys::file_ops::dir_sep_chars ());
292  dir = xpat;
293  xpat = file.substr (0, sep_pos);
294  file = (sep_pos != std::string::npos)
295  ? file.substr (sep_pos) : "";
296  if (xpat.empty ())
297  {
298  // don't glob if input is only disc root
299  std::wstring wpat = u8_to_wstring (pat(i));
300  if (PathFileExistsW (wpat.c_str ()))
301  {
302  if (sys::file_ops::is_dir_sep (pat(i).back ()))
303  dirlist.push_back (dir +
305  else
306  dirlist.push_back (dir);
307  }
308  continue;
309  }
310  }
311 
312  find_files (dirlist, dir, xpat, file);
313  }
314 
315  retval = string_vector (dirlist);
316 
317 #else
318 
319  int k = 0;
320 
321  void *glob_info = octave_create_glob_info_struct ();
322 
323  unwind_action cleanup_glob_info_struct
324  ([=] () { octave_destroy_glob_info_struct (glob_info); });
325 
326  for (int i = 0; i < npat; i++)
327  {
328  std::string xpat = pat(i);
329 
330  if (! xpat.empty ())
331  {
332  std::string escaped;
333  escaped.reserve (xpat.length ());
334 
335  for (std::size_t j = 0; j < xpat.length (); j++)
336  {
337 # if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
338  && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
339  if (xpat[j] == '\\')
340  escaped += '/';
341  else
342 # endif
343  {
344  if (xpat[j] == ']' || xpat[j] == '[')
345  escaped += '\\';
346 
347  escaped += xpat[j];
348  }
349  }
350 
351  // Replace trailing "*.*" by "*".
352  int len = escaped.length ();
353  if (len >= 3 && escaped.substr (len - 3) == "*.*")
354  escaped = escaped.substr (0, len - 2);
355 
356  int err = octave_glob_wrapper (escaped.c_str (),
358  glob_info);
359 
360  if (! err)
361  {
362  int n = octave_glob_num_matches (glob_info);
363 
364  const char *const *matches
365  = octave_glob_match_list (glob_info);
366 
367  // FIXME: we shouldn't have to check to see if
368  // a single match exists, but it seems that glob() won't
369  // check for us unless the pattern contains globbing
370  // characters. Hmm.
371 
372  if (n > 1
373  || (n == 1
374  && single_match_exists (std::string (matches[0]))))
375  {
376  retval.resize (k + n);
377 
378  for (int j = 0; j < n; j++)
379  {
380  std::string tmp = matches[j];
381 
382  std::string unescaped;
383  unescaped.reserve (tmp.length ());
384 
385  for (std::size_t m = 0; m < tmp.length (); m++)
386  {
387 # if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
388  && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
389  if (tmp[m] == '/')
390  unescaped += '\\';
391  else
392 # endif
393  {
394  if (tmp[m] == '\\'
395  && ++m == tmp.length ())
396  break;
397 
398  unescaped += tmp[m];
399  }
400  }
401 
402  retval[k++] = unescaped;
403  }
404  }
405 
406  octave_globfree_wrapper (glob_info);
407  }
408  }
409  }
410 #endif
411 
412  return retval.sort ();
413 }
414 
OCTAVE_END_NAMESPACE(octave)
ComplexNDArray concat(NDArray &ra, ComplexNDArray &rb, const Array< octave_idx_type > &ra_idx)
Definition: CNDArray.cc:418
string_vector & sort(bool make_uniq=false)
Definition: str-vec.cc:77
void resize(octave_idx_type n, const std::string &rfv="")
Definition: str-vec.h:95
octave_idx_type numel(void) const
Definition: str-vec.h:100
bool empty(void) const
Definition: str-vec.h:77
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
int octave_fnm_nomatch_wrapper(void)
Definition: glob-wrappers.c:92
int octave_glob_nosort_wrapper(void)
Definition: glob-wrappers.c:80
void octave_destroy_glob_info_struct(void *glob_info)
Definition: glob-wrappers.c:50
void octave_globfree_wrapper(void *glob_info)
Definition: glob-wrappers.c:74
void * octave_create_glob_info_struct(void)
Definition: glob-wrappers.c:43
int octave_glob_num_matches(void *glob_info)
Definition: glob-wrappers.c:62
int octave_glob_wrapper(const char *pattern, int flags, void *glob_info)
Definition: glob-wrappers.c:56
int octave_fnmatch_wrapper(const char *pattern, const char *name, int flags)
Definition: glob-wrappers.c:86
char ** octave_glob_match_list(void *glob_info)
Definition: glob-wrappers.c:68
std::string u8_from_wstring(const std::wstring &wchar_string)
Definition: lo-sysdep.cc:537
std::wstring u8_to_wstring(const std::string &utf8_string)
Definition: lo-sysdep.cc:514
T octave_idx_type m
Definition: mx-inlines.cc:773
octave_idx_type n
Definition: mx-inlines.cc:753
string_vector windows_glob(const string_vector &pat)
Definition: oct-glob.cc:240
bool fnmatch(const string_vector &pat, const std::string &str, int fnm_flags)
Definition: oct-glob.cc:67
static bool single_match_exists(const std::string &file)
Definition: oct-glob.cc:57
string_vector glob(const string_vector &pat)
Definition: oct-glob.cc:82
static const char dir_sep_char
Definition: shared-fcns.h:90
static bool is_dir_sep(char c)
Definition: shared-fcns.h:142
static std::string dir_sep_chars
Definition: shared-fcns.h:96
F77_RET_T len
Definition: xerbla.cc:61