以萬用字元比較字串 |
|
axsoft
版主 發表: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 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |