From 3534fda7dc7b174f4761cb7f2cf655ce0fb39365 Mon Sep 17 00:00:00 2001 From: Igor Belitskiy Date: Thu, 6 Apr 2023 09:34:29 +0600 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=B1=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE=D1=82=D0=B5?= =?UTF-8?q?=D0=BA=D0=B8=20=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B8=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7?= =?UTF-8?q?=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/load.cc | 33 +++++++++++++++++ source/load.h | 20 ++++++++++ source/my_process.cc | 72 ++++++++++++++++++++++++++++++++++++ source/my_process.h | 34 +++++++++++++++++ source/save.cc | 87 ++++++++++++++++++++++++++++++++++++++++++++ source/save.h | 27 ++++++++++++++ 6 files changed, 273 insertions(+) create mode 100644 source/load.cc create mode 100644 source/load.h create mode 100644 source/my_process.cc create mode 100644 source/my_process.h create mode 100644 source/save.cc create mode 100644 source/save.h diff --git a/source/load.cc b/source/load.cc new file mode 100644 index 0000000..558f211 --- /dev/null +++ b/source/load.cc @@ -0,0 +1,33 @@ +#include "load.h" + +namespace Lib_Load{ + void Load::set_sections(vector vec_sections){ + this->vec_sections = vec_sections; + } + map Load::get_load_data(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; + } + } + + } + } + return map_data; + } + +} \ No newline at end of file diff --git a/source/load.h b/source/load.h new file mode 100644 index 0000000..0a42947 --- /dev/null +++ b/source/load.h @@ -0,0 +1,20 @@ +#ifndef LOAD_H +#define LOAD_H + +#include "my_process.h" +namespace Lib_Load{ + class Load { + + private: + vector vec_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); + }; + + +} +#endif \ No newline at end of file diff --git a/source/my_process.cc b/source/my_process.cc new file mode 100644 index 0000000..79dad92 --- /dev/null +++ b/source/my_process.cc @@ -0,0 +1,72 @@ +#include "my_process.h" +#include "util.h" + +namespace My_Process{ +#define debug false +struct Utils::Result My_Process_call::call(string cmd) { + this->i_error_old = this->i_error; + struct Utils::Result obj_result; + string response = Utils::call(cmd); + obj_result.response = response; + if ((response.find("(null)") == std::string::npos) && (response.length() != 0 )) { + if (response.find("=") != std::string::npos) { + if (response.find("\n") != std::string::npos) { + response = response.substr(response.find("=")+1,response.length()); + response = response.substr(0,response.find("\n")); + obj_result.response = response; + obj_result.error = 0; + } + else { + obj_result.error = 1; + this->i_error += 1; + this->log_mess_error(cmd); + } + } + else { + obj_result.error = 2; + this->i_error += 1; + str_cmd_error = cmd; + this->log_mess_error(cmd); + } + } + else { + obj_result.error = 3; + this->i_error += 1; + str_cmd_error = cmd; + this->log_mess_error(cmd); + } + return obj_result; +} + +int My_Process::get_count_error() { + return this->i_error; +} + +void My_Process::set_back_count_error() { + this->i_error = this->i_error_old; +} + +void My_Process_system::call(string cmd, string thread_str = "") { + string cmd_new = cmd + " " + thread_str; + int response_cmd = system(cmd_new.c_str()); + if (response_cmd != 0) { + this->i_error += 1; + } +} + +void My_Process::set_count_error(int count_error) { + this->i_error = count_error; + this->str_cmd_error = ""; +} + +void My_Process::log_mess_error(string) { +} + +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; +} +} \ No newline at end of file diff --git a/source/my_process.h b/source/my_process.h new file mode 100644 index 0000000..809afe5 --- /dev/null +++ b/source/my_process.h @@ -0,0 +1,34 @@ +#ifndef SAVE_H +#define SAVE_H + +#include "util.h" +namespace My_Process{ +class My_Process { + public: + int i_error = 0; + int i_error_old = 0; + string str_cmd_error = ""; + int get_count_error(); + void set_count_error(int count_error); + void set_back_count_error(); + void log_mess_error(string); + string get_cmd_error(); +}; + + +class My_Process_call: public My_Process { + public: + My_Process_call(); + struct Utils::Result call(string cmd); + string call_all_sections(string cmd); + +}; + +class My_Process_system: public My_Process { + public: + My_Process_system(); + void call(string cmd, string thread_str); + +}; +} +#endif \ No newline at end of file diff --git a/source/save.cc b/source/save.cc new file mode 100644 index 0000000..3c155db --- /dev/null +++ b/source/save.cc @@ -0,0 +1,87 @@ +#include "save.h" + +namespace Lib_save { + +vector Save::get_error() { + return this->vec_errors; +} + +bool Save::get_state_save() { + return this->flag_no_save; +} + +void Save::set_data_global(std::map &map_global) { + this->map_global = &map_global; +} + +void Save::set_data_local(std::map &map_local) { + this->map_local = &map_local; +} + +void Save::set_data_gui(std::map &map_gui) { + this->map_gui = &map_gui; +} + +bool Save::check_save(string flag_save, string key_name) { + std::map :: iterator iter_map_data; + std::map :: iterator iter_map_data_old; + std::map *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 (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; +} + +void Save::save(std::map &map_gui, 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 = ""; + } + 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 = ""; + } + } + } + } +} +} diff --git a/source/save.h b/source/save.h new file mode 100644 index 0000000..8a9e90e --- /dev/null +++ b/source/save.h @@ -0,0 +1,27 @@ +#ifndef MY_PROCESS_H +#define MY_PROCESS_H + +#include "my_process.h" +namespace Lib_save { + +class Save { +private: + std::map *map_global; + std::map *map_local; + std::map *map_gui; + vector vec_errors; + 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) ; + vector get_error(); +}; +} +#endif \ No newline at end of file