aboutsummaryrefslogtreecommitdiff
path: root/src/util/settings.cpp
diff options
context:
space:
mode:
authorRussell Yanofsky <russ@yanofsky.org>2019-04-28 19:08:26 -0400
committerRussell Yanofsky <russ@yanofsky.org>2020-07-11 05:41:12 -0400
commiteb682c5700e7a9176d0104d470b83ff9aa3589e8 (patch)
treeb93a21cb97ffaa978e4fd3903cc481be65cf7017 /src/util/settings.cpp
parenta42631775afadf7e8cf07e917f733bd54dc3080e (diff)
util: Add ReadSettings and WriteSettings functions
Currently unused, but includes tests.
Diffstat (limited to 'src/util/settings.cpp')
-rw-r--r--src/util/settings.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/util/settings.cpp b/src/util/settings.cpp
index e4979df755..34894e994e 100644
--- a/src/util/settings.cpp
+++ b/src/util/settings.cpp
@@ -4,6 +4,7 @@
#include <util/settings.h>
+#include <tinyformat.h>
#include <univalue.h>
namespace util {
@@ -49,6 +50,62 @@ static void MergeSettings(const Settings& settings, const std::string& section,
}
} // namespace
+bool ReadSettings(const fs::path& path, std::map<std::string, SettingsValue>& values, std::vector<std::string>& errors)
+{
+ values.clear();
+ errors.clear();
+
+ fsbridge::ifstream file;
+ file.open(path);
+ if (!file.is_open()) return true; // Ok for file not to exist.
+
+ SettingsValue in;
+ if (!in.read(std::string{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()})) {
+ errors.emplace_back(strprintf("Unable to parse settings file %s", path.string()));
+ return false;
+ }
+
+ if (file.fail()) {
+ errors.emplace_back(strprintf("Failed reading settings file %s", path.string()));
+ return false;
+ }
+ file.close(); // Done with file descriptor. Release while copying data.
+
+ if (!in.isObject()) {
+ errors.emplace_back(strprintf("Found non-object value %s in settings file %s", in.write(), path.string()));
+ return false;
+ }
+
+ const std::vector<std::string>& in_keys = in.getKeys();
+ const std::vector<SettingsValue>& in_values = in.getValues();
+ for (size_t i = 0; i < in_keys.size(); ++i) {
+ auto inserted = values.emplace(in_keys[i], in_values[i]);
+ if (!inserted.second) {
+ errors.emplace_back(strprintf("Found duplicate key %s in settings file %s", in_keys[i], path.string()));
+ }
+ }
+ return errors.empty();
+}
+
+bool WriteSettings(const fs::path& path,
+ const std::map<std::string, SettingsValue>& values,
+ std::vector<std::string>& errors)
+{
+ SettingsValue out(SettingsValue::VOBJ);
+ for (const auto& value : values) {
+ out.__pushKV(value.first, value.second);
+ }
+ fsbridge::ofstream file;
+ file.open(path);
+ if (file.fail()) {
+ errors.emplace_back(strprintf("Error: Unable to open settings file %s for writing", path.string()));
+ return false;
+ }
+ file << out.write(/* prettyIndent= */ 1, /* indentLevel= */ 4) << std::endl;
+ file.close();
+ return true;
+}
+
SettingsValue GetSetting(const Settings& settings,
const std::string& section,
const std::string& name,