remove ignored pt.2
This commit is contained in:
89
settings/settings.cpp
Normal file
89
settings/settings.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
#include "settings.h"
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
#include "../utils/utils.h"
|
||||
|
||||
Settings::Settings(std::string path) {
|
||||
this->settings_file_path = path;
|
||||
|
||||
if (!std::filesystem::exists(path)) {
|
||||
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/",
|
||||
"stores_modules_url":"https://foxarmy.org/checks-parser/modules/modules/",
|
||||
"print_header": true,
|
||||
"print_total": true,
|
||||
"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;
|
||||
|
||||
output << settings;
|
||||
output.flush();
|
||||
output.close();
|
||||
this->settings = settings;
|
||||
} else {
|
||||
std::ifstream input(path);
|
||||
|
||||
nlohmann::json settings = nlohmann::json::parse(input);
|
||||
this->settings = settings;
|
||||
}
|
||||
|
||||
std::filesystem::create_directories(get_path_relative_to_home(this->settings["ofds_modules_dir"]));
|
||||
std::filesystem::create_directories(get_path_relative_to_home(this->settings["stores_modules_dir"]));
|
||||
}
|
||||
|
||||
void Settings::write_setting(std::string setting, std::string value) {
|
||||
std::ofstream output(this->settings_file_path, std::fstream::trunc);
|
||||
|
||||
this->settings[setting] = value;
|
||||
|
||||
output << this->settings;
|
||||
}
|
||||
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);
|
||||
|
||||
output << this->settings;
|
||||
}
|
||||
28
settings/settings.h
Normal file
28
settings/settings.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef SETTINGS_H
|
||||
#define SETTINGS_H
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
class Settings {
|
||||
|
||||
std::string settings_file_path;
|
||||
nlohmann::json settings;
|
||||
|
||||
public:
|
||||
Settings(std::string path);
|
||||
//Immediately saves to a file
|
||||
void write_setting(std::string setting, std::string value);
|
||||
//Waits for a flush
|
||||
void alter_setting(std::string setting, std::string value);
|
||||
std::string get_setting(std::string setting);
|
||||
|
||||
void set_settings_file_path(std::string path);
|
||||
std::string get_settings_file_path();
|
||||
|
||||
nlohmann::json& get_all_settings();
|
||||
|
||||
void flush();
|
||||
};
|
||||
|
||||
#endif // SETTINGS_H
|
||||
Reference in New Issue
Block a user