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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <fnmatch.h> |
|
28 #include <glob.h> |
|
29 |
|
30 #include <iostream.h> |
|
31 |
|
32 #include <string> |
|
33 |
|
34 #include "file-stat.h" |
2926
|
35 #include "glob-match.h" |
2924
|
36 #include "str-vec.h" |
|
37 |
|
38 bool |
|
39 glob_match::match (const string& s) |
|
40 { |
|
41 int npat = pat.length (); |
|
42 |
|
43 const char *str = s.c_str (); |
|
44 |
|
45 int fnmatch_flags = 0; |
|
46 |
|
47 if (flags & pathname) |
|
48 fnmatch_flags |= FNM_PATHNAME; |
|
49 |
|
50 if (flags & noescape) |
|
51 fnmatch_flags |= FNM_NOESCAPE; |
|
52 |
|
53 if (flags & period) |
|
54 fnmatch_flags |= FNM_PERIOD; |
|
55 |
|
56 for (int i = 0; i < npat; i++) |
|
57 if (fnmatch (pat(i).c_str (), str, fnmatch_flags) != FNM_NOMATCH) |
|
58 return true; |
|
59 |
|
60 return false; |
|
61 } |
|
62 |
|
63 Array<bool> |
|
64 glob_match::match (const string_vector& s) |
|
65 { |
|
66 int n = s.length (); |
|
67 |
|
68 Array<bool> retval (n); |
|
69 |
|
70 for (int i = 0; i < n; i++) |
|
71 retval(i) = match (s[i]); |
|
72 |
|
73 return retval; |
|
74 } |
|
75 |
|
76 static bool |
|
77 single_match_exists (const string& file) |
|
78 { |
|
79 file_stat s (file); |
|
80 |
|
81 return s.exists (); |
|
82 } |
|
83 |
|
84 string_vector |
|
85 glob_match::glob (void) |
|
86 { |
|
87 string_vector retval; |
|
88 |
|
89 int npat = pat.length (); |
|
90 |
|
91 int k = 0; |
|
92 |
|
93 for (int i = 0; i < npat; i++) |
|
94 { |
|
95 string xpat = pat(i); |
|
96 |
|
97 if (! xpat.empty ()) |
|
98 { |
|
99 glob_t glob_info; |
|
100 |
|
101 int err = ::glob (xpat.c_str (), GLOB_NOSORT, 0, &glob_info); |
|
102 |
|
103 if (! err) |
|
104 { |
|
105 int n = glob_info.gl_pathc; |
|
106 |
3072
|
107 const char * const *matches = glob_info.gl_pathv; |
2924
|
108 |
|
109 // XXX FIXME XXX -- we shouldn't have to check to see if |
|
110 // a single match exists, but it seems that glob() won't |
|
111 // check for us unless the pattern contains globbing |
|
112 // characters. Hmm. |
|
113 |
|
114 if (n > 1 |
|
115 || (n == 1 && single_match_exists (string (matches[0])))) |
|
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 |