renamed file
authorecalot <ecalot>
Wed, 17 Mar 2004 04:08:18 +0000 (04:08 +0000)
committerecalot <ecalot>
Wed, 17 Mar 2004 04:08:18 +0000 (04:08 +0000)
PR/src/lib/xml/parse.c [moved from PR/src/xml.c with 72% similarity]

similarity index 72%
rename from PR/src/xml.c
rename to PR/src/lib/xml/parse.c
index 0d46aba17feae1f1bf5fcb6d196d2241d6daeadf..f64c207f54d7a882c858c5c53bd5f7d66601aef2 100644 (file)
@@ -19,7 +19,7 @@
 */
 
 /*
-xml.c: Princed Resources : xml handling functions
+xmlparse.c: Princed Resources : xml parsing functions
 ¯¯¯¯¯
  Copyright 2003 Princed Development Team
   Created: 23 Oct 2003
@@ -35,32 +35,42 @@ xml.c: Princed Resources : xml handling functions
 |                  I M P L E M E N T A T I O N                  |
 \***************************************************************/
 
-//Includes
-#include "xml.h"
+/* Includes */
+#include "xmlparse.h"
 #include "disk.h"
+#include "memory.h"
 #include "pr.h"
 #include <string.h>
 #include <stdio.h> /* Included only for XML specific attribute code */
 #include <stdlib.h>
 
+extern FILE* outputStream;
+
 /****************************************************************\
 |                   Tag Tree Handling Functions                  |
 \****************************************************************/
 
-//private defines
+/* private defines */
 #define IsSpace(c) ((c==' ')||(c==9))||(c=='\n')||(c=='\r')
 #define IsChar(c) ((('a'<=c)&&(c<='z'))||(('A'<=c)&&(c<='Z'))||(c=='_')||(c=='-')||(c=='?'))
 
 #define Separate while (IsSpace(*i)) i++
 #define NextWord(i) while (IsChar(*(i))) (i)++
 
-#define FillAttr(a,b) if (equalsIgnoreCase(attr,b)) { if ((a)!=NULL) free(a); (a)=(val); return 1;}
-#define freeAllocation(m) if ((m)!=NULL) free(m)
+#define FillAttr(a,b) if (equalsIgnoreCase(attr,b)) { freeAllocation(a); (a)=(val); return 1;}
+
+#define TotalInheritance(attribute) \
+               if ((tag->attribute==NULL)&&(father->attribute!=NULL)) {\
+                       x=strlen(father->attribute)+1;\
+                       tag->attribute=(char*)malloc(x);\
+                       memcpy(tag->attribute,father->attribute,x);\
+               }
+
 
 #define ParseError return -1
 
 tTag* getTagStructure() {
-       //initializes
+       /* initializes */
        tTag* t;
        t=(tTag*)malloc(sizeof(tTag));
        if (t==NULL) return NULL;
@@ -69,7 +79,7 @@ tTag* getTagStructure() {
        t->next=NULL;
        t->tag=NULL;
        t->desc=NULL;
-       t->external=NULL;
+       t->path=NULL;
        t->file=NULL;
        t->itemtype=NULL;
        t->name=NULL;
@@ -88,7 +98,7 @@ void freeTagStructure(tTag* t) {
        freeTagStructure(t->next);
        freeAllocation(t->tag);
        freeAllocation(t->desc);
-       freeAllocation(t->external);
+       freeAllocation(t->path);
        freeAllocation(t->file);
        freeAllocation(t->itemtype);
        freeAllocation(t->name);
@@ -112,15 +122,17 @@ int attribFill(char* attr,char* val, tTag* t) {
        }
 
        FillAttr(t->desc,"desc");
-       FillAttr(t->external,"external");
+       FillAttr(t->path,"external"); /* external is a path alias for old compatibilities */
+       FillAttr(t->path,"path");
        FillAttr(t->file,"file");
        FillAttr(t->itemtype,"itemtype");
        FillAttr(t->name,"name");
+       FillAttr(t->name,"title"); /* title is a name alias */
        FillAttr(t->palette,"palette");
        FillAttr(t->type,"type");
        FillAttr(t->value,"value");
        FillAttr(t->version,"version");
-       FillAttr(t->number,"levelnumber"); //levelnumber is a number alias
+       FillAttr(t->number,"levelnumber"); /* levelnumber is a number alias */
        FillAttr(t->number,"number");
 
        return 0;
@@ -130,7 +142,7 @@ int attribFill(char* attr,char* val, tTag* t) {
 |                     Other Modules Functions                    |
 \****************************************************************/
 
-//Taken from parser.c
+/* Taken from parser.c */
 
 int equalsIgnoreCase(const char s1[],const char s2[]) {
        int i=0;
@@ -142,7 +154,7 @@ int equalsIgnoreCase(const char s1[],const char s2[]) {
 |                       XML Parsing Functions                    |
 \****************************************************************/
 
-//Parse text functions
+/* Parse text functions */
 int parseNext(char** pString, tTag* tag) {
        /*
          -3 Attribute not recognized
@@ -184,19 +196,19 @@ int parseNext(char** pString, tTag* tag) {
 
        if (!(IsSpace(*i)||(*i=='=')||(*i=='>'))) ParseError;
 
-       size=(long int)i-(long int)start; //Note: casted to long for portability with 64 bits architectures
-       attribute=(char*)malloc((1+size));
+       size=(long int)i-(long int)start; /* Note: casted to long for portability with 64 bits architectures */
+       attribute=(char*)malloc(1+size);
        if (attribute==NULL) return -2;
        memcpy(attribute,start,size);
        attribute[size]=0;
 
        if (*i=='=') {
                int k=0;
-               //It's a text attribute
+               /* It's a text attribute */
                i++;
                if (*i!='"') ParseError;
                i++;
-               //Parse until the next "
+               /* Parse until the next " */
                for(start=i; (k<MAX_VALUE_SIZE)&&(*i!='"')&&(*i!=0) ;i++) {
                        aux[k]=*i;
                        if (aux[k]=='\\') {
@@ -223,7 +235,7 @@ int parseNext(char** pString, tTag* tag) {
                        ParseError;
                }
                i++;
-               value=(char*)malloc((k+1));
+               value=(char*)malloc(k+1);
                if (value==NULL) {
                        free(attribute);
                        return -2;
@@ -231,8 +243,8 @@ int parseNext(char** pString, tTag* tag) {
                memcpy(value,aux,k);
                value[k]=0;
        } else {
-               //It's a boolean attribute, I'll define it
-               value=(char*)malloc((1));
+               /* It's a boolean attribute, I'll define it */
+               value=(char*)malloc(1);
                if (value==NULL) {
                        free(attribute);
                        return -2;
@@ -249,7 +261,6 @@ int parseNext(char** pString, tTag* tag) {
        return 0;
 }
 
-
 int getNextTag(char** pString, char** value) {
        /*
          -2 No memory
@@ -263,12 +274,12 @@ int getNextTag(char** pString, char** value) {
        char* i=*pString;
        int   result;
        char* start;
-       char  size;
+       int   size;
 
        Separate;
 
        if (*i=='<') {
-               //it is a tag
+               /* it is a tag */
                i++;
                Separate;
                if (*i=='/') {
@@ -293,8 +304,8 @@ int getNextTag(char** pString, char** value) {
                if (*i==0)    ParseError;
                i++;
 
-               size=(char)((long int)i-(long int)start); //Note: casted to long for portability with 64 bits architectures
-               *value=(char*)malloc((size));
+               size=(int)((long int)i-(long int)start); /* Note: casted to long for portability with 64 bits architectures */
+               *value=(char*)malloc(size);
                if (*value==NULL) return -2;
                memcpy(*value,start,size-1);
                (*value)[size-1]=0;
@@ -305,17 +316,16 @@ int getNextTag(char** pString, char** value) {
        while ((*i)&&((*i)!='<')) i++;
        if (!(*i)) return 3;
        if (start==i) return 4;
-       size=(char)((long int)i-(long int)start); //Note: casted to long for portability with 64 bits architectures
-       *value=(char*)malloc((1+size));
+       size=(int)((long int)i-(long int)start); /* Note: casted to long for portability with 64 bits architectures */
+       *value=(char*)malloc(1+size);
        if (*value==NULL) return -2;
        memcpy(*value,start,size);
-       //memset(*value,0,size);
        (*value)[size]=0;
        *pString=i;
        return 1;
 }
 
-//Parse Tree functions
+/* Parse Tree functions */
 tTag* makeTree(char** p,char* name, int* error,tTag* father) {
        /* *error
                -3 Attribute not recognized
@@ -334,64 +344,49 @@ tTag* makeTree(char** p,char* name, int* error,tTag* father) {
 
        while (!((*error)=parseNext(p, tag)));
 
-       if ((*error)<0) return NULL; //Fatal error
+       if ((*error)<0) return NULL; /* Fatal error */
        /*      (*error)
                        1  if tag end
                        2  if end
        */
        if ((*error)==1) {
-               *error=0; //No errors, end of the tag in the same tag <tag />
+               *error=0; /* No errors, end of the tag in the same tag <tag /> */
                return tag;
        }
 
-       //In case there are some empty attributes, they may be inherited
-       //BEGIN specific xml tag inheritance
+       /* In case there are some empty attributes, they may be inherited */
+       /* BEGIN specific xml tag inheritance */
        {
                int x;
                char* str;
-               //TODO: use macros
-               //TotalInheritance(tag->palette,father->palette);
-               if ((tag->palette==NULL)&&(father->palette!=NULL)) {
-                       x=strlen(father->palette)+1;
-                       tag->palette=(char*)malloc(x);
-                       memcpy(tag->palette,father->palette,x);
-               }
-               //TotalInheritance(file);
-               if ((tag->file==NULL)&&(father->file!=NULL)) {
-                       x=strlen(father->file)+1;
-                       tag->file=(char*)malloc(x);
-                       memcpy(tag->file,father->file,x);
-               }
-               //TotalInheritance(itemtype);
-               if ((tag->itemtype==NULL)&&(father->itemtype!=NULL)) {
-                       x=strlen(father->itemtype)+1;
-                       tag->itemtype=(char*)malloc(x);
-                       memcpy(tag->itemtype,father->itemtype,x);
-               }
-               //PartialConcatInheritance(tag->external,father->external,tag->value);
-               if ((tag->value==NULL)||(tag->external!=NULL)) {
-                       //Make sure externals do exist
-                       if (father->external==NULL) {father->external=(char*)malloc(1);*(father->external)=0;}
-                       if (tag->external==NULL) {tag->external=(char*)malloc(1);*(tag->external)=0;}
-
-                       //Create new variable
-                       x=strlen(father->external)+strlen(tag->external)+2;
+
+               TotalInheritance(palette);
+               TotalInheritance(itemtype);
+               TotalInheritance(file);
+               /* PartialConcatInheritance(tag->path,father->path,tag->value); */
+               if ((tag->value==NULL)||(tag->path!=NULL)) {
+                       /* Make sure paths do exist */
+                       if (father->path==NULL) {father->path=(char*)malloc(1);*(father->path)=0;}
+                       if (tag->path==NULL) {tag->path=(char*)malloc(1);*(tag->path)=0;}
+
+                       /* Create new variable */
+                       x=strlen(father->path)+strlen(tag->path)+2;
                        str=(char*)malloc(x);
                        if (str==NULL) {*error=2;return NULL;}
 
-                       //Set variable and destroy old variables
-                       sprintf(str,"%s/%s",father->external,tag->external);
-                       free(tag->external);
-                       if ((*(father->external))==0) free(father->external);
-                       tag->external=str;
-                       memcpy(tag->external,str,x);
+                       /* Set variable and destroy old variables */
+                       sprintf(str,"%s/%s",father->path,tag->path);
+                       free(tag->path);
+                       if ((*(father->path))==0) free(father->path);
+                       tag->path=str;
+                       memcpy(tag->path,str,x);
                }
        }
-       //END specific xml tag inheritance
-       //Parse Child tags
+       /* END specific xml tag inheritance */
+       /* Parse Child tags */
        while (1) {
                (*error)=getNextTag(p, &value);
-               if ((*error)<0) return NULL; //Fatal error
+               if ((*error)<0) return NULL; /* Fatal error */
                /*      (*error)
                                0  if next item is a tag
                                1  if it was a text
@@ -410,19 +405,19 @@ tTag* makeTree(char** p,char* name, int* error,tTag* father) {
                                        children=children->next;
                                }
                                if (*error) return NULL;
-                               //Add error handling
+                               /* Add error handling */
                                break;
                        case 1:
-                               if (tag->name!=NULL) free(tag->name);
+                               freeAllocation(tag->name);
                                tag->name=value;
                                break;
                        case 2:
-                               //"no errors" or "a wrong tag is closed"
+                               /* "no errors" or "a wrong tag is closed" */
                                *error=-(!(equalsIgnoreCase(value,tag->tag)));
                                free(value);
                                return tag;
                        case 3:
-                               *error=-1; //this tag wasn't closed
+                               *error=-1; /* this tag wasn't closed */
                                return tag;
                                break;
                }
@@ -430,15 +425,15 @@ tTag* makeTree(char** p,char* name, int* error,tTag* father) {
        return NULL;
 }
 
-#if 0
+#ifdef IGNOREVERIFYDATTYPES
 
 void showTag(int n,tTag* t) {
        int a;
        tTag* children;
 
-       for(a=0;a<n;a++) printf ("  ");
+       for(a=0;a<n;a++) fprintf (outputStream,"  ");
        if (t!=NULL) {
-               printf("%s (%s)\n",t->tag,t->desc);
+               fprintf(outputStream,"%s (%s@%s)\n",t->tag,t->file,t->value);
                children=t->child;
 
                while (children!=NULL) {
@@ -446,7 +441,7 @@ void showTag(int n,tTag* t) {
                        children=children->next;
                }
        } else {
-               printf("None\n");
+               fprintf(outputStream,"None\n");
        }
 }
 
@@ -467,7 +462,7 @@ tTag* parseXmlFile(const char* vFile,int* error) {
        tTag* father;
 
        if (!mLoadFileArray(vFile,(unsigned char**)(&l))) {
-               *error=-4; //File not open
+               *error=-4; /* File not open */
                return NULL;
        }
        p=l;
@@ -475,7 +470,7 @@ tTag* parseXmlFile(const char* vFile,int* error) {
        *error=getNextTag(&p, &value);
        if ((*error)<0) {
                free(l);
-               return NULL; //Fatal error will stop the execusion of the parsing
+               return NULL; /* Fatal error will stop the execusion of the parsing */
        }
 
        father=getTagStructure();
@@ -492,7 +487,7 @@ tTag* parseXmlFile(const char* vFile,int* error) {
        if (*error<0) {
                freeTagStructure(tag);
                free(father);
-               return NULL; //Fatal error will stop the execusion of the parsing
+               return NULL; /* Fatal error will stop the execusion of the parsing */
        }
        if (*error==3) {
                *error=0;