home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 14 / CD_ASCQ_14_0694.iso / maj / 653 / patmat.c < prev    next >
C/C++ Source or Header  |  1994-04-03  |  5KB  |  76 lines

  1. #include <stddef.h>
  2. #include <string.h>
  3.  
  4. /***********************************************************************
  5. **                                                                    **
  6. **   Function    :  patmat                                            **
  7. **                                                                    **
  8. **   Purpose     :  Pattern Matching                                  **
  9. **                                                                    **
  10. **   Usage       :  Pass two string pointers as parameters.The first  **
  11. **                  being a raw string & the second a pattern the raw **
  12. **                  string is to be matched against.If the raw string **
  13. **                  matches the pattern,then the function returns a   **
  14. **                  1,else it returns a 0.                            **
  15. **                                                                    **
  16. **                  e.g  patmat("abcdefghi","*ghi") returns a 1.      **
  17. **                       patmat("abcdefghi","??c??f*") returns a 1.   **
  18. **                       patmat("abcdefghi","*dh*") returns a 0.      **
  19. **                       patmat("abcdefghi","*def") returns a 0.      **
  20. **                                                                    **
  21. **                  The asterisk is a wild card to allow any charac-  **
  22. **                  ters from its first appearance to the next spec-  **
  23. **                  ific character.The character ? is a wild card     **
  24. **                  for only one character in the position it appears.**
  25. **                  Combinations such as "*?" or "?*" or "**" are     **
  26. **                  illegal for obvious reasons & the functions may   **
  27. **                  goof,though I think it will still work.           **
  28. **                                                                    **
  29. **   Author      :  Sreenath Chary              Nov 29 1988           **
  30. **                                                                    **
  31. **   Logic       :  The only simple way I could devise is to use      **
  32. **                  recursion.Each character in the pattern is        **
  33. **                  taken & compared with the character in the raw    **
  34. **                  string.If it matches then the remaining amount    **
  35. **                  of the string & the remaining amount of the       **
  36. **                  pattern are passed as parameters to patmat again  **
  37. **                  until the end of the pattern.If at any stage      **
  38. **                  the pattern does not match,then we go back one    **
  39. **                  level & at this level if the previous character   **
  40. **                  was a asterisk in the pattern,we hunt again from  **
  41. **                  where we left off,otherwise we return back one    **
  42. **                  more level with a not found & the process goes    **
  43. **                  on till the first level call.                     **
  44. **                                                                    **
  45. **                  Only one character at a time is considered,except **
  46. **                  when the character is an asterisk.You'll get the  **
  47. **                  logic as the program unfolds.                     **
  48. **                                                                    **
  49. ***********************************************************************/
  50.  
  51. patmat(char *raw,char *pat)
  52. {  int  i, slraw;
  53.  
  54.    if ((*pat == '\0') && (*raw == '\0'))    /*  if it is end of both  */
  55.      return( 1 ) ;                          /*  strings,then match    */
  56.    if (*pat == '\0')                        /*  if it is end of only  */
  57.      return( 0 ) ;                          /*  pat tehn mismatch     */
  58.    if (*pat == '*')                         /* if pattern is a '*'    */
  59.     { if (*(pat+1) == '\0')                 /*    if it is end of pat */
  60.          return( 1 ) ;                      /*    then match          */
  61.       for(i=0,slraw=strlen(raw);i<=slraw;i++)/*    else hunt for match*/
  62.         if ((*(raw+i) == *(pat+1)) ||       /*         or wild card   */
  63.             (*(pat+1) == '?'))
  64.          if (patmat(raw+i+1,pat+2) == 1)    /*      if found,match    */
  65.               return( 1 ) ;                 /*        rest of pat     */
  66.     }
  67.    else
  68.     { if (*raw == '\0')                     /*  if end of raw then    */
  69.          return( 0 ) ;                      /*     mismatch           */
  70.       if ((*pat == '?') || (*pat == *raw))  /*  if chars match then   */
  71.         if (patmat(raw+1,pat+1) == 1)       /*  try & match rest of it*/
  72.            return( 1 ) ;
  73.     }
  74.    return( 0 ) ;                            /*  no match found        */
  75. }
  76.