/** ****************************************************************************** @file /common/rm/rmxml.cpp @brief RM pristupujici ke xml @author Vta @version 1.0 ******************************************************************************/ #include "common/rm/rmxml.h" using namespace World; using namespace rm; void TRM_xml::loadXML(int id) { this->loadData(id); } char bu_path[K8_MAX_PATH_LEN]; void TRM_xml::loadData(int id) { this->lock(); if ((*(this->data))[id]==NULL) { try { this->getFileOf(id,bu_path,K8_MAX_PATH_LEN); } catch (E_8K_RM) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMXml can not find the path of the file having s id %i",id); THROW(E_8K_RM,"RMXml can not find a path of a file"); } TXMLdata xml; int retvalue=xml.readFile(bu_path); // soubor nactu if (retvalue!=XML_ERROR_OK) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMXml can not load the file having id %i: %s",id,bu_path); THROW(E_8K_RM,"RMXml can not load a file"); } // this->XMLto((*(this->data))[id],&xml); // prevedu do pameti int sw=this->index->getDataInt("xml",id,"type",0,NULL); switch (sw) { case STRUCT_TCL: { xmlToTcl((TCL_SCRIPT**)&((*(this->data))[id]),&xml,id); // TCL_SCRIPT * test=(TCL_SCRIPT*)(*(this->data))[id]; } break; case STRUCT_TERRAIN: { TERRAIN * temp=(TERRAIN*)(*(this->data))[id]; xmlToTerrain(&temp,&xml,id); /* Odstraneno 2.3.2006 PW if (this->index->getDataInt("xml",temp->income,"type",0,NULL)!=STRUCT_TCL) // kontroluji, zda je v Terrain odkaz doopravdy na script { GLOBALLOGID(PRIORITY_FATAL, "RMXml can not load terrain having id %i, which refers to a non-existing script(income)",id); THROW(E_8K_RM,"RMXml can not load terrain, which refers to a non-existing script(income)"); } */ (*(this->data))[id]=(void*)temp; } break; default: { GLOBALLOGID(PRIORITY_FATAL, "RMXml unknown type in object having id %i",id); THROW(E_8K_RM,"RMXml unknown type"); } break; } } this->unlock(); } /* void TRM_xml::setAll(DA * all) { this->lock(); this->data=all; this->unlock(); } */ void TRM_xml::getAll(DA ** all) { this->lock(); (*all)=this->data; // this->data=NULL; this->unlock(); } void TRM_xml::get(void ** data,int id) { this->lock(); *data = (*(this->data))[id]; // vratim this->unlock(); } void TRM_xml::freeData(int id) { this->lock(); if ((*(this->data))[id]!=NULL) { switch (this->index->getDataInt("xml",id,"type",0,NULL)) { case STRUCT_TCL: { // TCL_SCRIPT* test=(TCL_SCRIPT*)(*(this->data))[j]; freeTclStruct((TCL_SCRIPT*)(*(this->data))[id]); // dealokuje i vlastni strukturu } break; case STRUCT_TERRAIN: { freeTerrainStruct((TERRAIN*)(*(this->data))[id]); } break; } (*(this->data))[id]=NULL; } else { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMXml can not release object having id %i, which has not been loaded yet",id); THROW(E_8K_RM,"RMXml can not release object, which has not been loaded yet"); } this->unlock(); } char bu_data[K8_MAX_FILE_NAME_LEN]; void TRM_xml::getFileOf(int id,char * path,int pathsize) { int retvalue=this->index->getData(bu_data,K8_MAX_FILE_NAME_LEN,"xml",id,"file",0,NULL); // zjistim jmeno souboru if (retvalue==XML_ERROR_NO_SUCH_A_PATH) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMXml missing record about the file having id %i",id); THROW(E_8K_RM,"RMXml missing record about a file"); } try { strpartpath(path,RESOURCES_DIR,this->rmconfi->getPack(),XML_DIR,bu_data,K8_MAX_PATH_LEN,".xml"); } // pridam celou cestu catch (E_8K_RM) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "Corrupted files.xml"); THROW(E_8K_RM,"Corrupted files.xml"); } } void TRM_xml::modify(void * data,int id) { this->lock(); if ((*(this->data))[id]!=NULL) { (*(this->data))[id]=data; // modifikuji v pameti int structid=this->index->getDataInt("xml",id,"type",0,NULL); char * file=NULL; size_t size=0; this->index->getDataResize(&file,size,"xml",id,"file",0,NULL); this->add(data,structid,id,-1,file); // modifikuji v souboru KMemFree(file); } else { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMxml can not modify non exisiting file having id %i",id); THROW(E_8K_RM,"RMxml can not modify non exisiting file"); } this->unlock(); } int TRM_xml::getMaxId() { this->lock(); int maxindex; if (this->data->getMaxId()>this->index->getMaxId()) maxindex=this->data->getMaxId(); else maxindex=this->index->getMaxId(); // mela by byt aktivni jen tato vetev, v souboru je toho vic this->unlock(); return maxindex; } char bu_path2[K8_MAX_PATH_LEN]; int TRM_xml::add(void * data,int structid,int id,int startid,char * file) { this->lock(); int maxindex; if (id==-1) { // vezmu prvni volny - tj. max (id_ze_souboru,id_v_pameti) pro pripad, ze uz je neco ulozeno maxindex=this->getMaxId(); maxindex++; if (startid<1) startid=1; int i; for (i=startid;iindex->getData(NULL,0,"xml",i,"file",0,NULL)==XML_ERROR_NO_SUCH_A_PATH ) && ((*(this->data))[i]==NULL) ) { break; } } maxindex=i; // vezmu prvni volnou hodnotu } else { maxindex=id; } if (maxindex<1) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMxml can not add a file having id %i < 1",maxindex); THROW(E_8K_RM,"RMxml can not add a file having id less than 1"); } // priradim - tj. i pro pripad modifikace id=maxindex; this->index->setData(file,"xml",id,"file",0,NULL); // pridam do indexu this->index->setDataInt(structid,"xml",id,"type",0,NULL); // pridam do indexu // this->saveToFile(this->rmconfi->getPack(),this->rmconfi->getScriptFile()); TXMLdata xml(1,1); switch (this->index->getDataInt("xml",id,"type",0,NULL)) { case STRUCT_TCL: { tclToXML(&xml,(TCL_SCRIPT*)data); } break; case STRUCT_TERRAIN: { terrainToXML(&xml,(TERRAIN*)data); } break; } try { this->getFileOf(id,bu_path2,K8_MAX_PATH_LEN); } catch (E_8K_RM) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMXml can not find the file having id %i",id); THROW(E_8K_RM,"RMXml can not find a file"); } if (xml.saveToFile(bu_path2)==XML_ERROR_CANNOT_CREATE_FILE) { this->del(id); GLOBALLOGID(PRIORITY_FATAL, "Can not create the file %s",bu_path2); THROW(E_8K_RM,"Can not create a file"); } // zapisi ji take do data.xml aby byla v seznamu if (this->saveToFile(this->rmconfi->getPack(),this->rmconfi->getOtherFile())!=XML_ERROR_OK) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMxml can not write to the index file"); THROW(E_8K_RM,"RMxml can not write to the index file"); } this->unlock(); return maxindex; } void TRM_xml::del(int id) { this->lock(); this->index->delData(0,"xml",id,NULL); if (this->saveToFile(this->rmconfi->getPack(),this->rmconfi->getOtherFile())!=XML_ERROR_OK) // zapisi do souboru { this->unlock(); GLOBALLOGID(PRIORITY_MISSING_FILE,"RMxml can not write to the index file"); THROW(E_8K_RM,"RMxml can not write to the index file"); } if ((*(this->data))[id]!=NULL) // odstranim i z pameti { KMemFree((*(this->data))[id]); } this->unlock(); } void TRM_xml::getIds(DA * ids) { this->index->getListOfIds(ids,0,0,NULL); // hned pod korenem } int TRM_xml::getCount() { this->lock(); int num=this->index->getMaxId(); this->unlock(); return num; } int TRM_xml::getLoaded() { this->lock(); int num=0; int j; for (j=this->data->getNext(-1);j!=-1;j=this->data->getNext(j)) { num++; } this->unlock(); return num; } TRM_xml::TRM_xml(TRM_conf_i * rmc) // konstruktor nema smyl zamykat... { this->data=new DA; this->rmconfi=rmc; // this->structSize=-1; // neni povolena hodnota, tj. nebylo nastaveno int retvalue; try { // muze klidne do bu_path, jeste nic jineho nebezi strpartpath(bu_path,RESOURCES_DIR,this->rmconfi->getPack(),XML_DIR,this->rmconfi->getOtherFile(),K8_MAX_PATH_LEN,".xml"); } catch (E_8K_RM) { GLOBALLOGID(PRIORITY_FATAL, "files.xml is corrupted"); THROW(E_8K_RM,"files.xml is corrupted"); } retvalue=this->readInfoFile(bu_path); // do RMtxt nactu informace kde ma hledat vsechny raster if (retvalue!=XML_ERROR_OK) // chyba pri cteni fajlu o jazycich { GLOBALLOGID(PRIORITY_MISSING_FILE,"The file %s is corrupted",bu_path); THROW(E_8K_RM,"RMxml - The index file is corrupted"); } GLOBALLOGID(PRIORITY_CONFIRM ,"RMxml was created"); } TRM_xml::~TRM_xml() { delete this->rmconfi; int j; for (j=this->data->getNext(-1);j!=-1;j=this->data->getNext(j)) // tj. beru jen notNULL { freeData(j); } delete this->data; GLOBALLOGID(PRIORITY_CONFIRM ,"RMxml was destroyed"); }