From 541d16ba512d0164c9a828a7eef3cd5116992976 Mon Sep 17 00:00:00 2001 From: eharquin Date: Tue, 19 Nov 2024 17:06:57 +0100 Subject: [PATCH 1/2] add option for output folder and data path --- src/main.cpp | 101 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 78 insertions(+), 23 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index dd07a24..09dc78d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,43 +20,98 @@ #include "Utilities.hpp" #include "ProductToString.hpp" +#include // For filesystem operations in C++17 + + +#if defined(_WIN32) || defined(_WIN64) + #include + #include +#else + #include // For dirname() + #include + #include +#endif + +// Function to get the directory of the executable +std::string getExecutableDirectory(const char* argv0) { +#if defined(_WIN32) || defined(_WIN64) + // Windows: Use GetModuleFileNameA to get the executable path + char buffer[MAX_PATH]; + GetModuleFileNameA(NULL, buffer, MAX_PATH); // Get the path of the executable + std::string path(buffer); + size_t found = path.find_last_of("/\\"); // Find the last '/' or '\\' + return path.substr(0, found); // Return the directory part +#else + // Linux/macOS: Use dirname from to get the executable directory + char* path = strdup(argv0); // Make a copy of argv[0] because dirname modifies the string + std::string dir = dirname(path); + free(path); + return dir; +#endif +} -int main(int argc, char** argv){ +int main(int argc, char** argv) { + + // Default output directory + std::string outputDirectory = "output/"; + + const std::string executableDir = getExecutableDirectory(argv[0]); + std::string templateDataDirectory = executableDir + "/../data/"; + + // Check the program arguments + for (int i = 1; i < argc; ++i) { + if (std::strcmp(argv[i], "-o") == 0) { + if (i + 1 < argc) { + outputDirectory = argv[++i]; // Get the next argument as the output directory + } else { + std::cerr << "error: option '-o' requires a directory path" << std::endl; + return EXIT_FAILURE; + } + } + if (std::strcmp(argv[i], "-d") == 0) { + if (i + 1 < argc) { + templateDataDirectory = argv[++i]; // Get the next argument as the data directory + } else { + std::cerr << "error: option '-d' requires a directory path" << std::endl; + return EXIT_FAILURE; + } + } + } - // check the program arguments - if(argc != 2){ - std::cerr << "usage: " << argv[0] << " file.conf" << std::endl; + // Check if the required configuration file argument is provided + if (argc < 2) { + std::cerr << "usage: " << argv[0] << " [-o output_dir] [-d data_dir] file.conf" << std::endl; std::cerr << "where 'file.conf' is the file that defines your algebra." << std::endl; - return EXIT_FAILURE;; + return EXIT_FAILURE; } - // configuration file - const std::string configurationFilesDirectory = argv[1]; - - const std::string templateDataDirectory = "../data/"; - const std::string outputDirectory = "output/"; + // Configuration file + const std::string configurationFilesDirectory = argv[argc - 1]; // The last argument should be the config file + + - // read the configuration data + // Read the configuration data std::cout << "load meta data ..." << std::endl; + // Assuming MetaData class exists as shown in your code snippet MetaData metaData(configurationFilesDirectory); metaData.display(); - // define the arborescence + // Define the project directory structure std::cout << "define the project arborescence ..." << std::endl; - std::string projectDirectory = outputDirectory + "garamon_" + metaData.namespaceName; - std::string srcDirectoryMain = projectDirectory + "/src"; - std::string srcDirectory = projectDirectory + "/src/" + metaData.namespaceName; - std::string docDirectory = projectDirectory + "/doc"; - std::string docImageDirectory = projectDirectory + "/doc/images"; - std::string docHowToDirectory = projectDirectory + "/doc/HOWTO"; - std::string sampleDirectory = projectDirectory + "/sample"; - std::string srcSampleDirectory = projectDirectory + "/sample/src"; + std::string projectDirectory = outputDirectory + "/garamon_" + metaData.namespaceName; + std::string srcDirectoryMain = projectDirectory + "/src"; + std::string srcDirectory = projectDirectory + "/src/" + metaData.namespaceName; + std::string docDirectory = projectDirectory + "/doc"; + std::string docImageDirectory = projectDirectory + "/doc/images"; + std::string docHowToDirectory = projectDirectory + "/doc/HOWTO"; + std::string sampleDirectory = projectDirectory + "/sample"; + std::string srcSampleDirectory = projectDirectory + "/sample/src"; std::string moduleSampleDirectory = projectDirectory + "/sample/modules"; - // check if the directories are not already existing - if(directoryOrFileExists(projectDirectory)){ - std::cout.flush(); // does not fixe the problem of asynchronous output between cout and cerr + // Check if the directories are not already existing + if (directoryOrFileExists(projectDirectory)) { + std::cout.flush(); // Flushing the stream to avoid asynchronous output issues std::cerr << "error: a data or directory '" << projectDirectory << "' already exists" << std::endl; return EXIT_FAILURE; } From 7c6258999e3e1d89b1be65f573e136cb60a90b73 Mon Sep 17 00:00:00 2001 From: eharquin Date: Tue, 19 Nov 2024 17:45:27 +0100 Subject: [PATCH 2/2] fix executable path for output and data folder --- src/Directory.cpp | 23 +++++++++++++++++++++++ src/Directory.hpp | 1 + src/main.cpp | 30 ------------------------------ 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/src/Directory.cpp b/src/Directory.cpp index 7dff4eb..a2691a9 100644 --- a/src/Directory.cpp +++ b/src/Directory.cpp @@ -18,10 +18,15 @@ #include #include // for substitute +#include // For dirname() +#include + #if defined(_WIN32) && (defined(__MINGW32__) || defined(_MSC_BUILD)) #include #include + + #include #endif // __WINDOWS Mingw or MSVC compiler__ @@ -138,3 +143,21 @@ bool copyText(const std::string &src, const std::string &dest) { return srcFile && destFile; } + +// Function to get the directory of the executable +std::string getExecutableDirectory(const char* argv0) { +#if defined(_WIN32) || defined(_WIN64) + // Windows: Use GetModuleFileNameA to get the executable path + char buffer[MAX_PATH]; + GetModuleFileNameA(NULL, buffer, MAX_PATH); // Get the path of the executable + std::string path(buffer); + size_t found = path.find_last_of("/\\"); // Find the last '/' or '\\' + return path.substr(0, found); // Return the directory part +#else + // Linux/macOS: Use dirname from to get the executable directory + char* path = strdup(argv0); // Make a copy of argv[0] because dirname modifies the string + std::string dir = dirname(path); + free(path); + return dir; +#endif +} \ No newline at end of file diff --git a/src/Directory.hpp b/src/Directory.hpp index e6d8361..798744f 100644 --- a/src/Directory.hpp +++ b/src/Directory.hpp @@ -36,5 +36,6 @@ bool copyBin(const std::string &src, const std::string &dest); bool copyText(const std::string &src, const std::string &dest); +std::string getExecutableDirectory(const char* executable); #endif // GARAGEN_DIRECTORY_HPP__ \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 09dc78d..113c9d0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,36 +20,6 @@ #include "Utilities.hpp" #include "ProductToString.hpp" -#include // For filesystem operations in C++17 - - -#if defined(_WIN32) || defined(_WIN64) - #include - #include -#else - #include // For dirname() - #include - #include -#endif - -// Function to get the directory of the executable -std::string getExecutableDirectory(const char* argv0) { -#if defined(_WIN32) || defined(_WIN64) - // Windows: Use GetModuleFileNameA to get the executable path - char buffer[MAX_PATH]; - GetModuleFileNameA(NULL, buffer, MAX_PATH); // Get the path of the executable - std::string path(buffer); - size_t found = path.find_last_of("/\\"); // Find the last '/' or '\\' - return path.substr(0, found); // Return the directory part -#else - // Linux/macOS: Use dirname from to get the executable directory - char* path = strdup(argv0); // Make a copy of argv[0] because dirname modifies the string - std::string dir = dirname(path); - free(path); - return dir; -#endif -} - int main(int argc, char** argv) {