From 4c00c6238e2bf43cb1456f2f538615b93975038c Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Thu, 19 Mar 2020 14:07:56 -0600 Subject: [PATCH] Join config definition and config parser --- llarp/config/config.cpp | 117 ++++++++++++++++++++++------------------ llarp/config/config.hpp | 6 +-- 2 files changed, 67 insertions(+), 56 deletions(-) diff --git a/llarp/config/config.cpp b/llarp/config/config.cpp index 0a660242c..b6c625dfd 100644 --- a/llarp/config/config.cpp +++ b/llarp/config/config.cpp @@ -293,7 +293,7 @@ namespace llarp } */ (void)conf; - throw std::runtime_error("FIXME"); + // throw std::runtime_error("FIXME"); } void @@ -301,7 +301,7 @@ namespace llarp { // routers.emplace_back(val.begin(), val.end()); (void)conf; - throw std::runtime_error("FIXME"); + // throw std::runtime_error("FIXME"); } void @@ -309,7 +309,7 @@ namespace llarp { // services.emplace_back(str(key), str(val)); // str()'s here for gcc 5 compat (void)conf; - throw std::runtime_error("FIXME"); + // throw std::runtime_error("FIXME"); } void @@ -382,7 +382,7 @@ namespace llarp } */ (void)conf; - throw std::runtime_error("FIXME"); + // throw std::runtime_error("FIXME"); } void @@ -447,72 +447,83 @@ namespace llarp */ } - template < typename Section > - Section - find_section(const ConfigParser &, const std::string &) + bool + Config::Load(const char *fname) { - throw std::runtime_error("FIXME"); - /* - Section section; - - auto visitor = [&](const ConfigParser::SectionValues_t& sectionValues) { - return section.parseSectionValues(parser, sectionValues); - }; - - // TODO: exceptions, please. fuck. - // parser.VisitSection just passes-through the return value of our - // lambda from above - if(parser.VisitSection(name.c_str(), visitor)) + // TODO: DRY + try { - return section; - } + Configuration conf; + initializeConfig(conf); - return {}; - */ - } + ConfigParser parser; + if(!parser.LoadFile(fname)) + { + return false; + } - bool - Config::Load(const char* fname) - { - ConfigParser parser; - if (!parser.LoadFile(fname)) + parser.IterAll([&](string_view section, const SectionValues_t& values) { + for (const auto& pair : values) + { + conf.addConfigValue(section, pair.first, pair.second); + } + }); + + // TODO: hand parsed data to conf, pull out values + return true; + } + catch(const std::exception& e) { + LogError("Error trying to init and parse config from file: ", e.what()); return false; } - - return parse(parser); } bool Config::LoadFromStr(string_view str) { - ConfigParser parser; - if (!parser.LoadFromStr(str)) + // TODO: DRY + try + { + Configuration conf; + initializeConfig(conf); + + ConfigParser parser; + if(!parser.LoadFromStr(str)) + { + return false; + } + + // TODO: hand parsed data to conf, pull out values + return true; + } + catch(const std::exception& e) { + LogError("Error trying to init and parse config from string: ", e.what()); return false; } - - return parse(parser); } - bool - Config::parse(const ConfigParser& parser) - { - if (Lokinet_INIT()) - return false; - router = find_section(parser, "router"); - network = find_section(parser, "network"); - connect = find_section(parser, "connect"); - netdb = find_section(parser, "netdb"); - dns = find_section(parser, "dns"); - links = find_section(parser, "bind"); - services = find_section(parser, "services"); - system = find_section(parser, "system"); - api = find_section(parser, "api"); - lokid = find_section(parser, "lokid"); - bootstrap = find_section(parser, "bootstrap"); - logging = find_section(parser, "logging"); - return true; + void + Config::initializeConfig(Configuration& conf) + { + // TODO: this seems like a random place to put this, should this be closer + // to main() ? + if(Lokinet_INIT()) + throw std::runtime_error("Can't initializeConfig() when Lokinet_INIT() == true"); + + router.defineConfigOptions(conf); + network.defineConfigOptions(conf); + connect.defineConfigOptions(conf); + netdb.defineConfigOptions(conf); + dns.defineConfigOptions(conf); + links.defineConfigOptions(conf); + services.defineConfigOptions(conf); + system.defineConfigOptions(conf); + api.defineConfigOptions(conf); + lokid.defineConfigOptions(conf); + bootstrap.defineConfigOptions(conf); + logging.defineConfigOptions(conf); } fs::path diff --git a/llarp/config/config.hpp b/llarp/config/config.hpp index 8ab8131f5..f5f0af2c0 100644 --- a/llarp/config/config.hpp +++ b/llarp/config/config.hpp @@ -236,9 +236,9 @@ namespace llarp struct Config { - public: - bool - parse(const ConfigParser& parser); + private: + void + initializeConfig(Configuration& conf); public: RouterConfig router;