2 * File and other globbing (included by wb_utils.cc)
6 // wxIsWild(const char *pattern)
7 // wxMatchWild(const char *pattern, const char *str, bool dot_special)
11 //---------------------------------------------------------------------------------
22 # error "Can't use Unix file globbing under Windows!"
26 /*************************************************************************
28 * wxIsWild checks whether the pattern contains wildcards, and
29 * returns TRUE if it does, and FALSE if it does not (or if the
30 * pattern is NULL -- i.e. no string).
34 * 1) pattern - a character string
37 wxIsWild (const char *pattern)
67 // -----------------------------------------------------
68 // '*' = match 0 or more occurances of anything
69 // "[abc]" = match anyof "abc" (ranges supported)
70 // "{xx,yy,zz}" = match anyof "xx", "yy", or "zz"
71 // '?' = match any character
73 // '\' is used to "escape" special characters
77 wxMatchWild (const char *pattern, const char *str, bool dot_special)
81 bool done = FALSE, ret_code, ok;
82 // Below is for vi fans
83 const char OB = '{', CB = '}';
86 if (strcmp(pattern, "*.*") == 0)
87 pattern = "*"; // Hack for MS-DOS compat.
90 // dot_special means '.' only matches '.'
91 if (dot_special && *str == '.' && *pattern != *str)
94 while ((*pattern != '\0') && (!done) && (((*str == '\0') &&
95 ((*pattern == OB) || (*pattern == '*'))) || (*str != '\0')))
101 if (*pattern != '\0')
107 while ((*str != '\0') && (!(ret_code = wxMatchWild (pattern, str++, FALSE))));
112 while (*pattern != '\0')
119 if ((*pattern == '\0') || (*pattern == ']'))
124 if (*pattern == '\\')
127 if (*pattern == '\0')
133 if (*(pattern + 1) == '-')
142 if (*pattern == '\\')
145 if (*pattern == '\0')
151 if ((*str < c) || (*str > *pattern))
157 else if (*pattern != *str)
163 while ((*pattern != ']') && (*pattern != '\0'))
165 if ((*pattern == '\\') && (*(pattern + 1) != '\0'))
169 if (*pattern != '\0')
180 while ((*pattern != CB) && (*pattern != '\0'))
184 while (ok && (*cp != '\0') && (*pattern != '\0') &&
185 (*pattern != ',') && (*pattern != CB))
187 if (*pattern == '\\')
189 ok = (*pattern++ == *cp++);
191 if (*pattern == '\0')
200 while ((*pattern != CB) && (*pattern != '\0'))
202 if (*++pattern == '\\')
204 if (*++pattern == CB)
211 while (*pattern != CB && *pattern != ',' && *pattern != '\0')
213 if (*++pattern == '\\')
215 if (*++pattern == CB || *pattern == ',')
220 if (*pattern != '\0')
225 if (*str == *pattern)
235 while (*pattern == '*')
237 return ((*str == '\0') && (*pattern == '\0'));
240 #else /* MS-DOS/Windows glob() */
241 /*************************************************************************
243 * wxMatchWild matches the given pattern string against
244 * a text string, and returns TRUE if it matches, FALSE otherwise.
246 * A match means that the entire text string is used up in the matching.
247 * The pattern can contain the following wildcards.
249 * * -- matches any sequence of characters
250 * ? -- matches one character
252 * If one or other or both of the string arguments to wxMatchWild function is
253 * NULL (i.e. there isn't a string), then the function returns FALSE.
256 static bool wxPatternMatch (const char *pattern, const char *text, size_t i, size_t j);
258 // @@@@ dotSpecial is ignored by MS-DOS
260 wxMatchWild (const char *pattern, const char *text, bool /* dotSpecial */ )
262 if (pattern == NULL || text == NULL || *pattern == '\0' || *text == '\0')
264 return wxPatternMatch (pattern, text, 0, 0);
267 /*************************************************************************
269 * wxPatternMatch does the work for wxMatchWild. wxPatternMatch matches
270 * the given pattern string against a text string, and returns TRUE if
271 * it matches, FALSE otherwise. It is assumed that the string arguments
272 * to wxPatternMatch exist.
274 * A match means that the entire text string is used up in the matching.
275 * The pattern can contain the following wildcards.
277 * * -- matches any sequence of characters
278 * ? -- matches one character
280 * wxPatternMatch works by going down the pattern trying to match the
281 * the same index character in the pattern and string arrays, and stops
282 * when the end of the pattern or text string is reached. However, if a
283 * '*' wildcard is met, the algorithm checks to see whether the remaining
284 * pattern (after the wildcard) matches the rest of the text (i.e. the
285 * wxPatternMatch function is called recursively).
289 wxPatternMatch (const char *pattern, const char *text, size_t i, size_t j)
291 size_t pattern_length = strlen (pattern);
292 size_t text_length = strlen (text);
296 // MS-DOS file system is case INDEPENDENT
297 # define EQU(x,y) (wxToLower(x) == wxToLower(y))
299 # define EQU(x,y) ((x) == (y))
302 while (j < pattern_length && i < text_length)
304 if (EQU(text[i], pattern[j]) || pattern[j] == '?')
309 else if (pattern[j] == '*')
311 // If pattern ends in '*'
312 if (++j == pattern_length)
320 // after wildcard check to see whether rest of pattern matches
321 // up with rest of text
322 while (i < text_length && match != TRUE)
324 match = wxPatternMatch (pattern, text, i, j);
327 // text index is decremented so that it points to where
328 // the text string starts to match the rest of the pattern
332 else if (! EQU(text[i], pattern[j]))
338 if (j == pattern_length && i == text_length && match == TRUE)
343 // special case where pattern and text are the same except that pattern
344 // also only has '*' wildcards on the end
345 if (i == text_length && pattern[j] == '*' && match == TRUE)
347 for (; j < pattern_length; j++)
349 if (pattern[j] != '*')
360 #endif /* UNIX_GLOB */
361 //-----------------------------------------------------------------------------