Исправлена библиотека загрузки и сохранения

pull/3/head
Igor Belitskiy 2 years ago
parent 9227ac6216
commit 04a4399222

@ -1,17 +1,21 @@
#include "load.h"
namespace Lib_Load{
void Load::set_sections(vector<string> vec_sections){
this->vec_sections = vec_sections;
void Load::set_sections(string sections){
this->sections = sections;
}
map<string, string> Load::get_load_data(string str_flag_load) {
void Load::set_count_error(int count_error) {
process.set_count_error(count_error);
}
string Load::get_cmd_error() {
return process.get_cmd_error();
}
map<string, string>& Load::get_load_data(std::map <string, string> &map_temp, string str_flag_load) {
string cmd = "";
string response = "";
string key = "";
string value = "";
map<string, string> map_data;
for (const string &section: this->vec_sections) {
cmd = "ubconfig --default --source " + str_flag_load + " get " + section;
cmd = "ubconfig --default --source " + str_flag_load + " get " + this->sections;
response = process.call_all_sections(cmd);
vector<string> vec_str_key_value = Utils::split(response, '\n');
for (const string &param: vec_str_key_value) {
@ -20,14 +24,16 @@ namespace Lib_Load{
size_t index = param.find("=");
key = param.substr(0, index);
value = param.substr(index + 1, param.length());
map_data[key] = value;
cout << key << "=" << value << endl;
Utils::str_replace_all(value, " \"","");
Utils::str_replace_all(value, "\"","");
map_temp[key] = value;
}
}
}
return map_temp;
}
return map_data;
int Load::get_count_error() {
return process.get_count_error();
}
}

@ -6,13 +6,14 @@ namespace Lib_Load{
class Load {
private:
vector<string> vec_sections;
string sections;
My_Process::My_Process_call process = My_Process::My_Process_call();
public:
Load(/* args */);
~Load();
void set_sections(vector<string> vec_sections);
map<string, string> get_load_data(string str_flag_load);
void set_sections(string sections);
int get_count_error();
void set_count_error(int count_error);
string get_cmd_error();
map<string, string>& get_load_data(std::map <string, string> &map_temp, string str_flag_load);
};

@ -1,7 +1,7 @@
#include "my_process.h"
#include "util.h"
namespace My_Process{
namespace My_Process {
#define debug false
struct Utils::Result<string> My_Process_call::call(string cmd) {
this->i_error_old = this->i_error;
@ -66,7 +66,19 @@ string My_Process::get_cmd_error() {
return this->str_cmd_error;
}
string My_Process_call::call_all_sections(string cmd) {
string response = Utils::call(cmd);
return response;
char buffer[PATH_MAX] = {0};
std::string result = "";
FILE* pipe = popen(cmd.c_str(), "r");
if (!pipe) throw std::runtime_error("popen() failed!");
try {
while (fgets(buffer, sizeof buffer, pipe) != NULL) {
result += buffer;
}
} catch (...) {
pclose(pipe);
throw;
}
pclose(pipe);
return result;
}
}

@ -18,7 +18,6 @@ class My_Process {
class My_Process_call: public My_Process {
public:
My_Process_call();
struct Utils::Result<string> call(string cmd);
string call_all_sections(string cmd);
@ -26,7 +25,6 @@ class My_Process_call: public My_Process {
class My_Process_system: public My_Process {
public:
My_Process_system();
void call(string cmd, string thread_str);
};

@ -34,6 +34,11 @@ bool Save::check_save(string flag_save, string key_name) {
}
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;
}
@ -52,16 +57,32 @@ bool Save::check_save(string flag_save, string key_name) {
return true;
}
void Save::save(std::map <string, string> &map_gui, string sections, string str_flag_save) {
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 = false;
for (const auto &map_iter: map_gui) {
key = map_iter.first;
value = map_iter.second;
if (this->check_save(str_flag_save, key) == true) {
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 + "\"";
@ -74,7 +95,43 @@ void Save::save(std::map <string, string> &map_gui, string sections, string str_
}
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);
@ -83,5 +140,16 @@ void Save::save(std::map <string, string> &map_gui, string sections, string str_
}
}
}
}
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 = "";
}
}
}
}

@ -10,17 +10,21 @@ private:
std::map <string, string> *map_local;
std::map <string, string> *map_gui;
vector<string> vec_errors;
vector<string>* vec_param_names;
My_Process::My_Process_system process = My_Process::My_Process_system();
bool flag_no_save;
public:
Save(/* args */);
~Save();
void set_data_global(std::map <string, string> &map_global);
void set_data_local(std::map <string, string> &map_local);
void set_data_gui(std::map <string, string> &map_gui);
bool check_save(string flag_save, string key_name);
bool get_state_save();
void save(std::map <string, string> &map_gui, string sections, string str_flag_save) ;
int get_count_error();
string get_cmd_error();
void save_all(string sections, string str_flag_save);
void set_count_error(int count_error);
void set_vec_params(vector<string>& vec_param_names);
void save(string sections, string str_flag_save) ;
vector<string> get_error();
};
}

Loading…
Cancel
Save