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