GNU Octave  6.2.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-2021 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-stat.h"
37 #include "unwind-prot.h"
38 
39 // These functions are defined here and not in glob_match.cc so that we
40 // can include the glob.h file from gnulib, which defines glob to
41 // be rpl_glob. If we include glob.h in glob_match.cc, then it
42 // transforms the glob_match::glob function to be glob_match::rpl_glob,
43 // which is not what we want...
44 
45 namespace octave
46 {
47  static bool
48  single_match_exists (const std::string& file)
49  {
50  sys::file_stat s (file);
51 
52  return s.exists ();
53  }
54 
55  namespace sys
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  {
76 
77  int npat = pat.numel ();
78 
79  int k = 0;
80 
81  unwind_protect frame;
82 
83  void *glob_info = octave_create_glob_info_struct ();
84 
85  frame.add_fcn (octave_destroy_glob_info_struct, glob_info);
86 
87  for (int i = 0; i < npat; i++)
88  {
89  std::string xpat = pat(i);
90 
91  if (! xpat.empty ())
92  {
93 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
94  && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
95  std::replace_if (xpat.begin (), xpat.end (),
96  std::bind2nd (std::equal_to<char> (), '\\'),
97  '/');
98 #endif
99 
100  int err = octave_glob_wrapper (xpat.c_str (),
102  glob_info);
103 
104  if (! err)
105  {
106  int n = octave_glob_num_matches (glob_info);
107 
108  const char * const *matches
109  = octave_glob_match_list (glob_info);
110 
111  // FIXME: we shouldn't have to check to see if
112  // a single match exists, but it seems that glob() won't
113  // check for us unless the pattern contains globbing
114  // characters. Hmm.
115 
116  if (n > 1
117  || (n == 1
118  && single_match_exists (std::string (matches[0]))))
119  {
120  retval.resize (k+n);
121 
122  for (int j = 0; j < n; j++)
123  {
124  std::string tmp = matches[j];
125 
126 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
127  && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM)
128  std::replace_if (tmp.begin (), tmp.end (),
129  std::bind2nd (std::equal_to<char> (),
130  '/'),
131  '\\');
132 #endif
133 
134  retval[k++] = tmp;
135  }
136  }
137 
138  octave_globfree_wrapper (glob_info);
139  }
140  }
141  }
142 
143  return retval.sort ();
144  }
145 
146  // Glob like Windows "dir". Treat only * and ? as wildcards,
147  // and "*.*" matches filenames even if they do not contain ".".
150  {
152 
153  int npat = pat.numel ();
154 
155  int k = 0;
156 
157  unwind_protect frame;
158 
159  void *glob_info = octave_create_glob_info_struct ();
160 
161  frame.add_fcn (octave_destroy_glob_info_struct, glob_info);
162 
163  for (int i = 0; i < npat; i++)
164  {
165  std::string xpat = pat(i);
166 
167  if (! xpat.empty ())
168  {
169  std::string escaped;
170  escaped.reserve (xpat.length ());
171 
172  for (size_t j = 0; j < xpat.length (); j++)
173  {
174 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
175  && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
176  if (xpat[j] == '\\')
177  escaped += '/';
178  else
179 #endif
180  {
181  if (xpat[j] == ']' || xpat[j] == '[')
182  escaped += '\\';
183 
184  escaped += xpat[j];
185  }
186  }
187 
188  // Replace trailing "*.*" by "*".
189  int len = escaped.length ();
190  if (len >= 3 && escaped.substr (len - 3) == "*.*")
191  escaped = escaped.substr (0, len - 2);
192 
193  int err = octave_glob_wrapper (escaped.c_str (),
195  glob_info);
196 
197  if (! err)
198  {
199  int n = octave_glob_num_matches (glob_info);
200 
201  const char * const *matches
202  = octave_glob_match_list (glob_info);
203 
204  // FIXME: we shouldn't have to check to see if
205  // a single match exists, but it seems that glob() won't
206  // check for us unless the pattern contains globbing
207  // characters. Hmm.
208 
209  if (n > 1
210  || (n == 1
211  && single_match_exists (std::string (matches[0]))))
212  {
213  retval.resize (k + n);
214 
215  for (int j = 0; j < n; j++)
216  {
217  std::string tmp = matches[j];
218 
219  std::string unescaped;
220  unescaped.reserve (tmp.length ());
221 
222  for (size_t m = 0; m < tmp.length (); m++)
223  {
224 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
225  && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
226  if (tmp[m] == '/')
227  unescaped += '\\';
228  else
229 #endif
230  {
231  if (tmp[m] == '\\'
232  && ++m == tmp.length ())
233  break;
234 
235  unescaped += tmp[m];
236  }
237  }
238 
239  retval[k++] = unescaped;
240  }
241  }
242 
243  octave_globfree_wrapper (glob_info);
244  }
245  }
246  }
247 
248  return retval.sort ();
249  }
250  }
251 }
Array< T > sort(int dim=0, sortmode mode=ASCENDING) const
Size of the specified dimension.
Definition: Array.cc:1757
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
Definition: Array.cc:1011
void add_fcn(void(*fcn)(Params...), Args &&... args)
bool exists(void) const
Definition: file-stat.h:147
octave_idx_type numel(void) const
Definition: str-vec.h:100
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
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:149
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
static bool single_match_exists(const std::string &file)
Definition: oct-glob.cc:48
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811
F77_RET_T len
Definition: xerbla.cc:61