#include #include "CORE_xml.h" #include #include #include /*begin et end pointent dans une chaine de caracteres. MakeString retourne une nouvelle chaine */ static char* MakeString(char* begin, char* end){ char* r; char* a; int n=0; a=begin; while (a != end) {n++;a++;} r=(char*)malloc((n+1)*sizeof(char)); if (r==NULL) {return NULL;} strncpy(r,begin,n); *(r+n)='\0'; return r; } /*Retourne le premier tag trouvé(sans les < >) Retourne NULL si aucun tag trouvé*/ static char* FindTag(char* text){ char* a; char* b; a=strchr(text,'<'); if (a==NULL) {return NULL;} b=strchr(text,'>'); if (b==NULL) {return NULL;} return MakeString(a+1,b); } /*Retourne le contenu du tag (sans < >)de la chaine pointée par mark. Repositione mark à la fin du tag de fermeture. Penser à supprimer avec free la chaine retournée. */ static char* TagContent(char* tag, char** mark){ char* a; char* b; char* tagd; char* tagf; tagd=(char*)malloc((strlen(tag)+3)*sizeof(char)); tagf=(char*)malloc((strlen(tag)+4)*sizeof(char)); if (tagd==NULL || tagf==NULL) {return NULL;} strcpy(tagd,"<"); strcat(tagd,tag); strcat(tagd,">"); strcpy(tagf,""); a=strstr(*mark,tagd)+strlen(tagd); *mark=a; b=strstr(*mark,tagf); if(b == NULL){ free(tagd); free(tagf); return NULL; //Si il n'y a pas de tag de fermeture } *mark=b+strlen(tagf); free(tagd); free(tagf); return MakeString(a,b); } /*Lit un fichier et retourne son contenu dans une chaine de caractère*/ char* xml_load(char* location){ int n=0; int i=0; char* m; FILE* f=fopen(location,"r"); if(f == NULL){ #ifdef DEBUG PA_Print(1, "Loading \"%s\" FAILED\n", location); #endif return NULL; } while (fgetc(f) != EOF) {n++;} m=(char*)malloc(n*sizeof(char)); if (m == NULL){ #ifdef DEBUG PA_Print(1, "Loading \"%s\" FAILED\n", location); #endif return NULL; } rewind(f); for(i=0;i<(n-1);i++){ *(m+i)=(char)fgetc(f); } *(m+n-1)='\0'; fclose(f); #ifdef DEBUG PA_Print(1, "Loading \"%s\" SUCCESS\n", location); #endif return m; } /* Lit la chaine XML pour initialiser l'élément pointé par pt grace à la fonction TreatTag */ int xml_read(void* pt, int(*TreatTag)(void*, char*, char*), char* mark){ int ok; char* content; char* tag; tag=FindTag(mark); while(tag!=NULL){ content = TagContent(tag,&mark); if (content != NULL){ ok = TreatTag(pt,tag,content); if(ok==0){ #ifdef DEBUG PA_Print(1, "XML error <%s>\n", tag); #endif return 0; } free(content); } free(tag); tag=FindTag(mark); } return 1; }