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