diff options
Diffstat (limited to 'Source/XML/xml_helper.cpp')
-rw-r--r-- | Source/XML/xml_helper.cpp | 210 |
1 files changed, 94 insertions, 116 deletions
diff --git a/Source/XML/xml_helper.cpp b/Source/XML/xml_helper.cpp index 08db9c5d..4a7c1bcd 100644 --- a/Source/XML/xml_helper.cpp +++ b/Source/XML/xml_helper.cpp @@ -29,17 +29,12 @@ #include <XML/xml_helper.h> #include <Compat/fileio.h> -TiXmlElement *XmlHelper::findNode(TiXmlDocument &doc, std::string &item, TiXmlElement* element) -{ - //TiXmlElement* element = NULL; - while(item.size()) - { - if (element == NULL) - { +TiXmlElement *XmlHelper::findNode(TiXmlDocument &doc, std::string &item, TiXmlElement *element) { + // TiXmlElement* element = NULL; + while (item.size()) { + if (element == NULL) { element = doc.FirstChildElement(item.substr(0, item.find('/'))); - } - else - { + } else { element = element->FirstChildElement(item.substr(0, item.find('/'))); } @@ -49,25 +44,22 @@ TiXmlElement *XmlHelper::findNode(TiXmlDocument &doc, std::string &item, TiXmlEl if (std::string::npos != item.find('/')) item = item.substr(item.find('/') + 1); else - item = ""; + item = ""; } return element; } -TiXmlElement *XmlHelper::findNode(TiXmlDocument &doc, const char *item, TiXmlElement* element) -{ +TiXmlElement *XmlHelper::findNode(TiXmlDocument &doc, const char *item, TiXmlElement *element) { std::string i = item; return findNode(doc, i, element); } -bool XmlHelper::getNodeValue(TiXmlDocument &doc, const char *item, std::string &text) -{ +bool XmlHelper::getNodeValue(TiXmlDocument &doc, const char *item, std::string &text) { std::string istr(item); - TiXmlElement* element = findNode(doc, istr); + TiXmlElement *element = findNode(doc, istr); - if (element != NULL) - { + if (element != NULL) { text = element->GetText(); return true; } @@ -75,39 +67,33 @@ bool XmlHelper::getNodeValue(TiXmlDocument &doc, const char *item, std::string & return false; } -bool XmlHelper::getNodeValue(TiXmlDocument &doc, const char *item, double &d) -{ +bool XmlHelper::getNodeValue(TiXmlDocument &doc, const char *item, double &d) { std::string text; bool retval = getNodeValue(doc, item, text); - if (retval) - { + if (retval) { d = ::atof(text.c_str()); } return retval; } -bool XmlHelper::getNodeValue(TiXmlDocument &doc, const char *item, float &f) -{ +bool XmlHelper::getNodeValue(TiXmlDocument &doc, const char *item, float &f) { std::string text; bool retval = getNodeValue(doc, item, text); - - if (retval) - { + + if (retval) { f = (float)(::atof(text.c_str())); } - + return retval; } -bool XmlHelper::getNodeValue(TiXmlDocument &doc, TiXmlElement *element, const char *item, std::string &text) -{ +bool XmlHelper::getNodeValue(TiXmlDocument &doc, TiXmlElement *element, const char *item, std::string &text) { std::string istr(item); element = findNode(doc, istr, element); - if (element != NULL) - { + if (element != NULL) { text = element->GetText(); return true; } @@ -115,29 +101,25 @@ bool XmlHelper::getNodeValue(TiXmlDocument &doc, TiXmlElement *element, const ch return false; } -bool XmlHelper::getNodeValue(TiXmlDocument &doc, TiXmlElement *element, const char *item, double &d) -{ +bool XmlHelper::getNodeValue(TiXmlDocument &doc, TiXmlElement *element, const char *item, double &d) { std::string text; bool retval = getNodeValue(doc, element, item, text); - if (retval) - { + if (retval) { d = ::atof(text.c_str()); } return retval; } -bool XmlHelper::getNodeValue(TiXmlDocument &doc, TiXmlElement *element, const char *item, float &f) -{ +bool XmlHelper::getNodeValue(TiXmlDocument &doc, TiXmlElement *element, const char *item, float &f) { std::string text; bool retval = getNodeValue(doc, element, item, text); - - if (retval) - { + + if (retval) { f = (float)(::atof(text.c_str())); } - + return retval; } @@ -146,10 +128,9 @@ void GetRange(const TiXmlElement *el, const std::string &min_str, const std::string &max_str, float &min_val, - float &max_val) -{ + float &max_val) { int result = el->QueryFloatAttribute(val_str.c_str(), &min_val); - if(result != TIXML_SUCCESS){ + if (result != TIXML_SUCCESS) { result = el->QueryFloatAttribute(min_str.c_str(), &min_val); result = el->QueryFloatAttribute(max_str.c_str(), &max_val); } else { @@ -157,35 +138,33 @@ void GetRange(const TiXmlElement *el, } } -bool LoadXML( TiXmlDocument& doc, - const std::string &path, - const std::string type ) -{ +bool LoadXML(TiXmlDocument &doc, + const std::string &path, + const std::string type) { char abs_path[kPathSize]; bool retry = true; - if (FindFilePath(path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths | kAbsPath) == -1){ + if (FindFilePath(path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths | kAbsPath) == -1) { return false; } doc.LoadFile(abs_path); return true; } -bool LoadXMLRetryable( TiXmlDocument& doc, - const std::string &path, - const std::string type ) -{ +bool LoadXMLRetryable(TiXmlDocument &doc, + const std::string &path, + const std::string type) { char abs_path[kPathSize]; bool retry = true; - if (FindFilePath(path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths | kAbsPath) == -1){ + if (FindFilePath(path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths | kAbsPath) == -1) { ErrorResponse err; - while(retry){ - err = DisplayError("Error", - (type+" file \""+path+"\" did not load correctly.").c_str(), - _ok_cancel_retry); - if(err != _retry){ + while (retry) { + err = DisplayError("Error", + (type + " file \"" + path + "\" did not load correctly.").c_str(), + _ok_cancel_retry); + if (err != _retry) { return false; } - if (FindFilePath(path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths | kAbsPath) != -1){ + if (FindFilePath(path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths | kAbsPath) != -1) { retry = false; } } @@ -194,88 +173,87 @@ bool LoadXMLRetryable( TiXmlDocument& doc, return true; } -uint8_t* StackLoadText(const char* path, size_t* size_out) { - uint8_t* mem = NULL; +uint8_t *StackLoadText(const char *path, size_t *size_out) { + uint8_t *mem = NULL; long file_size = 0; - char abs_path[kPathSize]; + char abs_path[kPathSize]; bool retry = true; if (FindFilePath(path, abs_path, kPathSize, kDataPaths | kModPaths | kAbsPath) != -1) { - FILE* file = my_fopen(abs_path, "rb"); - if(file) { - fseek (file, 0, SEEK_END); - file_size = ftell(file); - if( file_size > 0 ) { - rewind (file); - - LOG_ASSERT(file_size < (1024 * 1024)); - - mem = (uint8_t*)alloc.stack.Alloc(file_size+1); - if(mem) { - size_t count = fread(mem, 1, file_size, file); - if( (long)count == file_size ) { - mem[file_size] = '\0'; - } else { - LOGE << "Did not read expected amount of data from file: " << abs_path << std::endl; - alloc.stack.Free(mem); - mem = NULL; - } - } else { - LOGF << "Could not allocate " << file_size + 1 << " bytes on stack for file " << abs_path << std::endl; - } - } - fclose(file); - } - } - - if( size_out ) { + FILE *file = my_fopen(abs_path, "rb"); + if (file) { + fseek(file, 0, SEEK_END); + file_size = ftell(file); + if (file_size > 0) { + rewind(file); + + LOG_ASSERT(file_size < (1024 * 1024)); + + mem = (uint8_t *)alloc.stack.Alloc(file_size + 1); + if (mem) { + size_t count = fread(mem, 1, file_size, file); + if ((long)count == file_size) { + mem[file_size] = '\0'; + } else { + LOGE << "Did not read expected amount of data from file: " << abs_path << std::endl; + alloc.stack.Free(mem); + mem = NULL; + } + } else { + LOGF << "Could not allocate " << file_size + 1 << " bytes on stack for file " << abs_path << std::endl; + } + } + fclose(file); + } + } + + if (size_out) { *size_out = file_size; } return mem; } -bool LoadText(void* &mem, const char* path){ - FILE* file = my_fopen(path, "rb"); - if(!file){ +bool LoadText(void *&mem, const char *path) { + FILE *file = my_fopen(path, "rb"); + if (!file) { return false; } - fseek (file, 0, SEEK_END); + fseek(file, 0, SEEK_END); long file_size = ftell(file); - rewind (file); - - mem = OG_MALLOC(file_size+1); - if(!mem){ + rewind(file); + + mem = OG_MALLOC(file_size + 1); + if (!mem) { FatalError("Error", "Could not allocate memory for file: %s", path); } size_t read = fread(mem, 1, file_size, file); - if( read != file_size ) { + if (read != file_size) { LOGW << "Read less than ftell told us we would!" << std::endl; } - ((char*)mem)[read] = '\0'; + ((char *)mem)[read] = '\0'; fclose(file); - + return true; } -bool LoadTextRetryable(void* &mem, - const std::string &path, - const std::string type ) -{ +bool LoadTextRetryable(void *&mem, + const std::string &path, + const std::string type) { char abs_path[kPathSize]; bool retry = true; if (FindFilePath(path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths | kAbsPath) == -1) { ErrorResponse err; - while(retry){ - err = DisplayError("Error", - (type+" file \""+path+"\" did not load correctly.").c_str(), - _ok_cancel_retry); - if(err != _retry){ + while (retry) { + err = DisplayError("Error", + (type + " file \"" + path + "\" did not load correctly.").c_str(), + _ok_cancel_retry); + if (err != _retry) { return false; } - if (FindFilePath(path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths | kAbsPath) != -1){ + if (FindFilePath(path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths | kAbsPath) != -1) { retry = false; } } @@ -284,9 +262,9 @@ bool LoadTextRetryable(void* &mem, return true; } -void LoadAttribIntoString(TiXmlElement* el, const char* attrib, std::string &str){ - const char* label = el->Attribute(attrib); - if(label){ +void LoadAttribIntoString(TiXmlElement *el, const char *attrib, std::string &str) { + const char *label = el->Attribute(attrib); + if (label) { str = label; } } |