1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// Pattern matching routines for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2010-11-29 00:06:39 +03:00
|
|
|
// Copyright 1998-2010 by Bill Spitzak and others.
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2005-04-16 04:13:17 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
/* Adapted from Rich Salz. */
|
|
|
|
#include <FL/filename.H>
|
1999-01-08 00:22:28 +03:00
|
|
|
#include <ctype.h>
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2009-01-20 14:10:29 +03:00
|
|
|
/**
|
2020-07-01 19:03:10 +03:00
|
|
|
Checks if a string \p s matches a pattern \p p.
|
2009-01-20 14:10:29 +03:00
|
|
|
The following syntax is used for the pattern:
|
|
|
|
- * matches any sequence of 0 or more characters.
|
|
|
|
- ? matches any single character.
|
2020-07-01 19:03:10 +03:00
|
|
|
- [set] matches any character in the set. Set can contain any single characters, or a-z to represent a range.
|
2009-01-20 14:10:29 +03:00
|
|
|
To match ] or - they must be the first characters. To match ^ or ! they must not be the first characters.
|
|
|
|
- [^set] or [!set] matches any character not in the set.
|
|
|
|
- {X|Y|Z} or {X,Y,Z} matches any one of the subexpressions literally.
|
|
|
|
- \\x quotes the character x so it has no special meaning.
|
|
|
|
- x all other characters must be matched exactly.
|
2010-01-01 21:30:49 +03:00
|
|
|
|
|
|
|
\b Include:
|
|
|
|
\code
|
|
|
|
#include <FL/filename.H>
|
|
|
|
\endcode
|
|
|
|
|
2009-01-20 14:10:29 +03:00
|
|
|
\param[in] s the string to check for a match
|
2020-07-01 19:03:10 +03:00
|
|
|
\param[in] p the string pattern
|
2009-01-20 14:10:29 +03:00
|
|
|
\return non zero if the string matches the pattern
|
|
|
|
*/
|
2002-03-26 00:08:42 +03:00
|
|
|
int fl_filename_match(const char *s, const char *p) {
|
1998-10-06 22:21:25 +04:00
|
|
|
int matched;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
switch(*p++) {
|
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
case '?' : // match any single character
|
1998-10-06 22:21:25 +04:00
|
|
|
if (!*s++) return 0;
|
|
|
|
break;
|
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
case '*' : // match 0-n of any characters
|
1998-10-06 22:21:25 +04:00
|
|
|
if (!*p) return 1; // do trailing * quickly
|
2002-03-26 00:08:42 +03:00
|
|
|
while (!fl_filename_match(s, p)) if (!*s++) return 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
return 1;
|
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
case '[': { // match one character in set of form [abc-d] or [^a-b]
|
1998-10-06 22:21:25 +04:00
|
|
|
if (!*s) return 0;
|
|
|
|
int reverse = (*p=='^' || *p=='!'); if (reverse) p++;
|
|
|
|
matched = 0;
|
|
|
|
char last = 0;
|
|
|
|
while (*p) {
|
2020-07-01 19:03:10 +03:00
|
|
|
if (*p=='-' && last) {
|
|
|
|
if (*s <= *++p && *s >= last ) matched = 1;
|
|
|
|
last = 0;
|
|
|
|
} else {
|
|
|
|
if (*s == *p) matched = 1;
|
|
|
|
}
|
|
|
|
last = *p++;
|
|
|
|
if (*p==']') break;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
if (matched == reverse) return 0;
|
|
|
|
s++; p++;}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '{' : // {pattern1|pattern2|pattern3}
|
|
|
|
NEXTCASE:
|
2002-03-26 00:08:42 +03:00
|
|
|
if (fl_filename_match(s,p)) return 1;
|
1998-10-06 22:21:25 +04:00
|
|
|
for (matched = 0;;) {
|
|
|
|
switch (*p++) {
|
|
|
|
case '\\': if (*p) p++; break;
|
|
|
|
case '{': matched++; break;
|
|
|
|
case '}': if (!matched--) return 0; break;
|
|
|
|
case '|': case ',': if (matched==0) goto NEXTCASE;
|
|
|
|
case 0: return 0;
|
|
|
|
}
|
|
|
|
}
|
2020-07-01 19:03:10 +03:00
|
|
|
case '|': // skip rest of |pattern|pattern} when called recursively
|
1998-10-06 22:21:25 +04:00
|
|
|
case ',':
|
|
|
|
for (matched = 0; *p && matched >= 0;) {
|
2020-07-01 19:03:10 +03:00
|
|
|
switch (*p++) {
|
|
|
|
case '\\': if (*p) p++; break;
|
|
|
|
case '{': matched++; break;
|
|
|
|
case '}': matched--; break;
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
break;
|
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
case 0: // end of pattern
|
1998-10-06 22:21:25 +04:00
|
|
|
return !*s;
|
|
|
|
|
2020-07-01 19:03:10 +03:00
|
|
|
case '\\': // quote next character
|
1998-10-06 22:21:25 +04:00
|
|
|
if (*p) p++;
|
2010-02-27 00:10:46 +03:00
|
|
|
/* FALLTHROUGH */
|
1999-01-08 00:22:28 +03:00
|
|
|
default:
|
|
|
|
if (tolower(*s) != tolower(*(p-1))) return 0;
|
|
|
|
s++;
|
2001-12-11 19:03:13 +03:00
|
|
|
break;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|