25 #include <string_view>
30 const fs::path &configuration_file_path) {
38 std::vector<std::pair<std::string, std::string>> &options,
39 std::list<SectionInfo> §ions) {
41 std::string::size_type pos;
43 while (std::getline(stream, str)) {
44 bool used_hash =
false;
45 if ((pos = str.find(
'#')) != std::string::npos) {
46 str = str.substr(0, pos);
49 const static std::string pattern =
" \t\r\n";
52 if (*str.begin() ==
'[' && *str.rbegin() ==
']') {
53 const std::string section = str.substr(1, str.size() - 2);
54 sections.emplace_back(
SectionInfo{section, filepath, linenr});
56 }
else if (*str.begin() ==
'-') {
58 "parse error on line %i: %s, options in configuration file "
59 "must be specified without leading -",
62 }
else if ((pos = str.find(
'=')) != std::string::npos) {
65 std::string value =
TrimString(str.substr(pos + 1), pattern);
67 name.find(
"rpcpassword") != std::string::npos) {
69 "parse error on line %i, using # in rpcpassword can be "
70 "ambiguous and should be avoided",
74 options.emplace_back(
name, value);
75 if ((pos =
name.rfind(
'.')) != std::string::npos &&
77 sections.emplace_back(
82 if (str.size() >= 2 && str.substr(0, 2) ==
"no") {
84 "option, use %s=1 instead",
96 const std::string &filepath,
98 bool ignore_invalid_keys) {
100 std::vector<std::pair<std::string, std::string>> options;
102 m_config_sections)) {
105 for (
const std::pair<std::string, std::string> &option : options) {
107 std::string key = option.first;
115 m_settings.ro_config[section][key].push_back(value);
117 if (ignore_invalid_keys) {
118 LogPrintf(
"Ignoring unknown configuration value %s\n",
122 option.first.c_str());
131 bool ignore_invalid_keys) {
134 m_settings.ro_config.clear();
135 m_config_sections.clear();
139 std::ifstream stream{conf_path};
144 ignore_invalid_keys)) {
150 bool use_conf_file{
true};
153 if (
auto *includes =
util::FindKey(m_settings.command_line_options,
158 use_conf_file =
false;
163 std::vector<std::string> conf_file_names;
165 auto add_includes = [&](
const std::string &network,
167 size_t num_values = 0;
172 for (
size_t i = std::max(
174 i < values->size(); ++i) {
175 conf_file_names.push_back((*values)[i].get_str());
177 num_values = values->size();
185 const size_t chain_includes = add_includes(chain_id);
186 const size_t default_includes = add_includes({});
188 for (
const std::string &conf_file_name : conf_file_names) {
189 std::ifstream conf_file_stream{
191 if (conf_file_stream.good()) {
193 error, ignore_invalid_keys)) {
196 LogPrintf(
"Included configuration file %s\n",
199 error =
"Failed to include configuration file " +
206 conf_file_names.clear();
207 add_includes(chain_id, chain_includes);
208 add_includes({}, default_includes);
210 if (chain_id_final != chain_id) {
213 add_includes(chain_id_final);
215 for (
const std::string &conf_file_name : conf_file_names) {
217 "warning: -includeconf cannot be used from "
218 "included files; ignoring -includeconf=%s\n",
227 error =
strprintf(
"specified data directory \"%s\" does not exist.",
228 GetArg(
"-datadir",
"").c_str());
236 if (path.is_absolute()) {
bool CheckValid(const std::string &key, const util::SettingsValue &val, unsigned int flags, std::string &error)
Check settings value validity according to flags.
bool CheckDataDirOption(const ArgsManager &args)
util::SettingsValue InterpretOption(std::string §ion, std::string &key, const std::string &value)
Interpret -nofoo as if the user supplied -foo=0.
std::optional< unsigned int > GetArgFlags(const std::string &name) const
Return Flags for known arg.
const fs::path & GetDataDirBase() const
Get data directory path.
void ClearPathCache()
Clear cached directory paths.
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
fs::path GetConfigFilePath() const
Return config file path (read-only)
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
bool ReadConfigStream(std::istream &stream, const std::string &filepath, std::string &error, bool ignore_invalid_keys=false)
bool ReadConfigFiles(std::string &error, bool ignore_invalid_keys=false)
std::string GetChainName() const
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
static bool GetConfigOptions(std::istream &stream, const std::string &filepath, std::string &error, std::vector< std::pair< std::string, std::string >> &options, std::list< SectionInfo > §ions)
fs::path AbsPathForConfigVal(const ArgsManager &args, const fs::path &path, bool net_specific)
Most paths passed as configuration arguments are treated as relative to the datadir if they are not a...
fs::path GetConfigFile(const ArgsManager &args, const fs::path &configuration_file_path)
bool error(const char *fmt, const Args &...args)
static std::string PathToString(const path &path)
Convert path object to byte string.
static path PathFromString(const std::string &string)
Convert byte string to path object.
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
auto FindKey(Map &&map, Key &&key) -> decltype(&map.at(key))
Map lookup helper.
std::string TrimString(const std::string &str, const std::string &pattern=" \f\n\r\t\v")
Accessor for list of settings that skips negated values when iterated over.
size_t negated() const
Number of negated values.
bool last_negated() const
True if the last value is negated.