ArtifactEngine/Shared/Settings.cpp
2026-04-06 18:32:22 -04:00

58 lines
1.2 KiB
C++

#include "Settings.h"
namespace Artifact {
Settings::Settings() : Settings(getDataPath() / "settings.conf") {}
Settings::Settings(Path path) : path(path) {
reload();
}
std::string trim(const std::string& str) {
size_t first = str.find_first_not_of(" \t\r\n");
if (first == std::string::npos) return "";
size_t last = str.find_last_not_of(" \t\r\n");
return str.substr(first, last - first + 1);
}
void Settings::reload() {
clear();
std::ifstream file(path);
if (!file.is_open()) {
// TODO: Log a warning
return;
}
std::string line;
while (std::getline(file, line)) {
line = trim(line);
if (line.empty() || line[0] == '#' || line[0] == ';') {
continue;
}
size_t eq_pos = line.find('=');
if (eq_pos == std::string::npos) {
continue;
}
std::string key = trim(line.substr(0, eq_pos));
std::string value = trim(line.substr(eq_pos + 1));
if (!key.empty()) {
data[key] = value;
}
}
}
void Settings::save() {
std::ofstream file(path);
if (!file.is_open()) {
return;
}
for (const auto & [key, value] : data) {
file << key << " = " << value << '\n';
}
}
}