Initial commit.
This commit is contained in:
commit
82b4f23c06
56 changed files with 3485 additions and 0 deletions
58
Shared/Settings.cpp
Normal file
58
Shared/Settings.cpp
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
#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';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue