1
0
forked from 0ad/0ad

add match_wildcard function (for vfs_next_dirent)

This was SVN commit r1383.
This commit is contained in:
janwas 2004-11-23 20:52:03 +00:00
parent 03db724b99
commit 9a00cd11d5
2 changed files with 57 additions and 0 deletions

View File

@ -366,3 +366,56 @@ void base32(const int len, const u8* in, u8* out)
}
}
// case-insensitive check if string <s> matches the pattern <w>,
// which may contain '?' or '*' wildcards. if so, return 1, otherwise 0.
// idea from http://www.codeproject.com/string/wildcmp.asp .
int match_wildcard(const char* s, const char* w)
{
// saved position in both strings, used to expand '*':
// s2 is advanced until match.
// initially 0 - we abort on mismatch before the first '*'.
const char* s2 = 0;
const char* w2;
while(*s)
{
const int wc = *w;
if(wc == '*')
{
// wildcard string ended with * => match.
if(*++w == '\0')
return 1;
w2 = w;
s2 = s+1;
}
// match one character
else if(toupper(wc) == toupper(*s) || wc == '?')
{
w++;
s++;
}
// mismatched character
else
{
// no '*' found yet => mismatch.
if(!s2)
return 0;
// resume at previous position+1
w = w2;
s = s2++;
}
}
// strip trailing * in wildcard string
while(*w == '*')
w++;
return (*w == '\0');
}

View File

@ -307,6 +307,10 @@ extern u16 fp_to_u16(double in);
extern void base32(const int len, const u8* in, u8* out);
// case-insensitive check if string <s> matches the pattern <w>,
// which may contain '?' or '*' wildcards. if so, return 1, otherwise 0.
extern int match_wildcard(const char* s, const char* w);
// design goals:
// fast (including startup time)