26#if defined (HAVE_CONFIG_H)
41#if defined (OCTAVE_USE_WINDOWS_API)
59 int npat = pat.
numel ();
61 const char *cstr = str.c_str ();
63 for (
int i = 0; i < npat; i++)
76 int npat = pat.
numel ();
85 for (
int i = 0; i < npat; i++)
87 std::string xpat = pat(i);
91#if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
92 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
93 std::replace (xpat.begin (), xpat.end (),
'\\',
'/');
104 const char *
const *matches
114 && sys::file_exists (std::string (matches[0]))))
118 for (
int j = 0; j < n; j++)
120 std::string tmp = matches[j];
122#if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
123 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM)
124 std::replace (tmp.begin (), tmp.end (),
'/',
'\\');
136 return retval.
sort ();
139#if defined (OCTAVE_USE_WINDOWS_API)
142find_files (std::list<std::string>& dirlist,
const std::string& dir,
143 const std::string& pat, std::string& file)
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);
150 bool is_trailing_file_sep = ! is_file_empty && file.empty ();
152 if (! pat.compare (
".") || ! pat.compare (
".."))
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) :
"";
164 if (pat_str.empty ())
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);
173 find_files (dirlist, sys::file_ops::concat (dir, 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);
183 if (h_find == INVALID_HANDLE_VALUE)
186 unwind_action close_h_find ([h_find] () { FindClose (h_find); });
192 if (! (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
193 && (! file.empty () || is_trailing_file_sep))
201 if (found_dir.compare (
".") && found_dir.compare (
".."))
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));
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) :
"";
218 find_files (dirlist, sys::file_ops::concat (dir, found_dir),
222 while (FindNextFileW (h_find, &ffd) != 0);
234 int npat = pat.
numel ();
236#if defined (OCTAVE_USE_WINDOWS_API)
238 std::list<std::string> dirlist;
240 for (
int i = 0; i < npat; i++)
242 std::string xpat = pat(i);
246 std::string dir =
"";
250 = xpat.find_first_of (sys::file_ops::dir_sep_chars ());
253 if (sep_pos == 0 && xpat.length () > 1
254 && sys::file_ops::is_dir_sep (xpat[1]))
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 (),
261 if (sep_pos != std::string::npos)
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 ());
269 std::string file = (sep_pos != std::string::npos)
270 ? xpat.substr (sep_pos) :
"";
271 xpat = xpat.substr (0, sep_pos);
273 if ((sep_pos == 2 || xpat.length () == 2) && xpat[1] ==
':')
278 while (file.length () > 1 && sys::file_ops::is_dir_sep (file[0]))
279 file = file.substr (1, std::string::npos);
281 sep_pos = file.find_first_of (sys::file_ops::dir_sep_chars ());
283 xpat = file.substr (0, sep_pos);
284 file = (sep_pos != std::string::npos)
285 ? file.substr (sep_pos) :
"";
290 if (PathFileExistsW (wpat.c_str ()))
292 if (sys::file_ops::is_dir_sep (pat(i).back ()))
293 dirlist.push_back (dir +
294 sys::file_ops::dir_sep_char ());
296 dirlist.push_back (dir);
302 find_files (dirlist, dir, xpat, file);
315 for (
int i = 0; i < npat; i++)
317 std::string xpat = pat(i);
322 escaped.reserve (xpat.length ());
324 for (std::size_t j = 0; j < xpat.length (); j++)
326# if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
327 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
333 if (xpat[j] ==
']' || xpat[j] ==
'[')
341 int len = escaped.length ();
342 if (
len >= 3 && escaped.substr (
len - 3) ==
"*.*")
343 escaped = escaped.substr (0,
len - 2);
353 const char *
const *matches
363 && sys::file_exists (std::string (matches[0]))))
367 for (
int j = 0; j < n; j++)
369 std::string tmp = matches[j];
371 std::string unescaped;
372 unescaped.reserve (tmp.length ());
374 for (std::size_t m = 0; m < tmp.length (); m++)
376# if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
377 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
384 && ++m == tmp.length ())
391 retval[k++] = unescaped;
401 return retval.
sort ();
404OCTAVE_END_NAMESPACE(sys)
405OCTAVE_END_NAMESPACE(octave)
string_vector & sort(bool make_uniq=false)
void resize(octave_idx_type n, const std::string &rfv="")
octave_idx_type numel() const
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)
bool fnmatch(const string_vector &pat, const std::string &str, int fnm_flags)
string_vector glob(const string_vector &pat)
std::string u8_from_wstring(const std::wstring &wchar_string)
std::wstring u8_to_wstring(const std::string &utf8_string)