1 // Copyright (c) 2019-present The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 5 #ifndef BITCOIN_COMMON_SETTINGS_H
6 #define BITCOIN_COMMON_SETTINGS_H
7 8 #include <util/fs.h>
9 10 #include <cstddef>
11 #include <map>
12 #include <string>
13 #include <vector>
14 15 class UniValue;
16 17 namespace common {
18 19 //! Settings value type (string/integer/boolean/null variant).
20 //!
21 //! @note UniValue is used here for convenience and because it can be easily
22 //! serialized in a readable format. But any other variant type that can
23 //! be assigned strings, int64_t, and bool values and has get_str(),
24 //! getInt<int64_t>(), get_bool(), isNum(), isBool(), isFalse(), isTrue() and
25 //! isNull() methods can be substituted if there's a need to move away
26 //! from UniValue. (An implementation with boost::variant was posted at
27 //! https://github.com/bitcoin/bitcoin/pull/15934/files#r337691812)
28 using SettingsValue = UniValue;
29 30 //! Stored settings. This struct combines settings from the command line, a
31 //! read-only configuration file, and a read-write runtime settings file.
32 struct Settings {
33 //! Map of setting name to forced setting value.
34 std::map<std::string, SettingsValue> forced_settings;
35 //! Map of setting name to list of command line values.
36 std::map<std::string, std::vector<SettingsValue>> command_line_options;
37 //! Map of setting name to read-write file setting value.
38 std::map<std::string, SettingsValue> rw_settings;
39 //! Map of config section name and setting name to list of config file values.
40 std::map<std::string, std::map<std::string, std::vector<SettingsValue>>> ro_config;
41 };
42 43 //! Read settings file.
44 bool ReadSettings(const fs::path& path,
45 std::map<std::string, SettingsValue>& values,
46 std::vector<std::string>& errors);
47 48 //! Write settings file.
49 bool WriteSettings(const fs::path& path,
50 const std::map<std::string, SettingsValue>& values,
51 std::vector<std::string>& errors);
52 53 //! Get settings value from combined sources: forced settings, command line
54 //! arguments, runtime read-write settings, and the read-only config file.
55 //!
56 //! @param ignore_default_section_config - ignore values in the default section
57 //! of the config file (part before any
58 //! [section] keywords)
59 //! @param ignore_nonpersistent - ignore non-persistent settings values (forced
60 //! settings values and values specified on the
61 //! command line). Only return settings in the
62 //! read-only config and read-write settings
63 //! files.
64 //! @param get_chain_type - enable special backwards compatible behavior
65 //! for GetChainType
66 SettingsValue GetSetting(const Settings& settings,
67 const std::string& section,
68 const std::string& name,
69 bool ignore_default_section_config,
70 bool ignore_nonpersistent,
71 bool get_chain_type);
72 73 //! Get combined setting value similar to GetSetting(), except if setting was
74 //! specified multiple times, return a list of all the values specified.
75 std::vector<SettingsValue> GetSettingsList(const Settings& settings,
76 const std::string& section,
77 const std::string& name,
78 bool ignore_default_section_config);
79 80 //! Return true if a setting is set in the default config file section, and not
81 //! overridden by a higher priority command-line or network section value.
82 //!
83 //! This is used to provide user warnings about values that might be getting
84 //! ignored unintentionally.
85 bool OnlyHasDefaultSectionSetting(const Settings& settings, const std::string& section, const std::string& name);
86 87 //! Accessor for list of settings that skips negated values when iterated over.
88 //! The last boolean `false` value in the list and all earlier values are
89 //! considered negated.
90 struct SettingsSpan {
91 explicit SettingsSpan() = default;
92 explicit SettingsSpan(const SettingsValue& value) noexcept : SettingsSpan(&value, 1) {}
93 explicit SettingsSpan(const SettingsValue* data, size_t size) noexcept : data(data), size(size) {}
94 explicit SettingsSpan(const std::vector<SettingsValue>& vec) noexcept;
95 const SettingsValue* begin() const; //!< Pointer to first non-negated value.
96 const SettingsValue* end() const; //!< Pointer to end of values.
97 bool empty() const; //!< True if there are any non-negated values.
98 bool last_negated() const; //!< True if the last value is negated.
99 size_t negated() const; //!< Number of negated values.
100 101 const SettingsValue* data = nullptr;
102 size_t size = 0;
103 };
104 105 //! Map lookup helper.
106 template <typename Map, typename Key>
107 auto FindKey(Map&& map, Key&& key) -> decltype(&map.at(key))
108 {
109 auto it = map.find(key);
110 return it == map.end() ? nullptr : &it->second;
111 }
112 113 } // namespace common
114 115 #endif // BITCOIN_COMMON_SETTINGS_H
116