From d88329fe7b27ccf6a1cdae97d020a12f51d253ad Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Wed, 25 Jul 2018 19:54:32 +0200 Subject: Rewrite ConfigParser to use pugixml. --- src/configparser.cc | 42 +++++++++++++++++++----------------------- src/configparser.h | 10 ++-------- 2 files changed, 21 insertions(+), 31 deletions(-) (limited to 'src') 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 +#include -#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 -#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 values; - std::string* str; }; -- cgit v1.2.3