Wildcard string compare (globbing)






4.90/5 (77 votes)
May 2, 2001

1304057
Matches a string against a wildcard string such as "*.*" or "bl?h.*" etc. This is good for file globbing or to match hostmasks.
Usage:
This is a fast, lightweight, and simple pattern matching function.
if (wildcmp("bl?h.*", "blah.jpg")) { //we have a match! } else { //no match =( }
Function:
int wildcmp(const char *wild, const char *string) { // Written by Jack Handy - jakkhandy@hotmail.com const char *cp = NULL, *mp = NULL; while ((*string) && (*wild != '*')) { if ((*wild != *string) && (*wild != '?')) { return 0; } wild++; string++; } while (*string) { if (*wild == '*') { if (!*++wild) { return 1; } mp = wild; cp = string+1; } else if ((*wild == *string) || (*wild == '?')) { wild++; string++; } else { wild = mp; string = cp++; } } while (*wild == '*') { wild++; } return !*wild; }