home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / XSTRCMP.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  4KB  |  127 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  XSTRCMP.C - Simple string pattern matching functions using DOS
  5. **              wildcards ('?' & '*').
  6. **
  7. **  Derived from code by Arjan Kentner (submitted by Steve Summit),
  8. **  modified by Bob Stout.
  9. */
  10.  
  11. #include <stdio.h>                  /* For NULL                         */
  12. #include <ctype.h>                  /* For toupper()                    */
  13. #include <assert.h>                 /* For assert()                     */
  14. #include "sniptype.h"               /* For Boolean_T                    */
  15. #include "dirent.h"                 /* Validate prototypes, also
  16.                                        includes extkword.h for PASCAL   */
  17.  
  18. /*
  19. **  Arguments: 1 - Pattern to match
  20. **             2 - String to match
  21. **
  22. **  Returns: True_ if match
  23. **           False_ if no match
  24. **           Error_ if passed a null pointer (see below)
  25. **
  26. **  Notes: 1. Two versions are supplied, one case-sensitive and one not.
  27. **         2. Each version consists of a recursive static function which
  28. **            does all the work, and a wrapper which checks for null
  29. **            pointers before calling the static function.
  30. **         3. If assert() is enabled (i.e. if NDEBUG is undefined or false),
  31. **            the wrapper functions will abort with an assertion error in
  32. **            case a null pointer is passed.
  33. **         4. If assert() is disabled (i.e. if NDEBUG is defined), the
  34. **            wrapper function will return Error_ to the calling program in
  35. **            case a null pointer is passed.
  36. */
  37.  
  38. /*
  39. **  Case-sensitive version
  40. */
  41.  
  42. static Boolean_T PASCAL patmat (const char *pat, const char *str)
  43. {
  44.       switch (*pat)
  45.       {
  46.       case '\0':
  47.             return !*str;
  48.  
  49.       case '*' :
  50.             return patmat(pat+1, str) || *str && patmat(pat, str+1);
  51.  
  52.       case '?' :
  53.             return *str && patmat(pat+1, str+1);
  54.  
  55.       default  :
  56.             return (*pat == *str) && patmat(pat+1, str+1);
  57.       }
  58. }
  59.  
  60. Boolean_T xstrcmp (const char *pat, const char *str)
  61. {
  62.       assert(str && pat);
  63.       if (NULL == str || NULL == pat)
  64.             return Error_;
  65.       else  return(patmat(pat, str));
  66. }
  67.  
  68. /*
  69. **  Case-insensitive version
  70. */
  71.  
  72. static Boolean_T PASCAL patimat (const char *pat, const char *str)
  73. {
  74.       switch (*pat)
  75.       {
  76.       case '\0':
  77.             return !*str;
  78.  
  79.       case '*' :
  80.             return patimat(pat+1, str) || *str && patimat(pat, str+1);
  81.  
  82.       case '?' :
  83.             return *str && patimat(pat+1, str+1);
  84.  
  85.       default  :
  86.             return (toupper(*pat) == toupper(*str)) && patimat(pat+1, str+1);
  87.       }
  88. }
  89.  
  90. Boolean_T xstricmp (const char *pat, const char *str)
  91. {
  92.       assert(str && pat);
  93.       if (NULL == str || NULL == pat)
  94.             return Error_;
  95.       else  return(patimat(pat, str));
  96. }
  97.  
  98. #ifdef TEST
  99.  
  100. #include <stdio.h>
  101.  
  102. main(int argc, char *argv[])
  103. {
  104.       if (3 != argc)
  105.       {
  106.             puts("Usage: XSTRCMP mask string");
  107.             return -1;
  108.       }
  109.       printf("xstrcmp(\"%s\", \"%s\") returned %d\n", argv[1], argv[2],
  110.             xstrcmp(argv[1], argv[2]));
  111.  
  112.       printf("xstricmp(\"%s\", \"%s\") returned %d\n", argv[1], argv[2],
  113.             xstricmp(argv[1], argv[2]));
  114.       
  115.       printf("xstricmp(NULL, \"%s\") returned %d\n", argv[2],
  116.             xstricmp(NULL, argv[2]));
  117.       
  118.       printf("xstricmp(\"%s\", NULL) returned %d\n", argv[1],
  119.             xstricmp(argv[1], NULL));
  120.       
  121.       printf("xstricmp(NULL, NULL) returned %d\n", xstricmp(NULL, NULL));
  122.       
  123.       return 0;
  124. }
  125.  
  126. #endif /* TEST */
  127.