axsoft
版主
![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif)
![](http://earthday.link.org.tw/images/EARTHfinal.gif) 發表:681 回覆:1056 積分:969 註冊:2002-03-13
發送簡訊給我
|
以萬用字元比較字串 作者:Bob Stout
以萬用字元比較字串,以*和?比較字串是否符合,很適合用在檔名的處理. ![]() < class="code">
/* Date last modified: 05-Jul-1997 */ /*
** XSTRCMP.C - Simple string pattern matching functions using DOS
** wildcards ('?' & '*').
**
** Derived from code by Arjan Kentner (submitted by Steve Summit),
** modified by Bob Stout.
*/ #include < stdio.h > /* For NULL */
#include < ctype.h > /* For toupper() */
//#include < assert.h > /* For assert() */
//#include "sniptype.h" /* For Boolean_T */
//#include "dirent.h" /* Validate prototypes, also
// includes extkword.h for */
//#define NDEBUG
#define Boolean_T int
#define Error_ -1
#define TEST /*
** Arguments: 1 - Pattern to match
** 2 - String to match
**
** Returns: True_ if match
** False_ if no match
** Error_ if passed a null pointer (see below)
**
** Notes: 1. Two versions are supplied, one case-sensitive and one not.
** 2. Each version consists of a recursive static function which
** does all the work, and a wrapper which checks for null
** pointers before calling the static function.
** 3. If assert() is enabled (i.e. if NDEBUG is undefined or false),
** the wrapper functions will abort with an assertion error in
** case a null pointer is passed.
** 4. If assert() is disabled (i.e. if NDEBUG is defined), the
** wrapper function will return Error_ to the calling program in
** case a null pointer is passed.
*/ /*
** Case-sensitive version
*/ static Boolean_T patmat (const char *pat, const char *str)
{
switch (*pat)
{
case '\0':
return !*str; case '*' :
return patmat(pat 1, str) || (*str && patmat(pat, str 1)); case '?' :
return *str && patmat(pat 1, str 1); default :
return (*pat == *str) && patmat(pat 1, str 1);
}
} Boolean_T xstrcmp (const char *pat, const char *str)
{
// assert(str && pat);
if (NULL == str || NULL == pat)
return Error_;
else return(patmat(pat, str));
} /*
** Case-insensitive version
*/ static Boolean_T patimat (const char *pat, const char *str)
{
switch (*pat)
{
case '\0':
return !*str; case '*' :
return patimat(pat 1, str) || (*str && patimat(pat, str 1)); case '?' :
return *str && patimat(pat 1, str 1); default :
return (toupper(*pat) == toupper(*str)) && patimat(pat 1, str 1);
}
} Boolean_T xstricmp (const char *pat, const char *str)
{
// assert(str && pat);
if (NULL == str || NULL == pat)
return Error_;
else return(patimat(pat, str));
} #ifdef TEST #include main(int argc, char *argv[])
{
if (3 != argc)
{
puts("Usage: XSTRCMP mask string");
return -1;
}
printf("xstrcmp(\"%s\", \"%s\") returned %d\n", argv[1], argv[2],
xstrcmp(argv[1], argv[2])); printf("xstricmp(\"%s\", \"%s\") returned %d\n", argv[1], argv[2],
xstricmp(argv[1], argv[2]));
printf("xstricmp(NULL, \"%s\") returned %d\n", argv[2],
xstricmp(NULL, argv[2]));
printf("xstricmp(\"%s\", NULL) returned %d\n", argv[1],
xstricmp(argv[1], NULL));
printf("xstricmp(NULL, NULL) returned %d\n", xstricmp(NULL, NULL));
return 0;
} #endif /* TEST */ 網路志工聯盟----Visita網站http://www.vista.org.tw
---[ 發問前請先找找舊文章 ]--- 發表人 - axsoft 於 2002/12/11 18:31:21
|