home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / programs / programming / maskthrow / c / match
Encoding:
Text File  |  1998-05-31  |  844 b   |  37 lines

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. typedef int bool;
  6.  
  7. bool match(const char *wildcard, const char *string)
  8. {
  9.   char process[256], *i = process;
  10.   while((unsigned char) *string >= ' ') *i++ = *string++;
  11.   *i = 0;
  12.   string = process;
  13.  
  14.   /* Match the beginning of string */
  15.   while((*wildcard != '*') && (*wildcard != 0))
  16.     if(*wildcard++ != *string++) return 0;
  17.   if(*wildcard++ == 0 && *string == 0) return 1;
  18.  
  19.   /* Match the middle */
  20.   while(strchr(wildcard, '*')) {
  21.     char match[256], *m = match;
  22.     int length = 0;
  23.     while(*wildcard != '*') length++, *m++ = *wildcard++;
  24.     *m = 0;
  25.     wildcard++;
  26.  
  27.     while(strncmp(match, string, length)) {
  28.       if(strlen(string) < length) return 0;
  29.       string++;
  30.     }
  31.   }
  32.  
  33.   /* Match the end */
  34.   if(strcmp(wildcard, string + strlen(string) - strlen(wildcard))) return 0;
  35.   return 1;
  36. }
  37.