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

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

@ -1,33 +1,39 @@
#include "load.h" #include "load.h"
namespace Lib_Load{ namespace Lib_Load{
void Load::set_sections(vector<string> vec_sections){ void Load::set_sections(string sections){
this->vec_sections = vec_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 cmd = "";
string response = ""; string response = "";
string key = ""; string key = "";
string value = ""; string value = "";
map<string, string> map_data; cmd = "ubconfig --default --source " + str_flag_load + " get " + this->sections;
for (const string &section: this->vec_sections) { response = process.call_all_sections(cmd);
cmd = "ubconfig --default --source " + str_flag_load + " get " + section; vector<string> vec_str_key_value = Utils::split(response, '\n');
response = process.call_all_sections(cmd); for (const string &param: vec_str_key_value) {
vector<string> vec_str_key_value = Utils::split(response, '\n'); if ((param.find("(null)") == std::string::npos) && (param.length() != 0 )) {
for (const string &param: vec_str_key_value) { if (param.find("=") != std::string::npos) {
if ((param.find("(null)") == std::string::npos) && (param.length() != 0 )) { size_t index = param.find("=");
if (param.find("=") != std::string::npos) { key = param.substr(0, index);
size_t index = param.find("="); value = param.substr(index + 1, param.length());
key = param.substr(0, index); Utils::str_replace_all(value, " \"","");
value = param.substr(index + 1, param.length()); Utils::str_replace_all(value, "\"","");
map_data[key] = value; map_temp[key] = value;
cout << key << "=" << value << endl;
}
} }
} }
} }
return map_data; return map_temp;
}
int Load::get_count_error() {
return process.get_count_error();
} }
} }

@ -6,13 +6,14 @@ namespace Lib_Load{
class Load { class Load {
private: private:
vector<string> vec_sections; string sections;
My_Process::My_Process_call process = My_Process::My_Process_call(); My_Process::My_Process_call process = My_Process::My_Process_call();
public: public:
Load(/* args */); void set_sections(string sections);
~Load(); int get_count_error();
void set_sections(vector<string> vec_sections); void set_count_error(int count_error);
map<string, string> get_load_data(string str_flag_load); 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 "my_process.h"
#include "util.h" #include "util.h"
namespace My_Process{ namespace My_Process {
#define debug false #define debug false
struct Utils::Result<string> My_Process_call::call(string cmd) { struct Utils::Result<string> My_Process_call::call(string cmd) {
this->i_error_old = this->i_error; this->i_error_old = this->i_error;
@ -66,7 +66,19 @@ string My_Process::get_cmd_error() {
return this->str_cmd_error; return this->str_cmd_error;
} }
string My_Process_call::call_all_sections(string cmd) { string My_Process_call::call_all_sections(string cmd) {
string response = Utils::call(cmd); char buffer[PATH_MAX] = {0};
return response; 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 { class My_Process_call: public My_Process {
public: public:
My_Process_call();
struct Utils::Result<string> call(string cmd); struct Utils::Result<string> call(string cmd);
string call_all_sections(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 { class My_Process_system: public My_Process {
public: public:
My_Process_system();
void call(string cmd, string thread_str); 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 = (*map_gui).find(key_name);
iter_map_data_old = (*map_data_old).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()) { if (iter_map_data_old == (*map_data_old).end() && iter_map_data != (*map_gui).end()) {
return true; return true;
} }
@ -52,36 +57,99 @@ bool Save::check_save(string flag_save, string key_name) {
return true; 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 key = "";
string value = ""; string value = "";
string cmd = ""; string cmd = "";
string str_error = ""; string str_error = "";
this->flag_no_save = false; this->flag_no_save = true;
for (const auto &map_iter: map_gui) { for (const auto &key: *vec_param_names) {
key = map_iter.first; if (map_gui->find(key) != map_gui->end()) {
value = map_iter.second; value = (*map_gui)[key];
if (this->check_save(str_flag_save, key) == true) { if (this->check_save(str_flag_save, key)) {
if (value.length() != 0) { if (value.length() != 0) {
cmd = "ubconfig --target " + str_flag_save + " set " + sections + " " + cmd = "ubconfig --target " + str_flag_save + " set " + sections + " " +
key + "=\"" + value + "\""; key + "=\"" + value + "\"";
} }
else if (value.length() == 0) { else if (value.length() == 0) {
cmd = "ubconfig --target " + str_flag_save + " remove " + sections + " " + key; cmd = "ubconfig --target " + str_flag_save + " remove " + sections + " " + key;
} }
else { else {
cmd = ""; 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 (cmd.length() != 0) { }
process.call(cmd, "");
this->flag_no_save = true; }
str_error = process.get_cmd_error(); }
if (str_error.length() != 0) { void Save::save_all(string sections, string str_flag_save) {
this->vec_errors.push_back(str_error); string key = "";
str_error = ""; 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 = "";
}
} }
} }
} }

@ -10,17 +10,21 @@ private:
std::map <string, string> *map_local; std::map <string, string> *map_local;
std::map <string, string> *map_gui; std::map <string, string> *map_gui;
vector<string> vec_errors; vector<string> vec_errors;
vector<string>* vec_param_names;
My_Process::My_Process_system process = My_Process::My_Process_system(); My_Process::My_Process_system process = My_Process::My_Process_system();
bool flag_no_save; bool flag_no_save;
public: public:
Save(/* args */);
~Save();
void set_data_global(std::map <string, string> &map_global); void set_data_global(std::map <string, string> &map_global);
void set_data_local(std::map <string, string> &map_local); void set_data_local(std::map <string, string> &map_local);
void set_data_gui(std::map <string, string> &map_gui); void set_data_gui(std::map <string, string> &map_gui);
bool check_save(string flag_save, string key_name); bool check_save(string flag_save, string key_name);
bool get_state_save(); 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(); vector<string> get_error();
}; };
} }

Loading…
Cancel
Save