home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 592b.lha / XTime_v1.0 / source / wildmat.c < prev    next >
C/C++ Source or Header  |  1991-11-23  |  2KB  |  87 lines

  1. /*
  2. **  Do shell-style pattern matching for ?, \, [], and * characters.
  3. **  Might not be robust in face of malformed patterns; e.g., "foo[a-"
  4. **  could cause a segmentation violation.
  5. **
  6. **  Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
  7. */
  8.  
  9. /* I had to modify this by renaming TRUE to VRAI and FALSE to FAUX.  The
  10. originals were causing warnings, so I just renamed them rather than spend
  11. a long time hunting down the problem.  It works, so what the heck?  I might
  12. scrap this whole routine in a later version as I don't like its case sen-
  13. sitivity. -KK */
  14.  
  15. void NoCase();
  16.  
  17. #define VRAI        1
  18. #define FAUX        0
  19.  
  20. static int
  21. Star(s, p)
  22.     register char    *s;
  23.     register char    *p;
  24. {
  25.     while (wildmat(s, p) == FAUX)
  26.     if (*++s == '\0')
  27.         return(FAUX);
  28.     return(VRAI);
  29. }
  30.  
  31.  
  32. int
  33. wildmat(s, p)
  34.     register char    *s;
  35.     register char    *p;
  36. {
  37.     register int      last;
  38.     register int      matched;
  39.     register int      reverse;
  40.  
  41.     NoCase(s);
  42.     NoCase(p);
  43.  
  44.     for ( ; *p; s++, p++)
  45.     switch (*p) {
  46.         case '\\':
  47.         /* Literal match with following character; fall through. */
  48.         p++;
  49.         default:
  50.         if (*s != *p)
  51.             return(FAUX);
  52.         continue;
  53.         case '?':
  54.         /* Match anything. */
  55.         if (*s == '\0')
  56.             return(FAUX);
  57.         continue;
  58.         case '*':
  59.         /* Trailing star matches everything. */
  60.         return(*++p ? Star(s, p) : VRAI);
  61.         case '[':
  62.         /* [^....] means inverse character class. */
  63.         if (reverse = p[1] == '^')
  64.             p++;
  65.         for (last = 0400, matched = FAUX; *++p && *p != ']'; last = *p)
  66.             /* This next line requires a good C compiler. */
  67.             if (*p == '-' ? *s <= *++p && *s >= last : *s == *p)
  68.             matched = VRAI;
  69.         if (matched == reverse)
  70.             return(FAUX);
  71.         continue;
  72.     }
  73.  
  74.     return(*s == '\0');
  75. }
  76.  
  77.  
  78. void NoCase(instring)  
  79. char *instring;
  80. {
  81.     while(*instring != 0)
  82.     {
  83.         *instring = tolower(*instring);
  84.         instring++;
  85.     }
  86. }
  87.