diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-08-06 17:04:50 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-08-06 17:04:50 +0200 |
commit | e9904604ce0199d696c4941dff4f5ad84d22355f (patch) | |
tree | 1380d5b4f882a61aa1b5e9ee08d4aa4d72c00f48 /src/Settings.cpp | |
parent | Separate lighting for each block face (diff) | |
download | AltCraft-e9904604ce0199d696c4941dff4f5ad84d22355f.tar AltCraft-e9904604ce0199d696c4941dff4f5ad84d22355f.tar.gz AltCraft-e9904604ce0199d696c4941dff4f5ad84d22355f.tar.bz2 AltCraft-e9904604ce0199d696c4941dff4f5ad84d22355f.tar.lz AltCraft-e9904604ce0199d696c4941dff4f5ad84d22355f.tar.xz AltCraft-e9904604ce0199d696c4941dff4f5ad84d22355f.tar.zst AltCraft-e9904604ce0199d696c4941dff4f5ad84d22355f.zip |
Diffstat (limited to 'src/Settings.cpp')
-rw-r--r-- | src/Settings.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/Settings.cpp b/src/Settings.cpp new file mode 100644 index 0000000..a585752 --- /dev/null +++ b/src/Settings.cpp @@ -0,0 +1,58 @@ +#include "Settings.hpp" + +#include <fstream> +#include <map> + +#include <nlohmann/json.hpp> +#include <easylogging++.h> + + +using json = nlohmann::json; + +std::map<std::string, std::string> values; + +const std::string saveFileName = "./settings.json"; + +void Settings::Load() { + std::ifstream stream(saveFileName); + if (!stream) { + LOG(ERROR) << "Loading settings failed: Can't open ifstream for " << saveFileName << ": " << strerror(errno); + return; + } + json j; + stream >> j; + + for (json::iterator it = j.begin(); it != j.end(); ++it) { + values.insert(std::make_pair(it.key(), it->get<std::string>())); + } + LOG(INFO) << "Loaded " << values.size() << " settings"; +} + +void Settings::Save() { + json j; + for (auto &it : values) { + j[it.first] = it.second; + } + std::string text = j.dump(4); + + std::ofstream stream(saveFileName); + if (!stream) { + LOG(ERROR) << "Saving settings failed: Can't open ofstream for " << saveFileName << ": "<< strerror(errno); + return; + } + stream << text; + LOG(INFO) << "Saved " << values.size() << " settings"; +} + +std::string Settings::Read(const std::string &key, const std::string &defaultValue) { + auto it = values.find(key); + if (it == values.end()) { + values.insert(std::make_pair(key, defaultValue)); + it = values.find(key); + } + return it->second; +} + +void Settings::Write(const std::string &key, const std::string &value) { + values[key] = value; +} |