home *** CD-ROM | disk | FTP | other *** search
- /*
- strmask.c 4/11/90 by mla
-
- % strmask
-
- OWL 1.2
- Copyright (c) 1990 by Oakland Group, Inc.
- ALL RIGHTS RESERVED.
-
- Revision History:
- -----------------
- 4/11/90 jmd removed revision history (then put it back for this message)
- 6/13/90 jdc preened
- */
-
-
- #include "oakhead.h"
- #include "strdecl.h"
- #include <ctype.h>
-
- #define STAR 0
- #define NOTSTAR 1
- #define RESET 2
-
- boolean strmask(char *str, char *mask)
- /*
- Tests string 'str' against mask string 'mask'
- Returns TRUE if the string matches the mask.
-
- The mask can contain '?' and '*' wild card characters.
- '?' matches any single character.
- '*' matches any number of any characters.
-
- For example:
- strmask("Hello", "Hello"); ---> TRUE
- strmask("Hello", "Jello"); ---> FALSE
- strmask("Hello", "H*o"); ---> TRUE
- strmask("Hello", "H*g"); ---> FALSE
- strmask("Hello", "?ello"); ---> FALSE
- strmask("Hello", "H????"); ---> TRUE
- strmask("H", "H????"); ---> FALSE
- */
- {
- char *sp, *mp, *reset_string, *reset_mask, *sn;
- int state;
-
- sp = str;
- mp = mask;
-
- while (1) {
- switch (*mp) {
- case '\0':
- return(*sp ? FALSE : TRUE);
- case '?':
- sp++;
- mp++;
- break;
- default:
- if (*mp == *sp) {
- sp++;
- mp++;
- break;
- }
- else {
- return(FALSE);
- }
- case '*':
- if (*(mp + 1) == '\0') {
- return(TRUE);
- }
- if ((sn = strchr(sp, *(mp + 1))) == NULL) {
- return(FALSE);
- }
-
- /* save place -- match rest of string */
- /* if fail, reset to here */
- reset_mask = mp;
- reset_string = sn + 1;
-
- mp = mp + 2;
- sp = sn + 1;
- state = NOTSTAR;
- while (state == NOTSTAR) {
- switch (*mp) {
- case '\0':
- if (*sp == '\0') {
- return(TRUE);
- }
- else {
- state = RESET;
- }
- break;
- case '?':
- sp++;
- mp++;
- break;
- default:
- if (*mp == *sp) {
- sp++;
- mp++;
- }
- else {
- state = RESET;
- }
- break;
- case '*':
- state = STAR;
- break;
- }
- }
- /* we've reach a new star or should reset to last star */
- if (state == RESET) {
- sp = reset_string;
- mp = reset_mask;
- }
- break;
- }
- }
- }
-