ULTIMA4: Properly implement ConfigElement reading XML properties

This commit is contained in:
Paul Gilbert 2020-03-21 09:59:08 -07:00 committed by Paul Gilbert
parent 9116dded37
commit b4f185eb4d
3 changed files with 28 additions and 83 deletions

View File

@ -67,7 +67,7 @@ const Common::String &XMLNode::reference(const Common::String &h, bool &exists)
const XMLNode *XMLNode::subtree(const Common::String &h) const {
if (h.find('/') == Common::String::npos) {
// Must refer to me.
if (_id.compareToIgnoreCase(h))
if (_id.equalsIgnoreCase(h))
return this;
} else {
// Otherwise we want to split the Common::String at the first /
@ -79,7 +79,7 @@ const XMLNode *XMLNode::subtree(const Common::String &h) const {
Common::String k2 = k.substr(0, k.find('/'));
for (Common::Array<XMLNode *>::const_iterator it = _nodeList.begin();
it != _nodeList.end(); ++it) {
if ((*it)->_id.compareToIgnoreCase(k2)) {
if ((*it)->_id.equalsIgnoreCase(k2)) {
return (*it)->subtree(k);
}
}

View File

@ -72,8 +72,11 @@ public:
const Common::StringMap &attributes() const {
return _attributes;
}
const Common::String &operator[](const Common::String &attrName) const {
return _attributes[attrName];
Common::String operator[](const Common::String &attrName) const {
return _attributes.contains(attrName) ? _attributes[attrName] : "";
}
const Common::Array<XMLNode *> &children() const {
return _nodeList;
}
typedef Std::pair<Common::String, Common::String> KeyType;

View File

@ -45,7 +45,7 @@ Config::~Config() {
}
ConfigElement Config::getElement(const Common::String &name) const {
Common::String key = Common::String::format("/config/%s", name.c_str());
Common::String key = Common::String::format("config/%s", name.c_str());
const Shared::XMLNode *node = _doc.getNode(key);
return ConfigElement(node);
}
@ -87,105 +87,47 @@ ConfigElement &ConfigElement::operator=(const ConfigElement &e) {
* Returns true if the property exists in the current config element
*/
bool ConfigElement::exists(const Common::String &name) const {
#ifdef TODO
xmlChar *prop = xmlGetProp(node, reinterpret_cast<const xmlChar *>(name.c_str()));
bool exists = prop != NULL;
xmlFree(prop);
return exists;
#else
return false;
#endif
return !(*_node)[name].empty();
}
Common::String ConfigElement::getString(const Common::String &name) const {
#ifdef TODO
xmlChar *prop = xmlGetProp(node, reinterpret_cast<const xmlChar *>(name.c_str()));
if (!prop)
return "";
Common::String result(reinterpret_cast<const char *>(prop));
xmlFree(prop);
return result;
#else
return "";
#endif
return (*_node)[name];
}
int ConfigElement::getInt(const Common::String &name, int defaultValue) const {
#ifdef TODO
long result;
xmlChar *prop;
prop = xmlGetProp(node, reinterpret_cast<const xmlChar *>(name.c_str()));
if (!prop)
return defaultValue;
result = strtol(reinterpret_cast<const char *>(prop), NULL, 0);
xmlFree(prop);
return static_cast<int>(result);
#else
return 0;
#endif
Common::String str = (*_node)[name];
return str.empty() ? defaultValue : atol(str.c_str());
}
bool ConfigElement::getBool(const Common::String &name) const {
#ifdef TODO
int result;
Common::String str = (*_node)[name];
if (str.empty())
return false;
xmlChar *prop = xmlGetProp(node, reinterpret_cast<const xmlChar *>(name.c_str()));
if (!prop)
return false;
if (xmlStrcmp(prop, reinterpret_cast<const xmlChar *>("true")) == 0)
result = true;
else
result = false;
xmlFree(prop);
return result;
#else
return false;
#endif
return toupper(str[0]) == 'T' || str == "1";
}
int ConfigElement::getEnum(const Common::String &name, const char *enumValues[]) const {
#ifdef TODO
int result = -1, i;
xmlChar *prop;
Common::String str = (*_node)[name];
if (str.empty())
return 0;
prop = xmlGetProp(node, reinterpret_cast<const xmlChar *>(name.c_str()));
if (!prop)
return 0;
for (i = 0; enumValues[i]; i++) {
if (xmlStrcmp(prop, reinterpret_cast<const xmlChar *>(enumValues[i])) == 0)
result = i;
for (int i = 0; enumValues[i]; ++i) {
if (str.equalsIgnoreCase(enumValues[i]))
return i;
}
if (result == -1)
errorFatal("invalid enum value for %s: %s", name.c_str(), prop);
xmlFree(prop);
return result;
#else
return 0;
#endif
error("invalid enum value for %s: %s", name.c_str(), str.c_str());
}
Std::vector<ConfigElement> ConfigElement::getChildren() const {
const Common::Array<Shared::XMLNode *> &children = _node->children();
Std::vector<ConfigElement> result;
#ifdef TODO
for (Shared::XMLNode * child = node->children; child; child = child->next) {
if (child->type == XML_ELEMENT_NODE)
result.push_back(ConfigElement(child));
}
#endif
for (Common::Array<Shared::XMLNode *>::const_iterator it = children.begin();
it != children.end(); ++it)
result.push_back(*it);
return result;
}