lib/wildcmp.c

Go to the documentation of this file.
00001 /* Wildcard matching. 
00002  *
00003  * This file is copyright 2002 Jim Kent, but license is hereby
00004  * granted for all use - public, private or commercial. */
00005 
00006 #include "common.h"
00007 
00008 static char const rcsid[] = "$Id: wildcmp.c,v 1.5 2003/05/06 07:33:44 kate Exp $";
00009 
00010 static int subMatch(char *str, char *wild)
00011 /* Returns number of characters that match between str and wild up
00012  * to the next wildcard in wild (or up to end of string.). */
00013 {
00014 int len = 0;
00015 
00016 for(;;)
00017     {
00018     if(toupper(*str++) != toupper(*wild++) )
00019         return(0);
00020     ++len;
00021     switch(*wild)
00022         {
00023         case 0:
00024         case '?':
00025         case '*':
00026             return(len);
00027         }
00028     }
00029 }
00030 
00031 boolean wildMatch(char *wildCard, char *string)
00032 /* does a case sensitive wild card match with a string.
00033  * * matches any string or no character.
00034  * ? matches any single character.
00035  * anything else etc must match the character exactly. */
00036 {
00037 boolean matchStar = 0;
00038 int starMatchSize;
00039 
00040 for(;;)
00041     {
00042 NEXT_WILD:
00043     switch(*wildCard)
00044         {
00045         case 0: /* end of wildcard */
00046             {
00047             if(matchStar)
00048                 {
00049                 while(*string++)
00050                     ;
00051                 return TRUE;
00052                 }
00053             else if(*string)
00054                 return FALSE;
00055             else
00056                 return TRUE;
00057             }
00058         case '*':
00059             matchStar = TRUE;
00060             break;
00061         case '?': /* anything will do */
00062             {
00063             if(*string == 0)
00064                 return FALSE; /* out of string, no match for ? */
00065             ++string;
00066             break;
00067             }
00068         default:
00069             {
00070             if(matchStar)
00071                 {
00072                 for(;;)
00073                     {
00074                     if(*string == 0) /* if out of string no match */
00075                         return FALSE;
00076 
00077                     /* note matchStar is re-used here for substring
00078                      * after star match length */
00079                     if((starMatchSize = subMatch(string,wildCard)) != 0)
00080                         {
00081                         string += starMatchSize;
00082                         wildCard += starMatchSize;
00083                         matchStar = FALSE;
00084                         goto NEXT_WILD;
00085                         }
00086                     ++string;
00087                     }
00088                 }
00089 
00090             /* default: they must be equal or no match */
00091             if(toupper(*string) != toupper(*wildCard))
00092                 return FALSE;
00093             ++string;
00094             break;
00095             }
00096         }
00097     ++wildCard;
00098     }
00099 }

Generated on Tue Dec 25 18:39:32 2007 for blat by  doxygen 1.5.2