全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2474
推到 Plurk!
推到 Facebook!

以萬用字元比較字串

 
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-12-11 18:30:18 IP:61.218.xxx.xxx 未訂閱

以萬用字元比較字串

作者: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
系統時間:2024-11-23 17:35:05
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!