GNU Octave 7.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-2022 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
54namespace octave
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
64 namespace sys
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_if (xpat.begin (), xpat.end (),
104 std::bind2nd (std::equal_to<char> (), '\\'),
105 '/');
106#endif
107
108 int err = octave_glob_wrapper (xpat.c_str (),
110 glob_info);
111
112 if (! err)
113 {
114 int n = octave_glob_num_matches (glob_info);
115
116 const char * const *matches
117 = octave_glob_match_list (glob_info);
118
119 // FIXME: we shouldn't have to check to see if
120 // a single match exists, but it seems that glob() won't
121 // check for us unless the pattern contains globbing
122 // characters. Hmm.
123
124 if (n > 1
125 || (n == 1
126 && single_match_exists (std::string (matches[0]))))
127 {
128 retval.resize (k+n);
129
130 for (int j = 0; j < n; j++)
131 {
132 std::string tmp = matches[j];
133
134#if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
135 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM)
136 std::replace_if (tmp.begin (), tmp.end (),
137 std::bind2nd (std::equal_to<char> (),
138 '/'),
139 '\\');
140#endif
141
142 retval[k++] = tmp;
143 }
144 }
145
146 octave_globfree_wrapper (glob_info);
147 }
148 }
149 }
150
151 return retval.sort ();
152 }
153
154#if defined (OCTAVE_USE_WINDOWS_API)
155
156 static void
157 find_files (std::list<std::string>& dirlist, const std::string& dir,
158 const std::string& pat, std::string& file)
159 {
160 // remove leading file separators
161 while (file.length () > 1 && sys::file_ops::is_dir_sep (file[0]))
162 file = file.substr (1, std::string::npos);
163
164 // find first file in directory that matches pattern in PAT
165 std::wstring wpat = u8_to_wstring (sys::file_ops::concat (dir, pat));
166 _WIN32_FIND_DATAW ffd;
167 HANDLE h_find = FindFirstFileW (wpat.c_str (), &ffd);
168 // ignore any error
169 if (h_find == INVALID_HANDLE_VALUE)
170 return;
171
172 unwind_action close_h_find ([=] () { FindClose (h_find); });
173
174 // find all files that match pattern
175 do
176 {
177 std::string found_dir = u8_from_wstring (ffd.cFileName);
178
179 if (file.empty ())
180 {
181 if (found_dir.compare (".") && found_dir.compare (".."))
182 dirlist.push_back (sys::file_ops::concat (dir, found_dir));
183 }
184 else
185 {
186 // get next component of path (or file name)
187 std::size_t sep_pos
188 = file.find_first_of (sys::file_ops::dir_sep_chars ());
189 std::string pat_str = file.substr (0, sep_pos);
190 std::string file_str = (sep_pos != std::string::npos
191 && file.length () > sep_pos+1)
192 ? file.substr (sep_pos+1) : "";
193
194 // call this function recursively with next path component in PAT
195 find_files (dirlist, sys::file_ops::concat (dir, found_dir),
196 pat_str, file_str);
197 }
198 }
199 while (FindNextFileW (h_find, &ffd) != 0);
200 }
201
202#endif
203
204 // Glob like Windows "dir". Treat only * and ? as wildcards,
205 // and "*.*" matches filenames even if they do not contain ".".
208 {
209 string_vector retval;
210
211 int npat = pat.numel ();
212
213#if defined (OCTAVE_USE_WINDOWS_API)
214
215 std::list<std::string> dirlist;
216
217 for (int i = 0; i < npat; i++)
218 {
219 std::string xpat = pat(i);
220 if (xpat.empty ())
221 continue;
222
223 // separate component until first dir separator
224 std::size_t sep_pos
225 = xpat.find_first_of (sys::file_ops::dir_sep_chars ());
226 std::string file = (sep_pos != std::string::npos
227 && xpat.length () > sep_pos+1)
228 ? xpat.substr (sep_pos+1) : "";
229 xpat = xpat.substr (0, sep_pos);
230
231 std::string dir = "";
232
233 if ((sep_pos == 2 || xpat.length () == 2) && xpat[1] == ':')
234 {
235 // include disc root with first file or folder
236
237 // remove leading file separators in path without disc root
238 while (file.length () > 1 && sys::file_ops::is_dir_sep (file[0]))
239 file = file.substr (1, std::string::npos);
240
241 sep_pos = file.find_first_of (sys::file_ops::dir_sep_chars ());
242 dir = xpat;
243 xpat = file.substr (0, sep_pos);
244 file = (sep_pos != std::string::npos
245 && file.length () > sep_pos+1)
246 ? file.substr (sep_pos+1) : "";
247 if (xpat.empty ())
248 {
249 // don't glob if input is only disc root
250 std::wstring wpat = u8_to_wstring (pat(i));
251 if (PathFileExistsW (wpat.c_str ()))
252 {
253 if (sys::file_ops::is_dir_sep (pat(i).back ()))
254 dirlist.push_back (dir +
256 else
257 dirlist.push_back (dir);
258 }
259 continue;
260 }
261 }
262
263 find_files (dirlist, dir, xpat, file);
264 }
265
266 retval = string_vector (dirlist);
267
268#else
269
270 int k = 0;
271
272 void *glob_info = octave_create_glob_info_struct ();
273
274 unwind_action cleanup_glob_info_struct
275 ([=] () { octave_destroy_glob_info_struct (glob_info); });
276
277 for (int i = 0; i < npat; i++)
278 {
279 std::string xpat = pat(i);
280
281 if (! xpat.empty ())
282 {
283 std::string escaped;
284 escaped.reserve (xpat.length ());
285
286 for (std::size_t j = 0; j < xpat.length (); j++)
287 {
288# if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
289 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
290 if (xpat[j] == '\\')
291 escaped += '/';
292 else
293# endif
294 {
295 if (xpat[j] == ']' || xpat[j] == '[')
296 escaped += '\\';
297
298 escaped += xpat[j];
299 }
300 }
301
302 // Replace trailing "*.*" by "*".
303 int len = escaped.length ();
304 if (len >= 3 && escaped.substr (len - 3) == "*.*")
305 escaped = escaped.substr (0, len - 2);
306
307 int err = octave_glob_wrapper (escaped.c_str (),
309 glob_info);
310
311 if (! err)
312 {
313 int n = octave_glob_num_matches (glob_info);
314
315 const char * const *matches
316 = octave_glob_match_list (glob_info);
317
318 // FIXME: we shouldn't have to check to see if
319 // a single match exists, but it seems that glob() won't
320 // check for us unless the pattern contains globbing
321 // characters. Hmm.
322
323 if (n > 1
324 || (n == 1
325 && single_match_exists (std::string (matches[0]))))
326 {
327 retval.resize (k + n);
328
329 for (int j = 0; j < n; j++)
330 {
331 std::string tmp = matches[j];
332
333 std::string unescaped;
334 unescaped.reserve (tmp.length ());
335
336 for (std::size_t m = 0; m < tmp.length (); m++)
337 {
338# if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
339 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
340 if (tmp[m] == '/')
341 unescaped += '\\';
342 else
343# endif
344 {
345 if (tmp[m] == '\\'
346 && ++m == tmp.length ())
347 break;
348
349 unescaped += tmp[m];
350 }
351 }
352
353 retval[k++] = unescaped;
354 }
355 }
356
357 octave_globfree_wrapper (glob_info);
358 }
359 }
360 }
361#endif
362
363 return retval.sort ();
364 }
365 }
366}
bool exists(void) const
Definition: file-stat.h:147
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
void * octave_create_glob_info_struct(void)
Definition: glob-wrappers.c:43
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
char ** octave_glob_match_list(void *glob_info)
Definition: glob-wrappers.c:68
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
std::string concat(const std::string &dir, const std::string &file)
Definition: file-ops.cc:349
char dir_sep_char(void)
Definition: file-ops.cc:229
std::string dir_sep_chars(void)
Definition: file-ops.cc:247
bool is_dir_sep(char c)
Definition: file-ops.cc:275
string_vector glob(const string_vector &pat)
Definition: oct-glob.cc:82
std::string u8_from_wstring(const std::wstring &wchar_string)
Definition: lo-sysdep.cc:513
bool fnmatch(const string_vector &pat, const std::string &str, int fnm_flags)
Definition: oct-glob.cc:67
std::wstring u8_to_wstring(const std::string &utf8_string)
Definition: lo-sysdep.cc:490
string_vector windows_glob(const string_vector &pat)
Definition: oct-glob.cc:207
static bool single_match_exists(const std::string &file)
Definition: oct-glob.cc:57
F77_RET_T len
Definition: xerbla.cc:61