MCPcopy Create free account
hub / github.com/ElementsProject/elements / ReadSettings

Function ReadSettings

src/util/settings.cpp:64–105  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

62} // namespace
63
64bool ReadSettings(const fs::path& path, std::map<std::string, SettingsValue>& values, std::vector<std::string>& errors)
65{
66 values.clear();
67 errors.clear();
68
69 // Ok for file to not exist
70 if (!fs::exists(path)) return true;
71
72 std::ifstream file;
73 file.open(path);
74 if (!file.is_open()) {
75 errors.emplace_back(strprintf("%s. Please check permissions.", fs::PathToString(path)));
76 return false;
77 }
78
79 SettingsValue in;
80 if (!in.read(std::string{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()})) {
81 errors.emplace_back(strprintf("Unable to parse settings file %s", fs::PathToString(path)));
82 return false;
83 }
84
85 if (file.fail()) {
86 errors.emplace_back(strprintf("Failed reading settings file %s", fs::PathToString(path)));
87 return false;
88 }
89 file.close(); // Done with file descriptor. Release while copying data.
90
91 if (!in.isObject()) {
92 errors.emplace_back(strprintf("Found non-object value %s in settings file %s", in.write(), fs::PathToString(path)));
93 return false;
94 }
95
96 const std::vector<std::string>& in_keys = in.getKeys();
97 const std::vector<SettingsValue>& in_values = in.getValues();
98 for (size_t i = 0; i < in_keys.size(); ++i) {
99 auto inserted = values.emplace(in_keys[i], in_values[i]);
100 if (!inserted.second) {
101 errors.emplace_back(strprintf("Found duplicate key %s in settings file %s", in_keys[i], fs::PathToString(path)));
102 }
103 }
104 return errors.empty();
105}
106
107bool WriteSettings(const fs::path& path,
108 const std::map<std::string, SettingsValue>& values,

Callers 3

ReadMethod · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85
ReadSettingsFileMethod · 0.85

Calls 11

existsFunction · 0.85
PathToStringFunction · 0.85
emplace_backMethod · 0.80
isObjectMethod · 0.80
clearMethod · 0.45
openMethod · 0.45
readMethod · 0.45
closeMethod · 0.45
writeMethod · 0.45
sizeMethod · 0.45
emptyMethod · 0.45

Tested by 1

BOOST_AUTO_TEST_CASEFunction · 0.68