Skip to content

Commit

Permalink
Remove dependency to UI, use RAII to handle resources (#1678)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
guilpier-code and flomnes authored Oct 9, 2023
1 parent 3b8c7b7 commit c79f00e
Show file tree
Hide file tree
Showing 25 changed files with 122 additions and 105 deletions.
1 change: 1 addition & 0 deletions src/analyzer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/analyzer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <yuni/yuni.h>
#include <antares/logs/logs.h>
#include <yuni/core/getopt.h>
#include "../ui/common/winmain.hxx"
#include <antares/args/args_to_utf8.h>
#include <antares/version.h>
#include "atsp/atsp.h"
#include <antares/logs/hostinfo.h>
Expand Down Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/libs/antares/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

OMESSAGE("Antares Core library")

add_subdirectory(args)
add_subdirectory(writer)
add_subdirectory(memory)

Expand Down
22 changes: 22 additions & 0 deletions src/libs/antares/args/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
44 changes: 44 additions & 0 deletions src/libs/antares/args/args_to_utf8.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

#include <yuni/yuni.h>
#include "antares/args/args_to_utf8.h"

#ifdef YUNI_OS_WINDOWS
#include <string.h>
#include <cstdlib>
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#endif // WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
#endif // YUNI_OS_WINDOWS

IntoUTF8ArgsTranslator::IntoUTF8ArgsTranslator(int argc, char** argv) : argc_(argc), argv_(argv)
{
}

std::pair<int, char**> 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
}
15 changes: 15 additions & 0 deletions src/libs/antares/args/include/antares/args/args_to_utf8.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <utility>

class IntoUTF8ArgsTranslator
{
public:
IntoUTF8ArgsTranslator(int argc, char** argv);
std::pair<int, char**> convert();
~IntoUTF8ArgsTranslator();

private:
int argc_;
char** argv_;
};
1 change: 1 addition & 0 deletions src/solver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions src/solver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include <antares/logs/logs.h>
#include "application.h"
#include "../ui/common/winmain.hxx" //TODO: remove that reverse dependency to UI
#include <antares/args/args_to_utf8.h>

#include <antares/fatal-error.h>
#include <antares/memory/memory.h>
Expand Down Expand Up @@ -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*/);

Expand Down
1 change: 1 addition & 0 deletions src/tools/batchrun/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ set(BATCHRUN_LIBS
antares-core #local.h
yuni-static-core
${CMAKE_THREADS_LIBS_INIT}
Antares::args_helper
Antares::study
)

Expand Down
6 changes: 3 additions & 3 deletions src/tools/batchrun/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include <yuni/core/string.h>
#include <yuni/core/getopt.h>
#include <antares/study/finder.h>
#include "../../ui/common/winmain.hxx"
#include <antares/args/args_to_utf8.h>
#include <antares/version.h>
#include <antares/locale.h>
#ifdef YUNI_OS_WINDOWS
Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions src/tools/cleaner/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/tools/cleaner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <antares/logs/logs.h>
#include <antares/study/finder.h>
#include <yuni/core/getopt.h>
#include "../../ui/common/winmain.hxx"
#include <antares/args/args_to_utf8.h>
#include <antares/utils/utils.h>
#include <antares/study/cleaner.h>
#include <antares/version.h>
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/tools/config/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ INSTALL(EXPORT antares-config

set(CONFIG_LIBS
yuni-static-core
Antares::args_helper
${CMAKE_THREADS_LIBS_INIT}
)

Expand Down
6 changes: 3 additions & 3 deletions src/tools/config/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <antares/resources/resources.h>
#include <iostream>
#include <yuni/core/getopt.h>
#include "../../ui/common/winmain.hxx"
#include <antares/args/args_to_utf8.h>
#include <antares/version.h>
#include <yuni/core/system/username.h>
#include <antares/locale.h>
Expand All @@ -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);

Expand Down
1 change: 1 addition & 0 deletions src/tools/finder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ INSTALL(EXPORT antares-study-finder
set(FINDER_LIBS
antares-core
yuni-static-core
Antares::args_helper
${CMAKE_THREADS_LIBS_INIT}
)

Expand Down
6 changes: 3 additions & 3 deletions src/tools/finder/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <antares/logs/logs.h>
#include <antares/study/finder.h>
#include <yuni/core/getopt.h>
#include "../../ui/common/winmain.hxx"
#include <antares/args/args_to_utf8.h>
#include <antares/utils/utils.h>
#include <antares/version.h>
#include <antares/locale.h>
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/tools/updater/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/tools/updater/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <antares/logs/logs.h>
#include <antares/study/finder.h>
#include <yuni/core/getopt.h>
#include "../../ui/common/winmain.hxx"
#include <antares/args/args_to_utf8.h>
#include <antares/utils/utils.h>
#include <antares/study/cleaner.h>
#include <antares/version.h>
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/tools/vacuum/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ set(VACUUM_LIBS
antares-fswalker
yuni-static-core
${CMAKE_THREADS_LIBS_INIT}
Antares::args_helper
Antares::study
)

Expand Down
5 changes: 3 additions & 2 deletions src/tools/vacuum/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include <antares/antares.h>
#include <fswalker/fswalker.h>
#include <antares/logs/logs.h>
#include "../../ui/common/winmain.hxx"
#include <antares/args/args_to_utf8.h>
#include <antares/version.h>
#include <antares/locale.h>
#include "modified-inode.h"
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/tools/yby-aggregator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/tools/yby-aggregator/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <yuni/yuni.h>
#include <antares/logs/logs.h>
#include <yuni/core/getopt.h>
#include "../../ui/common/winmain.hxx"
#include <antares/args/args_to_utf8.h>
#include <antares/utils/utils.h>
#include <antares/version.h>
#include <antares/sys/policy.h>
Expand Down Expand Up @@ -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]);
Expand Down
Loading

0 comments on commit c79f00e

Please sign in to comment.