home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_01_04 / 1n04022a < prev    next >
Text File  |  1990-07-18  |  3KB  |  62 lines

  1. /*
  2. **  Figure 4 - Wilcard pattern matching
  3. */
  4.  
  5. #include <stddef.h>
  6. #include <string.h>
  7.  
  8. /************************************************************************/
  9. /*                                                                      */
  10. /*   patmat()                                                           */
  11. /*                                                                      */
  12. /*   Usage       :  Pass two string pointers as parameters.The first    */
  13. /*                  being a raw string & the second a pattern the raw   */
  14. /*                  string is to be matched against.If the raw string   */
  15. /*                  matches the pattern,then the function returns a     */
  16. /*                  1,else it returns a 0.                              */
  17. /*                                                                      */
  18. /*                  e.g  patmat("abcdefghi","*ghi") returns a 1.        */
  19. /*                       patmat("abcdefghi","??c??f*") returns a 1.     */
  20. /*                       patmat("abcdefghi","*dh*") returns a 0.        */
  21. /*                       patmat("abcdefghi","*def") returns a 0.        */
  22. /*                                                                      */
  23. /*                  The asterisk is a wild card to allow any charac-    */
  24. /*                  ters from its first appearance to the next spec-    */
  25. /*                  ific character.The character ? is a wild card       */
  26. /*                  for only one character in the position it appears.  */
  27. /*                                                                      */
  28. /*   From public domain sources via FidoNet.                            */
  29. /*                                                                      */
  30. /************************************************************************/
  31.  
  32. int patmat(const char *raw, const char *pat)
  33. {
  34.     int  i ;
  35.  
  36.     if ((*pat == '\0') && (*raw == '\0'))   /*  if it is end of both    */
  37.         return( 1 ) ;                       /*  strings,then match      */
  38.     if (*pat == '\0')                       /*  if it is end of only    */
  39.         return( 0 ) ;                       /*  pat then mismatch       */
  40.     if (*pat == '*')                        /* if pattern is a '*'      */
  41.     {
  42.         if (*(pat+1) == '\0')               /*    if it is end of pat   */
  43.             return(1);                      /*    then match            */
  44.         for(i = 0; i <= strlen(raw); i++)   /*    else hunt for match   */
  45.             if ((*(raw+i) == *(pat+1)) ||   /*         or wild card     */
  46.                 (*(pat+1) == '?'))
  47.                                             /*      if found,match      */
  48.                     if (patmat(raw + i + 1, pat + 2) == 1)
  49.                         return(1);          /*        rest of pat       */
  50.     }
  51.     else
  52.     {
  53.         if (*raw == '\0')                   /*  if end of raw then      */
  54.             return(0);                      /*     mismatch             */
  55.         if ((*pat == '?') || (*pat == *raw))/*  if chars match then     */
  56.                                             /*  try & match rest of it  */
  57.             if (patmat(raw + 1, pat + 1) == 1)
  58.                 return(1);
  59.     }
  60.     return(0);                              /*  no match found          */
  61. }
  62.