[xml] how does this codes look



The following code takes a structure with a whack of info in it
converts, it to an xmlDoc and then writes the doc to a text file.

I am playing with splint(hence all the (void) casts everywhere).  Splint
is telling me that I should be freeing my xmlNodePtr vars as I leave the
addNode and addScheduleNode functions.  

Which brings me to wonder is the when I call xmlFreeDoc(doc) at the very
end of xmlWrite(last function) does it free the nodes added in the other
two functions, or are those pointers left hanging?  Thanks Nick
Torenvliet



#include <libxml2/libxml/parser.h>
#include <taskListStructs.h>
#include <globalFlags.h>
void 
addNode(struct process * procSet,int i, xmlNodePtr n){
        int j,k;
        xmlChar buffer[50];
        xmlNodePtr o,q,r;
        o = xmlNewNode(NULL,procSet[i].xc_type);
        (void)xmlNewProp(o,(const xmlChar *)"pId",procSet[i].xc_pId);
        (void)snprintf(buffer,sizeof(int),"%d",procSet[i].i_releaseTime);
        (void)xmlNewProp(o,(const xmlChar *)"releaseTime",buffer);
        (void)snprintf(buffer,sizeof(int),"%d",procSet[i].i_computationTime);
        (void)xmlNewProp(o,"computationTime",buffer);
        (void)snprintf(buffer,sizeof(int),"%d",procSet[i].i_deadLine);
        (void)xmlNewProp(o,(const xmlChar *)"deadLine",buffer);
        (void)snprintf(buffer,sizeof(int),"%d",procSet[i].i_period);
        (void)xmlNewProp(o,(const xmlChar *)"period",buffer);
        for(j=0;j<procSet[i].i_segmentCount;j++){
                q = xmlNewNode(NULL,"segment");
                (void)xmlNewProp(q,(const xmlChar *)"sId",
procSet[i].p_segment[j].xc_sId);
                (void)snprintf(buffer,sizeof(int),"%d",procSet[i].p_segment[j].i_releaseTime);
                (void)xmlNewProp(q,(const xmlChar *)"releaseTime",buffer);
                (void)snprintf(buffer,sizeof(int),"%d",procSet[i].p_segment[j].i_computationTime);
                (void)xmlNewProp(q,(const xmlChar *)"computationTime",buffer);
                (void)snprintf(buffer,sizeof(int),"%d",procSet[i].p_segment[j].i_deadLine);
                (void)xmlNewProp(q,(const xmlChar *)"deadLine",buffer);
                (void)xmlAddChild(o,q);
                for(k=0;k<procSet[i].p_segment[j].i_relationsCount;k++){
                        r =
xmlNewNode(NULL,procSet[i].p_segment[j].p_relation[k].xc_relType);
                        (void)xmlNewProp(r,(const xmlChar *)"sId",
procSet[i].p_segment[j].p_relation[k].xc_sId);
                        (void)xmlAddChild(q,r);
                }
        }
        (void)xmlAddChild(n,o);
        return;
}

void 
addScheduleNode(struct processList * p, xmlNodePtr n){
        int j,k;
        char buffer[50];
        xmlNodePtr o,q,r;
        o = xmlNewNode(NULL,"schedule");
        for(j=0;j<p->i_sliceCount;j++){
                q = xmlNewNode(NULL,"scheduleSlice");
                (void)xmlNewProp(q,"pId", p->p_schedule[j].xc_pId);
                (void)xmlNewProp(q,"sId", p->p_schedule[j].xc_sId);
                (void)sprintf(buffer,"%d",p->p_schedule[j].i_sTime);
                (void)xmlNewProp(q,"sTime",buffer);
                (void)sprintf(buffer,"%d",p->p_schedule[j].i_eTime);
                (void)xmlNewProp(q,"eTime",buffer);
                (void)xmlAddChild(o,q);
        }
        (void)xmlAddChild(n,o);
}

void 
writeXml(struct processList * p, char * prefix){
        int i;
        char buffer[50];
        xmlDocPtr doc;
        xmlNodePtr n;
        doc = xmlNewDoc("1.0");
        n = xmlNewNode(NULL, "taskList");
        xmlDocSetRootElement(doc, n);
        for (i=0;i<p->i_phkProcs;i++){
                (void)addNode(p->p_phkProcList,i,n);       
        }
        for (i=0;i<p->i_ahkaProcs;i++){
                (void)addNode(p->p_ahkaProcList,i,n);      
        }
        for (i=0;i<p->i_pskProcs;i++){
                (void)addNode(p->p_pskProcList,i,n);
        }
        if(p->i_sliceCount > 0){
                (void)addScheduleNode(p,n);
        }
        sprintf(buffer,"%s",p->xc_docName);
        strcat(buffer,prefix);
        xmlSaveFormatFileEnc(buffer,doc,"ISO-8859-1",1);
        xmlFreeDoc(doc);
        return;
}








[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]