b1053af5 |
1 | /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc. |
2 | * |
3 | * This library is free software; you can redistribute it and/or |
4 | * modify it under the terms of the GNU Lesser General Public |
5 | * License as published by the Free Software Foundation; either |
6 | * version 2 of the License, or (at your option) any later version. |
7 | * |
8 | * This library is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU Lesser General Public |
14 | * License along with this library; if not, write to the |
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
16 | * Boston, MA 02111-1307, USA. |
17 | */ |
18 | |
19 | /* |
20 | * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS |
21 | * file for a list of people on the GTK+ Team. See the ChangeLog |
22 | * files for a list of changes. These files are distributed with |
23 | * GTK+ at ftp://ftp.gtk.org/pub/gtk/. |
24 | */ |
25 | |
26 | /* |
27 | * Stripped down, converted to UTF-8 and test cases added |
28 | * |
29 | * Owen Taylor, 13 December 2002; |
30 | */ |
31 | |
32 | #include <config.h> |
33 | #include <string.h> |
34 | |
35 | #include <glib.h> |
36 | |
37 | /* We need to make sure that all constants are defined |
38 | * to properly compile this file |
39 | */ |
40 | #ifndef _GNU_SOURCE |
41 | #define _GNU_SOURCE |
42 | #endif |
43 | |
44 | static gunichar |
45 | get_char (const char **str) |
46 | { |
47 | gunichar c = g_utf8_get_char (*str); |
48 | *str = g_utf8_next_char (*str); |
49 | |
50 | #ifdef G_PLATFORM_WIN32 |
51 | c = g_unichar_tolower (c); |
52 | #endif |
53 | |
54 | return c; |
55 | } |
56 | |
57 | #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN) |
58 | #define DO_ESCAPE 0 |
59 | #else |
60 | #define DO_ESCAPE 1 |
61 | #endif |
62 | |
63 | static gunichar |
64 | get_unescaped_char (const char **str, |
65 | gboolean *was_escaped) |
66 | { |
67 | gunichar c = get_char (str); |
68 | |
69 | *was_escaped = DO_ESCAPE && c == '\\'; |
70 | if (*was_escaped) |
71 | c = get_char (str); |
72 | |
73 | return c; |
74 | } |
75 | |
76 | /* Match STRING against the filename pattern PATTERN, returning zero if |
77 | it matches, nonzero if not. */ |
78 | |
79 | static gboolean |
80 | gtk_fnmatch_intern (const char *pattern, |
81 | const char *string, |
82 | gboolean component_start, |
83 | gboolean no_leading_period) |
84 | { |
85 | const char *p = pattern, *n = string; |
86 | |
87 | while (*p) |
88 | { |
89 | const char *last_n = n; |
90 | |
91 | gunichar c = get_char (&p); |
92 | gunichar nc = get_char (&n); |
93 | |
94 | switch (c) |
95 | { |
96 | case '?': |
97 | if (nc == '\0') |
98 | return FALSE; |
99 | else if (nc == G_DIR_SEPARATOR) |
100 | return FALSE; |
101 | else if (nc == '.' && component_start && no_leading_period) |
102 | return FALSE; |
103 | break; |
104 | case '\\': |
105 | if (DO_ESCAPE) |
106 | c = get_char (&p); |
107 | if (nc != c) |
108 | return FALSE; |
109 | break; |
110 | case '*': |
111 | if (nc == '.' && component_start && no_leading_period) |
112 | return FALSE; |
113 | |
114 | { |
115 | const char *last_p = p; |
116 | |
117 | for (last_p = p, c = get_char (&p); |
118 | c == '?' || c == '*'; |
119 | last_p = p, c = get_char (&p)) |
120 | { |
121 | if (c == '?') |
122 | { |
123 | if (nc == '\0') |
124 | return FALSE; |
125 | else if (nc == G_DIR_SEPARATOR) |
126 | return FALSE; |
127 | else |
128 | { |
129 | last_n = n; nc = get_char (&n); |
130 | } |
131 | } |
132 | } |
133 | |
134 | /* If the pattern ends with wildcards, we have a |
135 | * guaranteed match unless there is a dir separator |
136 | * in the remainder of the string. |
137 | */ |
138 | if (c == '\0') |
139 | { |
140 | if (strchr (last_n, G_DIR_SEPARATOR) != NULL) |
141 | return FALSE; |
142 | else |
143 | return TRUE; |
144 | } |
145 | |
146 | if (DO_ESCAPE && c == '\\') |
147 | c = get_char (&p); |
148 | |
149 | for (p = last_p; nc != '\0';) |
150 | { |
151 | if ((c == '[' || nc == c) && |
152 | gtk_fnmatch_intern (p, last_n, component_start, no_leading_period)) |
153 | return TRUE; |
154 | |
155 | component_start = (nc == G_DIR_SEPARATOR); |
156 | last_n = n; |
157 | nc = get_char (&n); |
158 | } |
159 | |
160 | return FALSE; |
161 | } |
162 | |
163 | case '[': |
164 | { |
165 | /* Nonzero if the sense of the character class is inverted. */ |
166 | gboolean not; |
167 | gboolean was_escaped; |
168 | |
169 | if (nc == '\0' || nc == G_DIR_SEPARATOR) |
170 | return FALSE; |
171 | |
172 | if (nc == '.' && component_start && no_leading_period) |
173 | return FALSE; |
174 | |
175 | not = (*p == '!' || *p == '^'); |
176 | if (not) |
177 | ++p; |
178 | |
179 | c = get_unescaped_char (&p, &was_escaped); |
180 | for (;;) |
181 | { |
182 | register gunichar cstart = c, cend = c; |
183 | if (c == '\0') |
184 | /* [ (unterminated) loses. */ |
185 | return FALSE; |
186 | |
187 | c = get_unescaped_char (&p, &was_escaped); |
188 | |
189 | if (!was_escaped && c == '-' && *p != ']') |
190 | { |
191 | cend = get_unescaped_char (&p, &was_escaped); |
192 | if (cend == '\0') |
193 | return FALSE; |
194 | |
195 | c = get_char (&p); |
196 | } |
197 | |
198 | if (nc >= cstart && nc <= cend) |
199 | goto matched; |
200 | |
201 | if (!was_escaped && c == ']') |
202 | break; |
203 | } |
204 | if (!not) |
205 | return FALSE; |
206 | break; |
207 | |
208 | matched:; |
209 | /* Skip the rest of the [...] that already matched. */ |
210 | /* XXX 1003.2d11 is unclear if was_escaped is right. */ |
211 | while (was_escaped || c != ']') |
212 | { |
213 | if (c == '\0') |
214 | /* [... (unterminated) loses. */ |
215 | return FALSE; |
216 | |
217 | c = get_unescaped_char (&p, &was_escaped); |
218 | } |
219 | if (not) |
220 | return FALSE; |
221 | } |
222 | break; |
223 | |
224 | default: |
225 | if (c != nc) |
226 | return FALSE; |
227 | } |
228 | |
229 | component_start = (nc == G_DIR_SEPARATOR); |
230 | } |
231 | |
232 | if (*n == '\0') |
233 | return TRUE; |
234 | |
235 | return FALSE; |
236 | } |
237 | |
238 | /* Match STRING against the filename pattern PATTERN, returning zero if |
239 | * it matches, nonzero if not. |
240 | * |
241 | * GTK+ used to use a old version of GNU fnmatch() that was buggy |
242 | * in various ways and didn't handle UTF-8. The following is |
243 | * converted to UTF-8. To simplify the process of making it |
244 | * correct, this is special-cased to the combinations of flags |
245 | * that gtkfilesel.c uses. |
246 | * |
247 | * FNM_FILE_NAME - always set |
248 | * FNM_LEADING_DIR - never set |
249 | * FNM_NOESCAPE - set only on windows |
250 | * FNM_CASEFOLD - set only on windows |
251 | */ |
252 | gboolean |
253 | _gtk_fnmatch (const char *pattern, |
254 | const char *string, |
255 | gboolean no_leading_period) |
256 | { |
257 | return gtk_fnmatch_intern (pattern, string, TRUE, no_leading_period); |
258 | } |
259 | |
260 | #undef FNMATCH_TEST_CASES |
261 | #ifdef FNMATCH_TEST_CASES |
262 | |
263 | #define TEST(pat, str, no_leading_period, result) \ |
264 | g_assert (_gtk_fnmatch ((pat), (str), (no_leading_period)) == result) |
265 | |
266 | int main (int argc, char **argv) |
267 | { |
268 | TEST ("[a-]", "-", TRUE, TRUE); |
269 | |
270 | TEST ("a", "a", TRUE, TRUE); |
271 | TEST ("a", "b", TRUE, FALSE); |
272 | |
273 | /* Test what ? matches */ |
274 | TEST ("?", "a", TRUE, TRUE); |
275 | TEST ("?", ".", TRUE, FALSE); |
276 | TEST ("a?", "a.", TRUE, TRUE); |
277 | TEST ("a/?", "a/b", TRUE, TRUE); |
278 | TEST ("a/?", "a/.", TRUE, FALSE); |
279 | TEST ("?", "/", TRUE, FALSE); |
280 | |
281 | /* Test what * matches */ |
282 | TEST ("*", "a", TRUE, TRUE); |
283 | TEST ("*", ".", TRUE, FALSE); |
284 | TEST ("a*", "a.", TRUE, TRUE); |
285 | TEST ("a/*", "a/b", TRUE, TRUE); |
286 | TEST ("a/*", "a/.", TRUE, FALSE); |
287 | TEST ("*", "/", TRUE, FALSE); |
288 | |
289 | /* Range tests */ |
290 | TEST ("[ab]", "a", TRUE, TRUE); |
291 | TEST ("[ab]", "c", TRUE, FALSE); |
292 | TEST ("[^ab]", "a", TRUE, FALSE); |
293 | TEST ("[!ab]", "a", TRUE, FALSE); |
294 | TEST ("[^ab]", "c", TRUE, TRUE); |
295 | TEST ("[!ab]", "c", TRUE, TRUE); |
296 | TEST ("[a-c]", "b", TRUE, TRUE); |
297 | TEST ("[a-c]", "d", TRUE, FALSE); |
298 | TEST ("[a-]", "-", TRUE, TRUE); |
299 | TEST ("[]]", "]", TRUE, TRUE); |
300 | TEST ("[^]]", "a", TRUE, TRUE); |
301 | TEST ("[!]]", "a", TRUE, TRUE); |
302 | |
303 | /* Various unclosed ranges */ |
304 | TEST ("[ab", "a", TRUE, FALSE); |
305 | TEST ("[a-", "a", TRUE, FALSE); |
306 | TEST ("[ab", "c", TRUE, FALSE); |
307 | TEST ("[a-", "c", TRUE, FALSE); |
308 | TEST ("[^]", "a", TRUE, FALSE); |
309 | |
310 | /* Ranges and special no-wildcard matches */ |
311 | TEST ("[.]", ".", TRUE, FALSE); |
312 | TEST ("a[.]", "a.", TRUE, TRUE); |
313 | TEST ("a/[.]", "a/.", TRUE, FALSE); |
314 | TEST ("[/]", "/", TRUE, FALSE); |
315 | TEST ("[^/]", "a", TRUE, TRUE); |
316 | |
317 | /* Basic tests of * (and combinations of * and ?) */ |
318 | TEST ("a*b", "ab", TRUE, TRUE); |
319 | TEST ("a*b", "axb", TRUE, TRUE); |
320 | TEST ("a*b", "axxb", TRUE, TRUE); |
321 | TEST ("a**b", "ab", TRUE, TRUE); |
322 | TEST ("a**b", "axb", TRUE, TRUE); |
323 | TEST ("a**b", "axxb", TRUE, TRUE); |
324 | TEST ("a*?*b", "ab", TRUE, FALSE); |
325 | TEST ("a*?*b", "axb", TRUE, TRUE); |
326 | TEST ("a*?*b", "axxb", TRUE, TRUE); |
327 | |
328 | /* Test of *[range] */ |
329 | TEST ("a*[cd]", "ac", TRUE, TRUE); |
330 | TEST ("a*[cd]", "axc", TRUE, TRUE); |
331 | TEST ("a*[cd]", "axx", TRUE, FALSE); |
332 | |
333 | TEST ("a/[.]", "a/.", TRUE, FALSE); |
334 | TEST ("a*[.]", "a/.", TRUE, FALSE); |
335 | |
336 | /* Test of UTF-8 */ |
337 | |
338 | TEST ("ä", "ä", TRUE, TRUE); /* TEST ("ä", "ä", TRUE); */ |
339 | TEST ("?", "ä", TRUE, TRUE); /* TEST ("?", "ä", TRUE); */ |
340 | TEST ("*ö", "äö", TRUE, TRUE); /* TEST ("*ö", "äö", TRUE); */ |
341 | TEST ("*ö", "ääö", TRUE, TRUE); /* TEST ("*ö", "ääö", TRUE); */ |
342 | TEST ("[ä]", "ä", TRUE, TRUE); /* TEST ("[ä]", "ä", TRUE); */ |
343 | TEST ("[ä-ö]", "é", TRUE, TRUE); /* TEST ("[ä-ö]", "é", TRUE); */ |
344 | TEST ("[ä-ö]", "a", TRUE, FALSE); /* TEST ("[ä-ö]", "a", FALSE); */ |
345 | |
346 | #ifdef DO_ESCAPE |
347 | /* Tests of escaping */ |
348 | TEST ("\\\\", "\\", TRUE, TRUE); |
349 | TEST ("\\?", "?", TRUE, TRUE); |
350 | TEST ("\\?", "a", TRUE, FALSE); |
351 | TEST ("\\*", "*", TRUE, TRUE); |
352 | TEST ("\\*", "a", TRUE, FALSE); |
353 | TEST ("\\[a-b]", "[a-b]", TRUE, TRUE); |
354 | TEST ("[\\\\]", "\\", TRUE, TRUE); |
355 | TEST ("[\\^a]", "a", TRUE, TRUE); |
356 | TEST ("[a\\-c]", "b", TRUE, FALSE); |
357 | TEST ("[a\\-c]", "-", TRUE, TRUE); |
358 | TEST ("[a\\]", "a", TRUE, FALSE); |
359 | #endif /* DO_ESCAPE */ |
360 | |
361 | return 0; |
362 | } |
363 | |
364 | #endif /* FNMATCH_TEST_CASES */ |