Changeset View
Changeset View
Standalone View
Standalone View
src/util/system.cpp
Show First 20 Lines • Show All 859 Lines • ▼ Show 20 Lines | static std::string TrimString(const std::string &str, | ||||
std::string::size_type front = str.find_first_not_of(pattern); | std::string::size_type front = str.find_first_not_of(pattern); | ||||
if (front == std::string::npos) { | if (front == std::string::npos) { | ||||
return std::string(); | return std::string(); | ||||
} | } | ||||
std::string::size_type end = str.find_last_not_of(pattern); | std::string::size_type end = str.find_last_not_of(pattern); | ||||
return str.substr(front, end - front + 1); | return str.substr(front, end - front + 1); | ||||
} | } | ||||
static std::vector<std::pair<std::string, std::string>> | static bool | ||||
GetConfigOptions(std::istream &stream) { | GetConfigOptions(std::istream &stream, std::string &error, | ||||
std::vector<std::pair<std::string, std::string>> options; | std::vector<std::pair<std::string, std::string>> &options) { | ||||
std::string str, prefix; | std::string str, prefix; | ||||
std::string::size_type pos; | std::string::size_type pos; | ||||
int linenr = 1; | |||||
while (std::getline(stream, str)) { | while (std::getline(stream, str)) { | ||||
if ((pos = str.find('#')) != std::string::npos) { | if ((pos = str.find('#')) != std::string::npos) { | ||||
str = str.substr(0, pos); | str = str.substr(0, pos); | ||||
} | } | ||||
const static std::string pattern = " \t\r\n"; | const static std::string pattern = " \t\r\n"; | ||||
str = TrimString(str, pattern); | str = TrimString(str, pattern); | ||||
if (!str.empty()) { | if (!str.empty()) { | ||||
if (*str.begin() == '[' && *str.rbegin() == ']') { | if (*str.begin() == '[' && *str.rbegin() == ']') { | ||||
prefix = str.substr(1, str.size() - 2) + '.'; | prefix = str.substr(1, str.size() - 2) + '.'; | ||||
} else if (*str.begin() == '-') { | |||||
error = strprintf( | |||||
"parse error on line %i: %s, options in configuration file " | |||||
"must be specified without leading -", | |||||
linenr, str); | |||||
return false; | |||||
} else if ((pos = str.find('=')) != std::string::npos) { | } else if ((pos = str.find('=')) != std::string::npos) { | ||||
std::string name = | std::string name = | ||||
prefix + TrimString(str.substr(0, pos), pattern); | prefix + TrimString(str.substr(0, pos), pattern); | ||||
std::string value = TrimString(str.substr(pos + 1), pattern); | std::string value = TrimString(str.substr(pos + 1), pattern); | ||||
options.emplace_back(name, value); | options.emplace_back(name, value); | ||||
} else { | |||||
error = strprintf("parse error on line %i: %s", linenr, str); | |||||
if (str.size() >= 2 && str.substr(0, 2) == "no") { | |||||
error += strprintf(", if you intended to specify a negated " | |||||
"option, use %s=1 instead", | |||||
str); | |||||
} | |||||
return false; | |||||
} | } | ||||
} | } | ||||
++linenr; | |||||
} | } | ||||
return options; | return true; | ||||
} | } | ||||
bool ArgsManager::ReadConfigStream(std::istream &stream, std::string &error, | bool ArgsManager::ReadConfigStream(std::istream &stream, std::string &error, | ||||
bool ignore_invalid_keys) { | bool ignore_invalid_keys) { | ||||
LOCK(cs_args); | LOCK(cs_args); | ||||
std::vector<std::pair<std::string, std::string>> options; | |||||
for (const std::pair<std::string, std::string> &option : | if (!GetConfigOptions(stream, error, options)) { | ||||
GetConfigOptions(stream)) { | return false; | ||||
} | |||||
for (const std::pair<std::string, std::string> &option : options) { | |||||
std::string strKey = std::string("-") + option.first; | std::string strKey = std::string("-") + option.first; | ||||
std::string strValue = option.second; | std::string strValue = option.second; | ||||
if (InterpretNegatedOption(strKey, strValue)) { | if (InterpretNegatedOption(strKey, strValue)) { | ||||
m_config_args[strKey].clear(); | m_config_args[strKey].clear(); | ||||
} else { | } else { | ||||
m_config_args[strKey].push_back(strValue); | m_config_args[strKey].push_back(strValue); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 444 Lines • Show Last 20 Lines |