From 37be901c97e89f6ebb24e87933ddf57cd57371d5 Mon Sep 17 00:00:00 2001 From: Amlal Date: Tue, 22 Oct 2024 20:49:53 +0200 Subject: IMP: Refactor of the source code. Signed-off-by: Amlal --- src/IManifestBuilder.cc | 6 ++ src/IManifestBuilder.cxx | 6 -- src/JSONManifestBuilder.cc | 152 ++++++++++++++++++++++++++++++++++++++++++++ src/JSONManifestBuilder.cxx | 152 -------------------------------------------- 4 files changed, 158 insertions(+), 158 deletions(-) create mode 100644 src/IManifestBuilder.cc delete mode 100644 src/IManifestBuilder.cxx create mode 100644 src/JSONManifestBuilder.cc delete mode 100644 src/JSONManifestBuilder.cxx (limited to 'src') diff --git a/src/IManifestBuilder.cc b/src/IManifestBuilder.cc new file mode 100644 index 0000000..6746378 --- /dev/null +++ b/src/IManifestBuilder.cc @@ -0,0 +1,6 @@ +// ============================================================= // +// btb +// Copyright ZKA Web Services. +// ============================================================= // + +#include diff --git a/src/IManifestBuilder.cxx b/src/IManifestBuilder.cxx deleted file mode 100644 index 7a2f210..0000000 --- a/src/IManifestBuilder.cxx +++ /dev/null @@ -1,6 +0,0 @@ -// ============================================================= // -// btb -// Copyright ZKA Technologies. -// ============================================================= // - -#include diff --git a/src/JSONManifestBuilder.cc b/src/JSONManifestBuilder.cc new file mode 100644 index 0000000..e5db6b8 --- /dev/null +++ b/src/JSONManifestBuilder.cc @@ -0,0 +1,152 @@ +// ============================================================= // +// btb +// Copyright ZKA Web Services. +// ============================================================= // + +#include +#include + +#include +#include +#include + +using JSON = nlohmann::json; + +/// @brief Builds a JSON target from a JSON file. +/// @param arg_sz filename size (must be 1 or greater). +/// @param arg_val filename path (must be a valid JSON file). +/// @retval true succeeded building. +/// @retval false failed to build. +bool JSONManifestBuilder::Build(int arg_sz, const char* arg_val) +{ + std::string path; + + if (arg_sz < 0) + { + return false; + } + else + { + path = arg_val; + + if (!std::filesystem::exists(path)) + { + std::cout << "btb: error: file '" << path << "' does not exist." << std::endl; + return false; + } + } + + try + { + std::ifstream json(path); + + if (!json.good()) + { + return false; + } + + JSON json_obj = JSON::parse(json); + + std::string compiler = json_obj["compiler_path"].get(); + + JSON header_search_path = json_obj["headers_path"]; + JSON sources_files = json_obj["sources_path"]; + + std::string command = compiler + " "; + + for (auto& sources : sources_files) + { + command += sources.get() + " "; + } + + for (auto& headers : header_search_path) + { + command += "-I" + headers.get() + " "; + } + + JSON macros_list = json_obj["cpp_macros"]; + + for (auto& macro : macros_list) + { + command += "-D" + macro.get() + " "; + } + + JSON compiler_flags = json_obj["compiler_flags"]; + + for (auto& flag : compiler_flags) + { + command += flag.get() + " "; + } + + if (json_obj["compiler_std"].is_string()) + command += "-std=" + json_obj["compiler_std"].get() + " "; + + command += "-o " + json_obj["output_name"].get(); + + auto target = json_obj["output_name"].get(); + + std::cout << "btb: output path: " << target << "\n"; + std::cout << "btb: command: " << command << "\n"; + + std::system(command.c_str()); + + try + { + if (json_obj["run_after_build"].get()) + { + if (target.ends_with(".so") || + target.ends_with(".dll")) + { + std::cout << "btb: error: can't open DLL/SO, it mayn't contain an entrypoint." << std::endl; + return true; + } + else if (target.ends_with(".dll")) + { + auto file = std::ifstream(target); + std::stringstream ss; + ss << file.rdbuf(); + + if (ss.str()[0] == 'J' && + ss.str()[1] == 'o' && + ss.str()[2] == 'y' && + ss.str()[3] == '!') + std::cout << "btb: error: can't open Joy! DLL, it maynt't contain an entrypoint." << std::endl; + else if (ss.str()[0] == '!' && + ss.str()[1] == 'y' && + ss.str()[2] == 'o' && + ss.str()[3] == 'J') + std::cout << "btb: error: can't open !yoJ DLL, it maynt't contain an entrypoint." << std::endl; + else if (ss.str()[0] == 'M' && + ss.str()[1] == 'Z') + std::cout << "btb: error: can't open MZ DLL, it maynt't contain an entrypoint." << std::endl; + else if (ss.str()[0] == 0x7F && + ss.str()[1] == 'E') + { + std::cout << "btb: error: can't open ELF DLL, it maynt't contain an entrypoint." << std::endl; + } + + return true; + } + +#ifdef _WIN32 + std::system((".\\" + target).c_str()); +#else + std::system(("./" + target).c_str()); +#endif + } + } + catch (...) + { + return true; + } + } + catch (std::runtime_error& err) + { + std::cout << "btb: error: " << err.what() << std::endl; + perror("btb"); + + return false; + } + + return true; +} diff --git a/src/JSONManifestBuilder.cxx b/src/JSONManifestBuilder.cxx deleted file mode 100644 index 0aee8d8..0000000 --- a/src/JSONManifestBuilder.cxx +++ /dev/null @@ -1,152 +0,0 @@ -// ============================================================= // -// btb -// Copyright ZKA Technologies. -// ============================================================= // - -#include -#include - -#include -#include -#include - -using JSON = nlohmann::json; - -/// @brief Builds a JSON target from a JSON file. -/// @param arg_sz filename size (must be 1 or greater). -/// @param arg_val filename path (must be a valid JSON file). -/// @retval true succeeded building. -/// @retval false failed to build. -bool JSONManifestBuilder::Build(int arg_sz, const char* arg_val) -{ - std::string path; - - if (arg_sz < 0) - { - return false; - } - else - { - path = arg_val; - - if (!std::filesystem::exists(path)) - { - std::cout << "btb: error: file '" << path << "' does not exist." << std::endl; - return false; - } - } - - try - { - std::ifstream json(path); - - if (!json.good()) - { - return false; - } - - JSON json_obj = JSON::parse(json); - - std::string compiler = json_obj["compiler_path"].get(); - - JSON header_search_path = json_obj["headers_path"]; - JSON sources_files = json_obj["sources_path"]; - - std::string command = compiler + " "; - - for (auto& sources : sources_files) - { - command += sources.get() + " "; - } - - for (auto& headers : header_search_path) - { - command += "-I" + headers.get() + " "; - } - - JSON macros_list = json_obj["cpp_macros"]; - - for (auto& macro : macros_list) - { - command += "-D" + macro.get() + " "; - } - - JSON compiler_flags = json_obj["compiler_flags"]; - - for (auto& flag : compiler_flags) - { - command += flag.get() + " "; - } - - if (json_obj["compiler_std"].is_string()) - command += "-std=" + json_obj["compiler_std"].get() + " "; - - command += "-o " + json_obj["output_name"].get(); - - auto target = json_obj["output_name"].get(); - - std::cout << "btb: output path: " << target << "\n"; - std::cout << "btb: command: " << command << "\n"; - - std::system(command.c_str()); - - try - { - if (json_obj["run_after_build"].get()) - { - if (target.ends_with(".so") || - target.ends_with(".dll")) - { - std::cout << "btb: error: can't open DLL/SO, it mayn't contain an entrypoint." << std::endl; - return true; - } - else if (target.ends_with(".dll")) - { - auto file = std::ifstream(target); - std::stringstream ss; - ss << file.rdbuf(); - - if (ss.str()[0] == 'J' && - ss.str()[1] == 'o' && - ss.str()[2] == 'y' && - ss.str()[3] == '!') - std::cout << "btb: error: can't open Joy! DLL, it maynt't contain an entrypoint." << std::endl; - else if (ss.str()[0] == '!' && - ss.str()[1] == 'y' && - ss.str()[2] == 'o' && - ss.str()[3] == 'J') - std::cout << "btb: error: can't open !yoJ DLL, it maynt't contain an entrypoint." << std::endl; - else if (ss.str()[0] == 'M' && - ss.str()[1] == 'Z') - std::cout << "btb: error: can't open MZ DLL, it maynt't contain an entrypoint." << std::endl; - else if (ss.str()[0] == 0x7F && - ss.str()[1] == 'E') - { - std::cout << "btb: error: can't open ELF DLL, it maynt't contain an entrypoint." << std::endl; - } - - return true; - } - -#ifdef _WIN32 - std::system((".\\" + target).c_str()); -#else - std::system(("./" + target).c_str()); -#endif - } - } - catch (...) - { - return true; - } - } - catch (std::runtime_error& err) - { - std::cout << "btb: error: " << err.what() << std::endl; - perror("btb"); - - return false; - } - - return true; -} -- cgit v1.2.3