/** ******************************************************************************* @file /common/xml/package.cpp @brief Trida na prenaseni dat po siti @author Vta @version 1.0 ******************************************************************************/ #include "common/xml/package.h" #include "common/compatibility.h" #include "common/utils.h" // zrychleni #define KMemAlloc malloc #define KMemFree free int TPackage::readAttrib(va_list marker,char ***tags,int **ids,const char * firsttag,int firstid) // fce slouzici u vsech metod s promennym poctem parametru k jejich ziskani do pole { int i=0,j; DA daTags; DA daIds; char * tag; if (firsttag!=NULL) { daTags[i]=(char*)firsttag; daIds[i]=firstid; i++; } else { GLOBALLOGID(PRIORITY_WRONG_STRUCT, "Can not modify data in the root tag"); THROW(E_8K_PACKAGE,"Can not modify data in the root tag"); } do { tag = va_arg( marker, char *); // vezmu tag daTags[i]=tag; if (tag!=NULL) { daIds[i] = va_arg( marker, int); // a jeho id if (daIds[i]<0) { return XML_ERROR_INVALID_INDEX; // toto nelze } } i++; } while( tag != NULL ); i--; (*tags) = (char**)KMemAlloc (sizeof(char*)*i); (*ids) = (int*)KMemAlloc (sizeof(int)*i); for (j=0;jtype>0) { // v tuto chvili neni jeste dobre mit daTags v debug okne ;D ... neni to inicializovane int *ids; char **tags; int i=0; va_list marker; va_start( marker, firstid); i=this->readAttrib(marker,&tags,&ids,firsttag,firstid); va_end( marker ); if (i>0) // korenovy tag K8 jiste nema zadny obsah { char datawch[BUFF_INT_TO_STR]; snprintf(datawch,98,"%f",data2); datawch[99]=0; this->data->setData(datawch,ids,tags,i,0); } KMemFree(tags); // vnitrek daTags se nikde nealokoval ani sam neni KMemFree(ids); } else { GLOBALLOGID(PRIORITY_WRONG_STRUCT, "Can not use Set in this type of object"); THROW(E_8K_PACKAGE,"Can not use Set in this type of object"); } } void TPackage::setInt(int data2, const char * firsttag,int firstid,...) // nastavi data, cesta zadana primo pomoci atributu, kde se strida jmeno tagu s jeho id { if (this->type>0) { // v tuto chvili neni jeste dobre mit daTags v debug okne ;D ... neni to inicializovane int *ids; char **tags; int i=0; va_list marker; va_start( marker, firstid); i=this->readAttrib(marker,&tags,&ids,firsttag,firstid); va_end( marker ); if (i>0) // korenovy tag K8 jiste nema zadny obsah { char datawch[BUFF_INT_TO_STR]; snprintf(datawch,98,"%i",data2); datawch[99]=0; this->data->setData(datawch,ids,tags,i,0); } KMemFree(tags); // vnitrek daTags se nikde nealokoval ani sam neni KMemFree(ids); } else { GLOBALLOGID(PRIORITY_WRONG_STRUCT, "Can not use Set in this type of object"); THROW(E_8K_PACKAGE,"Can not use Set in this type of object"); } } int TPackage::getInt(const char * firsttag, int firstid,...) // nastavi data, cesta zadana primo pomoci atributu, kde se strida jmeno tagu s jeho id { // v tuto chvili neni jeste dobre mit daTags v debug okne ;D ... neni to inicializovane int *ids; char **tags; int result=0,i=0; va_list marker; va_start( marker, firstid); i=this->readAttrib(marker,&tags,&ids,firsttag,firstid); va_end( marker ); if (i>0) // korenovy tag K8 jiste nema zadny obsah { char datawch[BUFF_INT_TO_STR]; if (this->data->getData(datawch,BUFF_INT_TO_STR,ids,tags,i)==XML_ERROR_NO_SUCH_A_PATH) { KMemFree(tags); KMemFree(ids); GLOBALLOGID(PRIORITY_WRONG_STRUCT, "Non existing attribute (or its child) %s",firsttag); THROW(E_8K_PACKAGE,"Non existing attribute (or its child)"); } result=atoi(datawch); } KMemFree(tags); KMemFree(ids); return result; } double TPackage::getDouble(char * firsttag,int firstid,...) // nastavi data, cesta zadana primo pomoci atributu, kde se strida jmeno tagu s jeho id { // v tuto chvili neni jeste dobre mit daTags v debug okne ;D ... neni to inicializovane int *ids; char **tags; int result=0,i=0; va_list marker; va_start( marker, firstid); i=this->readAttrib(marker,&tags,&ids,firsttag,firstid); va_end( marker ); if (i>0) // korenovy tag K8 jiste nema zadny obsah { char datawch[BUFF_INT_TO_STR]; if (this->data->getData(datawch,BUFF_INT_TO_STR,ids,tags,i)==XML_ERROR_NO_SUCH_A_PATH) { KMemFree(tags); KMemFree(ids); GLOBALLOGID(PRIORITY_WRONG_STRUCT, "Non existing attribute (or its child) %s",firsttag); THROW(E_8K_PACKAGE,"Non existing attribute (or its child)"); } return atof(datawch); } KMemFree(tags); KMemFree(ids); return result; } void TPackage::setChar(const char* data2,const char * firsttag,int firstid...) // nastavi data, cesta zadana primo pomoci atributu, kde se strida jmeno tagu s jeho id { if (this->type>0) { // v tuto chvili neni jeste dobre mit daTags v debug okne ... neni to inicializovane int *ids; char **tags; int i=0; va_list marker; va_start( marker, firstid); i=this->readAttrib(marker,&tags,&ids,firsttag,firstid); va_end( marker ); if (i>0) // korenovy tag K8 jiste nema zadny obsah { this->data->setData((char*)data2,ids,tags,i,0); } KMemFree(tags); // vnitrek daTags se nikde nealokoval ani sam neni KMemFree(ids); } else { GLOBALLOGID(PRIORITY_WRONG_STRUCT, "Can not use Set in this type of object"); THROW(E_8K_PACKAGE,"Can not use Set in this type of object"); } } int TPackage::getCharR(char ** data2,int &size,char * firsttag, int firstid,...) { int *ids; char **tags; int result=0,i=0; va_list marker; va_start( marker, firstid); i=this->readAttrib(marker,&tags,&ids,firsttag,firstid); va_end( marker ); if (i>0) // korenovy tag K8 jiste nema zadny obsah { result=this->data->getData(NULL,0,ids,tags,i); if (size<=result) // nevejde se, <= kvuli koncove NULL { if ((*data2)!=NULL) KMemFree(*data2); (*data2)=(char*) KMemAlloc (sizeof(char)*(result+1)); size=result+1; } if (this->data->getData(*data2,result+1,ids,tags,i)==XML_ERROR_NO_SUCH_A_PATH) { KMemFree(tags); KMemFree(ids); GLOBALLOGID(PRIORITY_WRONG_STRUCT, "Non existing attribute (or its child) %s",firsttag); THROW(E_8K_PACKAGE,"Non existing attribute (or its child)"); } } KMemFree(tags); KMemFree(ids); return result; } int TPackage::getChar(char * data2,int size,char * firsttag,int firstid,...) // nastavi data, cesta zadana primo pomoci atributu, kde se strida jmeno tagu s jeho id { // v tuto chvili neni jeste dobre mit daTags v debug okne ... neni to inicializovane if (size>1) { int *ids; char **tags; int result=0,i=0; va_list marker; va_start( marker, firstid); i=this->readAttrib(marker,&tags,&ids,firsttag,firstid); va_end( marker ); if (i>0) // korenovy tag K8 jiste nema zadny obsah { if (this->data->getData(data2,size,ids,tags,i)==XML_ERROR_NO_SUCH_A_PATH) { // if (size>0) // data2[0]=0; KMemFree(tags); KMemFree(ids); GLOBALLOGID(PRIORITY_WRONG_STRUCT, "Non existing attribute (or its child) %s",firsttag); THROW(E_8K_PACKAGE,"Non existing attribute (or its child)"); } data2[size-1]=0; } KMemFree(tags); KMemFree(ids); return result; } else { return 0; } } void TPackage::setEmpty(const char * firsttag,int firstid,...) { if (this->type>0) { // v tuto chvili neni jeste dobre mit daTags v debug okne ... neni to inicializovane int *ids; char **tags; int i=0; va_list marker; va_start( marker, firstid); i=this->readAttrib(marker,&tags,&ids,firsttag,firstid); va_end( marker ); if (i>0) // korenovy tag K8 jiste nema zadny obsah { this->data->setData("",ids,tags,i,0); } KMemFree(tags); // vnitrek daTags se nikde nealokoval ani sam neni KMemFree(ids); } else { GLOBALLOGID(PRIORITY_WRONG_STRUCT, "Can not use Set in this type of object"); THROW(E_8K_PACKAGE,"Can not use Set in this type of object"); } } void TPackage::send(int from,int to,int transceiver) { if (this->type==1) { this->data->setDataInt(from,"from_",0,NULL); this->data->setDataInt(to,"to_",0,NULL); this->data->setDataInt(transceiver,"tr_",0,NULL); if (this->data->getDataInt("ofrom_",0,NULL)==-1) // nastavim i puvodniho odesilatele this->data->setDataInt(from,"ofrom_",0,NULL); // kdyz nebyl nastaven je to nova zprava KSendMessage(RQUEUE,MSG_NET,MOD_NET,MOD_NET,this->getXML()); } else { GLOBALLOGID(PRIORITY_WRONG_STRUCT, "Can not use Send in this type of object"); THROW(E_8K_PACKAGE,"Can not use Send in this type of object"); } } xmlcontainersmall * TPackage::getSubtag(const char * firsttag,int firstid,...) // nastavi data, cesta zadana primo pomoci atributu, kde se strida jmeno tagu s jeho id { // v tuto chvili neni jeste dobre mit daTags v debug okne ... neni to inicializovane int *ids; char **tags; int i=0; va_list marker; va_start( marker, firstid); i=this->readAttrib(marker,&tags,&ids,firsttag,firstid); va_end( marker ); xmlcontainersmall * xcs=NULL; if (i>0) // korenovy tag K8 jiste nema zadny obsah { xcs =this->data->getSubtag(ids,tags,i); } KMemFree(tags); // vnitrek daTags se nikde nealokoval ani sam neni KMemFree(ids); if (xcs==NULL) { // GLOBALLOGID(PRIORITY_WRONG_STRUCT, "Neni vracen zadny obsah pri spatne zadane ceste v TPackage:getSubtag"); // THROW(E_8K_PACKAGE,"Neni vracen zadny obsah pri spatne zadane ceste v TPackage:getSubtag"); } return xcs; } TXMLdata * TPackage::getXML() { return this->data; } int TPackage::getTransceiver() { return this->data->getDataInt("tr_",0,NULL); } int TPackage::msgTo() { return this->data->getDataInt("to_",0,NULL); } void TPackage::setOrigSender() { int i=this->data->getDataInt("ofrom_",0,NULL); this->data->setDataInt(i,"from",0,NULL); } int TPackage::msgFrom() { return this->data->getDataInt("from_",0,NULL); } int TPackage::msgOrigFrom() { return this->data->getDataInt("ofrom_",0,NULL); } void TPackage::setXML(TXMLdata * data) { if (this->data!=NULL) delete(this->data); this->data=data; } TPackage::TPackage(int id_struct) { this->data=new TXMLdata; if (id_struct>=0) this->data->setDataInt(id_struct,"_id_struct",0,NULL); this->type=1; } TPackage::~TPackage() { // if (this->type==1) delete (this->data); }