Refactoring; Use TinyXml methods to parse bool/int attribute values

This commit is contained in:
Daniel Marjamäki 2021-05-01 18:40:20 +02:00
parent dbe9eb2a27
commit e78612d280
2 changed files with 14 additions and 34 deletions

View File

@ -171,10 +171,11 @@ static std::string readAttrString(const tinyxml2::XMLElement *e, const char *att
static long long readAttrInt(const tinyxml2::XMLElement *e, const char *attr, bool *error) static long long readAttrInt(const tinyxml2::XMLElement *e, const char *attr, bool *error)
{ {
const char *value = e->Attribute(attr); int64_t value = 0;
if (!value && error) bool err = (e->QueryInt64Attribute(attr, &value) != XML_SUCCESS);
*error = true; if (error)
return value ? std::atoi(value) : 0; *error = err;
return value;
} }
bool CTU::FileInfo::CallBase::loadBaseFromXml(const tinyxml2::XMLElement *xmlElement) bool CTU::FileInfo::CallBase::loadBaseFromXml(const tinyxml2::XMLElement *xmlElement)

View File

@ -154,10 +154,7 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
if (strcmp(rootnode->Name(),"def") != 0) if (strcmp(rootnode->Name(),"def") != 0)
return Error(ErrorCode::UNSUPPORTED_FORMAT, rootnode->Name()); return Error(ErrorCode::UNSUPPORTED_FORMAT, rootnode->Name());
const char* format_string = rootnode->Attribute("format"); int format = rootnode->IntAttribute("format", 1); // Assume format version 1 if nothing else is specified (very old .cfg files had no 'format' attribute)
int format = 1; // Assume format version 1 if nothing else is specified (very old .cfg files had no 'format' attribute)
if (format_string)
format = atoi(format_string);
if (format > 2 || format <= 0) if (format > 2 || format <= 0)
return Error(ErrorCode::UNSUPPORTED_FORMAT); return Error(ErrorCode::UNSUPPORTED_FORMAT);
@ -193,16 +190,8 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
AllocFunc temp = {0}; AllocFunc temp = {0};
temp.groupId = allocationId; temp.groupId = allocationId;
if (memorynode->Attribute("init", "false")) temp.initData = memorynode->BoolAttribute("init", false);
temp.initData = false; temp.arg = memorynode->IntAttribute("arg", -1);
else
temp.initData = true;
const char *arg = memorynode->Attribute("arg");
if (arg)
temp.arg = atoi(arg);
else
temp.arg = -1;
const char *bufferSize = memorynode->Attribute("buffer-size"); const char *bufferSize = memorynode->Attribute("buffer-size");
if (!bufferSize) if (!bufferSize)
@ -228,13 +217,8 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
return Error(ErrorCode::BAD_ATTRIBUTE_VALUE, bufferSize); return Error(ErrorCode::BAD_ATTRIBUTE_VALUE, bufferSize);
} }
if (memorynodename == "realloc") { if (memorynodename == "realloc")
const char *reallocArg = memorynode->Attribute("realloc-arg"); temp.reallocArg = memorynode->IntAttribute("realloc-arg", 1);
if (reallocArg)
temp.reallocArg = atoi(reallocArg);
else
temp.reallocArg = 1;
}
if (memorynodename != "realloc") if (memorynodename != "realloc")
mAlloc[memorynode->GetText()] = temp; mAlloc[memorynode->GetText()] = temp;
@ -243,11 +227,7 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
} else if (memorynodename == "dealloc") { } else if (memorynodename == "dealloc") {
AllocFunc temp = {0}; AllocFunc temp = {0};
temp.groupId = allocationId; temp.groupId = allocationId;
const char *arg = memorynode->Attribute("arg"); temp.arg = memorynode->IntAttribute("arg", 1);
if (arg)
temp.arg = atoi(arg);
else
temp.arg = 1;
mDealloc[memorynode->GetText()] = temp; mDealloc[memorynode->GetText()] = temp;
} else if (memorynodename == "use") } else if (memorynodename == "use")
functions[memorynode->GetText()].use = true; functions[memorynode->GetText()].use = true;
@ -778,10 +758,9 @@ Library::Error Library::loadFunction(const tinyxml2::XMLElement * const node, co
else if (argnodename == "iterator") { else if (argnodename == "iterator") {
ac.iteratorInfo.it = true; ac.iteratorInfo.it = true;
const char* str = argnode->Attribute("type"); const char* str = argnode->Attribute("type");
ac.iteratorInfo.first = str ? (std::strcmp(str, "first") == 0) : false; ac.iteratorInfo.first = (str && std::strcmp(str, "first") == 0);
ac.iteratorInfo.last = str ? (std::strcmp(str, "last") == 0) : false; ac.iteratorInfo.last = (str && std::strcmp(str, "last") == 0);
str = argnode->Attribute("container"); ac.iteratorInfo.container = argnode->IntAttribute("container", 0);
ac.iteratorInfo.container = str ? std::atoi(str) : 0;
} }
else else