#include #include #include "tool.h" /* A partir d'un path de fichier extrait le nom de base et l'extension base peut etre egal a fname base peut etre NULL ext peut etre NULL ext ne contiendra pas de '.'. */ /* #Specification: extension L'extension est la s‚quence qui suit le dernier point dans le nom du fichier. */ export void file_baseext (const char *fname, char *base, char *ext) { char file[MAXSIZ_PATH]; char path[MAXSIZ_PATH]; char *pt = file; char *lastpt = NULL; path_splitlex (fname,path,file); while (*pt != '\0'){ if (*pt == '.') lastpt = pt; pt++; } if (lastpt != NULL){ *lastpt++ = '\0'; }else{ lastpt = ""; } if (ext != NULL){ strcpy (ext,lastpt); } if (base != NULL) strcpy(base,file); } /* determine si un path de fichier contient des wild card Retourne != 0 si oui */ export int file_iswild (const char *fpath) { return (strchr(fpath,'*')!=NULL || strchr(fpath,'?')!=NULL); } /* fait la correspondance entre deux path de liste A partir d'un path qui contient un wild card, d'un des elements issue de ce path, et d'un autre path avec wild card, produit ce qui devrait etre element correspondant ex: *.c toto.c et *.obj -> toto.obj \prj\b*.c \prj\bonjour.c * -> onjour Il peut y avoir plusieurs etoile et ? ATTENTION : elm1 doit correspondre strictement caractŠre pour caractŠre … list1. list1="\prj\doc\*.nap" et elm1="c:\prj\doc\toto.nap" n'est pas acceptable. Retourne 0 si ok, -1 si erreur */ export int file_correspond ( const char *list1, const char *elm1, const char *list2, char *elm2) { int ok = 0; while (*list2 != '\0'){ char carac = *list2++; if (carac == '*'){ const char *pt = strchr (list1,'*'); if (pt == NULL || (pt[1] != '\0' && pt[1] != '.')){ ok = -1; break; } elm1 += (int)(pt - list1); list1 = pt+1; while (*elm1 != '\0' && *elm1 != '.'){ *elm2++ = *elm1++; } }else if (carac == '?'){ const char *pt = strchr (list1,'?'); if (pt == NULL){ ok = -1; break; } elm1 += (int)(pt - list1); list1 = pt+1; *elm2++ = *elm1++; }else{ *elm2++ = carac; } } *elm2 = '\0'; if (file_iswild(list1)) ok = -1; return (ok); } #if 0 /* D‚termine si un fichier provient d'un wildcard Retourne != 0 si oui */ export int file_fromwild( const char *filename, const char *wildname) { int ok; char fpath[MAXSIZ_PATH]; char fname[MAXSIZ_NAME]; char wpath[MAXSIZ_PATH]; char wname[MAXSIZ_NAME]; WILD wild; path_splitlex (filename,fpath,fname); path_splitlex (wildname,wpath,wname); ok = (strcmp(fpath,wpath)==0 && wild_set (wname,&wild) && wild_test (fname,&wild)); return (ok); } #endif /* D‚termine si un fichier provient d'un wildcard. Il peut y avoir des wildcard mˆme dans la partie r‚pertoire. Fonction r‚cursive. Retourne != 0 si oui */ export int file_fromwild( const char *filename, const char *wildname) { int ok; if (filename[0] == '\0' || wildname[0] == '\0'){ ok = filename[0] == wildname[0]; }else{ char fpath[MAXSIZ_PATH]; char fname[MAXSIZ_NAME]; char wpath[MAXSIZ_PATH]; char wname[MAXSIZ_NAME]; WILD wild; path_splitlex (filename,fpath,fname); path_splitlex (wildname,wpath,wname); if (strcmp(filename,fpath)==0 || strcmp(wildname,wpath)==0){ /* Les paths ne sont pas divisibles d'avantage */ ok = strcmp(fpath,wpath)==0; }else{ ok = (strcmp(fpath,wpath)==0 || file_fromwild(fpath,wpath)!=0) && wild_set (wname,&wild) && wild_test (fname,&wild); } } return ok; } #ifdef TEST static struct { char *list1; char *elm1; char *list2; } tb[]={ "/usr/pierre/v3/doc/*.nai", "/usr/pierre/v3/doc/view.nai", "/usr/pierre/v3/*", "*.c", "toto.c", "*.obj", "/prj/*.*", "/prj/allo.x", "*.*", "/prj/*.*", "/prj/allo.x", "*.x", "/prj2/doc/*.nar","/prj2/doc/tool.nar", "/prj/*", NULL, }; static void near tst (char *path, char *wild) { printf ("fichier :%s: appartient … la liste :%s: -> %d\n",path,wild ,file_fromwild(path,wild)); } void main(void) { int i = 0; tst ("toto.c","*.c"); tst ("/prj/toto.c","/prj/*.c"); tst ("toto.c","*.cc"); tst ("/prj2/toto.c","/prj/*.c"); while (tb[i].list1 != NULL){ char res[MAXSIZ_PATH]; int ret = file_correspond (tb[i].list1,tb[i].elm1,tb[i].list2,res); printf ("%s , %s , %s -> ",tb[i].list1,tb[i].elm1,tb[i].list2); if (ret != -1){ printf (":%s:\n",res); }else{ printf ("*** erreur\n"); } i++; } } #endif