SDRPlusPlus/core/src/config.cpp

101 wiersze
2.5 KiB
C++
Czysty Zwykły widok Historia

2020-08-16 01:39:05 +00:00
#include <config.h>
#include <utils/flog.h>
#include <fstream>
2021-04-26 14:55:55 +00:00
#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 = std::filesystem::absolute(file).string();
2020-09-24 17:36:57 +00:00
}
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 == "") {
flog::error("Config manager tried to load file with no path specified");
2020-09-24 17:36:57 +00:00
return;
2020-08-16 01:39:05 +00:00
}
2020-09-24 17:36:57 +00:00
if (!std::filesystem::exists(path)) {
flog::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)) {
flog::error("Config file '{0}' isn't a file", path);
2020-09-24 17:36:57 +00:00
return;
2020-08-16 01:39:05 +00:00
}
2021-08-03 22:14:55 +00:00
try {
std::ifstream file(path.c_str());
file >> conf;
file.close();
}
2024-01-22 18:46:01 +00:00
catch (const std::exception& e) {
flog::error("Config file '{}' is corrupted, resetting it: {}", path, e.what());
2021-08-03 22:14:55 +00:00
conf = def;
save(false);
}
2020-09-24 17:36:57 +00:00
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() {
if (autoSaveEnabled) { return; }
autoSaveEnabled = true;
termFlag = false;
autoSaveThread = std::thread(&ConfigManager::autoSaveWorker, this);
2020-09-24 17:36:57 +00:00
}
void ConfigManager::disableAutoSave() {
if (!autoSaveEnabled) { return; }
{
std::lock_guard<std::mutex> lock(termMtx);
autoSaveEnabled = false;
termFlag = true;
2020-12-08 03:36:37 +00:00
}
termCond.notify_one();
if (autoSaveThread.joinable()) { autoSaveThread.join(); }
2020-09-24 17:36:57 +00:00
}
2020-09-06 13:39:09 +00:00
void ConfigManager::acquire() {
2020-09-24 17:36:57 +00:00
mtx.lock();
}
void ConfigManager::release(bool modified) {
changed |= modified;
2020-09-24 17:36:57 +00:00
mtx.unlock();
}
void ConfigManager::autoSaveWorker() {
while (autoSaveEnabled) {
if (!mtx.try_lock()) {
flog::warn("ConfigManager locked, waiting...");
2020-09-24 17:36:57 +00:00
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
continue;
}
if (changed) {
changed = false;
save(false);
2020-09-24 17:36:57 +00:00
}
mtx.unlock();
2021-04-21 16:36:45 +00:00
// Sleep but listen for wakeup call
{
std::unique_lock<std::mutex> lock(termMtx);
termCond.wait_for(lock, std::chrono::milliseconds(1000), [this]() { return termFlag; });
2021-04-21 16:36:45 +00:00
}
2020-09-06 13:39:09 +00:00
}
2021-04-19 23:38:32 +00:00
}