]> Pileus Git - ~andy/gtk/blob - gtk/fnmatch.c
The render vfunc takes a GdkDrawable* instead of a GdkWindow*, because
[~andy/gtk] / gtk / fnmatch.c
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 <string.h>
33
34 #include <glib.h>
35
36 /* We need to make sure that all constants are defined
37  * to properly compile this file
38  */
39 #ifndef _GNU_SOURCE
40 #define _GNU_SOURCE
41 #endif
42
43 static gunichar
44 get_char (const char **str)
45 {
46   gunichar c = g_utf8_get_char (*str);
47   *str = g_utf8_next_char (*str);
48
49 #ifdef G_PLATFORM_WIN32
50   c = g_unichar_tolower (c);
51 #endif
52
53   return c;
54 }
55
56 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
57 #define DO_ESCAPE 0
58 #else  
59 #define DO_ESCAPE 1
60 #endif  
61
62 static gunichar
63 get_unescaped_char (const char **str,
64                     gboolean    *was_escaped)
65 {
66   gunichar c = get_char (str);
67
68   *was_escaped = DO_ESCAPE && c == '\\';
69   if (*was_escaped)
70     c = get_char (str);
71   
72   return c;
73 }
74
75 /* Match STRING against the filename pattern PATTERN, returning zero if
76    it matches, nonzero if not.  */
77
78 static gboolean
79 gtk_fnmatch_intern (const char *pattern,
80                     const char *string,
81                     gboolean   component_start)
82 {
83   const char *p = pattern, *n = string;
84   
85   while (*p)
86     {
87       const char *last_n = n;
88       
89       gunichar c = get_char (&p);
90       gunichar nc = get_char (&n);
91       
92       switch (c)
93         {
94         case '?':
95           if (nc == '\0')
96             return FALSE;
97           else if (nc == G_DIR_SEPARATOR)
98             return FALSE;
99           else if (nc == '.' && component_start)
100             return FALSE;
101           break;
102         case '\\':
103           if (DO_ESCAPE)
104             c = get_char (&p);
105           if (nc != c)
106             return FALSE;
107           break;
108         case '*':
109           if (nc == '.' && component_start)
110             return FALSE;
111
112           {
113             const char *last_p = p;
114
115             for (last_p = p, c = get_char (&p);
116                  c == '?' || c == '*';
117                  last_p = p, c = get_char (&p))
118               {
119                 if (c == '?')
120                   {
121                     if (nc == '\0')
122                       return FALSE;
123                     else if (nc == G_DIR_SEPARATOR)
124                       return FALSE;
125                     else
126                       {
127                         last_n = n; nc = get_char (&n);
128                       }
129                   }
130               }
131
132             /* If the pattern ends with wildcards, we have a
133              * guaranteed match unless there is a dir separator
134              * in the remainder of the string.
135              */
136             if (c == '\0')
137               {
138                 if (strchr (last_n, G_DIR_SEPARATOR) != NULL)
139                   return FALSE;
140                 else
141                   return TRUE;
142               }
143
144             if (DO_ESCAPE && c == '\\')
145               c = get_char (&p);
146
147             for (p = last_p; nc != '\0';)
148               {
149                 if ((c == '[' || nc == c) &&
150                     gtk_fnmatch_intern (p, last_n, component_start))
151                   return TRUE;
152                 
153                 component_start = (nc == G_DIR_SEPARATOR);
154                 last_n = n;
155                 nc = get_char (&n);
156               }
157                   
158             return FALSE;
159           }
160
161         case '[':
162           {
163             /* Nonzero if the sense of the character class is inverted.  */
164             gboolean not;
165             gboolean was_escaped;
166
167             if (nc == '\0' || nc == G_DIR_SEPARATOR)
168               return FALSE;
169
170             if (nc == '.' && component_start)
171               return FALSE;
172
173             not = (*p == '!' || *p == '^');
174             if (not)
175               ++p;
176
177             c = get_unescaped_char (&p, &was_escaped);
178             for (;;)
179               {
180                 register gunichar cstart = c, cend = c;
181                 if (c == '\0')
182                   /* [ (unterminated) loses.  */
183                   return FALSE;
184
185                 c = get_unescaped_char (&p, &was_escaped);
186                 
187                 if (!was_escaped && c == '-' && *p != ']')
188                   {
189                     cend = get_unescaped_char (&p, &was_escaped);
190                     if (cend == '\0')
191                       return FALSE;
192
193                     c = get_char (&p);
194                   }
195
196                 if (nc >= cstart && nc <= cend)
197                   goto matched;
198
199                 if (!was_escaped && c == ']')
200                   break;
201               }
202             if (!not)
203               return FALSE;
204             break;
205
206           matched:;
207             /* Skip the rest of the [...] that already matched.  */
208             /* XXX 1003.2d11 is unclear if was_escaped is right.  */
209             while (was_escaped || c != ']')
210               {
211                 if (c == '\0')
212                   /* [... (unterminated) loses.  */
213                   return FALSE;
214
215                 c = get_unescaped_char (&p, &was_escaped);
216               }
217             if (not)
218               return FALSE;
219           }
220           break;
221
222         default:
223           if (c != nc)
224             return FALSE;
225         }
226
227       component_start = (nc == G_DIR_SEPARATOR);
228     }
229
230   if (*n == '\0')
231     return TRUE;
232
233   return FALSE;
234 }
235
236 /* Match STRING against the filename pattern PATTERN, returning zero if
237  *  it matches, nonzero if not.
238  *
239  * GTK+ used to use a old version of GNU fnmatch() that was buggy
240  * in various ways and didn't handle UTF-8. The following is
241  * converted to UTF-8. To simplify the process of making it
242  * correct, this is special-cased to the combinations of flags
243  * that gtkfilesel.c uses.
244  *
245  *   FNM_FILE_NAME   - always set
246  *   FNM_LEADING_DIR - never set
247  *   FNM_PERIOD      - always set
248  *   FNM_NOESCAPE    - set only on windows
249  *   FNM_CASEFOLD    - set only on windows
250  */
251 gboolean
252 _gtk_fnmatch (const char *pattern,
253               const char *string)
254 {
255   return gtk_fnmatch_intern (pattern, string, TRUE);
256 }
257
258 #undef FNMATCH_TEST_CASES
259 #ifdef FNMATCH_TEST_CASES
260
261 #define TEST(pat, str, result) \
262   g_assert (_gtk_fnmatch ((pat), (str)) == result)
263
264 int main (int argc, char **argv)
265 {
266   TEST ("[a-]", "-", TRUE);
267   
268   TEST ("a", "a", TRUE);
269   TEST ("a", "b", FALSE);
270
271   /* Test what ? matches */
272   TEST ("?", "a", TRUE);
273   TEST ("?", ".", FALSE);
274   TEST ("a?", "a.", TRUE);
275   TEST ("a/?", "a/b", TRUE);
276   TEST ("a/?", "a/.", FALSE);
277   TEST ("?", "/", FALSE);
278
279   /* Test what * matches */
280   TEST ("*", "a", TRUE);
281   TEST ("*", ".", FALSE);
282   TEST ("a*", "a.", TRUE);
283   TEST ("a/*", "a/b", TRUE);
284   TEST ("a/*", "a/.", FALSE);
285   TEST ("*", "/", FALSE);
286
287   /* Range tests */
288   TEST ("[ab]", "a", TRUE);
289   TEST ("[ab]", "c", FALSE);
290   TEST ("[^ab]", "a", FALSE);
291   TEST ("[!ab]", "a", FALSE);
292   TEST ("[^ab]", "c", TRUE);
293   TEST ("[!ab]", "c", TRUE);
294   TEST ("[a-c]", "b", TRUE);
295   TEST ("[a-c]", "d", FALSE);
296   TEST ("[a-]", "-", TRUE);
297   TEST ("[]]", "]", TRUE);
298   TEST ("[^]]", "a", TRUE);
299   TEST ("[!]]", "a", TRUE);
300
301   /* Various unclosed ranges */
302   TEST ("[ab", "a", FALSE);
303   TEST ("[a-", "a", FALSE);
304   TEST ("[ab", "c", FALSE);
305   TEST ("[a-", "c", FALSE);
306   TEST ("[^]", "a", FALSE);
307
308   /* Ranges and special no-wildcard matches */
309   TEST ("[.]", ".", FALSE);
310   TEST ("a[.]", "a.", TRUE);
311   TEST ("a/[.]", "a/.", FALSE);
312   TEST ("[/]", "/", FALSE);
313   TEST ("[^/]", "a", TRUE);
314   
315   /* Basic tests of * (and combinations of * and ?) */
316   TEST ("a*b", "ab", TRUE);
317   TEST ("a*b", "axb", TRUE);
318   TEST ("a*b", "axxb", TRUE);
319   TEST ("a**b", "ab", TRUE);
320   TEST ("a**b", "axb", TRUE);
321   TEST ("a**b", "axxb", TRUE);
322   TEST ("a*?*b", "ab", FALSE);
323   TEST ("a*?*b", "axb", TRUE);
324   TEST ("a*?*b", "axxb", TRUE);
325
326   /* Test of  *[range] */
327   TEST ("a*[cd]", "ac", TRUE);
328   TEST ("a*[cd]", "axc", TRUE);
329   TEST ("a*[cd]", "axx", FALSE);
330
331   TEST ("a/[.]", "a/.", FALSE);
332   TEST ("a*[.]", "a/.", FALSE);
333
334   /* Test of UTF-8 */
335
336   TEST ("ä", "ä", TRUE);      /* TEST ("ä", "ä", TRUE); */
337   TEST ("?", "ä", TRUE);       /* TEST ("?", "ä", TRUE); */
338   TEST ("*ö", "äö", TRUE);   /* TEST ("*ö", "äö", TRUE); */
339   TEST ("*ö", "ääö", TRUE); /* TEST ("*ö", "ääö", TRUE); */
340   TEST ("[ä]", "ä", TRUE);    /* TEST ("[ä]", "ä", TRUE); */
341   TEST ("[ä-ö]", "é", TRUE); /* TEST ("[ä-ö]", "é", TRUE); */
342   TEST ("[ä-ö]", "a", FALSE); /* TEST ("[ä-ö]", "a", FALSE); */
343
344 #ifdef DO_ESCAPE
345   /* Tests of escaping */
346   TEST ("\\\\", "\\", TRUE);
347   TEST ("\\?", "?", TRUE);
348   TEST ("\\?", "a", FALSE);
349   TEST ("\\*", "*", TRUE);
350   TEST ("\\*", "a", FALSE);
351   TEST ("\\[a-b]", "[a-b]", TRUE);
352   TEST ("[\\\\]", "\\", TRUE);
353   TEST ("[\\^a]", "a", TRUE);
354   TEST ("[a\\-c]", "b", FALSE);
355   TEST ("[a\\-c]", "-", TRUE);
356   TEST ("[a\\]", "a", FALSE);
357 #endif /* DO_ESCAPE */
358   
359   return 0;
360 }
361
362 #endif /* FNMATCH_TEST_CASES */