| 197 | bool loaded() const { return this->parsed; } |
| 198 | |
| 199 | bool save() |
| 200 | { |
| 201 | // Don't try to save if we've not successfully loaded anything |
| 202 | if (!this->parsed) |
| 203 | return false; |
| 204 | auto configPathString = this->getTyped<UString>("Config.File"); |
| 205 | |
| 206 | if (this->modifiedOptions.empty()) |
| 207 | { |
| 208 | // nothing to do |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | std::map<UString, std::list<UString>> configFileContents; |
| 213 | |
| 214 | for (auto &optionPair : this->modifiedOptions) |
| 215 | { |
| 216 | auto splitString = split(optionPair.first, "."); |
| 217 | if (splitString.size() < 1) |
| 218 | { |
| 219 | LogError("Invalid option string \"%s\"", optionPair.first); |
| 220 | continue; |
| 221 | } |
| 222 | UString sectionName; |
| 223 | for (unsigned i = 0; i < splitString.size() - 1; i++) |
| 224 | { |
| 225 | if (i != 0) |
| 226 | sectionName += "."; |
| 227 | sectionName += splitString[i]; |
| 228 | } |
| 229 | |
| 230 | auto optionName = splitString[splitString.size() - 1]; |
| 231 | UString configFileLine = |
| 232 | format("%s=%s", optionName, std::visit(ToStringVisitor(), optionPair.second)); |
| 233 | configFileContents[sectionName].push_back(configFileLine); |
| 234 | } |
| 235 | |
| 236 | try |
| 237 | { |
| 238 | fs::path configPath(configPathString); |
| 239 | auto dir = configPath.parent_path(); |
| 240 | if (!dir.empty()) |
| 241 | fs::create_directories(dir); |
| 242 | std::ofstream outFile(configPath.string()); |
| 243 | if (!outFile) |
| 244 | { |
| 245 | std::cerr << "Failed to open config file \"" << configPath << "\" for writing\n"; |
| 246 | return false; |
| 247 | } |
| 248 | for (auto §ion : configFileContents) |
| 249 | { |
| 250 | auto §ionName = section.first; |
| 251 | outFile << "[" << sectionName << "]\n"; |
| 252 | for (auto &line : section.second) |
| 253 | { |
| 254 | outFile << line << "\n"; |
| 255 | } |
| 256 | } |
nothing calls this directly
no test coverage detected