home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip51.zip / match.c < prev    next >
C/C++ Source or Header  |  1994-01-11  |  10KB  |  279 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   match.c
  4.  
  5.   The match() routine recursively compares a string to a "pattern" (regular
  6.   expression), returning TRUE if a match is found or FALSE if not.  This
  7.   version is specifically for use with unzip.c:  as did the previous match()
  8.   routines from SEA and J. Kercheval, it leaves the case (upper, lower, or
  9.   mixed) of the string alone, but converts any uppercase characters in the
  10.   pattern to lowercase if indicated by the global var pInfo->lcflag (which
  11.   is to say, string is assumed to have been converted to lowercase already,
  12.   if such was necessary).
  13.  
  14.   GRR:  reversed order of text, pattern in matche() (now same as match());
  15.         added ignore_case/ic flags, Case() macro.
  16.  
  17.   PK:   replaced matche() with recmatch() from Zip, modified to have an
  18.         ignore_case argument; replaced test frame with simpler one.
  19.  
  20.   ---------------------------------------------------------------------------
  21.  
  22.   Copyright on recmatch() from Zip's util.c (although recmatch() was almost
  23.   certainly written by Mark Adler...ask me how I can tell :-) ):
  24.  
  25.      Copyright (C) 1990-1992 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  26.      Kai Uwe Rommel and Igor Mandrichenko.
  27.  
  28.      Permission is granted to any individual or institution to use, copy,
  29.      or redistribute this software so long as all of the original files are
  30.      included unmodified, that it is not sold for profit, and that this copy-
  31.      right notice is retained.
  32.  
  33.   ---------------------------------------------------------------------------
  34.  
  35.   Match the pattern (wildcard) against the string (fixed):
  36.  
  37.      match(string, pattern, ignore_case);
  38.  
  39.   returns TRUE if string matches pattern, FALSE otherwise.  In the pattern:
  40.  
  41.      `*' matches any sequence of characters (zero or more)
  42.      `?' matches any character
  43.      [SET] matches any character in the specified set,
  44.      [!SET] or [^SET] matches any character not in the specified set.
  45.  
  46.   A set is composed of characters or ranges; a range looks like ``character
  47.   hyphen character'' (as in 0-9 or A-Z).  [0-9a-zA-Z_] is the minimal set of
  48.   characters allowed in the [..] pattern construct.  Other characters are
  49.   allowed (ie. 8 bit characters) if your system will support them.
  50.  
  51.   To suppress the special syntactic significance of any of ``[]*?!^-\'', in-
  52.   side or outside a [..] construct and match the character exactly, precede
  53.   it with a ``\'' (backslash).
  54.  
  55.   Note that "*.*" and "*." are treated specially under MS-DOS if DOSWILD is
  56.   defined.  See the DOSWILD section below for an explanation.
  57.  
  58.   ---------------------------------------------------------------------------*/
  59.  
  60.  
  61.  
  62. #include "unzip.h"    /* define ToLower() in here (for Unix, define ToLower
  63.                        * to be macro (using isupper()); otherwise just use
  64.                        * tolower() */
  65.  
  66. #if 0  /* this is not useful until it matches Amiga names insensitively */
  67. #ifdef AMIGA        /* some other platforms might also want to use this */
  68. #  define ANSI_CHARSET       /* MOVE INTO UNZIP.H EVENTUALLY */
  69. #endif
  70. #endif /* 0 */
  71.   
  72. #ifdef ANSI_CHARSET
  73. #  ifdef ToLower
  74. #    undef ToLower
  75. #  endif
  76.    /* uppercase letters are values 41 thru 5A, C0 thru D6, and D8 thru DE */
  77. #  define IsUpper(c) (c>=0xC0 ? c<=0xDE && c!=0xD7 : c>=0x41 && c<=0x5A)
  78. #  define ToLower(c) (IsUpper((uch) c) ? (unsigned) c | 0x20 : (unsigned) c)
  79. #endif
  80. #define Case(x)  (ic? ToLower(x) : (x))
  81.  
  82. #if 0                /* GRR:  add this to unzip.h someday... */
  83. #if !(defined(MSDOS) && defined(DOSWILD))
  84. #define match(s,p,ic)   (recmatch((uch *)p,(uch *)s,ic) == 1)
  85. int recmatch OF((uch *pattern, uch *string, int ignore_case));
  86. #endif
  87. #endif /* 0 */
  88. static int recmatch OF((uch *pattern, uch *string, int ignore_case));
  89.  
  90.  
  91.  
  92. /* match() is a shell to recmatch() to return only Boolean values. */
  93.  
  94. int match(string, pattern, ignore_case)
  95.     char *string, *pattern;
  96.     int ignore_case;
  97. {
  98. #if (defined(MSDOS) && defined(DOSWILD))
  99.     char *dospattern;
  100.     int j = strlen(pattern);
  101.  
  102. /*---------------------------------------------------------------------------
  103.     Optional MS-DOS preprocessing section:  compare last three chars of the
  104.     wildcard to "*.*" and translate to "*" if found; else compare the last
  105.     two characters to "*." and, if found, scan the non-wild string for dots.
  106.     If in the latter case a dot is found, return failure; else translate the
  107.     "*." to "*".  In either case, continue with the normal (Unix-like) match
  108.     procedure after translation.  (If not enough memory, default to normal
  109.     match.)  This causes "a*.*" and "a*." to behave as MS-DOS users expect.
  110.   ---------------------------------------------------------------------------*/
  111.  
  112.     if ((dospattern = (char *)malloc(j+1)) != NULL) {
  113.         strcpy(dospattern, pattern);
  114.         if (!strcmp(dospattern+j-3, "*.*")) {
  115.             dospattern[j-2] = '\0';                    /* nuke the ".*" */
  116.         } else if (!strcmp(dospattern+j-2, "*.")) {
  117.             char *p = strchr(string, '.');
  118.  
  119.             if (p) {   /* found a dot:  match fails */
  120.                 free(dospattern);
  121.                 return 0;
  122.             }
  123.             dospattern[j-1] = '\0';                    /* nuke the end "." */
  124.         }
  125.         j = recmatch((uch *)dospattern, (uch *)string, ignore_case);
  126.         free(dospattern);
  127.         return j == 1;
  128.     } else
  129. #endif /* MSDOS && DOSWILD */
  130.     return recmatch((uch *)pattern, (uch *)string, ignore_case) == 1;
  131. }
  132.  
  133.  
  134.  
  135. static int recmatch(p, s, ic)
  136.     uch *p;               /* sh pattern to match */
  137.     uch *s;               /* string to which to match it */
  138.     int ic;               /* true for case insensitivity */
  139. /* Recursively compare the sh pattern p with the string s and return 1 if
  140.  * they match, and 0 or 2 if they don't or if there is a syntax error in the
  141.  * pattern.  This routine recurses on itself no more deeply than the number
  142.  * of characters in the pattern. */
  143. {
  144.     unsigned int c;       /* pattern char or start of range in [-] loop */ 
  145.  
  146.     /* Get first character, the pattern for new recmatch calls follows */
  147.     c = *p++;
  148.  
  149.     /* If that was the end of the pattern, match if string empty too */
  150.     if (c == 0)
  151.         return *s == 0;
  152.  
  153.     /* '?' (or '%') matches any character (but not an empty string) */
  154. #ifdef VMS
  155.     if (c == '%')         /* GRR:  make this conditional, too? */
  156. #else /* !VMS */
  157.     if (c == '?')
  158. #endif /* ?VMS */
  159.         return *s ? recmatch(p, s + 1, ic) : 0;
  160.  
  161.     /* '*' matches any number of characters, including zero */
  162. #ifdef AMIGA
  163.     if (c == '#' && *p == '?')     /* "#?" is Amiga-ese for "*" */
  164.         c = '*', p++;
  165. #endif /* AMIGA */
  166.     if (c == '*') {
  167.         if (*p == 0)
  168.             return 1;
  169.         for (; *s; s++)
  170.             if ((c = recmatch(p, s, ic)) != 0)
  171.                 return (int)c;
  172.         return 2;       /* 2 means give up--match will return false */
  173.     }
  174.  
  175.     /* Parse and process the list of characters and ranges in brackets */
  176.     if (c == '[') {
  177.         int e;          /* flag true if next char to be taken literally */
  178.         uch *q;         /* pointer to end of [-] group */
  179.         int r;          /* flag true to match anything but the range */
  180.  
  181.         if (*s == 0)                           /* need a character to match */
  182.             return 0;
  183.         p += (r = (*p == '!' || *p == '^'));   /* see if reverse */
  184.         for (q = p, e = 0; *q; q++)            /* find closing bracket */
  185.             if (e)
  186.                 e = 0;
  187.             else
  188.                 if (*q == '\\')      /* GRR:  change to ^ for MS-DOS, OS/2? */
  189.                     e = 1;
  190.                 else if (*q == ']')
  191.                     break;
  192.         if (*q != ']')               /* nothing matches if bad syntax */
  193.             return 0;
  194.         for (c = 0, e = *p == '-'; p < q; p++) {  /* go through the list */
  195.             if (e == 0 && *p == '\\')             /* set escape flag if \ */
  196.                 e = 1;
  197.             else if (e == 0 && *p == '-')         /* set start of range if - */
  198.                 c = *(p-1);
  199.             else {
  200.                 unsigned int cc = Case(*s);
  201.  
  202.                 if (*(p+1) != '-')
  203.                     for (c = c ? c : *p; c <= *p; c++)  /* compare range */
  204.                         if (Case(c) == cc)
  205.                             return r ? 0 : recmatch(q + 1, s + 1, ic);
  206.                 c = e = 0;   /* clear range, escape flags */
  207.             }
  208.         }
  209.         return r ? recmatch(q + 1, s + 1, ic) : 0;  /* bracket match failed */
  210.     }
  211.  
  212.     /* if escape ('\'), just compare next character */
  213.     if (c == '\\' && (c = *p++) == 0)     /* if \ at end, then syntax error */
  214.         return 0;
  215.  
  216.     /* just a character--compare it */
  217.     return Case((uch)c) == Case(*s) ? recmatch(p, ++s, ic) : 0;
  218.  
  219. } /* end function recmatch() */
  220.  
  221.  
  222.  
  223.  
  224. #ifdef WILD_STAT_BUG   /* Turbo/Borland C, Watcom C, VAX C, Atari MiNT libs */
  225.  
  226. int iswild(p)
  227.     char *p;
  228. {
  229.     for (; *p; ++p)
  230.         if (*p == '\\' && *(p+1))
  231.             ++p;
  232. #ifdef VMS
  233.         else if (*p == '%' || *p == '*')
  234. #else /* !VMS */
  235. #ifdef AMIGA
  236.         else if (*p == '?' || *p == '*' || (*p=='#' && p[1]=='?') || *p == '[')
  237. #else /* !AMIGA */
  238.         else if (*p == '?' || *p == '*' || *p == '[')
  239. #endif /* ?AMIGA */
  240. #endif /* ?VMS */
  241.             return TRUE;
  242.  
  243.     return FALSE;
  244.  
  245. } /* end function iswild() */
  246.  
  247. #endif /* WILD_STAT_BUG */
  248.  
  249.  
  250.  
  251.  
  252. #ifdef TEST_MATCH
  253.  
  254. #define put(s) { fputs(s, stdout); fflush(stdout); }
  255.  
  256. void main(void)
  257. {
  258.     char pat[256], str[256];
  259.  
  260.     for (;;) {
  261.         put("Pattern (return to exit): ");
  262.         gets(pat);
  263.         if (!pat[0])
  264.             break;
  265.         for (;;) {
  266.             put("String (return for new pattern): ");
  267.             gets(str);
  268.             if (!str[0])
  269.                 break;
  270.             printf("Case sensitive: %s  insensitive: %s\n",
  271.               match(str, pat, 0) ? "YES" : "NO",
  272.               match(str, pat, 1) ? "YES" : "NO");
  273.         }
  274.     }
  275.     exit(0);
  276. }
  277.  
  278. #endif /* TEST_MATCH */
  279.