SDRPlusPlus/core/src/config.cpp

62 wiersze
1.5 KiB
C++
Czysty Zwykły widok Historia

2020-08-16 01:39:05 +00:00
#include <config.h>
namespace config {
bool configModified = false;
json config;
bool autoSaveRunning = false;
std::string _path;
std::thread _workerThread;
2020-09-06 13:39:09 +00:00
std::string rootDir;
2020-08-16 01:39:05 +00:00
void _autoSaveWorker() {
while (autoSaveRunning) {
if (configModified) {
configModified = false;
std::ofstream file(_path.c_str());
file << std::setw(4) << config;
file.close();
spdlog::info("Config saved");
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
void load(std::string path) {
if (!std::filesystem::exists(path)) {
spdlog::error("Config file does not exist");
return;
}
if (!std::filesystem::is_regular_file(path)) {
spdlog::error("Config file isn't a file...");
return;
}
_path = path;
std::ifstream file(path.c_str());
2020-09-19 23:36:25 +00:00
file >> config;
2020-08-16 01:39:05 +00:00
file.close();
}
void startAutoSave() {
if (autoSaveRunning) {
return;
}
autoSaveRunning = true;
_workerThread = std::thread(_autoSaveWorker);
}
void stopAutoSave() {
if (!autoSaveRunning) {
return;
}
autoSaveRunning = false;
_workerThread.join();
}
2020-09-06 13:39:09 +00:00
void setRootDirectory(std::string dir) {
rootDir = dir;
}
std::string getRootDirectory() {
return rootDir;
}
2020-08-16 01:39:05 +00:00
};