GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
oct-glob.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2010-2025 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
57bool
58fnmatch (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
73glob (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 ([glob_info] () { 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
142static void
143find_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 ([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 +
295 sys::file_ops::dir_sep_char ());
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 ([glob_info] () { octave_destroy_glob_info_struct (glob_info); });
315
316 for (int i = 0; i < npat; i++)
317 {
318 std::string xpat = pat(i);
319
320 if (! xpat.empty ())
321 {
322 std::string escaped;
323 escaped.reserve (xpat.length ());
324
325 for (std::size_t j = 0; j < xpat.length (); j++)
326 {
327# if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
328 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
329 if (xpat[j] == '\\')
330 escaped += '/';
331 else
332# endif
333 {
334 if (xpat[j] == ']' || xpat[j] == '[')
335 escaped += '\\';
336
337 escaped += xpat[j];
338 }
339 }
340
341 // Replace trailing "*.*" by "*".
342 int len = escaped.length ();
343 if (len >= 3 && escaped.substr (len - 3) == "*.*")
344 escaped = escaped.substr (0, len - 2);
345
346 int err = octave_glob_wrapper (escaped.c_str (),
348 glob_info);
349
350 if (! err)
351 {
352 int n = octave_glob_num_matches (glob_info);
353
354 const char *const *matches
355 = octave_glob_match_list (glob_info);
356
357 // FIXME: we shouldn't have to check to see if
358 // a single match exists, but it seems that glob() won't
359 // check for us unless the pattern contains globbing
360 // characters. Hmm.
361
362 if (n > 1
363 || (n == 1
364 && sys::file_exists (std::string (matches[0]))))
365 {
366 retval.resize (k + n);
367
368 for (int j = 0; j < n; j++)
369 {
370 std::string tmp = matches[j];
371
372 std::string unescaped;
373 unescaped.reserve (tmp.length ());
374
375 for (std::size_t m = 0; m < tmp.length (); m++)
376 {
377# if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
378 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
379 if (tmp[m] == '/')
380 unescaped += '\\';
381 else
382# endif
383 {
384 if (tmp[m] == '\\'
385 && ++m == tmp.length ())
386 break;
387
388 unescaped += tmp[m];
389 }
390 }
391
392 retval[k++] = unescaped;
393 }
394 }
395
396 octave_globfree_wrapper (glob_info);
397 }
398 }
399 }
400#endif
401
402 return retval.sort ();
403}
404
405OCTAVE_END_NAMESPACE(sys)
406OCTAVE_END_NAMESPACE(octave)
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:93
bool empty() const
Definition str-vec.h:75
octave_idx_type numel() const
Definition str-vec.h:98
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void * octave_create_glob_info_struct(void)
int octave_fnm_nomatch_wrapper(void)
int octave_glob_nosort_wrapper(void)
void octave_destroy_glob_info_struct(void *glob_info)
void octave_globfree_wrapper(void *glob_info)
char ** octave_glob_match_list(void *glob_info)
int octave_glob_num_matches(void *glob_info)
int octave_glob_wrapper(const char *pattern, int flags, void *glob_info)
int octave_fnmatch_wrapper(const char *pattern, const char *name, int flags)
std::string u8_from_wstring(const std::wstring &wchar_string)
Definition lo-sysdep.cc:746
std::wstring u8_to_wstring(const std::string &utf8_string)
Definition lo-sysdep.cc:723
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