You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
200 lines
6.2 KiB
200 lines
6.2 KiB
#include "save.h"
|
|
|
|
namespace Lib_save {
|
|
|
|
vector<string> Save::get_error() {
|
|
return this->vec_errors;
|
|
}
|
|
|
|
bool Save::get_state_save() {
|
|
return this->flag_no_save;
|
|
}
|
|
|
|
void Save::set_data_global(std::map <string, string> &map_global) {
|
|
this->map_global = &map_global;
|
|
}
|
|
|
|
void Save::set_data_local(std::map <string, string> &map_local) {
|
|
this->map_local = &map_local;
|
|
}
|
|
|
|
void Save::set_data_gui(std::map <string, string> &map_gui) {
|
|
this->map_gui = &map_gui;
|
|
}
|
|
|
|
bool Save::check_save(string flag_save, string key_name) {
|
|
std::map <string, string>:: iterator iter_map_data;
|
|
std::map <string, string>:: iterator iter_map_data_old;
|
|
std::map <string, string> *map_data_old;
|
|
if (flag_save == "system") {
|
|
map_data_old = map_local;
|
|
}
|
|
else if (flag_save == "global") {
|
|
map_data_old = map_global;
|
|
}
|
|
iter_map_data = (*map_gui).find(key_name);
|
|
iter_map_data_old = (*map_data_old).find(key_name);
|
|
if ((*map_local).find(key_name) != (*map_local).end() && (*map_global).find(key_name) != (*map_global).end()) {
|
|
if ((*map_local)[key_name] != (*map_global)[key_name]) {
|
|
return true;
|
|
}
|
|
}
|
|
if (iter_map_data_old == (*map_data_old).end() && iter_map_data != (*map_gui).end()) {
|
|
return true;
|
|
}
|
|
else if (iter_map_data->second != iter_map_data_old->second) {
|
|
return true;
|
|
}
|
|
else if (iter_map_data->second.length() == 0 && iter_map_data_old->second.length() == 0) {
|
|
return false;
|
|
}
|
|
else if (iter_map_data->second == iter_map_data_old->second) {
|
|
return false;
|
|
}
|
|
else {
|
|
return true;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
int Save::get_count_error() {
|
|
return process.get_count_error();
|
|
}
|
|
|
|
string Save::get_cmd_error() {
|
|
return process.get_cmd_error();
|
|
}
|
|
|
|
void Save::set_count_error(int count_error) {
|
|
process.set_count_error(count_error);
|
|
}
|
|
|
|
void Save::set_vec_params(vector<string>& vec_param_names) {
|
|
this->vec_param_names = &vec_param_names;
|
|
}
|
|
|
|
void Save::save(string sections, string str_flag_save) {
|
|
string key = "";
|
|
string value = "";
|
|
string cmd = "";
|
|
string str_error = "";
|
|
this->flag_no_save = true;
|
|
for (const auto &key: *vec_param_names) {
|
|
if (map_gui->find(key) != map_gui->end()) {
|
|
value = (*map_gui)[key];
|
|
if (this->check_save(str_flag_save, key)) {
|
|
if (value.length() != 0) {
|
|
cmd = "ubconfig --target " + str_flag_save + " set " + sections + " " +
|
|
key + "=\"" + value + "\"";
|
|
}
|
|
else if (value.length() == 0) {
|
|
cmd = "ubconfig --target " + str_flag_save + " remove " + sections + " " + key;
|
|
}
|
|
else {
|
|
cmd = "";
|
|
}
|
|
if (cmd.length() != 0) {
|
|
process.call(cmd, "");
|
|
this->flag_no_save = false;
|
|
str_error = process.get_cmd_error();
|
|
if (str_error.length() != 0) {
|
|
this->vec_errors.push_back(str_error);
|
|
str_error = "";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
void Save::save_all(string sections, string str_flag_save) {
|
|
string key = "";
|
|
string value = "";
|
|
string cmd_all = "ubconfig --target " + str_flag_save + " set " + sections;
|
|
size_t len_start_cmd_all = cmd_all.length();
|
|
string str_error = "";
|
|
this->flag_no_save = true;
|
|
string cmd = "";
|
|
string cmd_remove = "";
|
|
for (const auto &key: *vec_param_names) {
|
|
if (map_gui->find(key) != map_gui->end()) {
|
|
value = (*map_gui)[key];
|
|
if (this->check_save(str_flag_save, key)) {
|
|
if (value.length() != 0) {
|
|
cmd_all += " " + key + "=\"" + value + "\"";
|
|
}
|
|
else if (value.length() == 0) {
|
|
cmd = "ubconfig --target " + str_flag_save + " remove " + sections + " " + key;
|
|
}
|
|
else {
|
|
cmd = "";
|
|
}
|
|
if (cmd.length() != 0) {
|
|
process.call(cmd, "");
|
|
this->flag_no_save = false;
|
|
str_error = process.get_cmd_error();
|
|
if (str_error.length() != 0) {
|
|
this->vec_errors.push_back(str_error);
|
|
str_error = "";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
if (len_start_cmd_all != cmd_all.length()) {
|
|
process.call(cmd, "");
|
|
this->flag_no_save = false;
|
|
str_error = process.get_cmd_error();
|
|
if (str_error.length() != 0) {
|
|
this->vec_errors.push_back(str_error);
|
|
str_error = "";
|
|
}
|
|
}
|
|
}
|
|
void Save::save_all_1(string sections, string str_flag_save) {
|
|
string key = "";
|
|
string value = "";
|
|
string cmd_all = "ubconfig --target " + str_flag_save + " set " + sections;
|
|
size_t len_start_cmd_all = cmd_all.length();
|
|
string str_error = "";
|
|
this->flag_no_save = true;
|
|
string cmd = "";
|
|
string cmd_remove = "";
|
|
for (const auto &iter: (*map_gui)) {
|
|
value = iter.second;
|
|
key = iter.first;
|
|
if (this->check_save(str_flag_save, key)) {
|
|
if (value.length() != 0) {
|
|
cmd_all += " " + key + "=\"" + value + "\"";
|
|
}
|
|
else if (value.length() == 0) {
|
|
cmd = "ubconfig --target " + str_flag_save + " remove " + sections + " " + key;
|
|
}
|
|
else {
|
|
cmd = "";
|
|
}
|
|
if (cmd.length() != 0) {
|
|
process.call(cmd, "");
|
|
this->flag_no_save = false;
|
|
str_error = process.get_cmd_error();
|
|
if (str_error.length() != 0) {
|
|
this->vec_errors.push_back(str_error);
|
|
str_error = "";
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
if (len_start_cmd_all != cmd_all.length()) {
|
|
process.call(cmd, "");
|
|
this->flag_no_save = false;
|
|
str_error = process.get_cmd_error();
|
|
if (str_error.length() != 0) {
|
|
this->vec_errors.push_back(str_error);
|
|
str_error = "";
|
|
}
|
|
}
|
|
}
|
|
}
|