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