home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_01_04
/
1n04022a
< prev
next >
Wrap
Text File
|
1990-07-18
|
3KB
|
62 lines
/*
** Figure 4 - Wilcard pattern matching
*/
#include <stddef.h>
#include <string.h>
/************************************************************************/
/* */
/* patmat() */
/* */
/* Usage : Pass two string pointers as parameters.The first */
/* being a raw string & the second a pattern the raw */
/* string is to be matched against.If the raw string */
/* matches the pattern,then the function returns a */
/* 1,else it returns a 0. */
/* */
/* e.g patmat("abcdefghi","*ghi") returns a 1. */
/* patmat("abcdefghi","??c??f*") returns a 1. */
/* patmat("abcdefghi","*dh*") returns a 0. */
/* patmat("abcdefghi","*def") returns a 0. */
/* */
/* The asterisk is a wild card to allow any charac- */
/* ters from its first appearance to the next spec- */
/* ific character.The character ? is a wild card */
/* for only one character in the position it appears. */
/* */
/* From public domain sources via FidoNet. */
/* */
/************************************************************************/
int patmat(const char *raw, const char *pat)
{
int i ;
if ((*pat == '\0') && (*raw == '\0')) /* if it is end of both */
return( 1 ) ; /* strings,then match */
if (*pat == '\0') /* if it is end of only */
return( 0 ) ; /* pat then mismatch */
if (*pat == '*') /* if pattern is a '*' */
{
if (*(pat+1) == '\0') /* if it is end of pat */
return(1); /* then match */
for(i = 0; i <= strlen(raw); i++) /* else hunt for match */
if ((*(raw+i) == *(pat+1)) || /* or wild card */
(*(pat+1) == '?'))
/* if found,match */
if (patmat(raw + i + 1, pat + 2) == 1)
return(1); /* rest of pat */
}
else
{
if (*raw == '\0') /* if end of raw then */
return(0); /* mismatch */
if ((*pat == '?') || (*pat == *raw))/* if chars match then */
/* try & match rest of it */
if (patmat(raw + 1, pat + 1) == 1)
return(1);
}
return(0); /* no match found */
}