version 1.2, 2007/05/25 18:35:07
|
version 1.3, 2007/11/30 01:16:32
|
|
|
*/ | */ |
| |
#include "Match.h" | #include "Match.h" |
#include "Log.h" |
|
|
|
/* |
|
**============================================================================== |
|
** |
|
** Match() |
|
** |
|
** Attempt to match *str* to the *pattern*. Return 0 if there is a match. |
|
** The only special character in the string is the '*' character. |
|
** |
|
**============================================================================== |
|
*/ |
|
|
|
int Match(const char* pattern, const char* str) |
|
{ |
|
const char* p; |
|
const char* q; |
|
|
|
/* Now match expression to str. */ |
|
|
|
for (p = pattern, q = str; *p && *q; ) |
|
{ |
|
if (*p == '*') |
|
{ |
|
const char* r; |
|
|
|
p++; |
|
|
|
/* Recursively call to find the shortest match. */ |
|
|
|
for (r = q; *r; r++) |
|
{ |
|
if (Match(p, r) == 0) |
|
break; |
|
} |
|
|
|
q = r; |
|
|
|
} |
|
else if (*p == *q) |
|
{ |
|
p++; |
|
q++; |
|
} |
|
else |
|
return -1; |
|
} |
|
|
|
/* If src was exhausted but pattern has a single '*' remaining charcters, |
|
* then match the result. |
|
*/ |
|
|
|
if (p[0] == '*' && p[1] == '\0') |
|
return 0; |
|
|
|
/* If anything left over, then they do not match. */ |
|
|
|
if (*p || *q) |
|
return -1; |
|
|
|
return 0; |
|
} |
|