home *** CD-ROM | disk | FTP | other *** search
/ RBBS in a Box Volume 1 #2 / RBBS_vol1_no2.iso / add2 / fc_v003.exe / WILDCARD.C < prev   
Text File  |  1989-04-24  |  4KB  |  128 lines

  1. /****************************************************************************
  2.  *
  3.  *                              WILDCARD.C
  4.  *
  5.  *                      Wildcard matching routines ...
  6.  *
  7.  *                          By: Ronald C. Bieber
  8.  *
  9.  ***************************************************************************/
  10. #include <stdio.h>
  11. #include <dos.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14.  
  15. #define REST    '*'
  16. #define ANY     '?'
  17.  
  18. #define CMP(x, y)               (toupper((x)) == toupper((y)) ? 1 : 0)
  19.  
  20. struct  nmblk {                 /* filename structure used by wildcard */
  21.         char    node[8];
  22.         char    ext[3];
  23. };
  24. /******************************************************************************
  25.  *
  26.  *          Name:   parsname
  27.  *      Synopsis:   void parsname(str, pattern);
  28.  *                  struct nmblk    *str;          structure to hold name
  29.  *                  char            *pattern;      name to store in nmblk
  30.  *
  31.  *   Description:   parses the filename given in *pattern and stores it
  32.  *                  in the structure nmblk, seperating the name and 
  33.  *                  extension.
  34.  *
  35.  ****************************************************************************/
  36. int parsname(str, pattern)
  37. struct  nmblk   *str;
  38. char            *pattern;
  39. {
  40.     int     rest = -1,
  41.             cmp;
  42.  
  43.     if (strchr(pattern, '.') == NULL) {
  44.         strcpy(str->node, pattern);
  45.         str->ext[0] = '*';
  46.         str->ext[1] = 0;
  47.         return(0);
  48.     }
  49.  
  50.     while (++rest <= strlen(pattern))
  51.         if (pattern[rest] == '.')
  52.             cmp = rest;
  53.  
  54.     for (rest = 0; rest < cmp; rest++)
  55.         str->node[rest] = pattern[rest];
  56.  
  57.     str->node[rest] = 0;                /* append NULL to end */
  58.  
  59.     rest = 0;
  60.     cmp++;
  61.     while(pattern[cmp + rest] != 0)
  62.         str->ext[rest] = pattern[cmp + rest++];
  63.  
  64.     str->ext[rest] = 0;                 /* append NULL to end */
  65. }
  66. /******************************************************************************
  67.  *
  68.  *          Name:   matchwc
  69.  *      Synopsis:   int matchwc(first, second);
  70.  *                  char    *first;             first string to compare 
  71.  *                  char    *second;            2nd string to compare
  72.  *
  73.  *   Description:   This function does the actual wildcard matching.
  74.  *                  
  75.  *       Returns:   True if a match was found, false if not.
  76.  *
  77.  ****************************************************************************/
  78. int matchwc (first, second)
  79. char    *first;
  80. char    *second;
  81. {
  82.     char    *p,
  83.             *l,
  84.             c;
  85.     int     i = -1;
  86.  
  87.     p = first;
  88.     l = second;
  89.  
  90.     while ( (c = p[++i]) != NULL || l[i] != NULL) {
  91.         switch(c) {
  92.             case ANY:   break;
  93.             case REST:  return(1);
  94.               default:  if (!CMP(l[i], p[i]))
  95.                             return(0);
  96.         }
  97.     }
  98.     return(1);
  99. }
  100. /******************************************************************************
  101.  *
  102.  *          Name:   wildcards
  103.  *      Synopsis:   int wildcards(pattern, to);
  104.  *                  char    *pattern;       name to match (*, ? included)
  105.  *                  char    *to;            filename to match it to
  106.  *
  107.  *   Description:   User called function to match wildcard characters
  108.  *                  with a "for-real" type filename.
  109.  *                  
  110.  *       Returns:   True if a match was found, false if not.
  111.  ****************************************************************************/
  112. int wildcards(pattern, to)
  113. char    pattern[14];
  114. char    to[14];
  115. {
  116.     struct  nmblk   first,
  117.                     second;
  118.  
  119.     parsname(&first, pattern);
  120.     parsname(&second, to);
  121.  
  122.     if (matchwc(first.node, second.node) && matchwc(first.ext, second.ext))
  123.         return(1);
  124.     else
  125.         return(0);
  126.  
  127. }
  128.