diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 9e72be6..ec3f582 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -7,7 +7,7 @@ pkg_check_modules(GTK REQUIRED gtkmm-3.0) include_directories(${GTK_INCLUDE_DIRS}) link_directories(${GTK_LIBRARY_DIRS}) add_definitions(${GTK_CFLAGS_OTHER}) -find_package(ICU REQUIRED COMPONENTS uc dt in io) + #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Wextra -Werror -Wmissing-declarations -fdiagnostics-color=always -std=c++2a") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Wextra -Werror -Wmissing-declarations -fdiagnostics-color=always \ 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..43d37c3 --- /dev/null +++ b/source/my_process.cc @@ -0,0 +1,73 @@ +#include "my_process.h" +#include "util.h" + +namespace My_Process{ + +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 cmd) { + +} + +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..7f271c9 --- /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 cmd); + string get_cmd_error(); +}; + + +class My_Process_call: My_Process { + public: + My_Process_call(); + struct Utils::Result call(string cmd); + string call_all_sections(string cmd); + +}; + +class My_Process_system: My_Process { + public: + My_Process_system(); + void call(string cmd, string thread_str); + +}; +} +#endif \ No newline at end of file diff --git a/source/process.cc b/source/process.cc deleted file mode 100644 index 00c83c5..0000000 --- a/source/process.cc +++ /dev/null @@ -1,10 +0,0 @@ -#include "process.h" - -struct Result Process_call::call(string cmd) {} -int Process::get_count_error() { - return this->i_error; -} -void Process::set_count_error(int count_error) { - this->i_error = count_error; -} -void Process_system::call(string cmd) {} \ No newline at end of file diff --git a/source/process.h b/source/process.h deleted file mode 100644 index 8266223..0000000 --- a/source/process.h +++ /dev/null @@ -1,22 +0,0 @@ -#include "util.h" - -class Process { - int i_error = 0; - int i_error_old = 0; - virtual int get_count_error() = 0; - virtual void set_count_error(int count_error) = 0; -}; - - -class Process_call: Process { - public: - Process_call(); - struct Result call(string cmd); - -}; -class Process_system: Process { - public: - Process_system(); - void call(string cmd); - -}; \ No newline at end of file diff --git a/source/save.cc b/source/save.cc new file mode 100644 index 0000000..c36d9c5 --- /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_local(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_local(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, std::map &map_temp,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..1f5f194 --- /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_local(std::map &map_global); + void set_data_local(std::map &map_local); + void set_data_local(std::map &map_gui); + bool check_save(string flag_save, string key_name); + bool get_state_save(); + void save(std::map &map_gui, std::map &map_temp,string sections, string str_flag_save) ; + vector get_error(); +}; +} +#endif \ No newline at end of file diff --git a/source/util.cc b/source/util.cc index d2ca2dd..8d8c496 100644 --- a/source/util.cc +++ b/source/util.cc @@ -1,5 +1,6 @@ #include "util.h" +namespace Utils { array, 5> read_csv(const string& filename) { array, 5> array_vectors; @@ -66,6 +67,7 @@ string call(string cmd) { } return path; } + vector find_all(string &str_ntp, string substr) { size_t index = 0; vector sub_index; @@ -192,4 +194,5 @@ int synopsis_show(string str_link) { cmd = "su -l " + response_user + " -c \" DISPLAY=$DISPLAY " + cmd + " \""; } return system(cmd.c_str()); +} } \ No newline at end of file diff --git a/source/util.h b/source/util.h index fa83ff8..9044b95 100644 --- a/source/util.h +++ b/source/util.h @@ -1,3 +1,5 @@ +#ifndef UTIL_H +#define UTIL_H #include #include #include @@ -23,6 +25,7 @@ using namespace std; +namespace Utils { template struct Result{ Type response; @@ -44,3 +47,5 @@ unsigned short read_uid_min_max(string filename, string search); vector pars_users(); string read_passwd(string username); int synopsis_show(string str_link); +} +#endif \ No newline at end of file