From 11588c639e8912f1b28e981c1a2a0e4306dbd093 Mon Sep 17 00:00:00 2001 From: Chun Kuan Lee Date: Sat, 16 Jun 2018 02:31:26 +0000 Subject: [PATCH] Replace boost program_options --- src/util.cpp | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/src/util.cpp b/src/util.cpp index ab262b406..97b5205ab 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -72,7 +72,6 @@ #endif #include -#include #include #include #include @@ -811,17 +810,47 @@ fs::path GetConfigFile(const std::string& confPath) return AbsPathForConfigVal(fs::path(confPath), false); } +static std::string TrimString(const std::string& str, const std::string& pattern) +{ + std::string::size_type front = str.find_first_not_of(pattern); + if (front == std::string::npos) { + return std::string(); + } + std::string::size_type end = str.find_last_not_of(pattern); + return str.substr(front, end - front + 1); +} + +static std::vector> GetConfigOptions(std::istream& stream) +{ + std::vector> options; + std::string str, prefix; + std::string::size_type pos; + while (std::getline(stream, str)) { + if ((pos = str.find('#')) != std::string::npos) { + str = str.substr(0, pos); + } + const static std::string pattern = " \t\r\n"; + str = TrimString(str, pattern); + if (!str.empty()) { + if (*str.begin() == '[' && *str.rbegin() == ']') { + prefix = str.substr(1, str.size() - 2) + '.'; + } else if ((pos = str.find('=')) != std::string::npos) { + std::string name = prefix + TrimString(str.substr(0, pos), pattern); + std::string value = TrimString(str.substr(pos + 1), pattern); + options.emplace_back(name, value); + } + } + } + return options; +} + bool ArgsManager::ReadConfigStream(std::istream& stream, std::string& error, bool ignore_invalid_keys) { LOCK(cs_args); - std::set setOptions; - setOptions.insert("*"); - - for (boost::program_options::detail::config_file_iterator it(stream, setOptions), end; it != end; ++it) - { - std::string strKey = std::string("-") + it->string_key; - std::string strValue = it->value[0]; + for (const std::pair& option : GetConfigOptions(stream)) { + std::string strKey = std::string("-") + option.first; + std::string strValue = option.second; if (InterpretNegatedOption(strKey, strValue)) { m_config_args[strKey].clear(); @@ -831,7 +860,7 @@ bool ArgsManager::ReadConfigStream(std::istream& stream, std::string& error, boo // Check that the arg is known if (!IsArgKnown(strKey, error) && !ignore_invalid_keys) { - error = strprintf("Invalid configuration value %s", it->string_key.c_str()); + error = strprintf("Invalid configuration value %s", option.first.c_str()); return false; } }