diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-01-20 10:26:22 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-01-20 10:51:24 +0100 |
commit | db9b9f316d7bdcb9504092908bb18e82fc21de2f (patch) | |
tree | 3f7bb1577c5210a9523fd240556cf840665f3efb /src/utils/xml.h | |
parent | 0d1024b64155a05f45f247ad57d0f444db01c1e2 (diff) | |
download | mana-db9b9f316d7bdcb9504092908bb18e82fc21de2f.tar.gz mana-db9b9f316d7bdcb9504092908bb18e82fc21de2f.tar.bz2 mana-db9b9f316d7bdcb9504092908bb18e82fc21de2f.tar.xz mana-db9b9f316d7bdcb9504092908bb18e82fc21de2f.zip |
Made client config statically typed
This makes accessing the config values much faster, since it no longer
needs to do a lookup nor string conversion, which means we could remove
some needless copying of the values.
Overall it makes it easier to find out where settings are used and it
puts the defaults along with the declaration.
Options with default values are no longer saved to the config file. This
does not include unrecognized options, which are kept around to provide
some compatibility with older clients.
While most basic options have kept the same name, more complicated
settings like window geometry, shortcuts, outfits, etc. now have their
own XML elements. Older clients will ignore these and erase them when
saving the configuration.
Diffstat (limited to 'src/utils/xml.h')
-rw-r--r-- | src/utils/xml.h | 68 |
1 files changed, 40 insertions, 28 deletions
diff --git a/src/utils/xml.h b/src/utils/xml.h index 95f7be99..13c06988 100644 --- a/src/utils/xml.h +++ b/src/utils/xml.h @@ -47,7 +47,10 @@ namespace XML std::string_view name() const { return (const char *) node->name; } std::string_view textContent() const; - bool hasProperty(const char *name) const; + template<typename T> + bool attribute(const char *name, T &value); + + bool hasAttribute(const char *name) const; int getProperty(const char *name, int def) const; double getFloatProperty(const char *name, double def) const; std::string getProperty(const char *name, const std::string &def) const; @@ -98,45 +101,58 @@ namespace XML return {}; } - inline bool Node::hasProperty(const char *name) const + inline const char *Node::attribute(const char *name) const { - return xmlHasProp(node, BAD_CAST name) != nullptr; + if (node->type != XML_ELEMENT_NODE) + return nullptr; + + for (xmlAttrPtr prop = node->properties; prop; prop = prop->next) { + if (xmlStrEqual(prop->name, BAD_CAST name)) { + if (prop->children) + return reinterpret_cast<const char*>(prop->children->content); + else + return nullptr; + } + } + return nullptr; } - inline int Node::getProperty(const char *name, int def) const + template<typename T> + inline bool Node::attribute(const char *name, T &value) { - int ret = def; - - if (xmlChar *prop = xmlGetProp(node, BAD_CAST name)) + if (const char *str = attribute(name)) { - ret = atol((char*)prop); - xmlFree(prop); + fromString(str, value); + return true; } + return false; + } + inline bool Node::hasAttribute(const char *name) const + { + return attribute(name) != nullptr; + } + + inline int Node::getProperty(const char *name, int def) const + { + int ret = def; + if (const char *str = attribute(name)) + fromString(str, ret); return ret; } inline double Node::getFloatProperty(const char *name, double def) const { double ret = def; - - if (xmlChar *prop = xmlGetProp(node, BAD_CAST name)) - { - ret = atof((char*)prop); - xmlFree(prop); - } - + if (const char *str = attribute(name)) + fromString(str, ret); return ret; } inline std::string Node::getProperty(const char *name, const std::string &def) const { - if (xmlChar *prop = xmlGetProp(node, BAD_CAST name)) - { - std::string val = (char*)prop; - xmlFree(prop); - return val; - } + if (const char *str = attribute(name)) + return str; return def; } @@ -144,12 +160,8 @@ namespace XML inline bool Node::getBoolProperty(const char *name, bool def) const { bool ret = def; - - if (xmlChar *prop = xmlGetProp(node, BAD_CAST name)) - { - ret = getBoolFromString((char*) prop, def); - xmlFree(prop); - } + if (const char *str = attribute(name)) + ret = getBoolFromString(str, def); return ret; } |