home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip511.zip / match.c < prev    next >
C/C++ Source or Header  |  1994-04-22  |  10KB  |  281 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.   PaulK:  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 single 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 (i.e., 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.  Note also
  57.   that with VMSWILD defined, '%' is used instead of '?', and sets (ranges)
  58.   are disallowed.
  59.  
  60.   ---------------------------------------------------------------------------*/
  61.  
  62.  
  63.  
  64. /* define ToLower() in here (for Unix, define ToLower to be macro (using
  65.  * isupper()); otherwise just use tolower() */
  66. #include "unzip.h"
  67.  
  68. #if 0  /* this is not useful until it matches Amiga names insensitively */
  69. #ifdef AMIGA        /* some other platforms might also want to use this */
  70. #  define ANSI_CHARSET       /* MOVE INTO UNZIP.H EVENTUALLY */
  71. #endif
  72. #endif /* 0 */
  73.   
  74. #ifdef ANSI_CHARSET
  75. #  ifdef ToLower
  76. #    undef ToLower
  77. #  endif
  78.    /* uppercase letters are values 41 thru 5A, C0 thru D6, and D8 thru DE */
  79. #  define IsUpper(c) (c>=0xC0 ? c<=0xDE && c!=0xD7 : c>=0x41 && c<=0x5A)
  80. #  define ToLower(c) (IsUpper((uch) c) ? (unsigned) c | 0x20 : (unsigned) c)
  81. #endif
  82. #define Case(x)  (ic? ToLower(x) : (x))
  83.  
  84. #if 0                /* GRR:  add this to unzip.h someday... */
  85. #if !(defined(MSDOS) && defined(DOSWILD))
  86. #define match(s,p,ic)   (recmatch((uch *)p,(uch *)s,ic) == 1)
  87. int recmatch OF((uch *pattern, uch *string, int ignore_case));
  88. #endif
  89. #endif /* 0 */
  90. static int recmatch OF((uch *pattern, uch *string, int ignore_case));
  91.  
  92.  
  93.  
  94. /* match() is a shell to recmatch() to return only Boolean values. */
  95.  
  96. int match(string, pattern, ignore_case)
  97.     char *string, *pattern;
  98.     int ignore_case;
  99. {
  100. #if (defined(MSDOS) && defined(DOSWILD))
  101.     char *dospattern;
  102.     int j = strlen(pattern);
  103.  
  104. /*---------------------------------------------------------------------------
  105.     Optional MS-DOS preprocessing section:  compare last three chars of the
  106.     wildcard to "*.*" and translate to "*" if found; else compare the last
  107.     two characters to "*." and, if found, scan the non-wild string for dots.
  108.     If in the latter case a dot is found, return failure; else translate the
  109.     "*." to "*".  In either case, continue with the normal (Unix-like) match
  110.     procedure after translation.  (If not enough memory, default to normal
  111.     match.)  This causes "a*.*" and "a*." to behave as MS-DOS users expect.
  112.   ---------------------------------------------------------------------------*/
  113.  
  114.     if ((dospattern = (char *)malloc(j+1)) != NULL) {
  115.         strcpy(dospattern, pattern);
  116.         if (!strcmp(dospattern+j-3, "*.*")) {
  117.             dospattern[j-2] = '\0';                    /* nuke the ".*" */
  118.         } else if (!strcmp(dospattern+j-2, "*.")) {
  119.             char *p = strchr(string, '.');
  120.  
  121.             if (p) {   /* found a dot:  match fails */
  122.                 free(dospattern);
  123.                 return 0;
  124.             }
  125.             dospattern[j-1] = '\0';                    /* nuke the end "." */
  126.         }
  127.         j = recmatch((uch *)dospattern, (uch *)string, ignore_case);
  128.         free(dospattern);
  129.         return j == 1;
  130.     } else
  131. #endif /* MSDOS && DOSWILD */
  132.     return recmatch((uch *)pattern, (uch *)string, ignore_case) == 1;
  133. }
  134.  
  135.  
  136.  
  137. static int recmatch(p, s, ic)
  138.     uch *p;               /* sh pattern to match */
  139.     uch *s;               /* string to which to match it */
  140.     int ic;               /* true for case insensitivity */
  141. /* Recursively compare the sh pattern p with the string s and return 1 if
  142.  * they match, and 0 or 2 if they don't or if there is a syntax error in the
  143.  * pattern.  This routine recurses on itself no more deeply than the number
  144.  * of characters in the pattern. */
  145. {
  146.     unsigned int c;       /* pattern char or start of range in [-] loop */ 
  147.  
  148.     /* Get first character, the pattern for new recmatch calls follows */
  149.     c = *p++;
  150.  
  151.     /* If that was the end of the pattern, match if string empty too */
  152.     if (c == 0)
  153.         return *s == 0;
  154.  
  155.     /* '?' (or '%') matches any character (but not an empty string) */
  156. #ifdef VMSWILD
  157.     if (c == '%')
  158. #else
  159.     if (c == '?')
  160. #endif
  161.         return *s ? recmatch(p, s + 1, ic) : 0;
  162.  
  163.     /* '*' matches any number of characters, including zero */
  164. #ifdef AMIGA
  165.     if (c == '#' && *p == '?')     /* "#?" is Amiga-ese for "*" */
  166.         c = '*', p++;
  167. #endif /* AMIGA */
  168.     if (c == '*') {
  169.         if (*p == 0)
  170.             return 1;
  171.         for (; *s; s++)
  172.             if ((c = recmatch(p, s, ic)) != 0)
  173.                 return (int)c;
  174.         return 2;       /* 2 means give up--match will return false */
  175.     }
  176.  
  177. #ifndef VMSWILD
  178.     /* Parse and process the list of characters and ranges in brackets */
  179.     if (c == '[') {
  180.         int e;          /* flag true if next char to be taken literally */
  181.         uch *q;         /* pointer to end of [-] group */
  182.         int r;          /* flag true to match anything but the range */
  183.  
  184.         if (*s == 0)                           /* need a character to match */
  185.             return 0;
  186.         p += (r = (*p == '!' || *p == '^'));   /* see if reverse */
  187.         for (q = p, e = 0; *q; q++)            /* find closing bracket */
  188.             if (e)
  189.                 e = 0;
  190.             else
  191.                 if (*q == '\\')      /* GRR:  change to ^ for MS-DOS, OS/2? */
  192.                     e = 1;
  193.                 else if (*q == ']')
  194.                     break;
  195.         if (*q != ']')               /* nothing matches if bad syntax */
  196.             return 0;
  197.         for (c = 0, e = *p == '-'; p < q; p++) {  /* go through the list */
  198.             if (e == 0 && *p == '\\')             /* set escape flag if \ */
  199.                 e = 1;
  200.             else if (e == 0 && *p == '-')         /* set start of range if - */
  201.                 c = *(p-1);
  202.             else {
  203.                 unsigned int cc = Case(*s);
  204.  
  205.                 if (*(p+1) != '-')
  206.                     for (c = c ? c : *p; c <= *p; c++)  /* compare range */
  207.                         if (Case(c) == cc)
  208.                             return r ? 0 : recmatch(q + 1, s + 1, ic);
  209.                 c = e = 0;   /* clear range, escape flags */
  210.             }
  211.         }
  212.         return r ? recmatch(q + 1, s + 1, ic) : 0;  /* bracket match failed */
  213.     }
  214. #endif /* !VMSWILD */
  215.  
  216.     /* if escape ('\'), just compare next character */
  217.     if (c == '\\' && (c = *p++) == 0)     /* if \ at end, then syntax error */
  218.         return 0;
  219.  
  220.     /* just a character--compare it */
  221.     return Case((uch)c) == Case(*s) ? recmatch(p, ++s, ic) : 0;
  222.  
  223. } /* end function recmatch() */
  224.  
  225.  
  226.  
  227.  
  228.  
  229. int iswild(p)        /* originally only used for stat()-bug workaround in */
  230.     char *p;         /*  VAX C, Turbo/Borland C, Watcom C, Atari MiNT libs; */
  231. {                    /*  now used in process_zipfiles() as well */
  232.     for (; *p; ++p)
  233.         if (*p == '\\' && *(p+1))
  234.             ++p;
  235. #ifdef VMS
  236.         else if (*p == '%' || *p == '*')
  237. #else /* !VMS */
  238. #ifdef AMIGA
  239.         else if (*p == '?' || *p == '*' || (*p=='#' && p[1]=='?') || *p == '[')
  240. #else /* !AMIGA */
  241.         else if (*p == '?' || *p == '*' || *p == '[')
  242. #endif /* ?AMIGA */
  243. #endif /* ?VMS */
  244.             return TRUE;
  245.  
  246.     return FALSE;
  247.  
  248. } /* end function iswild() */
  249.  
  250.  
  251.  
  252.  
  253.  
  254. #ifdef TEST_MATCH
  255.  
  256. #define put(s) {fputs(s,stdout); fflush(stdout);}
  257.  
  258. void main(void)
  259. {
  260.     char pat[256], str[256];
  261.  
  262.     for (;;) {
  263.         put("Pattern (return to exit): ");
  264.         gets(pat);
  265.         if (!pat[0])
  266.             break;
  267.         for (;;) {
  268.             put("String (return for new pattern): ");
  269.             gets(str);
  270.             if (!str[0])
  271.                 break;
  272.             printf("Case sensitive: %s  insensitive: %s\n",
  273.               match(str, pat, 0) ? "YES" : "NO",
  274.               match(str, pat, 1) ? "YES" : "NO");
  275.         }
  276.     }
  277.     exit(0);
  278. }
  279.  
  280. #endif /* TEST_MATCH */
  281.