Skip to content

Commit 11588c6

Browse files
committed
Replace boost program_options
1 parent b1dc39d commit 11588c6

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

src/util.cpp

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
#endif
7373

7474
#include <boost/interprocess/sync/file_lock.hpp>
75-
#include <boost/program_options/detail/config_file.hpp>
7675
#include <boost/thread.hpp>
7776
#include <openssl/crypto.h>
7877
#include <openssl/rand.h>
@@ -811,17 +810,47 @@ fs::path GetConfigFile(const std::string& confPath)
811810
return AbsPathForConfigVal(fs::path(confPath), false);
812811
}
813812

813+
static std::string TrimString(const std::string& str, const std::string& pattern)
814+
{
815+
std::string::size_type front = str.find_first_not_of(pattern);
816+
if (front == std::string::npos) {
817+
return std::string();
818+
}
819+
std::string::size_type end = str.find_last_not_of(pattern);
820+
return str.substr(front, end - front + 1);
821+
}
822+
823+
static std::vector<std::pair<std::string, std::string>> GetConfigOptions(std::istream& stream)
824+
{
825+
std::vector<std::pair<std::string, std::string>> options;
826+
std::string str, prefix;
827+
std::string::size_type pos;
828+
while (std::getline(stream, str)) {
829+
if ((pos = str.find('#')) != std::string::npos) {
830+
str = str.substr(0, pos);
831+
}
832+
const static std::string pattern = " \t\r\n";
833+
str = TrimString(str, pattern);
834+
if (!str.empty()) {
835+
if (*str.begin() == '[' && *str.rbegin() == ']') {
836+
prefix = str.substr(1, str.size() - 2) + '.';
837+
} else if ((pos = str.find('=')) != std::string::npos) {
838+
std::string name = prefix + TrimString(str.substr(0, pos), pattern);
839+
std::string value = TrimString(str.substr(pos + 1), pattern);
840+
options.emplace_back(name, value);
841+
}
842+
}
843+
}
844+
return options;
845+
}
846+
814847
bool ArgsManager::ReadConfigStream(std::istream& stream, std::string& error, bool ignore_invalid_keys)
815848
{
816849
LOCK(cs_args);
817850

818-
std::set<std::string> setOptions;
819-
setOptions.insert("*");
820-
821-
for (boost::program_options::detail::config_file_iterator it(stream, setOptions), end; it != end; ++it)
822-
{
823-
std::string strKey = std::string("-") + it->string_key;
824-
std::string strValue = it->value[0];
851+
for (const std::pair<std::string, std::string>& option : GetConfigOptions(stream)) {
852+
std::string strKey = std::string("-") + option.first;
853+
std::string strValue = option.second;
825854

826855
if (InterpretNegatedOption(strKey, strValue)) {
827856
m_config_args[strKey].clear();
@@ -831,7 +860,7 @@ bool ArgsManager::ReadConfigStream(std::istream& stream, std::string& error, boo
831860

832861
// Check that the arg is known
833862
if (!IsArgKnown(strKey, error) && !ignore_invalid_keys) {
834-
error = strprintf("Invalid configuration value %s", it->string_key.c_str());
863+
error = strprintf("Invalid configuration value %s", option.first.c_str());
835864
return false;
836865
}
837866
}

0 commit comments

Comments
 (0)