home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / CSAPE32.ARJ / SOURCE / OWLSCR / STRMASK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-14  |  2.2 KB  |  120 lines

  1. /*
  2.     strmask.c      4/11/90 by mla
  3.  
  4.     % strmask
  5.  
  6.     OWL 1.2
  7.     Copyright (c) 1990 by Oakland Group, Inc.
  8.     ALL RIGHTS RESERVED.
  9.  
  10.     Revision History:
  11.     -----------------
  12.      4/11/90 jmd    removed revision history (then put it back for this message)
  13.      6/13/90 jdc    preened
  14. */
  15.  
  16.  
  17. #include "oakhead.h"
  18. #include "strdecl.h"
  19. #include <ctype.h>
  20.  
  21. #define    STAR    0
  22. #define    NOTSTAR    1
  23. #define    RESET    2
  24.  
  25. boolean strmask(char *str, char *mask)
  26. /*
  27.     Tests string 'str' against mask string 'mask'
  28.     Returns TRUE if the string matches the mask.
  29.  
  30.     The mask can contain '?' and '*' wild card characters.
  31.     '?' matches any    single character.
  32.     '*' matches any number of any characters.
  33.  
  34.     For example:
  35.                     strmask("Hello", "Hello");    ---> TRUE
  36.                     strmask("Hello", "Jello");    ---> FALSE
  37.                     strmask("Hello", "H*o");    ---> TRUE
  38.                     strmask("Hello", "H*g");    ---> FALSE
  39.                     strmask("Hello", "?ello");    ---> FALSE
  40.                     strmask("Hello", "H????");    ---> TRUE
  41.                     strmask("H", "H????");        ---> FALSE
  42. */
  43. {
  44.     char *sp, *mp, *reset_string, *reset_mask, *sn;
  45.     int state;
  46.  
  47.     sp = str;
  48.     mp = mask;
  49.  
  50.     while (1) {
  51.         switch (*mp) {
  52.         case '\0':
  53.             return(*sp ? FALSE : TRUE);
  54.         case '?':
  55.             sp++;
  56.             mp++;
  57.             break;
  58.         default:
  59.             if (*mp == *sp) {
  60.                 sp++;
  61.                 mp++;
  62.                 break;
  63.             }
  64.             else {
  65.                 return(FALSE);
  66.             }
  67.         case '*':
  68.             if (*(mp + 1) == '\0') {
  69.                 return(TRUE);
  70.             }
  71.             if ((sn = strchr(sp, *(mp + 1))) == NULL) {
  72.                 return(FALSE);
  73.             }
  74.  
  75.             /* save place -- match rest of string */
  76.             /* if fail, reset to here */
  77.             reset_mask = mp;
  78.             reset_string = sn + 1;
  79.  
  80.             mp = mp + 2;
  81.             sp = sn + 1;
  82.             state = NOTSTAR;
  83.             while (state == NOTSTAR) {
  84.                 switch (*mp) {
  85.                 case '\0':
  86.                     if (*sp == '\0') {
  87.                         return(TRUE);
  88.                     }
  89.                     else {
  90.                         state = RESET;
  91.                     }
  92.                     break;
  93.                 case '?':
  94.                     sp++;
  95.                     mp++;
  96.                     break;
  97.                 default:
  98.                     if (*mp == *sp) {
  99.                         sp++;
  100.                         mp++;
  101.                     }
  102.                     else {
  103.                         state = RESET;
  104.                     }
  105.                     break;
  106.                 case '*':
  107.                     state = STAR;
  108.                     break;
  109.                 }
  110.             }
  111.             /* we've reach a new star or should reset to last star */
  112.             if (state == RESET) {
  113.                 sp = reset_string;
  114.                 mp = reset_mask;
  115.             }
  116.             break;
  117.         }
  118.     }
  119. }
  120.