GNU Octave  9.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-2024 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 "lo-sysdep.h"
39 #include "unwind-prot.h"
40 
41 #if defined (OCTAVE_USE_WINDOWS_API)
42 # include <windows.h>
43 # include <shlwapi.h>
44 # include <wchar.h>
45 #endif
46 
47 // These functions are defined here and not in glob_match.cc so that we
48 // can include the glob.h file from gnulib, which defines glob to
49 // be rpl_glob. If we include glob.h in glob_match.cc, then it
50 // transforms the glob_match::glob function to be glob_match::rpl_glob,
51 // which is not what we want...
52 
54 
56 
57 bool
58 fnmatch (const string_vector& pat, const std::string& str, int fnm_flags)
59 {
60  int npat = pat.numel ();
61 
62  const char *cstr = str.c_str ();
63 
64  for (int i = 0; i < npat; i++)
65  if (octave_fnmatch_wrapper (pat(i).c_str (), cstr, fnm_flags)
67  return true;
68 
69  return false;
70 }
71 
73 glob (const string_vector& pat)
74 {
75  string_vector retval;
76 
77  int npat = pat.numel ();
78 
79  int k = 0;
80 
81  void *glob_info = octave_create_glob_info_struct ();
82 
83  unwind_action cleanup_glob_info_struct
84  ([=] () { octave_destroy_glob_info_struct (glob_info); });
85 
86  for (int i = 0; i < npat; i++)
87  {
88  std::string xpat = pat(i);
89 
90  if (! xpat.empty ())
91  {
92 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
93  && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
94  std::replace (xpat.begin (), xpat.end (), '\\', '/');
95 #endif
96 
97  int err = octave_glob_wrapper (xpat.c_str (),
99  glob_info);
100 
101  if (! err)
102  {
103  int n = octave_glob_num_matches (glob_info);
104 
105  const char *const *matches
106  = octave_glob_match_list (glob_info);
107 
108  // FIXME: we shouldn't have to check to see if
109  // a single match exists, but it seems that glob() won't
110  // check for us unless the pattern contains globbing
111  // characters. Hmm.
112 
113  if (n > 1
114  || (n == 1
115  && sys::file_exists (std::string (matches[0]))))
116  {
117  retval.resize (k+n);
118 
119  for (int j = 0; j < n; j++)
120  {
121  std::string tmp = matches[j];
122 
123 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
124  && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM)
125  std::replace (tmp.begin (), tmp.end (), '/', '\\');
126 #endif
127 
128  retval[k++] = tmp;
129  }
130  }
131 
132  octave_globfree_wrapper (glob_info);
133  }
134  }
135  }
136 
137  return retval.sort ();
138 }
139 
140 #if defined (OCTAVE_USE_WINDOWS_API)
141 
142 static void
143 find_files (std::list<std::string>& dirlist, const std::string& dir,
144  const std::string& pat, std::string& file)
145 {
146  // remove leading file separators
147  bool is_file_empty = file.empty ();
148  while (! file.empty () && sys::file_ops::is_dir_sep (file[0]))
149  file = file.substr (1, std::string::npos);
150 
151  bool is_trailing_file_sep = ! is_file_empty && file.empty ();
152 
153  if (! pat.compare (".") || ! pat.compare (".."))
154  {
155  // shortcut for trivial patterns that would expand to a folder name
156 
157  // get next component of path (or file name)
158  std::size_t sep_pos
159  = file.find_first_of (sys::file_ops::dir_sep_chars ());
160  std::string pat_str = file.substr (0, sep_pos);
161  std::string file_str = (sep_pos != std::string::npos)
162  ? file.substr (sep_pos) : "";
163 
164  // Original pattern ends with "." or "..". Take it as we have it.
165  if (pat_str.empty ())
166  {
167  if (is_trailing_file_sep)
168  pat_str = sys::file_ops::dir_sep_char ();
169  dirlist.push_back (sys::file_ops::concat (dir, pat) + pat_str);
170  return;
171  }
172 
173  // call this function recursively with next path component in PAT
174  find_files (dirlist, sys::file_ops::concat (dir, pat),
175  pat_str, file_str);
176  return;
177  }
178 
179  // find first file in directory that matches pattern in PAT
180  std::wstring wpat = u8_to_wstring (sys::file_ops::concat (dir, pat));
181  _WIN32_FIND_DATAW ffd;
182  HANDLE h_find = FindFirstFileW (wpat.c_str (), &ffd);
183  // ignore any error
184  if (h_find == INVALID_HANDLE_VALUE)
185  return;
186 
187  unwind_action close_h_find ([=] () { FindClose (h_find); });
188 
189  // find all files that match pattern
190  do
191  {
192  // must be directory if pattern continues
193  if (! (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
194  && (! file.empty () || is_trailing_file_sep))
195  continue;
196 
197  std::string found_dir = u8_from_wstring (ffd.cFileName);
198 
199  if (file.empty ())
200  {
201  // Don't include "." and ".." in matches.
202  if (found_dir.compare (".") && found_dir.compare (".."))
203  {
204  if (is_trailing_file_sep)
205  found_dir += sys::file_ops::dir_sep_char ();
206  dirlist.push_back (sys::file_ops::concat (dir, found_dir));
207  }
208  }
209  else
210  {
211  // get next component of path (or file name)
212  std::size_t sep_pos
213  = file.find_first_of (sys::file_ops::dir_sep_chars ());
214  std::string pat_str = file.substr (0, sep_pos);
215  std::string file_str = (sep_pos != std::string::npos)
216  ? file.substr (sep_pos) : "";
217 
218  // call this function recursively with next path component in PAT
219  find_files (dirlist, sys::file_ops::concat (dir, found_dir),
220  pat_str, file_str);
221  }
222  }
223  while (FindNextFileW (h_find, &ffd) != 0);
224 }
225 
226 #endif
227 
228 // Glob like Windows "dir". Treat only * and ? as wildcards,
229 // and "*.*" matches filenames even if they do not contain ".".
232 {
233  string_vector retval;
234 
235  int npat = pat.numel ();
236 
237 #if defined (OCTAVE_USE_WINDOWS_API)
238 
239  std::list<std::string> dirlist;
240 
241  for (int i = 0; i < npat; i++)
242  {
243  std::string xpat = pat(i);
244  if (xpat.empty ())
245  continue;
246 
247  std::string dir = "";
248 
249  // separate component until first file separator
250  std::size_t sep_pos
251  = xpat.find_first_of (sys::file_ops::dir_sep_chars ());
252 
253  // handle UNC paths
254  if (sep_pos == 0 && xpat.length () > 1
255  && sys::file_ops::is_dir_sep (xpat[1]))
256  {
257  // start pattern with a file, i.e., "\\SERVER\share\file"
258  sep_pos = xpat.find_first_of (sys::file_ops::dir_sep_chars (), 2);
259  if (sep_pos != std::string::npos)
260  sep_pos = xpat.find_first_of (sys::file_ops::dir_sep_chars (),
261  sep_pos + 1);
262  if (sep_pos != std::string::npos)
263  {
264  dir = xpat.substr(0, sep_pos);
265  xpat = xpat.substr (sep_pos+1);
266  sep_pos = xpat.find_first_of (sys::file_ops::dir_sep_chars ());
267  }
268  }
269 
270  std::string file = (sep_pos != std::string::npos)
271  ? xpat.substr (sep_pos) : "";
272  xpat = xpat.substr (0, sep_pos);
273 
274  if ((sep_pos == 2 || xpat.length () == 2) && xpat[1] == ':')
275  {
276  // include disc root with first file or folder
277 
278  // remove leading file separators in path without disc root
279  while (file.length () > 1 && sys::file_ops::is_dir_sep (file[0]))
280  file = file.substr (1, std::string::npos);
281 
282  sep_pos = file.find_first_of (sys::file_ops::dir_sep_chars ());
283  dir = xpat;
284  xpat = file.substr (0, sep_pos);
285  file = (sep_pos != std::string::npos)
286  ? file.substr (sep_pos) : "";
287  if (xpat.empty ())
288  {
289  // don't glob if input is only disc root
290  std::wstring wpat = u8_to_wstring (pat(i));
291  if (PathFileExistsW (wpat.c_str ()))
292  {
293  if (sys::file_ops::is_dir_sep (pat(i).back ()))
294  dirlist.push_back (dir +
296  else
297  dirlist.push_back (dir);
298  }
299  continue;
300  }
301  }
302 
303  find_files (dirlist, dir, xpat, file);
304  }
305 
306  retval = string_vector (dirlist);
307 
308 #else
309 
310  int k = 0;
311 
312  void *glob_info = octave_create_glob_info_struct ();
313 
314  unwind_action cleanup_glob_info_struct
315  ([=] () { octave_destroy_glob_info_struct (glob_info); });
316 
317  for (int i = 0; i < npat; i++)
318  {
319  std::string xpat = pat(i);
320 
321  if (! xpat.empty ())
322  {
323  std::string escaped;
324  escaped.reserve (xpat.length ());
325 
326  for (std::size_t j = 0; j < xpat.length (); j++)
327  {
328 # if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
329  && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
330  if (xpat[j] == '\\')
331  escaped += '/';
332  else
333 # endif
334  {
335  if (xpat[j] == ']' || xpat[j] == '[')
336  escaped += '\\';
337 
338  escaped += xpat[j];
339  }
340  }
341 
342  // Replace trailing "*.*" by "*".
343  int len = escaped.length ();
344  if (len >= 3 && escaped.substr (len - 3) == "*.*")
345  escaped = escaped.substr (0, len - 2);
346 
347  int err = octave_glob_wrapper (escaped.c_str (),
349  glob_info);
350 
351  if (! err)
352  {
353  int n = octave_glob_num_matches (glob_info);
354 
355  const char *const *matches
356  = octave_glob_match_list (glob_info);
357 
358  // FIXME: we shouldn't have to check to see if
359  // a single match exists, but it seems that glob() won't
360  // check for us unless the pattern contains globbing
361  // characters. Hmm.
362 
363  if (n > 1
364  || (n == 1
365  && sys::file_exists (std::string (matches[0]))))
366  {
367  retval.resize (k + n);
368 
369  for (int j = 0; j < n; j++)
370  {
371  std::string tmp = matches[j];
372 
373  std::string unescaped;
374  unescaped.reserve (tmp.length ());
375 
376  for (std::size_t m = 0; m < tmp.length (); m++)
377  {
378 # if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
379  && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
380  if (tmp[m] == '/')
381  unescaped += '\\';
382  else
383 # endif
384  {
385  if (tmp[m] == '\\'
386  && ++m == tmp.length ())
387  break;
388 
389  unescaped += tmp[m];
390  }
391  }
392 
393  retval[k++] = unescaped;
394  }
395  }
396 
397  octave_globfree_wrapper (glob_info);
398  }
399  }
400  }
401 #endif
402 
403  return retval.sort ();
404 }
405 
406 OCTAVE_END_NAMESPACE(sys)
407 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
bool empty() const
Definition: str-vec.h:77
octave_idx_type numel() const
Definition: str-vec.h:100
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
bool is_dir_sep(char c)
std::string dir_sep_chars()
char dir_sep_char()
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:746
bool file_exists(const std::string &filename, bool is_dir)
Definition: lo-sysdep.cc:341
std::wstring u8_to_wstring(const std::string &utf8_string)
Definition: lo-sysdep.cc:723
T octave_idx_type m
Definition: mx-inlines.cc:781
octave_idx_type n
Definition: mx-inlines.cc:761
string_vector windows_glob(const string_vector &pat)
Definition: oct-glob.cc:231
bool fnmatch(const string_vector &pat, const std::string &str, int fnm_flags)
Definition: oct-glob.cc:58
string_vector glob(const string_vector &pat)
Definition: oct-glob.cc:73
F77_RET_T len
Definition: xerbla.cc:61