annotate liboctave/util/oct-glob.cc @ 27919:1891570abac8

update Octave Project Developers copyright for the new year In files that have the "Octave Project Developers" copyright notice, update for 2020.
author John W. Eaton <jwe@octave.org>
date Mon, 06 Jan 2020 22:29:51 -0500
parents b442ec6dda5c
children bd51beb6205e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
1 /*
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
2
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
3 Copyright (C) 2010-2020 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27102
diff changeset
4
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27102
diff changeset
5 See the file COPYRIGHT.md in the top-level directory of this distribution
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27102
diff changeset
6 or <https://octave.org/COPYRIGHT.html/>.
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27102
diff changeset
7
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 This file is part of Octave.
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23696
diff changeset
11 Octave is free software: you can redistribute it and/or modify it
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22402
diff changeset
12 under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23696
diff changeset
13 the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22402
diff changeset
14 (at your option) any later version.
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22402
diff changeset
16 Octave is distributed in the hope that it will be useful, but
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22402
diff changeset
17 WITHOUT ANY WARRANTY; without even the implied warranty of
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22402
diff changeset
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22402
diff changeset
19 GNU General Public License for more details.
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21 You should have received a copy of the GNU General Public License
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22 along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23696
diff changeset
23 <https://www.gnu.org/licenses/>.
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
24
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25 */
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
27 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21202
diff changeset
28 # include "config.h"
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29 #endif
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30
10345
33b012d39dce Convert between back and forward slashes before and after calling glob.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 10314
diff changeset
31 #include <algorithm>
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 #include <string>
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33
21929
7ab7cd327257 hide fnmatch.h and glob.h headers
John W. Eaton <jwe@octave.org>
parents: 21761
diff changeset
34 #include "glob-wrappers.h"
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36 #include "oct-glob.h"
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37 #include "file-stat.h"
21929
7ab7cd327257 hide fnmatch.h and glob.h headers
John W. Eaton <jwe@octave.org>
parents: 21761
diff changeset
38 #include "unwind-prot.h"
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 // These functions are defined here and not in glob_match.cc so that we
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41 // can include the glob.h file from gnulib, which defines glob to
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42 // be rpl_glob. If we include glob.h in glob_match.cc, then it
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43 // transforms the glob_match::glob function to be glob_match::rpl_glob,
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
44 // which is not what we want...
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45
21734
11af9c03400c write namespace keyword and name on same line in C++ files
John W. Eaton <jwe@octave.org>
parents: 21726
diff changeset
46 namespace octave
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 {
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
48 static bool
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
49 single_match_exists (const std::string& file)
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
50 {
27102
84ff9953faa1 where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 27101
diff changeset
51 sys::file_stat s (file);
27101
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
52
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
53 return s.exists ();
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
54 }
097774bed4ed move some static functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
55
21734
11af9c03400c write namespace keyword and name on same line in C++ files
John W. Eaton <jwe@octave.org>
parents: 21726
diff changeset
56 namespace sys
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
57 {
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
58 bool
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
59 fnmatch (const string_vector& pat, const std::string& str, int fnm_flags)
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
60 {
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
61 int npat = pat.numel ();
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
62
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
63 const char *cstr = str.c_str ();
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
64
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
65 for (int i = 0; i < npat; i++)
21929
7ab7cd327257 hide fnmatch.h and glob.h headers
John W. Eaton <jwe@octave.org>
parents: 21761
diff changeset
66 if (octave_fnmatch_wrapper (pat(i).c_str (), cstr, fnm_flags)
7ab7cd327257 hide fnmatch.h and glob.h headers
John W. Eaton <jwe@octave.org>
parents: 21761
diff changeset
67 != octave_fnm_nomatch_wrapper ())
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
68 return true;
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
69
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
70 return false;
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
71 }
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
73 string_vector
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
74 glob (const string_vector& pat)
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
75 {
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
76 string_vector retval;
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
77
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
78 int npat = pat.numel ();
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
79
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
80 int k = 0;
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
81
23696
08036a7f3660 remove octave:: namespace tag from symbols used inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
82 unwind_protect frame;
21929
7ab7cd327257 hide fnmatch.h and glob.h headers
John W. Eaton <jwe@octave.org>
parents: 21761
diff changeset
83
7ab7cd327257 hide fnmatch.h and glob.h headers
John W. Eaton <jwe@octave.org>
parents: 21761
diff changeset
84 void *glob_info = octave_create_glob_info_struct ();
7ab7cd327257 hide fnmatch.h and glob.h headers
John W. Eaton <jwe@octave.org>
parents: 21761
diff changeset
85
7ab7cd327257 hide fnmatch.h and glob.h headers
John W. Eaton <jwe@octave.org>
parents: 21761
diff changeset
86 frame.add_fcn (octave_destroy_glob_info_struct, glob_info);
7ab7cd327257 hide fnmatch.h and glob.h headers
John W. Eaton <jwe@octave.org>
parents: 21761
diff changeset
87
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
88 for (int i = 0; i < npat; i++)
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
89 {
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
90 std::string xpat = pat(i);
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
91
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
92 if (! xpat.empty ())
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
93 {
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22155
diff changeset
94 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
95 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
96 std::replace_if (xpat.begin (), xpat.end (),
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
97 std::bind2nd (std::equal_to<char> (), '\\'),
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
98 '/');
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
99 #endif
10345
33b012d39dce Convert between back and forward slashes before and after calling glob.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 10314
diff changeset
100
21929
7ab7cd327257 hide fnmatch.h and glob.h headers
John W. Eaton <jwe@octave.org>
parents: 21761
diff changeset
101 int err = octave_glob_wrapper (xpat.c_str (),
7ab7cd327257 hide fnmatch.h and glob.h headers
John W. Eaton <jwe@octave.org>
parents: 21761
diff changeset
102 octave_glob_nosort_wrapper (),
7ab7cd327257 hide fnmatch.h and glob.h headers
John W. Eaton <jwe@octave.org>
parents: 21761
diff changeset
103 glob_info);
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
104
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
105 if (! err)
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
106 {
21929
7ab7cd327257 hide fnmatch.h and glob.h headers
John W. Eaton <jwe@octave.org>
parents: 21761
diff changeset
107 int n = octave_glob_num_matches (glob_info);
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
108
21929
7ab7cd327257 hide fnmatch.h and glob.h headers
John W. Eaton <jwe@octave.org>
parents: 21761
diff changeset
109 const char * const *matches
7ab7cd327257 hide fnmatch.h and glob.h headers
John W. Eaton <jwe@octave.org>
parents: 21761
diff changeset
110 = octave_glob_match_list (glob_info);
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
111
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
112 // FIXME: we shouldn't have to check to see if
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
113 // a single match exists, but it seems that glob() won't
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
114 // check for us unless the pattern contains globbing
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
115 // characters. Hmm.
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
116
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
117 if (n > 1
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
118 || (n == 1
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
119 && single_match_exists (std::string (matches[0]))))
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
120 {
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
121 retval.resize (k+n);
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
122
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
123 for (int j = 0; j < n; j++)
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
124 {
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
125 std::string tmp = matches[j];
10345
33b012d39dce Convert between back and forward slashes before and after calling glob.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 10314
diff changeset
126
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
127 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
128 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM)
21761
ff2b9f7069c5 * liboctave/util/oct-glob.cc: remove stray '`'
John D
parents: 21736
diff changeset
129 std::replace_if (tmp.begin (), tmp.end (),
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
130 std::bind2nd (std::equal_to<char> (),
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
131 '/'),
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
132 '\\');
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
133 #endif
10345
33b012d39dce Convert between back and forward slashes before and after calling glob.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 10314
diff changeset
134
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
135 retval[k++] = tmp;
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
136 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
137 }
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
138
21929
7ab7cd327257 hide fnmatch.h and glob.h headers
John W. Eaton <jwe@octave.org>
parents: 21761
diff changeset
139 octave_globfree_wrapper (glob_info);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
140 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
141 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
142 }
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
143
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
144 return retval.sort ();
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
145 }
22155
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
146
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
147 // Glob like Windows "dir". Treat only * and ? as wildcards,
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
148 // and "*.*" matches filenames even if they do not contain ".".
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
149 string_vector
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
150 windows_glob (const string_vector& pat)
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
151 {
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
152 string_vector retval;
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
153
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
154 int npat = pat.numel ();
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
155
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
156 int k = 0;
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
157
23696
08036a7f3660 remove octave:: namespace tag from symbols used inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
158 unwind_protect frame;
22155
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
159
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
160 void *glob_info = octave_create_glob_info_struct ();
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
161
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
162 frame.add_fcn (octave_destroy_glob_info_struct, glob_info);
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
163
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
164 for (int i = 0; i < npat; i++)
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
165 {
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
166 std::string xpat = pat(i);
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
167
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
168 if (! xpat.empty ())
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
169 {
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
170 std::string escaped;
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
171 escaped.reserve (xpat.length ());
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
172
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
173 for (size_t j = 0; j < xpat.length (); j++)
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
174 {
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22155
diff changeset
175 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
22155
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
176 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
177 if (xpat[j] == '\\')
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22155
diff changeset
178 escaped += '/';
22155
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
179 else
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
180 #endif
22402
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
181 {
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
182 if (xpat[j] == ']' || xpat[j] == '[')
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
183 escaped += '\\';
22155
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
184
22402
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
185 escaped += xpat[j];
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
186 }
22155
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
187 }
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
188
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
189 // Replace trailing "*.*" by "*".
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
190 int len = escaped.length ();
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
191 if (len >= 3 && escaped.substr (len - 3) == "*.*")
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
192 escaped = escaped.substr (0, len - 2);
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
193
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
194 int err = octave_glob_wrapper (escaped.c_str (),
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
195 octave_glob_nosort_wrapper (),
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
196 glob_info);
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
197
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
198 if (! err)
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
199 {
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
200 int n = octave_glob_num_matches (glob_info);
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
201
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
202 const char * const *matches
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
203 = octave_glob_match_list (glob_info);
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
204
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
205 // FIXME: we shouldn't have to check to see if
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
206 // a single match exists, but it seems that glob() won't
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
207 // check for us unless the pattern contains globbing
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
208 // characters. Hmm.
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
209
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
210 if (n > 1
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
211 || (n == 1
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
212 && single_match_exists (std::string (matches[0]))))
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
213 {
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
214 retval.resize (k + n);
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
215
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
216 for (int j = 0; j < n; j++)
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
217 {
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
218 std::string tmp = matches[j];
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
219
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
220 std::string unescaped;
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
221 unescaped.reserve (tmp.length ());
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
222
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
223 for (size_t m = 0; m < tmp.length (); m++)
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
224 {
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22155
diff changeset
225 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
22155
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
226 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
227 if (tmp[m] == '/')
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22155
diff changeset
228 unescaped += '\\';
22155
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
229 else
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
230 #endif
22402
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
231 {
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
232 if (tmp[m] == '\\'
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
233 && ++m == tmp.length ())
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
234 break;
22155
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
235
22402
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
236 unescaped += tmp[m];
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22323
diff changeset
237 }
22155
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
238 }
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
239
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
240 retval[k++] = unescaped;
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
241 }
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
242 }
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
243
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
244 octave_globfree_wrapper (glob_info);
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
245 }
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
246 }
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
247 }
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
248
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
249 return retval.sort ();
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21929
diff changeset
250 }
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
251 }
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
252 }