23 CONFIG_FILE_NETWORK_SECTION,
24 CONFIG_FILE_DEFAULT_SECTION
33 template <
typename Fn>
34 static void MergeSettings(
const Settings &settings,
35 const std::string §ion,
36 const std::string &
name, Fn &&fn) {
38 if (
auto *value =
FindKey(settings.forced_settings,
name)) {
39 fn(SettingsSpan(*value), Source::FORCED);
42 if (
auto *values =
FindKey(settings.command_line_options,
name)) {
43 fn(SettingsSpan(*values), Source::COMMAND_LINE);
47 fn(SettingsSpan(*value), Source::RW_SETTINGS);
50 if (!section.empty()) {
51 if (
auto *map =
FindKey(settings.ro_config, section)) {
53 fn(SettingsSpan(*values),
54 Source::CONFIG_FILE_NETWORK_SECTION);
59 if (
auto *map =
FindKey(settings.ro_config,
"")) {
61 fn(SettingsSpan(*values), Source::CONFIG_FILE_DEFAULT_SECTION);
68 std::map<std::string, SettingsValue> &values,
69 std::vector<std::string> &errors) {
80 if (!file.is_open()) {
87 if (!in.
read(std::string{std::istreambuf_iterator<char>(file),
88 std::istreambuf_iterator<char>()})) {
89 errors.emplace_back(
strprintf(
"Unable to parse settings file %s",
95 errors.emplace_back(
strprintf(
"Failed reading settings file %s",
104 strprintf(
"Found non-object value %s in settings file %s",
109 const std::vector<std::string> &in_keys = in.
getKeys();
110 const std::vector<SettingsValue> &in_values = in.
getValues();
111 for (
size_t i = 0; i < in_keys.size(); ++i) {
112 auto inserted = values.emplace(in_keys[i], in_values[i]);
113 if (!inserted.second) {
115 strprintf(
"Found duplicate key %s in settings file %s",
119 return errors.empty();
123 const std::map<std::string, SettingsValue> &values,
124 std::vector<std::string> &errors) {
126 for (
const auto &value : values) {
127 out.
pushKVEnd(value.first, value.second);
133 strprintf(
"Error: Unable to open settings file %s for writing",
137 file << out.
write( 1, 4) << std::endl;
143 const std::string &
name,
144 bool ignore_default_section_config,
145 bool ignore_nonpersistent,
bool get_chain_name) {
156 const bool never_ignore_negated_setting = span.
last_negated();
163 const bool reverse_precedence =
164 (
source == Source::CONFIG_FILE_NETWORK_SECTION ||
165 source == Source::CONFIG_FILE_DEFAULT_SECTION) &&
174 const bool skip_negated_command_line = get_chain_name;
181 if (ignore_default_section_config &&
182 source == Source::CONFIG_FILE_DEFAULT_SECTION &&
183 !never_ignore_negated_setting) {
188 if (ignore_nonpersistent &&
189 (
source == Source::COMMAND_LINE ||
source == Source::FORCED)) {
199 result = reverse_precedence ? span.
begin()[0] : span.
end()[-1];
210 const std::string §ion,
211 const std::string &
name,
212 bool ignore_default_section_config) {
213 std::vector<SettingsValue> result;
216 bool prev_negated_empty =
false;
226 const bool add_zombie_config_values =
227 (
source == Source::CONFIG_FILE_NETWORK_SECTION ||
228 source == Source::CONFIG_FILE_DEFAULT_SECTION) &&
232 if (ignore_default_section_config &&
233 source == Source::CONFIG_FILE_DEFAULT_SECTION) {
239 if (!done || add_zombie_config_values) {
240 for (
const auto &value : span) {
241 if (value.isArray()) {
242 result.insert(result.end(), value.getValues().begin(),
243 value.getValues().end());
245 result.push_back(value);
256 prev_negated_empty |= span.
last_negated() && result.empty();
262 const std::string §ion,
263 const std::string &
name) {
264 bool has_default_section_setting =
false;
265 bool has_other_setting =
false;
270 }
else if (
source == Source::CONFIG_FILE_DEFAULT_SECTION) {
271 has_default_section_setting =
true;
273 has_other_setting =
true;
279 return has_default_section_setting && !has_other_setting;
297 for (
size_t i =
size; i > 0; --i) {
298 if (
data[i - 1].isFalse()) {
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
const std::vector< UniValue > & getValues() const
const std::vector< std::string > & getKeys() const
void pushKVEnd(std::string key, UniValue val)
bool read(std::string_view raw)
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
static bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to byte string.
std::vector< SettingsValue > GetSettingsList(const Settings &settings, const std::string §ion, const std::string &name, bool ignore_default_section_config)
Get combined setting value similar to GetSetting(), except if setting was specified multiple times,...
SettingsValue GetSetting(const Settings &settings, const std::string §ion, const std::string &name, bool ignore_default_section_config, bool ignore_nonpersistent, bool get_chain_name)
Get settings value from combined sources: forced settings, command line arguments,...
bool ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
UniValue SettingsValue
Settings value type (string/integer/boolean/null variant).
bool OnlyHasDefaultSectionSetting(const Settings &settings, const std::string §ion, const std::string &name)
Return true if a setting is set in the default config file section, and not overridden by a higher pr...
bool WriteSettings(const fs::path &path, const std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Write settings file.
auto FindKey(Map &&map, Key &&key) -> decltype(&map.at(key))
Map lookup helper.
Accessor for list of settings that skips negated values when iterated over.
size_t negated() const
Number of negated values.
const SettingsValue * end() const
Pointer to end of values.
bool empty() const
True if there are any non-negated values.
const SettingsValue * data
bool last_negated() const
True if the last value is negated.
const SettingsValue * begin() const
Pointer to first non-negated value.