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