| 62 | } // namespace |
| 63 | |
| 64 | bool 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 | |
| 107 | bool WriteSettings(const fs::path& path, |
| 108 | const std::map<std::string, SettingsValue>& values, |