SDRPlusPlus/core/src/config.cpp

119 wiersze
2.9 KiB
C++
Czysty Zwykły widok Historia

2020-08-16 01:39:05 +00:00
#include <config.h>
#include <spdlog/spdlog.h>
#include <fstream>
#include <filesystem>
2020-08-16 01:39:05 +00:00
2020-09-24 17:36:57 +00:00
ConfigManager::ConfigManager() {
}
2020-12-08 03:36:37 +00:00
ConfigManager::~ConfigManager() {
disableAutoSave();
}
2020-09-24 17:36:57 +00:00
void ConfigManager::setPath(std::string file) {
path = file;
}
2020-09-24 17:50:22 +00:00
void ConfigManager::load(json def, bool lock) {
2020-09-24 17:36:57 +00:00
if (lock) { mtx.lock(); }
if (path == "") {
spdlog::error("Config manager tried to load file with no path specified");
return;
2020-08-16 01:39:05 +00:00
}
2020-09-24 17:36:57 +00:00
if (!std::filesystem::exists(path)) {
spdlog::warn("Config file '{0}' does not exist, creating it", path);
2020-09-24 17:50:22 +00:00
conf = def;
2020-09-24 17:36:57 +00:00
save(false);
2020-08-16 01:39:05 +00:00
}
2020-09-24 17:36:57 +00:00
if (!std::filesystem::is_regular_file(path)) {
spdlog::error("Config file '{0}' isn't a file", path);
return;
2020-08-16 01:39:05 +00:00
}
2020-09-24 17:36:57 +00:00
std::ifstream file(path.c_str());
file >> conf;
file.close();
if (lock) { mtx.unlock(); }
}
2020-08-16 01:39:05 +00:00
2020-09-24 17:36:57 +00:00
void ConfigManager::save(bool lock) {
if (lock) { mtx.lock(); }
std::ofstream file(path.c_str());
file << conf.dump(4);
file.close();
if (lock) { mtx.unlock(); }
}
2020-09-06 13:39:09 +00:00
2020-09-24 17:36:57 +00:00
void ConfigManager::enableAutoSave() {
2020-12-08 03:36:37 +00:00
if (!autoSaveEnabled) {
autoSaveEnabled = true;
autoSaveThread = std::thread(autoSaveWorker, this);
}
2020-09-24 17:36:57 +00:00
}
void ConfigManager::disableAutoSave() {
2020-12-08 03:36:37 +00:00
if (autoSaveEnabled) {
autoSaveEnabled = false;
autoSaveThread.join();
}
2020-09-24 17:36:57 +00:00
}
2020-09-06 13:39:09 +00:00
2020-09-24 17:36:57 +00:00
void ConfigManager::aquire() {
mtx.lock();
}
void ConfigManager::release(bool changed) {
this->changed |= changed;
mtx.unlock();
}
void ConfigManager::autoSaveWorker(ConfigManager* _this) {
while (_this->autoSaveEnabled) {
if (!_this->mtx.try_lock()) {
2020-10-19 22:32:17 +00:00
spdlog::warn("ConfigManager locked, waiting...");
2020-09-24 17:36:57 +00:00
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
continue;
}
if (_this->changed) {
_this->changed = false;
_this->save(false);
}
_this->mtx.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
2020-09-06 13:39:09 +00:00
}
2020-09-24 17:36:57 +00:00
}
2020-10-24 12:51:55 +00:00
// void ConfigManager::setResourceDir(std::string path) {
// if (!std::filesystem::exists(path)) {
// spdlog::error("Resource directory '{0}' does not exist", path);
// return;
// }
// if (!std::filesystem::is_regular_file(path)) {
// spdlog::error("Resource directory '{0}' is not a directory", path);
// return;
// }
// resDir = path;
// }
// std::string ConfigManager::getResourceDir() {
// return resDir;
// }
// void ConfigManager::setConfigDir(std::string path) {
// if (!std::filesystem::exists(path)) {
// spdlog::error("Resource directory '{0}' does not exist", path);
// return;
// }
// if (!std::filesystem::is_regular_file(path)) {
// spdlog::error("Resource directory '{0}' is not a directory", path);
// return;
// }
// resDir = path;
// }
// std::string ConfigManager::getConfigDir() {
// return configDir;
// }