From 4b23137fdb3401ed3e9f947c26de84ffbada9ea0 Mon Sep 17 00:00:00 2001 From: Peguen <73380451+Peguen@users.noreply.github.com> Date: Thu, 19 Dec 2024 13:38:50 +0100 Subject: [PATCH] WIP config path adaptions. --- doc/rst/configuration/options.rst | 6 +- ecal/core/CMakeLists.txt | 1 + ecal/core/include/ecal/config/logging.h | 11 +- ecal/core/include/ecal/ecal_util.h | 3 +- .../builder/logging_attribute_builder.cpp | 2 +- .../src/config/ecal_config_initializer.cpp | 2 +- ecal/core/src/config/ecal_path_processing.cpp | 415 +++++++++++------- ecal/core/src/config/ecal_path_processing.h | 58 ++- ecal/core/src/ecal_def.h | 32 +- ecal/tests/CMakeLists.txt | 1 + ecal/tests/cpp/config_test/CMakeLists.txt | 10 +- ecal/tests/cpp/ecal_utils_test/CMakeLists.txt | 45 ++ .../cpp/ecal_utils_test/src/string_test.cpp | 47 ++ 13 files changed, 439 insertions(+), 194 deletions(-) create mode 100644 ecal/tests/cpp/ecal_utils_test/CMakeLists.txt create mode 100644 ecal/tests/cpp/ecal_utils_test/src/string_test.cpp diff --git a/doc/rst/configuration/options.rst b/doc/rst/configuration/options.rst index 98370e3b14..ce5a9e170f 100644 --- a/doc/rst/configuration/options.rst +++ b/doc/rst/configuration/options.rst @@ -15,7 +15,7 @@ Loading strategy (Priority) =========================== The eCAL configuration file is loaded based on the following priorities, whereever it is found first. -If you want a specific eCAL Node to run with another ``ecal.yaml`` than the others, you can set the ``ECAL_DATA`` variable before starting the process, e.g. from a batch or shell skript. +If you want a specific eCAL Node to run with another ``ecal.yaml`` than the others, you can set the ``ECAL_CONFIG`` variable before starting the process, e.g. from a batch or shell skript. In addition, some eCAL applications support providing a path from the command line option ``--ecal-config-file``. .. important:: @@ -23,12 +23,12 @@ In addition, some eCAL applications support providing a path from the command li - |fa-windows| Windows: - 1. ``%ECAL_DATA%/ecal.yaml`` + 1. ``%ECAL_CONFIG%/ecal.yaml`` 2. ``%ProgramData%/ecal/ecal.yaml`` - |fa-ubuntu| Ubuntu: - 1. ``$ECAL_DATA/ecal.yaml`` + 1. ``$ECAL_CONFIG/ecal.yaml`` 2. ``/etc/ecal/ecal.yaml`` (from ``CMAKE_INSTALL_SYSCONFDIR``) 3. ``/etc/ecal/ecal.yaml`` (fallback) diff --git a/ecal/core/CMakeLists.txt b/ecal/core/CMakeLists.txt index c849a4f225..b6f8ca1084 100644 --- a/ecal/core/CMakeLists.txt +++ b/ecal/core/CMakeLists.txt @@ -68,6 +68,7 @@ set(ecal_config_src src/config/default_configuration.cpp src/config/ecal_config.cpp src/config/ecal_config_initializer.cpp + src/config/ecal_path_processing.cpp src/config/transport_layer.cpp src/types/ecal_custom_data_types.cpp ) diff --git a/ecal/core/include/ecal/config/logging.h b/ecal/core/include/ecal/config/logging.h index e50ddb95fd..e42a1edeb9 100644 --- a/ecal/core/include/ecal/config/logging.h +++ b/ecal/core/include/ecal/config/logging.h @@ -29,11 +29,6 @@ #include #include -namespace -{ - // After switchting to c++17, this can be replaced by an inline constexpr - static const eCAL_Logging_Filter log_filter_default = log_level_info | log_level_warning | log_level_error | log_level_fatal; -} namespace eCAL { @@ -65,9 +60,9 @@ namespace eCAL struct Configuration { - Sink console { true, log_level_error | log_level_fatal}; //!< default: true, log_level_error - Sink file { false, log_level_none }; //!< default: false, log_level_none - Sink udp { true, log_filter_default }; //!< default: true, log_filter_default + Sink console { true, log_level_warning | log_level_error | log_level_fatal }; //!< default: true, log_level_warning | log_level_error | log_level_fatal + Sink file { false, log_level_none }; //!< default: false, log_level_none + Sink udp { true, log_level_info | log_level_warning | log_level_error | log_level_fatal }; //!< default: true, log_level_info | log_level_warning | log_level_error | log_level_fatal File::Configuration file_config; UDP::Configuration udp_config; diff --git a/ecal/core/include/ecal/ecal_util.h b/ecal/core/include/ecal/ecal_util.h index 69925f7679..f546c82b0a 100644 --- a/ecal/core/include/ecal/ecal_util.h +++ b/ecal/core/include/ecal/ecal_util.h @@ -36,9 +36,8 @@ namespace eCAL { /** * @brief Retrieve eCAL configuration path. - * This path is for the global eCAL configuration files + * This path is for the local eCAL configuration files * like ecal.yaml. - * This path is read only for standard users. * * @return eCAL configuration path. **/ diff --git a/ecal/core/src/config/builder/logging_attribute_builder.cpp b/ecal/core/src/config/builder/logging_attribute_builder.cpp index b6459defd9..327a9cc628 100644 --- a/ecal/core/src/config/builder/logging_attribute_builder.cpp +++ b/ecal/core/src/config/builder/logging_attribute_builder.cpp @@ -24,7 +24,7 @@ namespace eCAL attributes.file_config.path = log_config_.provider.file_config.path; if (attributes.file_config.path.empty()) { - // check ECAL_DATA + // check ECAL_CONFIG // Creates path if not exists attributes.file_config.path = Util::GeteCALLogPath(); } diff --git a/ecal/core/src/config/ecal_config_initializer.cpp b/ecal/core/src/config/ecal_config_initializer.cpp index 2c9fb9a942..bb1581c5ec 100644 --- a/ecal/core/src/config/ecal_config_initializer.cpp +++ b/ecal/core/src/config/ecal_config_initializer.cpp @@ -36,7 +36,7 @@ namespace eCAL { void Configuration::InitFromFile(const std::string& yaml_path_) { - const std::string yaml_path = eCAL::Path::checkForValidConfigFilePath(yaml_path_); + const std::string yaml_path = eCAL::Config::checkForValidConfigFilePath(yaml_path_); if (!yaml_path.empty()) { #ifdef ECAL_CORE_CONFIGURATION diff --git a/ecal/core/src/config/ecal_path_processing.cpp b/ecal/core/src/config/ecal_path_processing.cpp index d4297c7464..687c769883 100644 --- a/ecal/core/src/config/ecal_path_processing.cpp +++ b/ecal/core/src/config/ecal_path_processing.cpp @@ -18,7 +18,7 @@ */ /** - * @brief Function definitions for path processing + * @brief eCAL config path processing function collection **/ #include "ecal_path_processing.h" @@ -34,106 +34,20 @@ // for cwd #ifdef ECAL_OS_WINDOWS - #include - // to remove deprecated warning - #define getcwd _getcwd - constexpr int MAXIMUM_PATH_LENGTH = _MAX_PATH; + #include + #include #endif #ifdef ECAL_OS_LINUX #include #include #include - #include - constexpr int MAXIMUM_PATH_LENGTH = PATH_MAX; + #include <.h> #endif namespace { -#ifdef ECAL_OS_WINDOWS - const char path_separator('\\'); -#endif /* ECAL_OS_WINDOWS */ -#ifdef ECAL_OS_LINUX - const char path_separator('/'); -#endif /* ECAL_OS_LINUX */ - - bool setPathSep(std::string& file_path_) - { - if (file_path_.empty()) - return false; - - if (file_path_.back() != path_separator) - { - file_path_ += path_separator; - } - - return true; - } - - std::string eCALUserSpacePath() - { - std::string path; - } - - std::string eCALDataEnvPath() - { - std::string ecal_data_path = getEnvVar(ECAL_DATA_VAR); - setPathSep(ecal_data_path); - return ecal_data_path; - } - - std::string cwdPath() - { - char temp[MAXIMUM_PATH_LENGTH]; - - if (getcwd(temp, sizeof(temp)) != nullptr) - { - std::string cwdPath{temp}; - setPathSep(cwdPath); - return cwdPath; - } - - return {}; - } - - std::string eCALDataCMakePath() - { - std::string cmake_data_path; -#ifdef ECAL_OS_LINUX - const std::string ecal_install_config_dir(ECAL_INSTALL_CONFIG_DIR); - const std::string ecal_install_prefix(ECAL_INSTALL_PREFIX); - - if ((!ecal_install_config_dir.empty() && (ecal_install_config_dir[0] == path_separator)) - || ecal_install_prefix.empty()) - { - cmake_data_path = ecal_install_config_dir; - } - else if (!ecal_install_prefix.empty()) - { - cmake_data_path = ecal_install_prefix + path_separator + ecal_install_config_dir; - } - setPathSep(cmake_data_path); -#endif /* ECAL_OS_LINUX */ - return cmake_data_path; - } - - std::string eCALDataSystemPath() - { - std::string system_data_path; -#ifdef ECAL_OS_WINDOWS - system_data_path = getEnvVar("ProgramData"); - if(setPathSep(system_data_path)) - { - system_data_path += std::string("eCAL"); - setPathSep(system_data_path); - } -#endif /* ECAL_OS_WINDOWS */ - -#ifdef ECAL_OS_LINUX - system_data_path = "/etc/ecal"; - setPathSep(system_data_path); -#endif /* ECAL_OS_LINUX */ - return system_data_path; - } + // get the path separator from the current OS (win: "\\", unix: "/") + static const std::string path_separator(1, EcalUtils::Filesystem::NativeSeparator()); bool direxists(const std::string& path_) { @@ -141,14 +55,14 @@ namespace return (status.IsOk() && (status.GetType() == EcalUtils::Filesystem::Type::Dir)); } - void createdir(const std::string& path_) + bool createdir(const std::string& path_) { - EcalUtils::Filesystem::MkDir(path_, EcalUtils::Filesystem::Current); + return EcalUtils::Filesystem::MkDir(path_, EcalUtils::Filesystem::Current); } bool isValidConfigFilePath(const std::string& path_, const std::string& file_name_) { - const std::string file_path = path_ + file_name_; + const std::string file_path = EcalUtils::String::Join(path_separator, std::vector{path_, file_name_}); const EcalUtils::Filesystem::FileStatus ecal_ini_status(file_path, EcalUtils::Filesystem::Current); return ecal_ini_status.IsOk() && (ecal_ini_status.GetType() == EcalUtils::Filesystem::Type::RegularFile); } @@ -169,48 +83,218 @@ namespace return (*it); // If valid path is not encountered, defaults should be used - return std::string(""); + return {}; } // Returns the default paths for possible ecal configurations // Order: - // 1. ECAL_DATA environment varible path if set - // 2. cmake configured data paths (linux only) - // 3. system data path + // 1. ECAL_CONFIG environment varible path if set + // 2. local user path + // 3. system path like etc, ProgramData std::vector getEcalDefaultPaths() { std::vector ecal_default_paths; - // ----------------------------------------------------------- - // precedence 1: ECAL_DATA variable (windows and linux) - // ----------------------------------------------------------- - ecal_default_paths.emplace_back(eCALDataEnvPath()); + // ----------------------------------------------------------- + // precedence 1: ECAL_CONFIG variable (windows and linux) + // ----------------------------------------------------------- + ecal_default_paths.emplace_back(eCAL::Config::eCALConfigEnvPath()); + + // ----------------------------------------------------------- + // precedence 2: local user path + // ----------------------------------------------------------- + ecal_default_paths.emplace_back(eCAL::Config::eCALLocalUserPath()); + + // ----------------------------------------------------------- + // precedence 3: eCAL data system path + // ----------------------------------------------------------- + ecal_default_paths.emplace_back(eCAL::Config::eCALDataSystemPath()); + return ecal_default_paths; + } + +#ifdef ECAL_OS_WINDOWS + std::string getKnownFolderPath(REFKNOWNFOLDERID id_) + { + std::string return_path; + PWSTR path_tmp = nullptr; + + // Retrieve the known folder path: users local app data + auto ret = SHGetKnownFolderPath(id_, 0, nullptr, &path_tmp); + + if (ret != S_OK) + { + if (path_tmp != nullptr) + // Free the memory allocated by SHGetKnownFolderPath + CoTaskMemFree(path_tmp); + + return {}; + } + + // Convert the wide-character string to a multi-byte string + // For supporting full Unicode compatibility + int size_needed = WideCharToMultiByte(CP_UTF8, 0, path_tmp, -1, nullptr, 0, nullptr, nullptr); + if (size_needed > 0) + { + // Exclude the null terminator from the size + return_path.resize(size_needed - 1); + WideCharToMultiByte(CP_UTF8, 0, path_tmp, -1, &return_path[0], size_needed, nullptr, nullptr); + } + + // Free the memory allocated by SHGetKnownFolderPath + CoTaskMemFree(path_tmp); + + return return_path; + } +#endif /* ECAL_OS_WINDOWS */ + + // returns local users home path + // e.g. C:\Users\username\AppData\Local in windows + // or /home/username in linux + // returns empty string if not found + std::string getLocalUserPath() + { + std::string userspace_path; + #ifdef ECAL_OS_WINDOWS + + userspace_path = getKnownFolderPath(FOLDERID_LocalAppData); + + #elif defined(ECAL_OS_LINUX) + + std::string hdir = getEnvVar(ECAL_LINUX_HOME_VAR); + + #endif /* ECAL_OS_LINUX */ + return userspace_path; + } + + std::string getSystemPath() + { + std::string system_path; + #ifdef ECAL_OS_WINDOWS + + system_path = getKnownFolderPath(FOLDERID_ProgramData); + + #elif defined(ECAL_OS_LINUX) + + system_path = ECAL_LINUX_DATA_SYSTEM_PATH; + + #endif /* ECAL_OS_LINUX */ + return system_path; + } + + std::string getTempPath() + { + std::string temp_path; + + #ifdef ECAL_OS_WINDOWS - // ----------------------------------------------------------- - // precedence 2: cmake configured data paths (linux only) - // ----------------------------------------------------------- - ecal_default_paths.emplace_back(eCALDataCMakePath()); + char temp_path_buffer[MAX_PATH]; + DWORD path_length = GetTempPathA(MAX_PATH, temp_path_buffer); + if (path_length > 0 && path_length < MAX_PATH) + { + temp_path = std::string(temp_path_buffer, path_length); + } + + #elif defined(ECAL_OS_LINUX) - // ----------------------------------------------------------- - // precedence 3: system data path - // ----------------------------------------------------------- - ecal_default_paths.emplace_back(eCALDataSystemPath()); - return ecal_default_paths; + temp_path = getEnvVar(ECAL_LINUX_TMP_VAR); + + #endif /* ECAL_OS_LINUX */ + + return temp_path; } } namespace eCAL { - namespace Path + namespace Config { + std::string eCALLocalUserPath() + { + std::string userspace_path = getLocalUserPath(); + + if (!userspace_path.empty()) + { + #ifdef ECAL_OS_WINDOWS + + return EcalUtils::String::Join(path_separator, std::vector{userspace_path, ECAL_FOLDER_NAME_WINDOWS}); + + #elif defined(ECAL_OS_LINUX) + + return EcalUtils::String::Join(path_separator, std::vector{userspace_path, ECAL_FOLDER_NAME_HOME_LINUX}); + + #endif + } + + return {}; + } + + std::string eCALConfigEnvPath() + { + return getEnvVar(ECAL_CONFIG_VAR); + } + + std::string eCALConfigLogPath() + { + // check first if the ECAL_LOG environment variable is set + std::string env_path = getEnvVar(ECAL_LOG_VAR); + if (!env_path.empty()) + { + return env_path; + } + + // if no environment variable is set, check the local user path + std::string local_user_path = eCALLocalUserPath(); + if (!local_user_path.empty()) + { + return EcalUtils::String::Join(path_separator, std::vector{local_user_path, ECAL_FOLDER_NAME_LOG}); + } + + #ifdef ECAL_OS_WINDOWS + // only works on windows, as ProgramData is writable for the normal user + // check the system_data_path if available + std::string system_data_path = getSystemPath(); + if (!system_data_path.empty()) + { + return EcalUtils::String::Join(path_separator, std::vector{system_data_path, ECAL_FOLDER_NAME_WINDOWS, ECAL_FOLDER_NAME_LOG}); + } + #endif + + return {}; + } + + std::string eCALDataSystemPath() + { + std::string system_path = getSystemPath(); + std::string return_path; + + if (!system_path.empty()) + { + #ifdef ECAL_OS_WINDOWS + + // system path = "ProgramData" if available + return_path = EcalUtils::String::Join(path_separator, std::vector{system_path, ECAL_FOLDER_NAME_WINDOWS}); + + #elif defined(ECAL_OS_LINUX) + + // system path = "/etc" if available + return_path = EcalUtils::String::Join(path_separator, std::vector{system_path, ECAL_FOLDER_NAME_HOME_LINUX}); + + #endif + } + + return return_path;; + } + std::string checkForValidConfigFilePath(const std::string& config_file_) { // ----------------------------------------------------------- // precedence 0: relative path to executable // ----------------------------------------------------------- - const std::string cwd_directory_path = cwdPath(); + const std::string cwd_directory_path = EcalUtils::Filesystem::CurrentWorkingDir(); std::vector ecal_default_paths = getEcalDefaultPaths(); - ecal_default_paths.emplace(ecal_default_paths.begin(), cwd_directory_path); + + // insert cwd on 2nd position, so that ECAL_CONFIG dir has precedence + ecal_default_paths.insert(ecal_default_paths.begin() + 1, cwd_directory_path); const std::string found_path = findValidConfigPath(ecal_default_paths, config_file_); @@ -222,7 +306,7 @@ namespace eCAL return found_path + config_file_; } - } // namespace Path + } // namespace Config namespace Util { @@ -235,86 +319,113 @@ namespace eCAL return findValidConfigPath(search_directories, ECAL_DEFAULT_CFG); } + // Returns the path to the eCAL installation directory std::string GeteCALHomePath() { std::string home_path; - -#ifdef ECAL_OS_WINDOWS + #ifdef ECAL_OS_WINDOWS + // check ECAL_HOME home_path = getEnvVar(ECAL_HOME_VAR); - if (!home_path.empty()) - { - if (*home_path.rbegin() != path_separator) home_path += path_separator; - } - if (!std::string(ECAL_HOME_PATH_WINDOWS).empty()) //-V815 + + // TODO PG: Check, why there is a ECAL_FOLDER_NAME_WINDOWS - What is the expected behaviour? + if (!std::string(ECAL_FOLDER_NAME_WINDOWS).empty()) //-V815 { - home_path += path_separator; - home_path += ECAL_HOME_PATH_WINDOWS; + home_path = EcalUtils::String::Join(path_separator, std::vector{home_path, ECAL_FOLDER_NAME_WINDOWS}); } -#endif /* ECAL_OS_WINDOWS */ -#ifdef ECAL_OS_LINUX + #elif defined(ECAL_OS_LINUX) + const char *hdir = nullptr; - hdir = getenv("HOME"); + home_path = getEnvVar(ECAL_LINUX_HOME_VAR); if (hdir == nullptr) { hdir = getpwuid(getuid())->pw_dir; } home_path += hdir; - if (!std::string(ECAL_HOME_PATH_LINUX).empty()) + if (!std::string(ECAL_FOLDER_NAME_HOME_LINUX).empty()) { - home_path += "/"; - home_path += ECAL_HOME_PATH_LINUX; + home_path = EcalUtils::String::Join(path_separator, std::vector{home_path, ECAL_FOLDER_NAME_WINDOWS}); } -#endif /* ECAL_OS_LINUX */ + + #endif /* ECAL_OS_LINUX */ // create if not exists if (!direxists(home_path)) { - createdir(home_path); + return {}; } - home_path += path_separator; return(home_path); } std::string GeteCALUserSettingsPath() { - std::string settings_path; -#ifdef ECAL_OS_WINDOWS - settings_path = GeteCALConfigPath(); -#endif /* ECAL_OS_WINDOWS */ + std::string config_path; + #ifdef ECAL_OS_WINDOWS -#ifdef ECAL_OS_LINUX - settings_path = GeteCALHomePath(); -#endif /* ECAL_OS_LINUX */ - settings_path += std::string(ECAL_SETTINGS_PATH); + config_path = GeteCALConfigPath(); + + #elif defined(ECAL_OS_LINUX) + + config_path = GeteCALHomePath(); + + #endif /* ECAL_OS_LINUX */ + + if (config_path.empty()) + { + return {}; + } + + std::string settings_path = EcalUtils::String::Join(path_separator, std::vector{config_path, ECAL_FOLDER_NAME_CFG}); if (!direxists(settings_path)) { - createdir(settings_path); + if (!createdir(settings_path)) + { + return {}; + } } - settings_path += path_separator; return(settings_path); } std::string GeteCALLogPath() { - if (!eCAL::GetConfiguration().logging.provider.file_config.path.empty()) + const auto& config_log_file_path = eCAL::GetConfiguration().logging.provider.file_config.path; + if (!config_log_file_path.empty() && direxists(config_log_file_path)) + { + return config_log_file_path; + } + else if (!config_log_file_path.empty()) + { + if (createdir(config_log_file_path)) + return config_log_file_path; + } + + { + if (direxists(config_log_file_path)) + { + return config_log_file_path; + } + } + { + if (direxists) return eCAL::GetConfiguration().logging.provider.file_config.path; } std::string log_path; -#ifdef ECAL_OS_WINDOWS + #ifdef ECAL_OS_WINDOWS + log_path = GeteCALConfigPath(); -#endif /* ECAL_OS_WINDOWS */ -#ifdef ECAL_OS_LINUX - log_path = GeteCALHomePath(); -#endif /* ECAL_OS_LINUX */ + #elif defined(ECAL_OS_LINUX) + + log_path = GeteCALConfigPath(); + + #endif /* ECAL_OS_LINUX */ - log_path += std::string(ECAL_LOG_PATH); + log_path += std::string(ECAL_FOLDER_NAME_LOG); if (!direxists(log_path)) { @@ -330,4 +441,4 @@ namespace eCAL return eCAL::GetConfiguration().GetYamlFilePath(); } } // namespace Util -} // namespace eCAL \ No newline at end of file +} // namespace eCAL diff --git a/ecal/core/src/config/ecal_path_processing.h b/ecal/core/src/config/ecal_path_processing.h index 7e754d9b90..2f7b8a4ec6 100644 --- a/ecal/core/src/config/ecal_path_processing.h +++ b/ecal/core/src/config/ecal_path_processing.h @@ -18,27 +18,71 @@ */ /** - * @brief Function definitions for path processing + * @brief eCAL config path processing function collection **/ #include namespace eCAL { - namespace Path + namespace Config { - /** + /** * @brief Check the following paths if the specified config file exists and return the first valid complete path * * The function checks the following paths in order: - * 1. Current working directory - * 2. ECAL_DATA environment variable path - * 3. CMake configured data paths (Linux only) - * 4. System data path + * 1. ECAL_CONFIG_DIR environment variable path + * 2. Current working directory + * 3. Local user path + * 4. Global system path * * @param config_file_ The name of the configuration file to check. * @return std::string The first valid complete path to the configuration file. + * Returns empty string if non could be found. */ std::string checkForValidConfigFilePath(const std::string& config_file_); + + + /** + * @brief Returns the path to the local user settings directory if it exists. + * + * @return std::string The path to the local user settings directory. + * Returns empty string if the path does not exist. + */ + std::string eCALLocalUserPath(); + + /** + * @brief Returns the default path to the eCAL data system directory if it exists. + * + * @returns std::string The path to the eCAL data system directory. + * Returns empty string if the path does not exist. + */ + std::string eCALDataSystemPath(); + + /** + * @brief Returns the path to the eCAL directory specified by the environment variable ECAL_CONFIG. + * + * @returns std::string The path to the eCAL config directory. + * Returns empty string if the path does not exist. + */ + std::string eCALConfigEnvPath(); + + /** + * @brief Returns the path to the eCAL directory specified by the environment variable ECAL_LOG. + * + * In case the path does not exist yet, the function tries to create it. + * (e.g. ProgramData/eCAL/logs [win], ~/.ecal/logs [unix]) + * If the creation fails, the function returns an empty string. + * + * @returns std::string The path to the eCAL log directory. + */ + std::string eCALConfigLogPath(); + + /** + * @brief Returns the path to the eCAL temporary directory, which is /ecal_tmp. + * + * @returns std::string The path to the eCAL temporary directory. + */ + std::string eCALTmpPath(); } } \ No newline at end of file diff --git a/ecal/core/src/ecal_def.h b/ecal/core/src/ecal_def.h index a4994eba5e..273b2bc732 100644 --- a/ecal/core/src/ecal_def.h +++ b/ecal/core/src/ecal_def.h @@ -32,33 +32,41 @@ /* config settings */ /**********************************************************************************************/ /* base data path name */ -constexpr const char* ECAL_HOME_PATH_WINDOWS = ""; -constexpr const char* ECAL_HOME_PATH_LINUX = ".ecal"; -constexpr const char* ECAL_LOG_PATH = "logs"; -constexpr const char* ECAL_SETTINGS_PATH = "cfg"; +constexpr const char* ECAL_FOLDER_NAME_WINDOWS = "eCAL"; +constexpr const char* ECAL_FOLDER_NAME_LINUX = "ecal"; +constexpr const char* ECAL_FOLDER_NAME_HOME_LINUX = ".ecal"; +constexpr const char* ECAL_FOLDER_NAME_LOG = "logs"; +constexpr const char* ECAL_FOLDER_NAME_CFG = "cfg"; /* ini file name */ -constexpr const char* ECAL_DEFAULT_CFG = "ecal.yaml"; +constexpr const char* ECAL_DEFAULT_CFG = "ecal.yaml"; /* environment variables */ -constexpr const char* ECAL_HOME_VAR = "ECAL_HOME"; -constexpr const char* ECAL_DATA_VAR = "ECAL_DATA"; +constexpr const char* ECAL_HOME_VAR = "ECAL_HOME"; +constexpr const char* ECAL_CONFIG_VAR = "ECAL_CONFIG"; +constexpr const char* ECAL_LOG_VAR = "ECAL_LOG"; +constexpr const char* ECAL_LINUX_HOME_VAR = "HOME"; +constexpr const char* ECAL_LINUX_TMP_VAR = "TMPDIR"; + +/* system paths */ +constexpr const char* ECAL_LINUX_SYSTEM_PATH = "/etc"; +constexpr const char* ECAL_LINUX_INSTALL_PATH = "/usr/bin"; /* eCAL udp multicast defines */ -constexpr unsigned int NET_UDP_MULTICAST_PORT_REG_OFF = 0U; // to delete -constexpr unsigned int NET_UDP_MULTICAST_PORT_LOG_OFF = 1U; // to delete -constexpr unsigned int NET_UDP_MULTICAST_PORT_SAMPLE_OFF = 2U; // to delete +constexpr unsigned int NET_UDP_MULTICAST_PORT_REG_OFF = 0U; // to delete +constexpr unsigned int NET_UDP_MULTICAST_PORT_LOG_OFF = 1U; // to delete +constexpr unsigned int NET_UDP_MULTICAST_PORT_SAMPLE_OFF = 2U; // to delete /* timeout for create / open a memory file using mutex lock in ms */ constexpr unsigned int PUB_MEMFILE_CREATE_TO = 200U; constexpr unsigned int PUB_MEMFILE_OPEN_TO = 200U; /* memory file access timeout */ -constexpr unsigned int EXP_MEMFILE_ACCESS_TIMEOUT = 100U; +constexpr unsigned int EXP_MEMFILE_ACCESS_TIMEOUT = 100U; /**********************************************************************************************/ /* events */ /**********************************************************************************************/ /* common stop event prefix to shut down a local user process */ -constexpr const char* EVENT_SHUTDOWN_PROC = "ecal_shutdown_process"; +constexpr const char* EVENT_SHUTDOWN_PROC = "ecal_shutdown_process"; diff --git a/ecal/tests/CMakeLists.txt b/ecal/tests/CMakeLists.txt index 6f35e36095..542d4db082 100644 --- a/ecal/tests/CMakeLists.txt +++ b/ecal/tests/CMakeLists.txt @@ -38,6 +38,7 @@ if(ECAL_CORE_REGISTRATION) add_subdirectory(cpp/registration_test_public) endif() +add_subdirectory(cpp/ecal_utils_test) add_subdirectory(cpp/event_test) add_subdirectory(cpp/expmap_test) add_subdirectory(cpp/logging_test) diff --git a/ecal/tests/cpp/config_test/CMakeLists.txt b/ecal/tests/cpp/config_test/CMakeLists.txt index 43b7c155af..91788e0db1 100644 --- a/ecal/tests/cpp/config_test/CMakeLists.txt +++ b/ecal/tests/cpp/config_test/CMakeLists.txt @@ -28,15 +28,8 @@ if(ECAL_CORE_CONFIGURATION) yaml_cpp_create_compatibility_targets() endif() -set(cmd_parser_src - ${ECAL_CORE_PROJECT_ROOT}/core/src/util/advanced_tclap_output.cpp - ${ECAL_PROJECT_ROOT}/lib/ecal_utils/src/filesystem.cpp - ${ECAL_PROJECT_ROOT}/lib/ecal_utils/src/str_convert.cpp -) - set(config_test_src src/config_test.cpp - ${cmd_parser_src} ${ECAL_CORE_PROJECT_ROOT}/core/src/config/default_configuration.cpp ) @@ -57,7 +50,8 @@ target_include_directories(${PROJECT_NAME} PRIVATE target_link_libraries(${PROJECT_NAME} PRIVATE - eCAL::core + eCAL::core + eCAL::ecal-utils Threads::Threads tclap::tclap ) diff --git a/ecal/tests/cpp/ecal_utils_test/CMakeLists.txt b/ecal/tests/cpp/ecal_utils_test/CMakeLists.txt new file mode 100644 index 0000000000..1439cd0be6 --- /dev/null +++ b/ecal/tests/cpp/ecal_utils_test/CMakeLists.txt @@ -0,0 +1,45 @@ +# ========================= eCAL LICENSE ================================= +# +# Copyright (C) 2016 - 2024 Continental Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ========================= eCAL LICENSE ================================= + +project(test_ecal_utils) + +find_package(Threads REQUIRED) +find_package(GTest REQUIRED) + +set(ecal_utils_test_src + src/string_test.cpp +) + +ecal_add_gtest(${PROJECT_NAME} ${ecal_utils_test_src}) + +target_include_directories(${PROJECT_NAME} PRIVATE $) + +target_link_libraries(${PROJECT_NAME} + PRIVATE + eCAL::ecal-utils + Threads::Threads) + +target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14) + +ecal_install_gtest(${PROJECT_NAME}) + +set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER tests/cpp/util) + +source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES + ${${PROJECT_NAME}_src} +) diff --git a/ecal/tests/cpp/ecal_utils_test/src/string_test.cpp b/ecal/tests/cpp/ecal_utils_test/src/string_test.cpp new file mode 100644 index 0000000000..60fe60a333 --- /dev/null +++ b/ecal/tests/cpp/ecal_utils_test/src/string_test.cpp @@ -0,0 +1,47 @@ +#include + +#include "ecal_utils/filesystem.h" +#include "ecal_utils/string.h" + +#include +#include + +TEST(ecal_utils_cpp /*unused*/, join_paths /*unused*/) +{ + std::vector string_vec; + // Test the JoinPaths function + + string_vec.push_back(""); + if (EcalUtils::Filesystem::OsStyle::Current == EcalUtils::Filesystem::OsStyle::Windows) + { + string_vec[0] = "C:"; + } + string_vec.push_back("Users"); + string_vec.push_back("User"); + string_vec.push_back("Documents"); + string_vec.push_back("ecal"); + string_vec.push_back("test"); + string_vec.push_back("src"); + string_vec.push_back("string_test.cpp"); + + // path with trailing file + std::string expected_path; + char path_separator = EcalUtils::Filesystem::NativeSeparator(EcalUtils::Filesystem::OsStyle::Current); + + expected_path = string_vec[0] + path_separator + string_vec[1] + path_separator + string_vec[2] + path_separator + + string_vec[3] + path_separator + string_vec[4] + path_separator + string_vec[5] + path_separator + + string_vec[6] + path_separator + string_vec[7]; + + std::string joined_path = EcalUtils::String::Join(std::string(1, path_separator), string_vec); + EXPECT_EQ(joined_path, expected_path); + + // path without trailing file + string_vec[7] = "string"; + expected_path = string_vec[0] + path_separator + string_vec[1] + path_separator + string_vec[2] + path_separator + + string_vec[3] + path_separator + string_vec[4] + path_separator + string_vec[5] + path_separator + + string_vec[6] + path_separator + string_vec[7]; + + joined_path = EcalUtils::String::Join(std::string(1, path_separator), string_vec); + EXPECT_EQ(joined_path, expected_path); + +} \ No newline at end of file