2024-11-22 23:26:42 +03:00
|
|
|
#include "settings.h"
|
|
|
|
#include <fstream>
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <string>
|
|
|
|
#include "../utils/utils.h"
|
|
|
|
|
2024-12-01 19:37:17 +03:00
|
|
|
#if __GNUC__ < 8 && __clang_major__ < 17
|
2024-12-01 18:53:16 +03:00
|
|
|
# include <experimental/filesystem>
|
2024-12-01 20:04:51 +03:00
|
|
|
using namespace std::experimental;
|
|
|
|
using namespace std::experimental::filesystem;
|
2024-12-01 18:53:16 +03:00
|
|
|
#else
|
|
|
|
# include <filesystem>
|
2024-12-01 19:37:17 +03:00
|
|
|
using namespace std::filesystem;
|
2024-12-01 18:53:16 +03:00
|
|
|
#endif
|
|
|
|
|
2024-11-22 23:26:42 +03:00
|
|
|
Settings::Settings(std::string path) {
|
|
|
|
this->settings_file_path = path;
|
|
|
|
|
2024-12-01 19:37:17 +03:00
|
|
|
if (!exists(path)) {
|
2024-11-22 23:26:42 +03:00
|
|
|
std::ofstream output(path);
|
|
|
|
|
|
|
|
nlohmann::json settings = R"({
|
|
|
|
"ofds_modules_dir":".local/share/checks_parser/modules/ofd",
|
|
|
|
"stores_modules_dir":".local/share/checks_parser/modules/stores",
|
|
|
|
"ofds_modules_url":"https://foxarmy.org/checks-parser/modules/ofd/",
|
2024-12-03 15:24:21 +03:00
|
|
|
"stores_modules_url":"https://foxarmy.org/checks-parser/modules/stores/",
|
2024-11-22 23:26:42 +03:00
|
|
|
"print_header": true,
|
|
|
|
"print_total": true,
|
2024-12-04 18:44:40 +03:00
|
|
|
"language":"en_US",
|
2024-11-22 23:26:42 +03:00
|
|
|
"output_order": {
|
|
|
|
"goods_name": {
|
|
|
|
"position":1,
|
|
|
|
"name":"Goods name"
|
|
|
|
},
|
|
|
|
"goods_price_per_unit": {
|
|
|
|
"position":2,
|
|
|
|
"name":"Goods price per unit"
|
|
|
|
},
|
|
|
|
"goods_quantity": {
|
|
|
|
"position":3,
|
|
|
|
"name":"Goods quantity"
|
|
|
|
},
|
|
|
|
"goods_net_weight": {
|
|
|
|
"position":4,
|
|
|
|
"name":"Goods net weight"
|
|
|
|
},
|
|
|
|
"goods_total": {
|
|
|
|
"position":5,
|
|
|
|
"name":"Goods total"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})"_json;
|
|
|
|
|
2024-12-04 04:05:02 +03:00
|
|
|
output << settings.dump(4);
|
2024-11-22 23:26:42 +03:00
|
|
|
output.flush();
|
|
|
|
output.close();
|
|
|
|
this->settings = settings;
|
|
|
|
} else {
|
|
|
|
std::ifstream input(path);
|
|
|
|
|
|
|
|
nlohmann::json settings = nlohmann::json::parse(input);
|
|
|
|
this->settings = settings;
|
|
|
|
}
|
|
|
|
|
2024-12-01 19:37:17 +03:00
|
|
|
create_directories(get_path_relative_to_home(this->settings["ofds_modules_dir"]));
|
|
|
|
create_directories(get_path_relative_to_home(this->settings["stores_modules_dir"]));
|
2024-11-22 23:26:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Settings::write_setting(std::string setting, std::string value) {
|
|
|
|
std::ofstream output(this->settings_file_path, std::fstream::trunc);
|
|
|
|
|
|
|
|
this->settings[setting] = value;
|
|
|
|
|
2024-12-04 04:05:02 +03:00
|
|
|
output << this->settings.dump(4);
|
2024-11-22 23:26:42 +03:00
|
|
|
}
|
|
|
|
std::string Settings::get_setting(std::string setting) {
|
|
|
|
return this->settings[setting];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Settings::set_settings_file_path(std::string path) {
|
|
|
|
this->settings_file_path = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Settings::get_settings_file_path() {
|
|
|
|
return this->settings_file_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
nlohmann::json& Settings::get_all_settings() { return this->settings; }
|
|
|
|
|
|
|
|
void Settings::alter_setting(std::string setting, std::string value) {
|
|
|
|
this->settings[setting] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Settings::flush() {
|
|
|
|
std::ofstream output(this->settings_file_path, std::fstream::trunc);
|
|
|
|
|
2024-12-04 04:05:02 +03:00
|
|
|
output << this->settings.dump(4);
|
2024-11-22 23:26:42 +03:00
|
|
|
}
|