diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2018-07-25 19:54:32 +0200 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2018-08-12 11:13:54 +0200 |
commit | d88329fe7b27ccf6a1cdae97d020a12f51d253ad (patch) | |
tree | 5acce236a9a0ab1c55914de8cb57437bbbbb3aba /src | |
parent | 753cb561f3f72662430c89414f971c8137beb43c (diff) |
Rewrite ConfigParser to use pugixml.
Diffstat (limited to 'src')
-rw-r--r-- | src/configparser.cc | 42 | ||||
-rw-r--r-- | src/configparser.h | 10 |
2 files changed, 21 insertions, 31 deletions
diff --git a/src/configparser.cc b/src/configparser.cc index ef657b0..0227d15 100644 --- a/src/configparser.cc +++ b/src/configparser.cc @@ -27,37 +27,33 @@ #include "configparser.h" #include <hugin.hpp> +#include <pugixml.hpp> -#include "saxparser.h" - -ConfigParser::ConfigParser() -{ - str = nullptr; -} - -void ConfigParser::characterData(const std::string& data) +bool ConfigParser::parseString(const std::string& xml) { - if(str) + pugi::xml_document doc; + pugi::xml_parse_result result = doc.load_buffer(xml.data(), xml.size()); + if(result.status) { - str->append(data); + ERR(configparser, "XML parse error: %d", (int)result.offset); + return false; } -} -void ConfigParser::startTag(const std::string& name, const attr_t& attr) -{ - if(name == "value" && attr.find("name") != attr.end()) - { - values[attr.at("name")] = ""; - str = &values[attr.at("name")]; - } -} + //TODO: handle xml version -void ConfigParser::endTag(const std::string& name) -{ - if(name == "value") + pugi::xml_node config_node = doc.child("config"); + for(pugi::xml_node value_node : config_node.children("value")) { - str = nullptr; + auto name = value_node.attribute("name").as_string(); + if(std::string(name) == "") + { + continue; + } + auto value = value_node.child_value(); + values[name] = value; } + + return true; } std::string ConfigParser::value(const std::string& name, const std::string& def) diff --git a/src/configparser.h b/src/configparser.h index 29e47b0..71b33eb 100644 --- a/src/configparser.h +++ b/src/configparser.h @@ -28,20 +28,14 @@ #include <unordered_map> -#include "saxparser.h" - class ConfigParser - : public SAXParser { public: - ConfigParser(); + //! Returns false on failure, true on success. + bool parseString(const std::string& xml); - void characterData(const std::string& data) override; - void startTag(const std::string& name, const attr_t& attr) override; - void endTag(const std::string& name) override; std::string value(const std::string& name, const std::string& def = ""); private: std::unordered_map<std::string, std::string> values; - std::string* str; }; |