From 9a00cd11d55c1362e8b0eb0c977ffca9ce5d099c Mon Sep 17 00:00:00 2001 From: janwas Date: Tue, 23 Nov 2004 20:52:03 +0000 Subject: [PATCH] add match_wildcard function (for vfs_next_dirent) This was SVN commit r1383. --- source/lib/lib.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++ source/lib/lib.h | 4 ++++ 2 files changed, 57 insertions(+) diff --git a/source/lib/lib.cpp b/source/lib/lib.cpp index d105efa0d4..fe0b9663a6 100755 --- a/source/lib/lib.cpp +++ b/source/lib/lib.cpp @@ -366,3 +366,56 @@ void base32(const int len, const u8* in, u8* out) } } + + + + + +// case-insensitive check if string matches the pattern , +// 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'); +} diff --git a/source/lib/lib.h b/source/lib/lib.h index 85dc313abb..c862417ceb 100755 --- a/source/lib/lib.h +++ b/source/lib/lib.h @@ -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 matches the pattern , +// 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)