2924
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
2924
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
2924
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <fnmatch.h> |
|
28 #include <glob.h> |
|
29 |
3503
|
30 #include <iostream> |
2924
|
31 #include <string> |
|
32 |
|
33 #include "file-stat.h" |
2926
|
34 #include "glob-match.h" |
2924
|
35 #include "str-vec.h" |
|
36 |
|
37 bool |
3504
|
38 glob_match::match (const std::string& s) |
2924
|
39 { |
|
40 int npat = pat.length (); |
|
41 |
|
42 const char *str = s.c_str (); |
|
43 |
|
44 int fnmatch_flags = 0; |
|
45 |
|
46 if (flags & pathname) |
|
47 fnmatch_flags |= FNM_PATHNAME; |
|
48 |
|
49 if (flags & noescape) |
|
50 fnmatch_flags |= FNM_NOESCAPE; |
|
51 |
|
52 if (flags & period) |
|
53 fnmatch_flags |= FNM_PERIOD; |
|
54 |
|
55 for (int i = 0; i < npat; i++) |
|
56 if (fnmatch (pat(i).c_str (), str, fnmatch_flags) != FNM_NOMATCH) |
|
57 return true; |
|
58 |
|
59 return false; |
|
60 } |
|
61 |
|
62 Array<bool> |
|
63 glob_match::match (const string_vector& s) |
|
64 { |
|
65 int n = s.length (); |
|
66 |
|
67 Array<bool> retval (n); |
|
68 |
|
69 for (int i = 0; i < n; i++) |
|
70 retval(i) = match (s[i]); |
|
71 |
|
72 return retval; |
|
73 } |
|
74 |
|
75 static bool |
3504
|
76 single_match_exists (const std::string& file) |
2924
|
77 { |
|
78 file_stat s (file); |
|
79 |
|
80 return s.exists (); |
|
81 } |
|
82 |
|
83 string_vector |
|
84 glob_match::glob (void) |
|
85 { |
|
86 string_vector retval; |
|
87 |
|
88 int npat = pat.length (); |
|
89 |
|
90 int k = 0; |
|
91 |
|
92 for (int i = 0; i < npat; i++) |
|
93 { |
3504
|
94 std::string xpat = pat(i); |
2924
|
95 |
|
96 if (! xpat.empty ()) |
|
97 { |
|
98 glob_t glob_info; |
|
99 |
|
100 int err = ::glob (xpat.c_str (), GLOB_NOSORT, 0, &glob_info); |
|
101 |
|
102 if (! err) |
|
103 { |
|
104 int n = glob_info.gl_pathc; |
|
105 |
3072
|
106 const char * const *matches = glob_info.gl_pathv; |
2924
|
107 |
5775
|
108 // FIXME -- we shouldn't have to check to see if |
2924
|
109 // a single match exists, but it seems that glob() won't |
|
110 // check for us unless the pattern contains globbing |
|
111 // characters. Hmm. |
|
112 |
|
113 if (n > 1 |
3504
|
114 || (n == 1 |
|
115 && single_match_exists (std::string (matches[0])))) |
2924
|
116 { |
|
117 retval.resize (k+n); |
|
118 |
|
119 for (int j = 0; j < n; j++) |
|
120 retval[k++] = matches[j]; |
|
121 } |
|
122 |
|
123 globfree (&glob_info); |
|
124 } |
|
125 } |
|
126 } |
|
127 |
|
128 return retval.qsort (); |
|
129 } |
|
130 |
|
131 /* |
|
132 ;;; Local Variables: *** |
|
133 ;;; mode: C++ *** |
|
134 ;;; End: *** |
|
135 */ |
|
136 |