diff --git a/source/load.cc b/source/load.cc index 558f211..e8a361b 100644 --- a/source/load.cc +++ b/source/load.cc @@ -1,33 +1,39 @@ #include "load.h" namespace Lib_Load{ - void Load::set_sections(vector vec_sections){ - this->vec_sections = vec_sections; + void Load::set_sections(string sections){ + this->sections = sections; } - map 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& Load::get_load_data(std::map &map_temp, string str_flag_load) { string cmd = ""; string response = ""; string key = ""; string value = ""; - map map_data; - for (const string §ion: this->vec_sections) { - cmd = "ubconfig --default --source " + str_flag_load + " get " + section; - response = process.call_all_sections(cmd); - vector vec_str_key_value = Utils::split(response, '\n'); - for (const string ¶m: vec_str_key_value) { - if ((param.find("(null)") == std::string::npos) && (param.length() != 0 )) { - if (param.find("=") != std::string::npos) { - 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; - } + cmd = "ubconfig --default --source " + str_flag_load + " get " + this->sections; + response = process.call_all_sections(cmd); + vector vec_str_key_value = Utils::split(response, '\n'); + for (const string ¶m: vec_str_key_value) { + if ((param.find("(null)") == std::string::npos) && (param.length() != 0 )) { + if (param.find("=") != std::string::npos) { + size_t index = param.find("="); + key = param.substr(0, index); + value = param.substr(index + 1, param.length()); + Utils::str_replace_all(value, " \"",""); + Utils::str_replace_all(value, "\"",""); + map_temp[key] = value; } - } } - return map_data; + return map_temp; + } + int Load::get_count_error() { + return process.get_count_error(); } } \ No newline at end of file diff --git a/source/load.h b/source/load.h index 0a42947..26aaf96 100644 --- a/source/load.h +++ b/source/load.h @@ -6,13 +6,14 @@ namespace Lib_Load{ class Load { private: - vector vec_sections; + string sections; My_Process::My_Process_call process = My_Process::My_Process_call(); public: - Load(/* args */); - ~Load(); - void set_sections(vector vec_sections); - map 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& get_load_data(std::map &map_temp, string str_flag_load); }; diff --git a/source/my_process.cc b/source/my_process.cc index 79dad92..658afd5 100644 --- a/source/my_process.cc +++ b/source/my_process.cc @@ -1,7 +1,7 @@ #include "my_process.h" #include "util.h" -namespace My_Process{ +namespace My_Process { #define debug false struct Utils::Result 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; } } \ No newline at end of file diff --git a/source/my_process.h b/source/my_process.h index 809afe5..f780860 100644 --- a/source/my_process.h +++ b/source/my_process.h @@ -18,7 +18,6 @@ class My_Process { class My_Process_call: public My_Process { public: - My_Process_call(); struct Utils::Result 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); }; diff --git a/source/save.cc b/source/save.cc index 3c155db..584e7ea 100644 --- a/source/save.cc +++ b/source/save.cc @@ -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,36 +57,99 @@ bool Save::check_save(string flag_save, string key_name) { return true; } -void Save::save(std::map &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& 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) { - 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 = ""; + 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 = ""; + } + } } - if (cmd.length() != 0) { - process.call(cmd, ""); - this->flag_no_save = true; - 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 = ""; + } } } } diff --git a/source/save.h b/source/save.h index 8a9e90e..bdc2f56 100644 --- a/source/save.h +++ b/source/save.h @@ -10,17 +10,21 @@ private: std::map *map_local; std::map *map_gui; vector vec_errors; + vector* 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 &map_global); void set_data_local(std::map &map_local); void set_data_gui(std::map &map_gui); bool check_save(string flag_save, string key_name); bool get_state_save(); - void save(std::map &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& vec_param_names); + void save(string sections, string str_flag_save) ; vector get_error(); }; }