void XMLdocFree(struct XMLdocdata *ptr) { int i; if (ptr==NULL) return; if (ptr->ent !=NULL) EntityFree(ptr->ent); if (ptr->tag !=NULL) TagDBFree(ptr->tag); if (ptr->tagdb !=NULL) TagDBFreeTable(ptr->tagdb); /* free up the rest of this stuff */ if (ptr->root !=NULL) XMLDBFreeBuffer(ptr->root); if (ptr->xml.ptr !=NULL) { for (i=0;i<ptr->xml.num;i++) { XMLdocFreeElement(ptr->xml.ptr[i]); } free(ptr->xml.ptr); } free(ptr); return; }
int XMLdocRenderElement(struct TagDBdata *tag,struct TagDBtable *tagdb, struct XMLdocelement *x,struct XMLDBelement *e) { int i,j,s=0; struct XMLdocmod *m=NULL; struct XMLDBbuffer *action=NULL; struct XMLDBbuffer *data=NULL; char *path=NULL; int xtp=0; int atp=0; path=x->path; action=x->action; xtp=x->type.xml; atp=x->type.action; TagDBSetTable(tag,tagdb); if (x->tagdb !=NULL) TagDBSetTable(tag,x->tagdb); if (e !=NULL) data=e->data; else { data=x->defval; xtp=x->type.defval; } if (e !=NULL) { /* decide if we need a modifier */ for (i=0;i<e->atnum;i++) { for (j=0;j<x->mod.num;j++) { m=x->mod.ptr[j]; if (strcmp(e->atval[i],m->name)==0) break; } if (j<x->mod.num) { /* we have a matching attribute */ if (m->action !=NULL) action=m->action; if (m->tagdb !=NULL) TagDBSetTable(tag,m->tagdb); if (m->path !=NULL) path=m->path; atp=m->type.action; xtp=m->type.xml; } } } /* we are now in a position to render this element */ if (action==NULL) { /* no action - render as text */ if (xtp==0) { /* data is text */ s=TagDBDecode(data->buf,data->sze,tag); s=TagDBDecode(NULL,0,tag); } else { struct XMLdocfile file; file.fname=NULL; if (data !=NULL) file.fname=getDBstring(data); file.path=path; file.text=TagDBDecode; file.data=tag; file.type=xtp-1; s= XMLdocRenderFile(&file); if (file.fname !=NULL) free(file.fname); } } else { /* use an entity decoder to handle the action */ struct Entitydata *ent; ent=EntityMake(); if (ent==NULL) return -1; s=EntityAddEntityString(ent,"<"); s=EntityAddEntityString(ent,">"); s=EntityAddSymbolString(ent,"<"); s=EntityAddSymbolString(ent,">"); if (xtp==0) { s=EntityAddEntityString(ent,"&xml;"); if (s==0) s=EntityAddSymbol(ent,data); } else { struct XMLdocfile file; file.fname=NULL; if (data !=NULL) file.fname=getDBstring(data); file.path=path; file.text=TagDBDecode; file.data=tag; file.type=xtp-1; s=EntityAddEntityString(ent,"&filename;"); if (s==0) s=EntityAddEntityString(ent,"&file;"); if (s==0) s=EntityAddSymbol(ent,e->data); if (s==0) s=EntitySetDecodeTrap(ent,XMLdocFileTrap,&file); } EntitySetText(ent,TagDBDecode,tag); if (atp==0) { s=EntityDecode(action->buf,action->sze,ent); if (s==0) s=EntityDecode(NULL,0,ent); } else { FILE *fp; char *afname=NULL; char lbuf[256]; afname=getDBstring(action); if (afname !=NULL) { fp=fopen(afname,"r"); if (fp !=NULL) { while ((s==0) && (fgets(lbuf,255,fp) !=NULL)) { s=EntityDecode(lbuf,strlen(lbuf),ent); if (s !=0) break; } if (s==0) s=EntityDecode(NULL,0,ent); fclose(fp); } free(afname); } } EntityFree(ent); } return 0; }
int TagDBAddTable(struct TagDBtable *ptr, struct XMLDBbuffer *name,struct XMLDBbuffer *start, struct XMLDBbuffer *end) { char *symbol[]={"\"","\\","<",">","&",0}; char *entity[]={""","'","<",">","&",0}; int n,s=0; struct TagDBtag **tmp=NULL; struct TagDBtag *t=NULL; struct Entitydata *entptr=NULL; if (ptr==NULL) return -1; if (ptr->tag==NULL) ptr->tag=malloc(sizeof(struct TagDBtag *)); else { tmp=realloc(ptr->tag,sizeof(struct TagDBtag *)*(ptr->num+1)); if (tmp==NULL) return -1; ptr->tag=tmp; } if (ptr->tag==NULL) return -1; n=ptr->num; ptr->tag[n]=malloc(sizeof(struct TagDBtag)); if (ptr->tag[n]==NULL) return -1; t=ptr->tag[n]; t->name=malloc(name->sze+1); if (t->name==NULL) return -1; memcpy(t->name,name->buf,name->sze); t->name[name->sze]=0; t->start.encode=NULL; t->end.encode=NULL; t->start.decode=NULL; t->end.decode=NULL; entptr=EntityMake(); if (entptr==NULL) return -1; s=EntityAddEntityStringArray(entptr,entity); s=EntityAddSymbolStringArray(entptr,symbol); if (s !=0) { EntityFree(entptr); return -1; } if (start !=NULL) { t->start.encode=XMLDBCopyBuffer(start); if (t->start.encode==NULL) { EntityFree(entptr); return -1; } t->start.decode=EntityDecodeBuffer(entptr,start); if (t->start.decode==NULL) { EntityFree(entptr); return -1; } } if (end !=NULL) { t->end.encode=XMLDBCopyBuffer(end); if (t->end.encode==NULL) { EntityFree(entptr); return -1; } t->end.decode=EntityDecodeBuffer(entptr,end); if (t->end.decode==NULL) { EntityFree(entptr); return -1; } } EntityFree(entptr); ptr->num++; return 0; }
int TagDBStart(char *name,char end,int atnum,char **atname,char **atval, char *buf,int sze,void *data) { int i; struct TagDBdata *ptr=NULL; struct TagDBtable *tagdb=NULL; struct TagDBtag *t=NULL; ptr=(struct TagDBdata *) data; if (ptr->text.func==NULL) return 0; tagdb=ptr->tagdb; if (tagdb !=NULL) { for (i=0;i<tagdb->num;i++) { t=tagdb->tag[i]; if (strcmp(name,t->name)==0) break; } if (i<tagdb->num) { /* buffer attributes */ struct XMLDBbuffer *abuf=NULL; struct XMLDBbuffer *tbuf=NULL; struct Entitydata *entptr; entptr=EntityMake(); if (entptr==NULL) return -1; EntityAddEntityString(entptr,"&attr;"); abuf=XMLDBMakeBuffer(100); for (i=0;i<atnum;i++) { if (i !=0) XMLDBAddBuffer(abuf," ",1); XMLDBAddBuffer(abuf,atname[i],strlen(atname[i])); if (atval[i] !=0) { XMLDBAddBuffer(abuf,"=\"",2); XMLDBAddBuffer(abuf,atval[i],strlen(atval[i])); XMLDBAddBuffer(abuf,"\"",1); } } if (end !=0) XMLDBAddBuffer(abuf,&end,1); EntityAddSymbol(entptr,abuf); tbuf=EntityDecodeBuffer(entptr,t->start.decode); if (tbuf==NULL) return -1; (ptr->text.func)(tbuf->buf, tbuf->sze,ptr->text.data); XMLDBFreeBuffer(tbuf); XMLDBFreeBuffer(abuf); EntityFree(entptr); return 0; } } (ptr->text.func)("<",1,ptr->text.data); (ptr->text.func)(name,strlen(name),ptr->text.data); for (i=0;i<atnum;i++) { (ptr->text.func)(" ",1,ptr->text.data); (ptr->text.func)(atname[i],strlen(atname[i]),ptr->text.data); if (atval[i] !=0) { (ptr->text.func)("=\"",2,ptr->text.data); (ptr->text.func)(atval[i],strlen(atval[i]),ptr->text.data); (ptr->text.func)("\"",1,ptr->text.data); } } if (end !=0) (ptr->text.func)(&end,1,ptr->text.data); (ptr->text.func)(">",1,ptr->text.data); return 0; }