match.c
Upload User: andy_li
Upload Date: 2007-01-06
Package Size: 1019k
Code Size: 10k
Development Platform:

MultiPlatform

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