1 // Copyright (c) 2019-2022 The Limenka 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 LIMENKA_COMMON_SETTINGS_H
6 #define LIMENKA_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/limenka/limenka/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 list of r/w config file values.
38 std::map<std::string, std::vector<SettingsValue>> rw_config;
39 //! Map of setting name to read-write file setting value.
40 std::map<std::string, SettingsValue> rw_settings;
41 //! Map of config section name and setting name to list of config file values.
42 std::map<std::string, std::map<std::string, std::vector<SettingsValue>>> ro_config;
43 };
44 45 //! Read settings file.
46 bool ReadSettings(const fs::path& path,
47 std::map<std::string, SettingsValue>& values,
48 std::vector<std::string>& errors);
49 50 //! Write settings file.
51 bool WriteSettings(const fs::path& path,
52 const std::map<std::string, SettingsValue>& values,
53 std::vector<std::string>& errors);
54 55 //! Get settings value from combined sources: forced settings, command line
56 //! arguments, runtime read-write settings, and the read-only config file.
57 //!
58 //! @param ignore_default_section_config - ignore values in the default section
59 //! of the config file (part before any
60 //! [section] keywords)
61 //! @param ignore_nonpersistent - ignore non-persistent settings values (forced
62 //! settings values and values specified on the
63 //! command line). Only return settings in the
64 //! read-only config and read-write settings
65 //! files.
66 //! @param get_chain_type - enable special backwards compatible behavior
67 //! for GetChainType
68 SettingsValue GetSetting(const Settings& settings,
69 const std::string& section,
70 const std::string& name,
71 bool ignore_default_section_config,
72 bool ignore_nonpersistent,
73 bool get_chain_type);
74 75 //! Get combined setting value similar to GetSetting(), except if setting was
76 //! specified multiple times, return a list of all the values specified.
77 std::vector<SettingsValue> GetSettingsList(const Settings& settings,
78 const std::string& section,
79 const std::string& name,
80 bool ignore_default_section_config);
81 82 //! Return true if a setting is set in the default config file section, and not
83 //! overridden by a higher priority command-line or network section value.
84 //!
85 //! This is used to provide user warnings about values that might be getting
86 //! ignored unintentionally.
87 bool OnlyHasDefaultSectionSetting(const Settings& settings, const std::string& section, const std::string& name);
88 89 //! Accessor for list of settings that skips negated values when iterated over.
90 //! The last boolean `false` value in the list and all earlier values are
91 //! considered negated.
92 struct SettingsSpan {
93 explicit SettingsSpan() = default;
94 explicit SettingsSpan(const SettingsValue& value) noexcept : SettingsSpan(&value, 1) {}
95 explicit SettingsSpan(const SettingsValue* data, size_t size) noexcept : data(data), size(size) {}
96 explicit SettingsSpan(const std::vector<SettingsValue>& vec) noexcept;
97 const SettingsValue* begin() const; //!< Pointer to first non-negated value.
98 const SettingsValue* end() const; //!< Pointer to end of values.
99 bool empty() const; //!< True if there are any non-negated values.
100 bool last_negated() const; //!< True if the last value is negated.
101 size_t negated() const; //!< Number of negated values.
102 103 const SettingsValue* data = nullptr;
104 size_t size = 0;
105 };
106 107 //! Map lookup helper.
108 template <typename Map, typename Key>
109 auto FindKey(Map&& map, Key&& key) -> decltype(&map.at(key))
110 {
111 auto it = map.find(key);
112 return it == map.end() ? nullptr : &it->second;
113 }
114 115 } // namespace common
116 117 #endif // LIMENKA_COMMON_SETTINGS_H
118