From c79f00e99c37d00e0abecc94863f636355f6358a Mon Sep 17 00:00:00 2001 From: guilpier-code <62292552+guilpier-code@users.noreply.github.com> Date: Mon, 9 Oct 2023 15:14:36 +0200 Subject: [PATCH] Remove dependency to UI, use RAII to handle resources (#1678) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Remove dependency to UI : removal from solver * Remove dependency to UI : calling the main arguments pre-processing from outside * Remove dependency to UI : removal from analyzer * Remove dependency to UI : replace the dependency to UI in other stand alone targets + correct new target include files paths * Remove dependency to UI : forgot to add header file to repository * Remove dependency to UI : introduce a RAII to automatically destroy the allocated memory * Add & use IntoUTF8ArgsTranslator::convert (#1684) * Add & use IntoUTF8ArgsTranslator::convert * Fix header inclusion * Fix windows build * Fix build * Fix build (bis) * Remove ref to argc, make IntoUTF8ArgsTranslator's API homogenous * Apply clang-format --------- Co-authored-by: Florian Omnès --- src/analyzer/CMakeLists.txt | 1 + src/analyzer/main.cpp | 5 +- src/libs/antares/CMakeLists.txt | 1 + src/libs/antares/args/CMakeLists.txt | 22 ++++++ src/libs/antares/args/args_to_utf8.cpp | 44 ++++++++++++ .../args/include/antares/args/args_to_utf8.h | 15 ++++ src/solver/CMakeLists.txt | 1 + src/solver/main.cpp | 8 +-- src/tools/batchrun/CMakeLists.txt | 1 + src/tools/batchrun/main.cpp | 6 +- src/tools/cleaner/CMakeLists.txt | 1 + src/tools/cleaner/main.cpp | 6 +- src/tools/config/CMakeLists.txt | 1 + src/tools/config/main.cpp | 6 +- src/tools/finder/CMakeLists.txt | 1 + src/tools/finder/main.cpp | 6 +- src/tools/updater/CMakeLists.txt | 1 + src/tools/updater/main.cpp | 5 +- src/tools/vacuum/CMakeLists.txt | 1 + src/tools/vacuum/main.cpp | 5 +- src/tools/yby-aggregator/CMakeLists.txt | 1 + src/tools/yby-aggregator/main.cpp | 6 +- src/ui/common/winmain.hxx | 71 ------------------- src/ui/simulator/cmake/application.cmake | 5 +- src/ui/simulator/main.cpp | 7 +- 25 files changed, 122 insertions(+), 105 deletions(-) create mode 100644 src/libs/antares/args/CMakeLists.txt create mode 100644 src/libs/antares/args/args_to_utf8.cpp create mode 100644 src/libs/antares/args/include/antares/args/args_to_utf8.h delete mode 100644 src/ui/common/winmain.hxx diff --git a/src/analyzer/CMakeLists.txt b/src/analyzer/CMakeLists.txt index 77a74e2957..1a3759f1c3 100644 --- a/src/analyzer/CMakeLists.txt +++ b/src/analyzer/CMakeLists.txt @@ -51,6 +51,7 @@ INSTALL(EXPORT antares-analyzer target_link_libraries(antares-${ANTARES_PRG_VERSION}-analyzer PRIVATE yuni-static-core + Antares::args_helper ${CMAKE_THREADS_LIBS_INIT} antares-core #local.h PUBLIC diff --git a/src/analyzer/main.cpp b/src/analyzer/main.cpp index fc328791a4..22b7f37ca7 100644 --- a/src/analyzer/main.cpp +++ b/src/analyzer/main.cpp @@ -28,7 +28,7 @@ #include #include #include -#include "../ui/common/winmain.hxx" +#include #include #include "atsp/atsp.h" #include @@ -115,7 +115,8 @@ int main(int argc, char* argv[]) InitializeDefaultLocale(); logs.applicationName("analyzer"); - argv = AntaresGetUTF8Arguments(argc, argv); + IntoUTF8ArgsTranslator toUTF8ArgsTranslator(argc, argv); + std::tie(argc, argv) = toUTF8ArgsTranslator.convert(); String optSettings; diff --git a/src/libs/antares/CMakeLists.txt b/src/libs/antares/CMakeLists.txt index 6f7ce1063e..91c83bd02f 100644 --- a/src/libs/antares/CMakeLists.txt +++ b/src/libs/antares/CMakeLists.txt @@ -1,6 +1,7 @@ OMESSAGE("Antares Core library") +add_subdirectory(args) add_subdirectory(writer) add_subdirectory(memory) diff --git a/src/libs/antares/args/CMakeLists.txt b/src/libs/antares/args/CMakeLists.txt new file mode 100644 index 0000000000..eb9559c360 --- /dev/null +++ b/src/libs/antares/args/CMakeLists.txt @@ -0,0 +1,22 @@ +set(SRC_ARGS_HELPER + include/antares/args/args_to_utf8.h + args_to_utf8.cpp +) + +source_group("misc\\args_helper" FILES ${SRC_ARGS_HELPER}) + +add_library(args_helper + ${SRC_ARGS_HELPER} +) + +add_library(Antares::args_helper ALIAS args_helper) + +target_link_libraries(args_helper + PRIVATE + yuni-static-core + ) + +target_include_directories(args_helper + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/include +) \ No newline at end of file diff --git a/src/libs/antares/args/args_to_utf8.cpp b/src/libs/antares/args/args_to_utf8.cpp new file mode 100644 index 0000000000..7ac874e4d1 --- /dev/null +++ b/src/libs/antares/args/args_to_utf8.cpp @@ -0,0 +1,44 @@ + +#include +#include "antares/args/args_to_utf8.h" + +#ifdef YUNI_OS_WINDOWS +#include +#include +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN 1 +#endif // WIN32_LEAN_AND_MEAN +#include +#include +#endif // YUNI_OS_WINDOWS + +IntoUTF8ArgsTranslator::IntoUTF8ArgsTranslator(int argc, char** argv) : argc_(argc), argv_(argv) +{ +} + +std::pair IntoUTF8ArgsTranslator::convert() +{ +#ifdef YUNI_OS_WINDOWS + wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &argc_); + argv_ = (char**)malloc(argc_ * sizeof(char*)); + for (int i = 0; i != argc_; ++i) + { + const uint len = (uint)wcslen(wargv[i]); + const uint newLen = WideCharToMultiByte(CP_UTF8, 0, wargv[i], len, NULL, 0, NULL, NULL); + argv_[i] = (char*)malloc((newLen + 1) * sizeof(char)); + memset(argv_[i], 0, (newLen + 1) * sizeof(char)); + WideCharToMultiByte(CP_UTF8, 0, wargv[i], len, argv_[i], newLen, NULL, NULL); + argv_[i][newLen] = '\0'; + } +#endif + return {argc_, argv_}; +} + +IntoUTF8ArgsTranslator::~IntoUTF8ArgsTranslator() +{ +#ifdef YUNI_OS_WINDOWS + for (int i = 0; i != argc_; ++i) + free(argv_[i]); + free(argv_); +#endif +} diff --git a/src/libs/antares/args/include/antares/args/args_to_utf8.h b/src/libs/antares/args/include/antares/args/args_to_utf8.h new file mode 100644 index 0000000000..cb1c0085c6 --- /dev/null +++ b/src/libs/antares/args/include/antares/args/args_to_utf8.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +class IntoUTF8ArgsTranslator +{ +public: + IntoUTF8ArgsTranslator(int argc, char** argv); + std::pair convert(); + ~IntoUTF8ArgsTranslator(); + +private: + int argc_; + char** argv_; +}; diff --git a/src/solver/CMakeLists.txt b/src/solver/CMakeLists.txt index e30a6089c4..7372ff3b83 100644 --- a/src/solver/CMakeLists.txt +++ b/src/solver/CMakeLists.txt @@ -74,6 +74,7 @@ add_executable(antares-solver set_target_properties(antares-solver PROPERTIES OUTPUT_NAME ${exec_name}) set(ANTARES_SOLVER_LIBS + Antares::args_helper Antares::date Antares::benchmarking Antares::result_writer diff --git a/src/solver/main.cpp b/src/solver/main.cpp index b261d55f9b..367278b7b9 100644 --- a/src/solver/main.cpp +++ b/src/solver/main.cpp @@ -28,7 +28,7 @@ #include #include "application.h" -#include "../ui/common/winmain.hxx" //TODO: remove that reverse dependency to UI +#include #include #include @@ -127,15 +127,13 @@ int main(int argc, char** argv) InitializeDefaultLocale(); // Getting real UTF8 arguments - argv = AntaresGetUTF8Arguments(argc, argv); - + IntoUTF8ArgsTranslator toUTF8ArgsTranslator(argc, argv); + std::tie(argc, argv) = toUTF8ArgsTranslator.convert(); Antares::Solver::Application application; application.prepare(argc, argv); application.execute(); application.writeExectutionInfo(); - FreeUTF8Arguments(argc, argv); - // to avoid a bug from wxExecute, we should wait a little before returning SuspendMilliSeconds(200 /*ms*/); diff --git a/src/tools/batchrun/CMakeLists.txt b/src/tools/batchrun/CMakeLists.txt index b0957b354a..3656f3d28d 100644 --- a/src/tools/batchrun/CMakeLists.txt +++ b/src/tools/batchrun/CMakeLists.txt @@ -39,6 +39,7 @@ set(BATCHRUN_LIBS antares-core #local.h yuni-static-core ${CMAKE_THREADS_LIBS_INIT} + Antares::args_helper Antares::study ) diff --git a/src/tools/batchrun/main.cpp b/src/tools/batchrun/main.cpp index 0f0000b9f5..8cf5a97261 100644 --- a/src/tools/batchrun/main.cpp +++ b/src/tools/batchrun/main.cpp @@ -35,7 +35,7 @@ #include #include #include -#include "../../ui/common/winmain.hxx" +#include #include #include #ifdef YUNI_OS_WINDOWS @@ -78,8 +78,8 @@ int main(int argc, char* argv[]) InitializeDefaultLocale(); logs.applicationName("batchrun"); - argv = AntaresGetUTF8Arguments(argc, argv); - + IntoUTF8ArgsTranslator toUTF8ArgsTranslator(argc, argv); + std::tie(argc, argv) = toUTF8ArgsTranslator.convert(); // Initializing the toolbox Antares::Resources::Initialize(argc, argv, true); diff --git a/src/tools/cleaner/CMakeLists.txt b/src/tools/cleaner/CMakeLists.txt index da5694f626..ea87db4a79 100644 --- a/src/tools/cleaner/CMakeLists.txt +++ b/src/tools/cleaner/CMakeLists.txt @@ -26,6 +26,7 @@ INSTALL(EXPORT antares-study-cleaner set(CLEANER_LIBS yuni-static-core + Antares::args_helper Antares::study Antares::sys antares-core #version.h diff --git a/src/tools/cleaner/main.cpp b/src/tools/cleaner/main.cpp index 3282ccbd8b..729bdb760f 100644 --- a/src/tools/cleaner/main.cpp +++ b/src/tools/cleaner/main.cpp @@ -28,7 +28,7 @@ #include #include #include -#include "../../ui/common/winmain.hxx" +#include #include #include #include @@ -97,8 +97,8 @@ int main(int argc, char* argv[]) InitializeDefaultLocale(); Antares::logs.applicationName("cleaner"); - argv = AntaresGetUTF8Arguments(argc, argv); - + IntoUTF8ArgsTranslator toUTF8ArgsTranslator(argc, argv); + std::tie(argc, argv) = toUTF8ArgsTranslator.convert(); String::Vector optInput; bool optPrintOnly = false; bool optMrProper = false; diff --git a/src/tools/config/CMakeLists.txt b/src/tools/config/CMakeLists.txt index c85d140d5b..f2d2a49add 100644 --- a/src/tools/config/CMakeLists.txt +++ b/src/tools/config/CMakeLists.txt @@ -30,6 +30,7 @@ INSTALL(EXPORT antares-config set(CONFIG_LIBS yuni-static-core + Antares::args_helper ${CMAKE_THREADS_LIBS_INIT} ) diff --git a/src/tools/config/main.cpp b/src/tools/config/main.cpp index 17a85efb8b..c4f34aecbc 100644 --- a/src/tools/config/main.cpp +++ b/src/tools/config/main.cpp @@ -29,7 +29,7 @@ #include #include #include -#include "../../ui/common/winmain.hxx" +#include #include #include #include @@ -44,8 +44,8 @@ int main(int argc, char* argv[]) InitializeDefaultLocale(); logs.applicationName("config"); - argv = AntaresGetUTF8Arguments(argc, argv); - + IntoUTF8ArgsTranslator toUTF8ArgsTranslator(argc, argv); + std::tie(argc, argv) = toUTF8ArgsTranslator.convert(); // Initializing the toolbox Antares::Resources::Initialize(argc, argv, true); diff --git a/src/tools/finder/CMakeLists.txt b/src/tools/finder/CMakeLists.txt index ef8b1ae4db..7d58acaf2f 100644 --- a/src/tools/finder/CMakeLists.txt +++ b/src/tools/finder/CMakeLists.txt @@ -28,6 +28,7 @@ INSTALL(EXPORT antares-study-finder set(FINDER_LIBS antares-core yuni-static-core + Antares::args_helper ${CMAKE_THREADS_LIBS_INIT} ) diff --git a/src/tools/finder/main.cpp b/src/tools/finder/main.cpp index 10dafea6bc..2b3e04c2b7 100644 --- a/src/tools/finder/main.cpp +++ b/src/tools/finder/main.cpp @@ -28,7 +28,7 @@ #include #include #include -#include "../../ui/common/winmain.hxx" +#include #include #include #include @@ -73,8 +73,8 @@ int main(int argc, char* argv[]) InitializeDefaultLocale(); logs.applicationName("finder"); - argv = AntaresGetUTF8Arguments(argc, argv); - + IntoUTF8ArgsTranslator toUTF8ArgsTranslator(argc, argv); + std::tie(argc, argv) = toUTF8ArgsTranslator.convert(); Yuni::String::Vector optInput; bool optExtra = false; bool optCSV = false; diff --git a/src/tools/updater/CMakeLists.txt b/src/tools/updater/CMakeLists.txt index ec70330b8e..6fd814588a 100644 --- a/src/tools/updater/CMakeLists.txt +++ b/src/tools/updater/CMakeLists.txt @@ -26,6 +26,7 @@ INSTALL(EXPORT antares-study-updater set(UPDATER_LIBS yuni-static-core + Antares::args_helper Antares::study Antares::sys antares-core #version.h diff --git a/src/tools/updater/main.cpp b/src/tools/updater/main.cpp index af67d4c190..4650493f7d 100644 --- a/src/tools/updater/main.cpp +++ b/src/tools/updater/main.cpp @@ -29,7 +29,7 @@ #include #include #include -#include "../../ui/common/winmain.hxx" +#include #include #include #include @@ -170,7 +170,8 @@ int main(int argc, char* argv[]) InitializeDefaultLocale(); logs.applicationName("updater"); - argv = AntaresGetUTF8Arguments(argc, argv); + IntoUTF8ArgsTranslator toUTF8ArgsTranslator(argc, argv); + std::tie(argc, argv) = toUTF8ArgsTranslator.convert(); String::Vector optInput; bool optCleanup = false; diff --git a/src/tools/vacuum/CMakeLists.txt b/src/tools/vacuum/CMakeLists.txt index b9c01826c0..34e4142e49 100644 --- a/src/tools/vacuum/CMakeLists.txt +++ b/src/tools/vacuum/CMakeLists.txt @@ -36,6 +36,7 @@ set(VACUUM_LIBS antares-fswalker yuni-static-core ${CMAKE_THREADS_LIBS_INIT} + Antares::args_helper Antares::study ) diff --git a/src/tools/vacuum/main.cpp b/src/tools/vacuum/main.cpp index c6ffd6bd5f..e88cc3ffe7 100644 --- a/src/tools/vacuum/main.cpp +++ b/src/tools/vacuum/main.cpp @@ -35,7 +35,7 @@ #include #include #include -#include "../../ui/common/winmain.hxx" +#include #include #include #include "modified-inode.h" @@ -247,7 +247,8 @@ int main(int argc, char** argv) InitializeDefaultLocale(); logs.applicationName("vacuum"); - argv = AntaresGetUTF8Arguments(argc, argv); + IntoUTF8ArgsTranslator toUTF8ArgsTranslator(argc, argv); + std::tie(argc, argv) = toUTF8ArgsTranslator.convert(); String::Vector optInput; String::Vector optEachFolderIn; uint optMaxDays = 90; // days diff --git a/src/tools/yby-aggregator/CMakeLists.txt b/src/tools/yby-aggregator/CMakeLists.txt index 3824b7ef1f..0c60a5cc33 100644 --- a/src/tools/yby-aggregator/CMakeLists.txt +++ b/src/tools/yby-aggregator/CMakeLists.txt @@ -48,6 +48,7 @@ INSTALL(EXPORT antares-ybyaggregator set(YBY_AGGREGATOR_LIBS antares-core #version.h + Antares::args_helper Antares::date Antares::logs yuni-static-core diff --git a/src/tools/yby-aggregator/main.cpp b/src/tools/yby-aggregator/main.cpp index 31664f797d..d44095d34e 100644 --- a/src/tools/yby-aggregator/main.cpp +++ b/src/tools/yby-aggregator/main.cpp @@ -28,7 +28,7 @@ #include #include #include -#include "../../ui/common/winmain.hxx" +#include #include #include #include @@ -602,8 +602,8 @@ int main(int argc, char* argv[]) if (not memory.initializeTemporaryFolder()) return EXIT_FAILURE; - argv = AntaresGetUTF8Arguments(argc, argv); - + IntoUTF8ArgsTranslator toUTF8ArgsTranslator(argc, argv); + std::tie(argc, argv) = toUTF8ArgsTranslator.convert(); // Load the local policy settings LocalPolicy::Open(); LocalPolicy::CheckRootPrefix(argv[0]); diff --git a/src/ui/common/winmain.hxx b/src/ui/common/winmain.hxx deleted file mode 100644 index fc6fb3baf7..0000000000 --- a/src/ui/common/winmain.hxx +++ /dev/null @@ -1,71 +0,0 @@ -/* -** Copyright 2007-2023 RTE -** Authors: Antares_Simulator Team -** -** This file is part of Antares_Simulator. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the GNU General Public License as published by -** the Free Software Foundation, either version 3 of the License, or -** (at your option) any later version. -** -** There are special exceptions to the terms and conditions of the -** license as they are applied to this software. View the full text of -** the exceptions in file COPYING.txt in the directory of this software -** distribution -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with Antares_Simulator. If not, see . -** -** SPDX-License-Identifier: licenceRef-GPL3_WITH_RTE-Exceptions -*/ - -#include - -#ifdef YUNI_OS_WINDOWS -#include -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN 1 -#endif -#include -#include -#include -#include -#endif - -#ifdef YUNI_OS_WINDOWS -char** AntaresGetUTF8Arguments(int argc, char**) -{ - wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &argc); - char** argvUTF8 = (char**)malloc(argc * sizeof(char*)); - for (int i = 0; i != argc; ++i) - { - const uint len = (uint)wcslen(wargv[i]); - const uint newLen = WideCharToMultiByte(CP_UTF8, 0, wargv[i], len, NULL, 0, NULL, NULL); - argvUTF8[i] = (char*)malloc((newLen + 1) * sizeof(char)); - memset(argvUTF8[i], 0, (newLen + 1) * sizeof(char)); - WideCharToMultiByte(CP_UTF8, 0, wargv[i], len, argvUTF8[i], newLen, NULL, NULL); - argvUTF8[i][newLen] = '\0'; - } - return argvUTF8; -} - -void FreeUTF8Arguments(int argc, char** argv) -{ - for (int i = 0; i != argc; ++i) - free(argv[i]); - free(argv); -} - -#else - -#define AntaresGetUTF8Arguments(ARGC, ARGV) ARGV - -#define FreeUTF8Arguments(ARGC, ARGV) - -#endif diff --git a/src/ui/simulator/cmake/application.cmake b/src/ui/simulator/cmake/application.cmake index b4d394621f..012acba2fb 100644 --- a/src/ui/simulator/cmake/application.cmake +++ b/src/ui/simulator/cmake/application.cmake @@ -54,8 +54,6 @@ SET(SRC_APPLICATION application/study.h application/study.cpp - # The main - ../common/winmain.hxx main.cpp ) @@ -72,7 +70,8 @@ target_link_libraries(antares-ui-application PRIVATE ${wxWidgets_LIBRARIES} antares-ui-common - antares-core + antares-core + Antares::args_helper Antares::sys Antares::study ) diff --git a/src/ui/simulator/main.cpp b/src/ui/simulator/main.cpp index 3780e5253e..bf36e9564a 100644 --- a/src/ui/simulator/main.cpp +++ b/src/ui/simulator/main.cpp @@ -29,7 +29,7 @@ #include "application/application.h" #include "application/main.h" #include -#include "../common/winmain.hxx" +#include #include #include #include @@ -54,7 +54,7 @@ int main(int argc, char* argv[]) return EXIT_FAILURE; // We have one or several arguments - argv = AntaresGetUTF8Arguments(argc, argv); + IntoUTF8ArgsTranslator toUTF8ArgsTranslator(argc, argv); // locale InitializeDefaultLocale(); @@ -76,7 +76,6 @@ int main(int argc, char* argv[]) // An error has occured if (options(argc, argv) == GetOpt::ReturnCode::error) { - FreeUTF8Arguments(argc, argv); return options.errors() ? 1 : 0; } @@ -84,7 +83,6 @@ int main(int argc, char* argv[]) if (optVersion) { std::cout << ANTARES_VERSION_STR << "\n"; - FreeUTF8Arguments(argc, argv); return 0; } } @@ -105,7 +103,6 @@ int main(int argc, char* argv[]) const int ret = wxEntry(argc, argv); LocalPolicy::Close(); - FreeUTF8Arguments(argc, argv); return ret; }