/* 28/07/88 : Jacques Gelinas Comparaison de wildcard */ #include #include #include #include #include "vfs.h" /* Compile un selection avec wildcard dans une structure pour comparaison retourne 0 si wild invalide retourne 1 sinon */ int wild_set ( const char *str, WILD *wild) { strcpy (wild->str,str); return (1); } /* Compare une chaine avec wildcard avec une chaine precompilee retourne 1 si semblable, 0 sinon */ int wild_test ( const char *str, /* nom de fichier (sans path) a comparer */ const WILD *wild) /* wildcard compile par wild_set */ { int ok = 0; const char *pt = wild->str; while (*str != '\0' && *pt != '\0'){ while (*pt != '\0' && *pt == *str){ pt++; str++; } if (*pt == '?'){ if (*str == '\0') break; pt++; str++; }else if (*pt == '*'){ while (*pt == '*') pt++; while (*str != '\0' && *str != *pt) str++; }else{ break; } } ok = *pt == '\0' && *str == '\0'; return ok; } void wild_setall (const char *tbsel[], const char *tbuns[], WILD_SELECT *sel) { int i = 0; while (tbsel[i]!=NULL){ WILD *psel; assert (itbsel[i] = (WILD*) malloc(sizeof(WILD)); wild_set (tbsel[i],psel); i++; } sel->nbsel = i; i = 0; while (tbuns[i]!=NULL){ WILD *psel; assert (itbuns[i] = (WILD*) malloc(sizeof(WILD)); wild_set (tbuns[i],psel); i++; } sel->nbuns = i; } void wild_unsetall (WILD_SELECT *sel) { int i; for (i=0; inbsel; i++) free (sel->tbsel[i]); for (i=0; inbuns; i++) free (sel->tbuns[i]); } int wild_testall (const char *str, const WILD_SELECT *sel) { int i; for (i=0; inbsel; i++){ if (wild_test(str,sel->tbsel[i])){ int j; for (j=0; jnbuns; j++){ if (wild_test(str,sel->tbuns[j])) return (0); } return (1); } } return (0); } #ifdef TEST #include #include static struct { char *wild; char *essai; } t[]={ "*", "ALLO", "??*", "ALLO", "*.?", "ALLO.C", "*.?", "ALLO.CC", "allo?.?", "allo1.C", "allo*.C", "allo12.C", "allo*.*", "allo12.C", NULL }; void main(void) { int i; WILD wild; i = 0; while (t[i].wild != NULL){ wild_set (t[i].wild,&wild); printf ("%-15s --> '%s' =? %-15s --> %d\n",t[i].wild ,wild.str ,t[i].essai ,wild_test (t[i].essai,&wild)); i++; } } #endif