From 0df066c3e92e7f2470542cd5f23c474c0c537723 Mon Sep 17 00:00:00 2001 From: Peguen <73380451+Peguen@users.noreply.github.com> Date: Thu, 12 Dec 2024 10:44:03 +0100 Subject: [PATCH] Split ecal_config_initializer path and util functions to separate file and header. --- .../src/config/ecal_config_initializer.cpp | 323 +---------------- ecal/core/src/config/ecal_path_processing.cpp | 333 ++++++++++++++++++ ecal/core/src/config/ecal_path_processing.h | 44 +++ ecal/core/src/ecal_def.h | 4 + 4 files changed, 384 insertions(+), 320 deletions(-) create mode 100644 ecal/core/src/config/ecal_path_processing.cpp create mode 100644 ecal/core/src/config/ecal_path_processing.h diff --git a/ecal/core/src/config/ecal_config_initializer.cpp b/ecal/core/src/config/ecal_config_initializer.cpp index a45d07601a..2c9fb9a942 100644 --- a/ecal/core/src/config/ecal_config_initializer.cpp +++ b/ecal/core/src/config/ecal_config_initializer.cpp @@ -22,211 +22,21 @@ **/ #include "ecal/ecal_config.h" -#include "ecal/ecal_util.h" +#include "ecal_path_processing.h" #include "ecal_global_accessors.h" -#include "ecal_def.h" -#include "ecal/ecal_process.h" #ifdef ECAL_CORE_CONFIGURATION #include "configuration_reader.h" #include "configuration_to_yaml.h" #endif -#include - -// for cwd -#ifdef ECAL_OS_WINDOWS - #include - // to remove deprecated warning - #define getcwd _getcwd - constexpr int MAXIMUM_PATH_LENGTH = _MAX_PATH; -#endif -#ifdef ECAL_OS_LINUX - #include - #include - #include - #include - constexpr int MAXIMUM_PATH_LENGTH = PATH_MAX; -#endif - -#include "ecal_utils/filesystem.h" -#include "util/getenvvar.h" -#include "ecal_utils/ecal_utils.h" -#include "ecal/ecal_log.h" - -#include -#include -#include - -namespace -{ - // copied and adapted from ecal_config_reader.cpp -#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 eCALDataEnvPath() - { - std::string ecal_data_path = getEnvVar("ECAL_DATA"); - 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; - } - - bool isValidConfigFilePath(const std::string& path_, const std::string& file_name_) - { - const std::string file_path = 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); - } - - // Returns path, where the specified ini file is found, or empty string if not found - std::string findValidConfigPath(std::vector paths_, const std::string& file_name_) - { - auto it = std::find_if(paths_.begin(), paths_.end(), [&file_name_](const std::string& path_) - { - if (path_.empty()) - return false; - - return isValidConfigFilePath(path_, file_name_); - }); - - // We should have encountered a valid path - if (it != paths_.end()) - return (*it); - - // If valid path is not encountered, defaults should be used - return std::string(""); - } - - // 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 - std::vector getEcalDefaultPaths() - { - std::vector ecal_default_paths; - // ----------------------------------------------------------- - // precedence 1: ECAL_DATA variable (windows and linux) - // ----------------------------------------------------------- - ecal_default_paths.emplace_back(eCALDataEnvPath()); - - // ----------------------------------------------------------- - // precedence 2: cmake configured data paths (linux only) - // ----------------------------------------------------------- - ecal_default_paths.emplace_back(eCALDataCMakePath()); - - // ----------------------------------------------------------- - // precedence 3: system data path - // ----------------------------------------------------------- - ecal_default_paths.emplace_back(eCALDataSystemPath()); - return ecal_default_paths; - } - - // Check the following paths if the specified config file exists and return the first valid complete path - // 1. current working directory - // 2. ECAL_DATA environment variable path - // 3. cmake configured data paths (linux only) - // 4. system data path - std::string checkForValidConfigFilePath(const std::string& config_file_) - { - // ----------------------------------------------------------- - // precedence 0: relative path to executable - // ----------------------------------------------------------- - const std::string cwd_directory_path = cwdPath(); - - std::vector ecal_default_paths = getEcalDefaultPaths(); - ecal_default_paths.emplace(ecal_default_paths.begin(), cwd_directory_path); - - const std::string found_path = findValidConfigPath(ecal_default_paths, config_file_); - - // check in case user provided whole path - if (found_path.empty()) - { - return isValidConfigFilePath(config_file_, "") ? config_file_ : found_path; - } - - return found_path + config_file_; - } -} - namespace eCAL { void Configuration::InitFromFile(const std::string& yaml_path_) { - const std::string yaml_path = checkForValidConfigFilePath(yaml_path_); + const std::string yaml_path = eCAL::Path::checkForValidConfigFilePath(yaml_path_); if (!yaml_path.empty()) { #ifdef ECAL_CORE_CONFIGURATION @@ -303,131 +113,4 @@ namespace eCAL { return GetConfiguration().application; } -} - - -// Utils definitions from former ecal_config_reader.cpp -namespace -{ - bool direxists(const std::string& path_) - { - const EcalUtils::Filesystem::FileStatus status(path_, EcalUtils::Filesystem::Current); - return (status.IsOk() && (status.GetType() == EcalUtils::Filesystem::Type::Dir)); - } - - void createdir(const std::string& path_) - { - EcalUtils::Filesystem::MkDir(path_, EcalUtils::Filesystem::Current); - } - -} - -namespace eCAL -{ - namespace Util - { - std::string GeteCALConfigPath() - { - // Return the possible default paths that could contain the yaml file - const std::vector search_directories = getEcalDefaultPaths(); - - // Check for first directory which contains the ini file. - return findValidConfigPath(search_directories, ECAL_DEFAULT_CFG); - } - - std::string GeteCALHomePath() - { - std::string home_path; - -#ifdef ECAL_OS_WINDOWS - // check ECAL_HOME - home_path = getEnvVar("ECAL_HOME"); - if (!home_path.empty()) - { - if (*home_path.rbegin() != path_separator) home_path += path_separator; - } - if (!std::string(ECAL_HOME_PATH_WINDOWS).empty()) //-V815 - { - home_path += path_separator; - home_path += ECAL_HOME_PATH_WINDOWS; - } -#endif /* ECAL_OS_WINDOWS */ - -#ifdef ECAL_OS_LINUX - const char *hdir = nullptr; - hdir = getenv("HOME"); - if (hdir == nullptr) { - hdir = getpwuid(getuid())->pw_dir; - } - home_path += hdir; - if (!std::string(ECAL_HOME_PATH_LINUX).empty()) - { - home_path += "/"; - home_path += ECAL_HOME_PATH_LINUX; - } -#endif /* ECAL_OS_LINUX */ - - // create if not exists - if (!direxists(home_path)) - { - createdir(home_path); - } - - 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 */ - -#ifdef ECAL_OS_LINUX - settings_path = GeteCALHomePath(); -#endif /* ECAL_OS_LINUX */ - settings_path += std::string(ECAL_SETTINGS_PATH); - - if (!direxists(settings_path)) - { - createdir(settings_path); - } - - settings_path += path_separator; - return(settings_path); - } - - std::string GeteCALLogPath() - { - if (!eCAL::GetConfiguration().logging.provider.file_config.path.empty()) - { - return eCAL::GetConfiguration().logging.provider.file_config.path; - } - - std::string log_path; -#ifdef ECAL_OS_WINDOWS - log_path = GeteCALConfigPath(); -#endif /* ECAL_OS_WINDOWS */ - -#ifdef ECAL_OS_LINUX - log_path = GeteCALHomePath(); -#endif /* ECAL_OS_LINUX */ - - log_path += std::string(ECAL_LOG_PATH); - - if (!direxists(log_path)) - { - createdir(log_path); - } - - log_path += path_separator; - return(log_path); - } - - std::string GeteCALActiveIniFile() - { - return eCAL::GetConfiguration().GetYamlFilePath(); - } - } -} \ No newline at end of file +} \ No newline at end of file diff --git a/ecal/core/src/config/ecal_path_processing.cpp b/ecal/core/src/config/ecal_path_processing.cpp new file mode 100644 index 0000000000..d4297c7464 --- /dev/null +++ b/ecal/core/src/config/ecal_path_processing.cpp @@ -0,0 +1,333 @@ +/* ========================= 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 ================================= +*/ + +/** + * @brief Function definitions for path processing +**/ + +#include "ecal_path_processing.h" + +#include "ecal_def.h" +#include "ecal_utils/ecal_utils.h" +#include "ecal_utils/filesystem.h" +#include "ecal/ecal_config.h" +#include "ecal/ecal_util.h" +#include "util/getenvvar.h" + +#include + +// for cwd +#ifdef ECAL_OS_WINDOWS + #include + // to remove deprecated warning + #define getcwd _getcwd + constexpr int MAXIMUM_PATH_LENGTH = _MAX_PATH; +#endif +#ifdef ECAL_OS_LINUX + #include + #include + #include + #include + constexpr int MAXIMUM_PATH_LENGTH = PATH_MAX; +#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; + } + + bool direxists(const std::string& path_) + { + const EcalUtils::Filesystem::FileStatus status(path_, EcalUtils::Filesystem::Current); + return (status.IsOk() && (status.GetType() == EcalUtils::Filesystem::Type::Dir)); + } + + void createdir(const std::string& path_) + { + 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 EcalUtils::Filesystem::FileStatus ecal_ini_status(file_path, EcalUtils::Filesystem::Current); + return ecal_ini_status.IsOk() && (ecal_ini_status.GetType() == EcalUtils::Filesystem::Type::RegularFile); + } + + // Returns path, where the specified ini file is found, or empty string if not found + std::string findValidConfigPath(std::vector paths_, const std::string& file_name_) + { + auto it = std::find_if(paths_.begin(), paths_.end(), [&file_name_](const std::string& path_) + { + if (path_.empty()) + return false; + + return isValidConfigFilePath(path_, file_name_); + }); + + // We should have encountered a valid path + if (it != paths_.end()) + return (*it); + + // If valid path is not encountered, defaults should be used + return std::string(""); + } + + // 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 + std::vector getEcalDefaultPaths() + { + std::vector ecal_default_paths; + // ----------------------------------------------------------- + // precedence 1: ECAL_DATA variable (windows and linux) + // ----------------------------------------------------------- + ecal_default_paths.emplace_back(eCALDataEnvPath()); + + // ----------------------------------------------------------- + // precedence 2: cmake configured data paths (linux only) + // ----------------------------------------------------------- + ecal_default_paths.emplace_back(eCALDataCMakePath()); + + // ----------------------------------------------------------- + // precedence 3: system data path + // ----------------------------------------------------------- + ecal_default_paths.emplace_back(eCALDataSystemPath()); + return ecal_default_paths; + } +} + +namespace eCAL +{ + namespace Path + { + std::string checkForValidConfigFilePath(const std::string& config_file_) + { + // ----------------------------------------------------------- + // precedence 0: relative path to executable + // ----------------------------------------------------------- + const std::string cwd_directory_path = cwdPath(); + + std::vector ecal_default_paths = getEcalDefaultPaths(); + ecal_default_paths.emplace(ecal_default_paths.begin(), cwd_directory_path); + + const std::string found_path = findValidConfigPath(ecal_default_paths, config_file_); + + // check in case user provided whole path + if (found_path.empty()) + { + return isValidConfigFilePath(config_file_, "") ? config_file_ : found_path; + } + + return found_path + config_file_; + } + } // namespace Path + + namespace Util + { + std::string GeteCALConfigPath() + { + // Return the possible default paths that could contain the yaml file + const std::vector search_directories = getEcalDefaultPaths(); + + // Check for first directory which contains the ini file. + return findValidConfigPath(search_directories, ECAL_DEFAULT_CFG); + } + + std::string GeteCALHomePath() + { + std::string home_path; + +#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 + { + home_path += path_separator; + home_path += ECAL_HOME_PATH_WINDOWS; + } +#endif /* ECAL_OS_WINDOWS */ + +#ifdef ECAL_OS_LINUX + const char *hdir = nullptr; + hdir = getenv("HOME"); + if (hdir == nullptr) { + hdir = getpwuid(getuid())->pw_dir; + } + home_path += hdir; + if (!std::string(ECAL_HOME_PATH_LINUX).empty()) + { + home_path += "/"; + home_path += ECAL_HOME_PATH_LINUX; + } +#endif /* ECAL_OS_LINUX */ + + // create if not exists + if (!direxists(home_path)) + { + createdir(home_path); + } + + 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 */ + +#ifdef ECAL_OS_LINUX + settings_path = GeteCALHomePath(); +#endif /* ECAL_OS_LINUX */ + settings_path += std::string(ECAL_SETTINGS_PATH); + + if (!direxists(settings_path)) + { + createdir(settings_path); + } + + settings_path += path_separator; + return(settings_path); + } + + std::string GeteCALLogPath() + { + if (!eCAL::GetConfiguration().logging.provider.file_config.path.empty()) + { + return eCAL::GetConfiguration().logging.provider.file_config.path; + } + + std::string log_path; +#ifdef ECAL_OS_WINDOWS + log_path = GeteCALConfigPath(); +#endif /* ECAL_OS_WINDOWS */ + +#ifdef ECAL_OS_LINUX + log_path = GeteCALHomePath(); +#endif /* ECAL_OS_LINUX */ + + log_path += std::string(ECAL_LOG_PATH); + + if (!direxists(log_path)) + { + createdir(log_path); + } + + log_path += path_separator; + return(log_path); + } + + std::string GeteCALActiveIniFile() + { + return eCAL::GetConfiguration().GetYamlFilePath(); + } + } // namespace Util +} // namespace eCAL \ No newline at end of file diff --git a/ecal/core/src/config/ecal_path_processing.h b/ecal/core/src/config/ecal_path_processing.h new file mode 100644 index 0000000000..7e754d9b90 --- /dev/null +++ b/ecal/core/src/config/ecal_path_processing.h @@ -0,0 +1,44 @@ +/* ========================= 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 ================================= +*/ + +/** + * @brief Function definitions for path processing +**/ + +#include + +namespace eCAL +{ + namespace Path + { + /** + * @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 + * + * @param config_file_ The name of the configuration file to check. + * @return std::string The first valid complete path to the configuration file. + */ + std::string checkForValidConfigFilePath(const std::string& config_file_); + } +} \ No newline at end of file diff --git a/ecal/core/src/ecal_def.h b/ecal/core/src/ecal_def.h index 4690ab5eee..a4994eba5e 100644 --- a/ecal/core/src/ecal_def.h +++ b/ecal/core/src/ecal_def.h @@ -40,6 +40,10 @@ constexpr const char* ECAL_SETTINGS_PATH = "cfg"; /* ini file name */ 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"; + /* 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