/** ****************************************************************************** @file /common/rm/rmxmlstruct.cpp @brief Funkce na prevod struktur do XML a zpet @author Vta @version 1.0 ******************************************************************************/ #include "rmxmlstruct.h" using namespace World; namespace rm { void freeTclStruct(TCL_SCRIPT * data) { int i; if (data->input!=NULL) { for (i=0;iinput_size;i++) { if (data->input[i].name!=NULL) KMemFree(data->input[i].name); } KMemFree(data->input); } KMemFree(data->code); if (data->output!=NULL) { for (i=0;ioutput_size;i++) { if (data->output[i].name!=NULL) KMemFree(data->output[i].name); } KMemFree(data->output); } KMemFree(data); } void xmlToTcl(TCL_SCRIPT ** data,TXMLdata * xml,int id) { (*data)=(TCL_SCRIPT *) KMemAlloc(sizeof(TCL_SCRIPT)); (*data)->id=id; int i; size_t size=0; xml->getDataInt("head",0,"param",0,NULL); (*data)->input_size=xml->getMaxId("head",0,"param",0,NULL); if (((*data)->input_size)<0) { (*data)->input_size=0; // GLOBALLOGID(PRIORITY_ERROR_IN_XML,"Bad XML file having id %i",id); // THROW(E_8K_RM,"Bad XML file"); } (*data)->input=(TCL_VAR*)KMemAlloc(sizeof(TCL_VAR)*((*data)->input_size)); char type[MAX_TYPE_LENGTH]; for (i=1;i<=((*data)->input_size);i++) // v xml jsou indexovany od 1, v kodu pak od 0 { if (xml->getData(type,MAX_TYPE_LENGTH,"head",0,"param",i,"type",0,NULL)==XML_ERROR_NO_SUCH_A_PATH) { GLOBALLOGID(PRIORITY_ERROR_IN_XML,"Bad XML file (script) having id %i",id); THROW(E_8K_RM,"Spatny XML soubor"); } if (strcmp(type,"int")==0) { (*data)->input[i - 1].type = TVT_INT; } else if ((strcmp(type, "double")==0) || strcmp(type, "float") == 0) { (*data)->input[i - 1].type = TVT_FLOAT; } else if (strcmp(type,"string")==0) { (*data)->input[i - 1].type = TVT_STRING; } else if (strcmp(type, "unit") == 0) { (*data)->input[i - 1].type = TVT_UNIT; } else if (strcmp(type, "building") == 0) { (*data)->input[i - 1].type = TVT_BUILDING; } else if (strcmp(type, "hex") == 0) { (*data)->input[i - 1].type = TVT_HEX; } else if (strcmp(type, "int_list") == 0) { (*data)->input[i - 1].type = TVT_INT_LIST; } else if (strcmp(type, "float_list") == 0) { (*data)->input[i - 1].type = TVT_FLOAT_LIST; } else if (strcmp(type, "string_list") == 0) { (*data)->input[i - 1].type = TVT_STRING_LIST; } else { GLOBALLOGID(PRIORITY_ERROR_IN_XML,"Bad XML file (script) having id %i - unknown type %s",id,type); THROW(E_8K_RM,"Spatny XML soubor"); } (*data)->input[i-1].name=0; size=0; if (xml->getDataResize(&((*data)->input[i-1].name),size,"head",0,"param",i,"name",0,NULL)==XML_ERROR_NO_SUCH_A_PATH) { GLOBALLOGID(PRIORITY_ERROR_IN_XML,"Bad XML file (script) having id %i",id); THROW(E_8K_RM,"Spatny XML soubor"); } } size=0; (*data)->code=0; xml->getDataResize(&((*data)->code),size,"body",0,NULL); (*data)->output_size=xml->getMaxId("tail",0,"result",0,NULL); if (((*data)->output_size)<0) { (*data)->output_size=0; } (*data)->output=(TCL_VAR*)KMemAlloc(sizeof(TCL_VAR)*((*data)->output_size)); for (i=1;i<=(*data)->output_size;i++) { if (xml->getData(type,10,"tail",0,"result",i,"type",0,NULL)==XML_ERROR_NO_SUCH_A_PATH) { GLOBALLOGID(PRIORITY_ERROR_IN_XML,"Bad XML file (script) having id %i",id); THROW(E_8K_RM,"Spatny XML soubor"); } if (strcmp(type,"int")==0) { (*data)->output[i - 1].type = TVT_INT; } else if ((strcmp(type, "double") == 0) || (strcmp(type, "float") == 0)) { (*data)->output[i - 1].type = TVT_FLOAT; } else if (strcmp(type,"string")==0) { (*data)->output[i - 1].type = TVT_STRING; } else if (strcmp(type, "unit") == 0) { (*data)->output[i - 1].type = TVT_UNIT; } else if (strcmp(type, "building") == 0) { (*data)->output[i - 1].type = TVT_BUILDING; } else if (strcmp(type, "hex") == 0) { (*data)->output[i - 1].type = TVT_HEX; } else if (strcmp(type, "int_list") == 0) { (*data)->output[i - 1].type = TVT_INT_LIST; } else if (strcmp(type, "float_list") == 0) { (*data)->output[i - 1].type = TVT_FLOAT_LIST; } else if (strcmp(type, "string_list") == 0) { (*data)->output[i - 1].type = TVT_STRING_LIST; } else if (strcmp(type, "int_array") == 0) { (*data)->output[i - 1].type = TVT_INT_ARRAY; } else if (strcmp(type, "float_array") == 0) { (*data)->output[i - 1].type = TVT_FLOAT_ARRAY; } else if (strcmp(type, "string_array") == 0) { (*data)->output[i - 1].type = TVT_STRING_ARRAY; } else { GLOBALLOGID(PRIORITY_ERROR_IN_XML,"Bad XML file (script) having id %i - unknown type",id); THROW(E_8K_RM,"Spatny XML soubor"); } (*data)->output[i-1].name=0; size=0; if (xml->getDataResize(&((*data)->output[i-1].name),size,"tail",0,"result",i,"name",0,NULL)==XML_ERROR_NO_SUCH_A_PATH) { GLOBALLOGID(PRIORITY_ERROR_IN_XML,"Bad XML file (script) having id %i",id); THROW(E_8K_RM,"Spatny XML soubor"); } } } void tclToXML(TXMLdata * xml,TCL_SCRIPT * data) { xml->setDTD("\n]>"); int i; for (i=0;iinput_size;i++) { xml->setData(data->input[i].name,"head",0,"param",i+1,"name",0,NULL); switch (data->input[i].type) { case TVT_INT: { xml->setData("int","head",0,"param",i+1,"type",0,NULL); } break; case TVT_FLOAT: { xml->setData("float","head",0,"param",i+1,"type",0,NULL); } break; case TVT_STRING: { xml->setData("string","head",0,"param",i+1,"type",0,NULL); } break; } } xml->setData(data->code,"body",0,NULL); for (i=0;ioutput_size;i++) { xml->setData(data->output[i].name,"tail",0,"result",i+1,"name",0,NULL); switch (data->output[i].type) { case TVT_INT: { xml->setData("int","tail",0,"result",i+1,"type",0,NULL); } break; case TVT_FLOAT: { xml->setData("float","tail",0,"result",i+1,"type",0,NULL); } break; case TVT_STRING: { xml->setData("string","tail",0,"result",i+1,"type",0,NULL); } break; } } } void xmlToTerrain(TERRAIN ** data,TXMLdata * xml,int id) // data se budou alokovat { (*data)=(TERRAIN *) KMemAlloc(sizeof(TERRAIN)); if (((*data)->hit_probability_reduction=(float)xml->getDataDouble("hit_probability_reduction",0,NULL))<0) { GLOBALLOGID(PRIORITY_ERROR_IN_XML,"Bad XML file (terrain) having id %i",id); THROW(E_8K_RM,"Spatny XML soubor"); } if (((*data)->visibility_reduction=xml->getDataInt("visibility_reduction",0,NULL))<0) { GLOBALLOGID(PRIORITY_ERROR_IN_XML,"Bad XML file (terrain) having id %i",id); THROW(E_8K_RM,"Spatny XML soubor"); } if ((*data)->visibility_reduction>1) { GLOBALLOGID(PRIORITY_ERROR_IN_XML,"Bad XML file (terrain) having id %i",id); THROW(E_8K_RM,"Spatny XML soubor"); } if (((*data)->healing_reduction=(float)xml->getDataDouble("healing_reduction",0,NULL))<0) { GLOBALLOGID(PRIORITY_ERROR_IN_XML,"Bad XML file (terrain) having id %i",id); THROW(E_8K_RM,"Spatny XML soubor"); } } void terrainToXML(TXMLdata * xml,TERRAIN * data) { xml->setDataDouble(data->healing_reduction,"healing_reduction",0,NULL); xml->setDataDouble(data->hit_probability_reduction,"hit_probability_reduction",0,NULL); xml->setDataInt(data->visibility_reduction,"visibility_reduction",0,NULL); } void freeTerrainStruct(TERRAIN * data) { KMemFree(data); } } // namespace