1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include "settings.h"
#include <glibmm/miscutils.h>
#include <boost/algorithm/string.hpp>
#include <filesystem>
#include <fstream>
#include "../lua/default-strategy.h"
#include "../util/logger.h"
Settings::Settings() {
initStrArray("recent-files", std::vector<std::string>(10));
initStrArray("recent-strategies", std::vector<std::string>(10));
}
std::string Settings::getConfigFilename() {
return (std::filesystem::path(GetConfigDir()) / "settings").string();
}
std::string Settings::GetStrategyFilename() const {
return (std::filesystem::path(GetConfigDir()) / "strategy.lua").string();
}
std::string Settings::GetConfigDir() {
std::filesystem::path configPath =
std::filesystem::path(Glib::get_user_config_dir()) / "aoflagger";
if (!std::filesystem::is_directory(configPath)) {
// We don't want to crash if the dir can't be created; we will just report
// an error to the cmd line
try {
std::filesystem::create_directories(configPath);
} catch (std::exception& exception) {
Logger::Error << "Failed to create config directory: " << exception.what()
<< '\n';
}
}
return configPath.string();
}
void Settings::InitializeWorkStrategy() {
std::string filename = GetStrategyFilename();
std::ofstream str(filename);
str.write(reinterpret_cast<const char*>(data_strategies_generic_default_lua),
data_strategies_generic_default_lua_len);
if (!str)
throw std::runtime_error(
"Failed to write working file for Lua strategy: " + filename +
", size " + std::to_string(data_strategies_generic_default_lua_len));
}
void Settings::Load() {
std::string configFilename = getConfigFilename();
if (std::filesystem::exists(configFilename)) {
std::ifstream file(configFilename);
std::string line;
std::getline(file, line);
while (file) {
boost::algorithm::trim(line);
if (!line.empty() && line[0] != '#') {
size_t sep = line.find('=');
if (sep == std::string::npos)
throw std::runtime_error("Invalid key-value pair in config file " +
configFilename);
std::string key = boost::algorithm::trim_copy(line.substr(0, sep));
std::string value = boost::algorithm::trim_copy(line.substr(sep + 1));
if (key.empty() || value.empty())
throw std::runtime_error("Empty key or value in config file " +
configFilename);
set(key, value);
}
std::getline(file, line);
}
}
}
void Settings::Save() const {
std::string configFilename = getConfigFilename();
std::ofstream file(configFilename);
if (!file)
throw std::runtime_error("Error opening config file: " + configFilename);
file
<< "# This is the user settings file for the AOFlagger software package\n"
"# Any unchanged settings will be preceded by a hash symbol (#)\n"
"# Some of these settings can be found in the 'rfigui' application "
"under\n"
"# menu 'edit', option 'preferences'.\n"
"\n";
for (const auto& item : _settings) {
if (!item.second.HasValue()) file << "# ";
file << item.first << '=' << item.second.ValueOrDefault().ValueToString()
<< '\n';
}
}
|