Project

General

Profile

Files » xml.c

Frédéric Blanc, 2016-01-29 10:31

 
/*********************************************************************************************************/
//XML
/*********************************************************************************************************/
#include "var.h"
#include <cvixml.h>
#define CONFIG_XML "config.xml"
/**
* @brief lecture fichier XML
* @param aucun
* @return 0
* @date 11/01/2016
*
*/
int load_xml (void )
{
char message_LOG[255];
CVIXMLDocument doc_XML= 0;
CVIXMLElement curElem=0;

CVIXMLAttribute attribue;
char chemin_nom[MAX_PATHNAME_LEN];
char valeur[255];
int i;
GetDir (chemin_nom);
if(strstr(chemin_nom,DIR_PROGRAMFILE)!=NULL)
{
strcpy( chemin_nom, DIR_PROGRAMDATA);
MakeDir(chemin_nom);
}
sprintf (chemin_nom, "%s\\%s", chemin_nom,CONFIG_XML);
sprintf (message_LOG,"load XML %s",chemin_nom);
save_LOG(message_LOG);
CVIXMLLoadDocument (chemin_nom,&doc_XML );
if(!doc_XML)
{
save_LOG("Load file xml");
save_LOG(chemin_nom);
return -1;
}
CVIXMLGetRootElement(doc_XML,&curElem); //root
//variable d'environnement
CVIXMLGetChildElementByTag (curElem,"ENV" ,&curElem );
CVIXMLGetAttributeByName(curElem,"CtrlMODE",&attribue);
CVIXMLGetAttributeValue (attribue, valeur);
Ctrl.mode=atoi(valeur);
CVIXMLGetAttributeByName(curElem,"racine",&attribue);
CVIXMLGetAttributeValue (attribue, Ctrl.racine);
CVIXMLGetParentElement (curElem, &curElem); //UP
//PANEL
CVIXMLGetRootElement(doc_XML,&curElem); //root CONFIG
CVIXMLGetChildElementByTag (curElem,"PANEL" ,&curElem );
for(i=0;i<MAX_PANEL;i++)
{
fnct_ID_panel(i,valeur);
CVIXMLGetChildElementByTag (curElem,valeur ,&curElem );
//TOP
CVIXMLGetAttributeByName(curElem,"top",&attribue);
CVIXMLGetAttributeValue (attribue, valeur);
T_panel[i].top=atoi(valeur);
//angle gauche
CVIXMLGetAttributeByName(curElem,"left",&attribue);
CVIXMLGetAttributeValue (attribue, valeur);
T_panel[i].left=atoi(valeur);
//hauteur panel
CVIXMLGetAttributeByName(curElem,"height",&attribue);
CVIXMLGetAttributeValue (attribue, valeur);
T_panel[i].height=atoi(valeur);
//largeur panel
CVIXMLGetAttributeByName(curElem,"width",&attribue);
CVIXMLGetAttributeValue (attribue, valeur);
T_panel[i].width=atoi(valeur);
CVIXMLGetParentElement (curElem, &curElem); //UP
}
//INSTRUMENT
CVIXMLGetRootElement(doc_XML,&curElem); //root
CVIXMLGetChildElementByTag (curElem,"INSTRUMENT" ,&curElem );
for(i=0;i<MAX_INSTRUMENT;i++)
{
fnct_ID_inst(i,valeur);
CVIXMLGetChildElementByTag (curElem,valeur ,&curElem ); //nom instrument
CVIXMLGetAttributeByName(curElem,"nbmodule",&attribue); //nombre de module
CVIXMLGetAttributeValue (attribue, valeur);
instrument[i].nbmodule=atoi(valeur);
CVIXMLGetAttributeByName(curElem,"typeCOM",&attribue); //type com
CVIXMLGetAttributeValue (attribue, valeur);
instrument[i].port.typeCOM=atoi(valeur);
CVIXMLGetAttributeByName(curElem,"LANG",&attribue); //type com
CVIXMLGetAttributeValue (attribue, valeur);
instrument[i].port.language=atoi(valeur);
CVIXMLGetChildElementByTag (curElem,"RS232" ,&curElem ); //RS232
CVIXMLGetAttributeByName(curElem,"num",&attribue); //mum port
CVIXMLGetAttributeValue (attribue, valeur);
instrument[i].port.numCOM=atoi(valeur);
CVIXMLGetAttributeByName(curElem,"baudRate",&attribue); //baudRate
CVIXMLGetAttributeValue (attribue, valeur);
instrument[i].port.baudRate=atoi(valeur);
CVIXMLGetAttributeByName(curElem,"dataBits",&attribue); //dataBits
CVIXMLGetAttributeValue (attribue, valeur);
instrument[i].port.dataBits=(char) atoi(valeur);
CVIXMLGetAttributeByName(curElem,"parity",&attribue); //parity
CVIXMLGetAttributeValue (attribue, valeur);
instrument[i].port.parity=(char) atoi(valeur);
CVIXMLGetAttributeByName(curElem,"stopBits",&attribue); //stopBits
CVIXMLGetAttributeValue (attribue, valeur);
instrument[i].port.stopBits=(char) atoi(valeur);
CVIXMLGetAttributeByName(curElem,"flow",&attribue); //flow
CVIXMLGetAttributeValue (attribue, valeur);
instrument[i].port.flow=(char) atoi(valeur);
CVIXMLGetAttributeByName(curElem,"termchar",&attribue); //termchar
CVIXMLGetAttributeValue (attribue, valeur);
instrument[i].port.termchar=(char) atoi(valeur);
CVIXMLGetParentElement (curElem, &curElem); //UP
CVIXMLGetChildElementByTag (curElem,"USB" ,&curElem ); //USB
CVIXMLGetAttributeByName(curElem,"num",&attribue); //mum port
CVIXMLGetAttributeValue (attribue, instrument[i].port.numUSB);
CVIXMLGetParentElement (curElem, &curElem); //UP
CVIXMLGetChildElementByTag (curElem,"GPIB" ,&curElem ); //GPIB
CVIXMLGetAttributeByName(curElem,"num",&attribue); //mum port
CVIXMLGetAttributeValue (attribue, valeur);
instrument[i].port.numGPIB=atoi(valeur);
CVIXMLGetAttributeByName(curElem,"numcarte",&attribue); //mumcarte port
CVIXMLGetAttributeValue (attribue, valeur);
instrument[i].port.numcarteGPIB=atoi(valeur);
CVIXMLGetParentElement (curElem, &curElem); //UP
CVIXMLGetChildElementByTag (curElem,"ETH" ,&curElem ); //ETH
CVIXMLGetAttributeByName(curElem,"num",&attribue); //mum IP
CVIXMLGetAttributeValue (attribue, instrument[i].port.numETH);
CVIXMLGetAttributeByName(curElem,"numcarte",&attribue); //mumcarte port
CVIXMLGetAttributeValue (attribue, valeur);
instrument[i].port.numcarteETH=atoi(valeur);
CVIXMLGetParentElement (curElem, &curElem); //UP
CVIXMLGetParentElement (curElem, &curElem); //UP

}
return 0;
}
/**
* @brief sauvegarde fichier XML
* @param aucun
* @return 0
* @date 11/01/2016
*
*/
int save_xml (void)
{
char message_LOG[255];
CVIXMLDocument doc_XML= 0;
CVIXMLElement curElem=0;

char chemin_nom[MAX_PATHNAME_LEN];
char valeur[255];
char i;
GetDir (chemin_nom);
if(strstr(chemin_nom,DIR_PROGRAMFILE)!=NULL)
{
strcpy( chemin_nom, DIR_PROGRAMDATA);
MakeDir(chemin_nom);
}
sprintf (chemin_nom, "%s\\%s", chemin_nom,CONFIG_XML);
sprintf (message_LOG,"save XML %s",chemin_nom);
save_LOG(message_LOG);
CVIXMLNewDocument ("CONFIG", &doc_XML);
if(!doc_XML)
{
save_LOG("Save file xml");
save_LOG(chemin_nom);
return -1;
}
CVIXMLGetRootElement(doc_XML,&curElem); //root
//variable d'environnement
CVIXMLNewElement (curElem,-1,"ENV" ,&curElem );
sprintf(valeur,"%d",Ctrl.mode);
CVIXMLAddAttribute(curElem,"CtrlMODE",valeur);
CVIXMLAddAttribute(curElem,"racine",Ctrl.racine);
CVIXMLGetParentElement (curElem, &curElem); //UP
//PANEL
CVIXMLNewElement (curElem,-1,"PANEL" ,&curElem );
for(i=0;i<MAX_PANEL;i++)
{
fnct_ID_panel(i,valeur);
CVIXMLNewElement (curElem,-1,valeur ,&curElem );
// top
GetPanelAttribute (T_panel[i].panel, ATTR_TOP, &T_panel[i].top);
sprintf(valeur,"%d",T_panel[i].top);
CVIXMLAddAttribute(curElem,"top",valeur);
//angle gauche
GetPanelAttribute (T_panel[i].panel, ATTR_LEFT, &T_panel[i].left);
sprintf(valeur,"%d",T_panel[i].left);
CVIXMLAddAttribute(curElem,"left",valeur);
//hauteur panel
GetPanelAttribute (T_panel[i].panel, ATTR_HEIGHT, &T_panel[i].height);
sprintf(valeur,"%d",T_panel[i].height);
CVIXMLAddAttribute(curElem,"height",valeur);
//hauteur largeur
GetPanelAttribute (T_panel[i].panel, ATTR_WIDTH, &T_panel[i].width);
sprintf(valeur,"%d",T_panel[i].width);
CVIXMLAddAttribute(curElem,"width",valeur);
CVIXMLGetParentElement (curElem, &curElem); //UP
}
CVIXMLGetRootElement(doc_XML,&curElem); //root
//INSTRUMENT
CVIXMLNewElement (curElem,-1,"INSTRUMENT" ,&curElem );
for(i=0;i<MAX_INSTRUMENT;i++)
{
fnct_ID_inst(i,valeur);
CVIXMLNewElement (curElem,-1,valeur ,&curElem ); //nom instrument
sprintf(valeur,"%d",instrument[i].nbmodule);
CVIXMLAddAttribute(curElem,"nbmodule",valeur); //type nbmodule
sprintf(valeur,"%d",instrument[i].port.typeCOM);
CVIXMLAddAttribute(curElem,"typeCOM",valeur); //type com
sprintf(valeur,"%d",instrument[i].port.language);
CVIXMLAddAttribute(curElem,"LANG",valeur); //language
//RS232
CVIXMLNewElement (curElem,-1,"RS232" ,&curElem );
sprintf(valeur,"%d",instrument[i].port.numCOM);
CVIXMLAddAttribute (curElem,"num",valeur); //mum port
sprintf(valeur,"%d",instrument[i].port.baudRate);
CVIXMLAddAttribute(curElem,"baudRate",valeur); //baudRate
sprintf(valeur,"%d",instrument[i].port.dataBits);
CVIXMLAddAttribute(curElem,"dataBits",valeur); //dataBits
sprintf(valeur,"%d",instrument[i].port.parity);
CVIXMLAddAttribute(curElem,"parity",valeur); //parity
sprintf(valeur,"%d",instrument[i].port.stopBits);
CVIXMLAddAttribute(curElem,"stopBits",valeur); //stopBits
sprintf(valeur,"%d",instrument[i].port.flow);
CVIXMLAddAttribute(curElem,"flow",valeur); //flow
sprintf(valeur,"%d",instrument[i].port.termchar);
CVIXMLAddAttribute(curElem,"termchar",valeur); //termchar
CVIXMLGetParentElement (curElem, &curElem); //UP
CVIXMLNewElement (curElem,-1,"USB" ,&curElem ); //USB
sprintf(valeur,"%s",instrument[i].port.numUSB);
CVIXMLAddAttribute(curElem,"num",valeur); //mum port
CVIXMLGetParentElement (curElem, &curElem); //UP
CVIXMLNewElement (curElem,-1,"GPIB" ,&curElem ); //GPIB
sprintf(valeur,"%d",instrument[i].port.numGPIB);
CVIXMLAddAttribute(curElem,"num",valeur); //mum port
sprintf(valeur,"%d",instrument[i].port.numcarteGPIB);
CVIXMLAddAttribute(curElem,"numcarte",valeur); //mum carteport
CVIXMLGetParentElement (curElem, &curElem); //UP
CVIXMLNewElement (curElem,-1,"ETH" ,&curElem ); //ETH
CVIXMLAddAttribute(curElem,"num",instrument[i].port.numETH); //mum IP
sprintf(valeur,"%d",instrument[i].port.numcarteETH);
CVIXMLAddAttribute(curElem,"numcarte",valeur); //mum carteport
CVIXMLGetParentElement (curElem, &curElem); //UP
CVIXMLGetParentElement (curElem, &curElem); //UP

}
CVIXMLGetRootElement(doc_XML,&curElem); //root
CVIXMLSaveDocument (doc_XML, 1, chemin_nom);
return 0;
}
/**
* @brief load fichier consigne
* @param aucun
* @return 0
* @date 25/02/2010
*
*/
int load_consigne_xml (char chemin_nom[])
{
CVIXMLDocument doc_XML= 0;
CVIXMLElement curElem=0;

CVIXMLAttribute attribue;
char valeur[255];
char i;
CVIXMLLoadDocument (chemin_nom,&doc_XML );
if(!doc_XML)
return -1;
//
CVIXMLGetRootElement(doc_XML,&curElem); //root
CVIXMLGetChildElementByTag (curElem,"CYCLE" ,&curElem );
CVIXMLGetAttributeByName(curElem,"nb_cycle",&attribue); //nb cycle
CVIXMLGetAttributeValue (attribue, valeur);
Ctrl.nb_cycle=atoi(valeur);
CVIXMLGetAttributeByName(curElem,"mode_loop",&attribue); //mode_loop
CVIXMLGetAttributeValue (attribue, valeur);
Ctrl.mode_loop=atoi(valeur);
CVIXMLGetAttributeByName(curElem,"nb_loop",&attribue); //nb_loop
CVIXMLGetAttributeValue (attribue, valeur);
Ctrl.nb_loop=atoi(valeur);
CVIXMLGetAttributeByName(curElem,"vminstop",&attribue); //vminstop
CVIXMLGetAttributeValue (attribue, valeur);
Ctrl.vminstop=atoi(valeur);
CVIXMLGetAttributeByName(curElem,"name_Batt",&attribue); //name_Batt
CVIXMLGetAttributeValue (attribue, Ctrl.name_Batt);
for(i=1;i<=Ctrl.nb_cycle;i++)
{
sprintf(valeur,"Cycle_%i",i); //cycle
CVIXMLGetChildElementByTag (curElem,valeur ,&curElem );
//MODE
CVIXMLGetChildElementByTag (curElem,"MODE" ,&curElem );
CVIXMLGetElementValue (curElem,valeur );
T_cycle[i].mode=atoi(valeur);
CVIXMLGetParentElement (curElem, &curElem); //UP
//MODE CMAX
CVIXMLGetChildElementByTag (curElem,"MODE_STOP" ,&curElem );
CVIXMLGetElementValue (curElem,valeur );
T_cycle[i].mode_stop=atoi(valeur);
CVIXMLGetParentElement (curElem, &curElem); //UP
//CURR
CVIXMLGetChildElementByTag (curElem,"CURR" ,&curElem );
CVIXMLGetElementValue (curElem,valeur );
T_cycle[i].curr=atof(valeur);
CVIXMLGetParentElement (curElem, &curElem); //UP
//VMAX
CVIXMLGetChildElementByTag (curElem,"VMAX" ,&curElem );
CVIXMLGetElementValue (curElem,valeur );
T_cycle[i].Vmax=atof(valeur);
CVIXMLGetParentElement (curElem, &curElem); //UP
//VMIN
CVIXMLGetChildElementByTag (curElem,"VMIN" ,&curElem );
CVIXMLGetElementValue (curElem,valeur );
T_cycle[i].Vmin=atof(valeur);
CVIXMLGetParentElement (curElem, &curElem); //UP
//IMIN
CVIXMLGetChildElementByTag (curElem,"IMIN" ,&curElem );
CVIXMLGetElementValue (curElem,valeur );
T_cycle[i].Imin=atof(valeur);
CVIXMLGetParentElement (curElem, &curElem); //UP
//CMAX
CVIXMLGetChildElementByTag (curElem,"CMAX" ,&curElem );
CVIXMLGetElementValue (curElem,valeur );
T_cycle[i].Cmax=atof(valeur) ;
CVIXMLGetParentElement (curElem, &curElem); //UP
//TIMER
CVIXMLGetChildElementByTag (curElem,"TIMER" ,&curElem );
CVIXMLGetElementValue (curElem,valeur );
T_cycle[i].Timermax=atoi(valeur) ;
CVIXMLGetParentElement (curElem, &curElem); //UP
CVIXMLGetParentElement (curElem, &curElem); //UP
}
CVIXMLGetParentElement (curElem, &curElem); //UP
return 0;
}
/**
* @brief sauvegarde fichier consigne
* @param aucun
* @return 0
* @date 25/02/2010
*
*/
int save_consigne_xml (char chemin_nom[])
{
CVIXMLDocument doc_XML= 0;
CVIXMLElement curElem=0;

char valeur[255];
char i;
CVIXMLNewDocument ("CONFIG", &doc_XML);
if(!doc_XML)
{
save_LOG("Save consigne xml");
save_LOG(chemin_nom);
return -1;
}
CVIXMLGetRootElement(doc_XML,&curElem); //root
CVIXMLNewElement (curElem,-1,"CYCLE" ,&curElem );
sprintf(valeur,"%d",Ctrl.nb_cycle);
CVIXMLAddAttribute (curElem,"nb_cycle",valeur); //nb_cycle
sprintf(valeur,"%d",Ctrl.mode_loop);
CVIXMLAddAttribute (curElem,"mode_loop",valeur); //mode_loop
sprintf(valeur,"%d",Ctrl.nb_loop);
CVIXMLAddAttribute (curElem,"nb_loop",valeur); // nb_loop
sprintf(valeur,"%d",Ctrl.vminstop);
CVIXMLAddAttribute (curElem,"vminstop",valeur); // vminstop
CVIXMLAddAttribute (curElem,"name_Batt",Ctrl.name_Batt); //name_Batt
for(i=1;i<=Ctrl.nb_cycle;i++)
{
sprintf(valeur,"Cycle_%i",i);
CVIXMLNewElement (curElem,-1,valeur ,&curElem );
//MODE
CVIXMLNewElement (curElem,-1,"MODE" ,&curElem );
sprintf(valeur,"%d",T_cycle[i].mode);
CVIXMLSetElementValue (curElem,valeur );
CVIXMLGetParentElement (curElem, &curElem); //UP
//MODE CMAX
CVIXMLNewElement (curElem,-1,"MODE_STOP" ,&curElem );
sprintf(valeur,"%d",T_cycle[i].mode_stop);
CVIXMLSetElementValue (curElem,valeur );
CVIXMLGetParentElement (curElem, &curElem); //UP
//CURR
CVIXMLNewElement (curElem,-1,"CURR" ,&curElem );
sprintf(valeur,"%2.3E",T_cycle[i].curr);
CVIXMLSetElementValue (curElem,valeur );
CVIXMLGetParentElement (curElem, &curElem); //UP
//VMAX
CVIXMLNewElement (curElem,-1,"VMAX" ,&curElem );
sprintf(valeur,"%2.3E",T_cycle[i].Vmax);
CVIXMLSetElementValue (curElem,valeur );
CVIXMLGetParentElement (curElem, &curElem); //UP
//VMIN
CVIXMLNewElement (curElem,-1,"VMIN" ,&curElem );
sprintf(valeur,"%2.3E",T_cycle[i].Vmin);
CVIXMLSetElementValue (curElem,valeur );
CVIXMLGetParentElement (curElem, &curElem); //UP
//IMIN
CVIXMLNewElement (curElem,-1,"IMIN" ,&curElem );
sprintf(valeur,"%2.3E",T_cycle[i].Imin);
CVIXMLSetElementValue (curElem,valeur );
CVIXMLGetParentElement (curElem, &curElem); //UP
//CMAX
CVIXMLNewElement (curElem,-1,"CMAX" ,&curElem );
sprintf(valeur,"%2.3E",T_cycle[i].Cmax);
CVIXMLSetElementValue (curElem,valeur );
CVIXMLGetParentElement (curElem, &curElem); //UP
//TIMER
CVIXMLNewElement (curElem,-1,"TIMER" ,&curElem );
sprintf(valeur,"%d",T_cycle[i].Timermax);
CVIXMLSetElementValue (curElem,valeur );
CVIXMLGetParentElement (curElem, &curElem); //UP
CVIXMLGetParentElement (curElem, &curElem); //UP
}
CVIXMLSaveDocument (doc_XML, 1, chemin_nom);
return 0;
}
(24-24/25)