/** ****************************************************************************** @file /common/rm/rmsymbol.cpp @brief RM přistupující k symbolům @author Vta @version 1.0 ******************************************************************************/ #include "common/rm/rmsymbol.h" namespace rm { void TRM_symbol::freeData(int id) { this->freeSymbols(id); } void TRM_symbol::freeSymbols(int id) { this->lock(); if ((*(this->symbols))[id]==NULL) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMsymbol can not release group %i",id); THROW(E_8K_RM,"RMsymbol can not release a group"); } else { delete ((*(this->symbols))[id]); (*(this->symbols))[id]=NULL; } this->unlock(); } void TRM_symbol::loadData(int id) { this->loadSymbols(id); } /// Pomocny buffer char bu_data[K8_MAX_FILE_NAME_LEN-4]; void TRM_symbol::getFileOfSymbol(int id,char * path,int pathsize) { try // typ, jmeno souboru { this->index->getData(bu_data,K8_MAX_FILE_NAME_LEN-4,"symbol",id,"file",0,NULL); } catch (E_8K_RM) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMsymbol record of the group id %i is missing",id); THROW(E_8K_RM,"RMsymbol The requested data is missing"); } try { strpartpath(path,RESOURCES_DIR,this->rmconfi->getPack(),SYMBOLS_DIR,bu_data,pathsize,".xml"); } catch (E_8K_RM) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "files.xml is corrupted"); THROW(E_8K_RM,"files.xml is corrupted"); } } // bez pripony .xml /// Pomocny buffer static char bu_path[K8_MAX_PATH_LEN]; void TRM_symbol::loadSymbols(int id) { this->lock(); if ((*(this->symbols))[id]==NULL) { try { this->getFileOfSymbol(id,bu_path,K8_MAX_PATH_LEN); } catch (E_8K_RM) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMsymbol can not find the path of the group %i",id); THROW(E_8K_RM,"RMsymbol can not find the path to the requested file"); } (*(this->symbols))[id]=new DA; TXMLdata * temp = new TXMLdata; int res=temp->readFile(bu_path); if (res!=XML_ERROR_OK) { GLOBALLOGID(PRIORITY_FATAL, "RMsymbol can not load the file %s of a group id %i",bu_path,id); THROW(E_8K_RM,"RMsymbol can not load the requested file"); } int min=temp->getMinId(); int max=temp->getMaxId(); DA ids(max-min+1,min); temp->getListOfIds(&ids,NULL,0,NULL); // DA daids; // max=temp->getListOfIds(&daids); int rasterId=this->index->getDataInt("symbol",id,"rasterid",0,NULL); if (rasterId<0) { GLOBALLOGID(PRIORITY_FATAL, "RMsymbol The group %i does not refer to any raster",id); THROW(E_8K_RM,"RMsymbol The requested group does not refer to any raster"); } int i; for (i=ids.getNext(-1);i!=-1;i=ids.getNext(i)) { if (ids[i]!=0) // kvuli poslednimu prvku ids[max] { (*(*(this->symbols))[id])[i]=(TSymbolData*) KMemAlloc(sizeof(TSymbolData)); (*(*(this->symbols))[id])[i]->x=temp->getDataInt("symbol",i,"x",0,NULL); (*(*(this->symbols))[id])[i]->y=temp->getDataInt("symbol",i,"y",0,NULL); (*(*(this->symbols))[id])[i]->width=temp->getDataInt("symbol",i,"width",0,NULL); (*(*(this->symbols))[id])[i]->height=temp->getDataInt("symbol",i,"height",0,NULL); (*(*(this->symbols))[id])[i]->id=i; (*(*(this->symbols))[id])[i]->rasterId=rasterId; (*(*(this->symbols))[id])[i]->groupId=id; if ( ((*(*(this->symbols))[id])[i]->x<0) || ((*(*(this->symbols))[id])[i]->y<0) || ((*(*(this->symbols))[id])[i]->width<0) || ((*(*(this->symbols))[id])[i]->height<0) ) { GLOBALLOGID(PRIORITY_FATAL, "RMsymbol the symbol %i in the group %i is corrupted",i,id); THROW(E_8K_RM,"RMsymbol the requested symbol is corrupted"); } } } delete(temp); } this->unlock(); } void TRM_symbol::requestSymbolsDep(int id) { this->lock(); this->request(id); this->unlock(); TRM_raster_i * RMrasteri=(TRM_raster_i *)KSendMessage(RQUEUE,MSG_GET_RM_RASTER_I,MOD_RM,MOD_RM,NULL); RMrasteri->request(this->getRaster(id)); delete(RMrasteri); } /// Pomocny buffer static char bu_path2[K8_MAX_PATH_LEN]; void TRM_symbol::saveGroupToFile(int groupId) { if ((*(this->symbols))[groupId]!=NULL) { try { this->getFileOfSymbol(group,bu_path2,K8_MAX_PATH_LEN); } catch (E_8K_RM) { GLOBALLOGID(PRIORITY_FATAL, "RMsymbol can not find the path of group having id %i",group); THROW(E_8K_RM,"RMsymbol can not find a path of a group"); } TXMLdata * temp = new TXMLdata; int i; for (i=(*(this->symbols))[groupId]->getNext(-1);i!=-1;i=(*(this->symbols))[groupId]->getNext(i)) { temp->setDataInt((*(*(this->symbols))[groupId])[i]->x,"symbol",i,"x",0,NULL); temp->setDataInt((*(*(this->symbols))[groupId])[i]->y,"symbol",i,"y",0,NULL); temp->setDataInt((*(*(this->symbols))[groupId])[i]->width,"symbol",i,"width",0,NULL); temp->setDataInt((*(*(this->symbols))[groupId])[i]->height,"symbol",i,"height",0,NULL); } if (temp->saveToFile(bu_path2)!=XML_ERROR_OK) // zapisu { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMsybmol can not write to the file %s",bu_path2); THROW(E_8K_RM,"RMsybmol can not write to a file"); } delete (temp); } } void TRM_symbol::addSymbol(TSymbolData * sym) { this->lock(); if ((*(this->symbols))[sym->groupId]!=NULL) { if ((*(*(this->symbols))[sym->groupId])[sym->id]==NULL) { (*(*(this->symbols))[sym->groupId])[sym->id]=(TSymbolData *) KMemAlloc(sizeof(TSymbolData)); (*(*(this->symbols))[sym->groupId])[sym->id]->id=sym->id; (*(*(this->symbols))[sym->groupId])[sym->id]->height=sym->height; (*(*(this->symbols))[sym->groupId])[sym->id]->rasterId=sym->rasterId; (*(*(this->symbols))[sym->groupId])[sym->id]->width=sym->width; (*(*(this->symbols))[sym->groupId])[sym->id]->groupId=sym->groupId; (*(*(this->symbols))[sym->groupId])[sym->id]->x=sym->x; (*(*(this->symbols))[sym->groupId])[sym->id]->y=sym->y; try { this->saveGroupToFile(sym->groupId); } catch (E_8K_RM) { unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMsymbol can not save the file of group %i",sym->groupId); THROW(E_8K_RM,"RMsymbol can not save the file of a group"); } } else { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMsymbol can not add the symbol %i which already exists",sym->id); THROW(E_8K_RM,"RMsymbol can not add a symbol which already exits"); } } else { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMsymbol can not add any symbol to non-existing group %i",sym->groupId); THROW(E_8K_RM,"RMsymbol can not add a symbol to a non-existing group"); } this->unlock(); } void TRM_symbol::modifySymbol(TSymbolData * sym) { this->lock(); if ((*(this->symbols))[sym->groupId]!=NULL) { if ((*(*(this->symbols))[sym->groupId])[sym->id]!=NULL) { (*(*(this->symbols))[sym->groupId])[sym->id]->id=sym->id; (*(*(this->symbols))[sym->groupId])[sym->id]->height=sym->height; (*(*(this->symbols))[sym->groupId])[sym->id]->rasterId=sym->rasterId; (*(*(this->symbols))[sym->groupId])[sym->id]->groupId=sym->groupId; (*(*(this->symbols))[sym->groupId])[sym->id]->width=sym->width; (*(*(this->symbols))[sym->groupId])[sym->id]->x=sym->x; (*(*(this->symbols))[sym->groupId])[sym->id]->y=sym->y; try { this->saveGroupToFile(sym->groupId); } catch (E_8K_RM) { unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMsymbol can not save the group %i",sym->groupId); THROW(E_8K_RM,"RMsymbol can not save a group"); } } else { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMsymbol the symbol id %i does not exists in group %i",sym->id,sym->groupId); THROW(E_8K_RM,"RMsymbol can not modify non-existing symbol"); } } else { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMsymbol the group %i does not exist - can not add any symbol",sym->groupId); THROW(E_8K_RM,"RMsymbol can not add any symboly to a non-existing group"); } this->unlock(); } void TRM_symbol::delSymbol(int group,int id) { this->lock(); if ((*(this->symbols))[group]!=NULL) { if ((*(*(this->symbols))[group])[id]==NULL) { this->unlock(); GLOBALLOGID(PRIORITY_MISSING_FILE,"RMsymbol can not remove the symbol %i in the group %i",id,group); THROW(E_8K_RM,"RMsymbol can not remove a symbol"); } else { KMemFree((*(*(this->symbols))[group])[id]); try { this->saveGroupToFile(group); } catch (E_8K_RM) { unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMmap can not save the group %i",group); THROW(E_8K_RM,"RMmap can not save a group"); } } } else { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMsymbol the group %i does not exists - can not remove a symbol",group); THROW(E_8K_RM,"RMsymbol can not remove any symbol from non-existing group"); } this->unlock(); } void TRM_symbol::getSymbol(TSymbolData ** sym,int symgroupid,int symid) { this->lock(); if ((*(this->symbols))[symgroupid]!=NULL) // tj uz na nej byl zavolan load { if( (*(*(this->symbols))[symgroupid])[symid] != NULL) { (*sym)=(*(*(this->symbols))[symgroupid])[symid]; } else { (*sym)=NULL; /* this->unlock(); GLOBALLOGID(PRIORITY_MISSING_FILE,"V RMsymbol v skupine %d neexistuje symbol %d", symgroupid, symid); THROW(E_8K_RM,"V RMsymbol neexistuje symbol v skupine"); */ } } else { this->unlock(); GLOBALLOGID(PRIORITY_MISSING_FILE,"RMsymbol can not access not loaded data having id %i",symgroupid); THROW(E_8K_RM,"RMsymbol can not access not loaded data"); } this->unlock(); } void TRM_symbol::getSymbols(DA * sym,int id) { this->lock(); if ((*(this->symbols))[id]!=NULL) // tj uz na nej byl zavolan load { sym=(*(this->symbols))[id]; } else { this->unlock(); GLOBALLOGID(PRIORITY_MISSING_FILE,"RMsymbol can not access not loaded data having id %i",id); THROW(E_8K_RM,"RMsymbol can not access not loaded data"); } this->unlock(); } void TRM_symbol::getSymbols(TSymbolData ** sym,int id,int size) { this->lock(); if ((*(this->symbols))[id]!=NULL) // tj uz na nej byl zavolan load { int i=(*(this->symbols))[id]->getNext(-1); // pozice v DA int j=0; // pozice v sym while ((i!=-1) && (jsymbols))[id])[i]; j++; i=(*(this->symbols))[id]->getNext(i); } } else { this->unlock(); GLOBALLOGID(PRIORITY_MISSING_FILE,"RMsymbol can not access not loaded data having id %i",id); THROW(E_8K_RM,"RMsymbol can not access not loaded data"); } this->unlock(); } void TRM_symbol::delGroup(int groupId) { this->lock(); if (this->index->delData(0,"symbol",groupId)==XML_ERROR_NO_SUCH_A_PATH) { this->unlock(); GLOBALLOGID(PRIORITY_MISSING_FILE,"RMsymbol can not remove a non-existing group id %i",groupId); THROW(E_8K_RM,"RMsymbol can not remove a non-existing group"); } if (this->saveToFile(this->rmconfi->getPack(),this->rmconfi->getSymbolFile())!=XML_ERROR_OK) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMsymbol can not write to the index file"); THROW(E_8K_RM,"RMsymbol can not write to the index file"); } this->unlock(); } void TRM_symbol::addGroup(int group,char *file,int rasterId) { this->lock(); if (group<1) { if (group==-1) // vezmu prvni volny { int max=this->index->getMaxId(); max++; int i; for (i=1;iindex->getData(NULL,0,"symbol",group,"file",0,NULL)==XML_ERROR_NO_SUCH_A_PATH) break; } group=i; } else { this->unlock(); GLOBALLOGID(PRIORITY_MISSING_FILE,"RMsymbol can not add any group with id %i",group); THROW(E_8K_RM,"RMmap can not add any group with id less than 1"); } } int result=this->index->getData(NULL,0,"symbol",group,"file",0,NULL); if (result==XML_ERROR_NO_SUCH_A_PATH) // pouze pokud tam jeste neni! { this->index->setDataInt(rasterId,"symbol",group,"rasterid",0,NULL); // vytvorim novou skupinu this->index->setData(file,"symbol",group,"file",0,NULL); if (this->saveToFile(this->rmconfi->getPack(),this->rmconfi->getSymbolFile())!=XML_ERROR_OK) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMsymbol can not write to the index file"); THROW(E_8K_RM,"RMsymbol can not write to the index file"); } } else { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMsymbol the group %i already exists",group); THROW(E_8K_RM,"RMsymbol the group already exists"); } this->unlock(); } int TRM_symbol::getRaster(int id) { this->lock(); int group=-1; try { group=this->index->getDataInt("symbol",id,"rasterid",0,NULL); } catch (E_8K_RM) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMsymbol The index file is corrupted at index %i",id); THROW(E_8K_RM,"RMsymbol The index file is corrupted"); } this->unlock(); return group; } int TRM_symbol::getSymbolsCount(int id) { this->lock(); int count = 0; int j; for (j=(*(this->symbols))[id]->getNext(-1);j!=-1;j=(*(this->symbols))[id]->getNext(j)) { count++; } this->unlock(); return count; } int TRM_symbol::getGroupsOfSymbolsLoaded() { this->lock(); int num=0; int j; for (j=this->symbols->getNext(-1);j!=-1;j=this->symbols->getNext(j)) { num++; } this->unlock(); return num; } int TRM_symbol::getGroupsOfSymbolsCount() { this->lock(); int num=this->index->getNumId(); this->unlock(); return num; } TRM_symbol::TRM_symbol(TRM_conf_i * rmc) // konstruktor nema smyl zamykat... { this->symbols=new DA*>; this->rmconfi=rmc; size_t pathsize=100; char *path=(char*)KMemAlloc(pathsize*sizeof(char)); int retvalue; try { strpath(&path,RESOURCES_DIR,this->rmconfi->getPack(),XML_DIR,this->rmconfi->getSymbolFile(),pathsize,".xml"); } catch (E_8K_RM) { KMemFree(path); // pro pripad vyjimky GLOBALLOGID(PRIORITY_FATAL, "Corrupted files.xml"); THROW(E_8K_RM,"Corrupted files.xml"); } retvalue=this->readInfoFile(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",path); THROW(E_8K_RM,"The index file is corrupted"); } KMemFree(path); GLOBALLOGID(PRIORITY_CONFIRM ,"RMsymbol was created"); } TRM_symbol::~TRM_symbol() { delete (this->rmconfi); int i; int j; for (j=this->symbols->getNext(-1);j!=-1;j=this->symbols->getNext(j)) { for (i=(*(this->symbols))[j]->getNext(-1);i!=-1;i=(*(this->symbols))[j]->getNext(i)) { KMemFree((*(*(this->symbols))[j])[i]); } delete ((*(this->symbols))[j]); } delete (this->symbols); GLOBALLOGID(PRIORITY_CONFIRM ,"RMsymbol was destroyed"); } }