/*
? matches any single character
* matches 0 or more characters of any type
% matches 0 or more non-space characters (can be used to match a single
word)
~ matches 1 or more space characters (can be used for whitespace between
words)
*/
int egg_matchwilds(const char *haystack, const char *needle)
{
if (needle == NULL || haystack == NULL)
return 1;
while (*needle)
{
/* ? = one single character *haystack is not NULL if *needle isn't */
if (*needle == *haystack || *needle == '?')
{
needle++;
haystack++;
}
else if (*haystack == '\0' && *needle != '*')
return 1;
else if (*needle != '%' && *needle != '~' && *needle != '*')
return 1;
else if (*needle == '~')
{
/* One or more space characters */
if (*haystack != ' ' && *haystack != '\t')
return 1;
while (*haystack)
{
if (*haystack != ' ' && *haystack != '\t')
break;
haystack++;
}
needle++;
}
else if (*needle == '%')
{
while (*haystack)
{
if (*haystack == ' ' || *haystack == '\t')
break;
haystack++;
}
needle++;
}
else if (*needle == '*')
{
needle++;
while (*haystack && *needle)
{
if (*haystack == *needle)
{
/* Greedy, but works okay */
if (!egg_matchwilds(haystack+1,needle-1))
return 0;
break;
}
haystack++;
}
}
}
return 0;
}