Skip to content

Commit

Permalink
Clean up code and make clangtidy happy.
Browse files Browse the repository at this point in the history
  • Loading branch information
Peguen committed May 17, 2024
1 parent b7e0bed commit 287c24f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 83 deletions.
4 changes: 2 additions & 2 deletions doc/rst/configuration/src/hello_config/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ int main(int argc, char** argv)
custom_config.InitConfigWithDefaultIni();

// .. or specify an own .ini file to use
custom_conig.InitConfig("C:\\eCAL_local.ini");
custom_config.InitConfig("C:\\eCAL_local.ini");

// Set the values in a try/catch block, as wrong configuration leads to exceptions
try
Expand All @@ -26,7 +26,7 @@ int main(int argc, char** argv)
// Increase the send buffer, size increase in 1024 bytes steps
custom_config.transport_layer_options.mc_options.sndbuf = (5242880 + 10 * 1024);
}
catch (std::invalid_argument e)
catch (std::invalid_argument& e)
{
throw std::runtime_error("Error while configuring eCALConfig: " + std::string(e.what()));
}
Expand Down
135 changes: 61 additions & 74 deletions ecal/core/src/config/ecal_cmd_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,11 @@ namespace

std::string cwdPath()
{
std::string cwd_path = {};
char* buffer;

if ( (buffer = getcwd(NULL, 0)) == NULL )
throw std::runtime_error("getcwd() : cannot read current working directory.");
else
{
cwd_path = std::string(buffer);
free(buffer);
}
std::string cwd_path = { getcwd(nullptr, 0) };

if (cwd_path.empty())
throw std::runtime_error("getcwd() : cannot read current working directory.");

SetPathSep(cwd_path);
return cwd_path;
}
Expand All @@ -73,8 +67,8 @@ namespace
{
std::string cmake_data_path;
#ifdef ECAL_OS_LINUX
std::string ecal_install_config_dir(ECAL_INSTALL_CONFIG_DIR);
std::string ecal_install_prefix(ECAL_INSTALL_PREFIX);
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())
Expand Down Expand Up @@ -105,27 +99,20 @@ namespace
return system_data_path;
}

bool isValidConfigFilePath(std::string file_path_)
bool isValidConfigFilePath(const std::string& file_path_)
{
// check existence of user defined file
const EcalUtils::Filesystem::FileStatus ecal_ini_status(file_path_, EcalUtils::Filesystem::Current);
if (ecal_ini_status.IsOk() && (ecal_ini_status.GetType() == EcalUtils::Filesystem::Type::RegularFile))
{
return true;
}

return false;
return ecal_ini_status.IsOk() && (ecal_ini_status.GetType() == EcalUtils::Filesystem::Type::RegularFile);
}

std::string appendFileNameToPathIfPathIsValid(std::string path_, std::string file_name_)
void appendFileNameToPathIfPathIsValid(std::string& path_, const std::string& file_name_)
{
if (path_.empty())
return path_;
else
return path_ + path_separator + file_name_;
if (!path_.empty())
path_ += path_separator + file_name_;
}

void parseConfigKeysToMap(std::vector<std::string> config_keys_, eCAL::Config::ConfigKey2DMap& map_)
void parseConfigKeysToMap(const std::vector<std::string>& config_keys_, eCAL::Config::ConfigKey2DMap& map_)
{
// each string has the format "section/key:value"
for (const auto& full_key : config_keys_)
Expand All @@ -143,6 +130,55 @@ namespace
map_[section][key] = value;
}
}

std::string checkForValidConfigFilePath(const std::string config_file_)
{
// differences to ecal_config_reader implementation are:
// 1. it does not use the default ini file name, instead uses the specified file
// 2. it searches relative to the executable path and takes it as highest priority
// 3. it throws a runtime error, if it cannot find the specified file

// -----------------------------------------------------------
// precedence 1: relative path to executable
// -----------------------------------------------------------
std::string cwd_directory_path = cwdPath();
appendFileNameToPathIfPathIsValid(cwd_directory_path, config_file_);

// -----------------------------------------------------------
// precedence 2: ECAL_DATA variable (windows and linux)
// -----------------------------------------------------------
std::string ecal_data_path = eCALDataEnvPath();
appendFileNameToPathIfPathIsValid(ecal_data_path, config_file_);

// -----------------------------------------------------------
// precedence 3: cmake configured data paths (linux only)
// -----------------------------------------------------------
std::string cmake_data_path = eCALDataCMakePath();
appendFileNameToPathIfPathIsValid(cmake_data_path, config_file_);

// -----------------------------------------------------------
// precedence 4: system data path
// -----------------------------------------------------------
std::string system_data_path = eCALDataSystemPath();
appendFileNameToPathIfPathIsValid(system_data_path, config_file_);

// Check for first directory which contains the ini file.
std::vector<std::string> search_directories{ cwd_directory_path, ecal_data_path, cmake_data_path, system_data_path };

auto it = std::find_if(search_directories.begin(), search_directories.end(), isValidConfigFilePath);
// We should have encountered a valid path
if (it != search_directories.end())
return (*it);

// Check if user specified complete path, in case all other precedence paths exist
if (isValidConfigFilePath(config_file_))
{
return config_file_;
}

// If valid path is not encountered, throw error
throw std::runtime_error("[CMD Parser] Specified config file: \"" + config_file_ + "\" not found.");
}
}

namespace eCAL
Expand All @@ -151,9 +187,6 @@ namespace eCAL
{
CmdParser::CmdParser()
: m_dump_config{false}
, m_config_keys{}
, m_task_parameter{}
, m_user_ini{}
{}

CmdParser::CmdParser(int argc_ , char **argv_)
Expand Down Expand Up @@ -209,52 +242,6 @@ namespace eCAL
{
for (size_t i = 0; i < static_cast<size_t>(argc_); ++i) if (argv_[i] != nullptr) m_task_parameter.emplace_back(argv_[i]);
}

}

std::string CmdParser::checkForValidConfigFilePath(std::string config_file_)
{
// differences to ecal_config_reader implementation are:
// 1. it does not use the default ini file name, instead uses the specified file
// 2. it searches relative to the executable path and takes it as highest priority
// 3. it throws a runtime error, if it cannot find the specified file

// -----------------------------------------------------------
// precedence 1: relative path to executable
// -----------------------------------------------------------
const std::string cwd_directory_path{ appendFileNameToPathIfPathIsValid(cwdPath(), config_file_) };

// -----------------------------------------------------------
// precedence 2: ECAL_DATA variable (windows and linux)
// -----------------------------------------------------------
const std::string ecal_data_path{ appendFileNameToPathIfPathIsValid(eCALDataEnvPath(), config_file_) };

// -----------------------------------------------------------
// precedence 3: cmake configured data paths (linux only)
// -----------------------------------------------------------
const std::string cmake_data_path{ appendFileNameToPathIfPathIsValid(eCALDataCMakePath(), config_file_) };

// -----------------------------------------------------------
// precedence 4: system data path
// -----------------------------------------------------------
const std::string system_data_path( appendFileNameToPathIfPathIsValid(eCALDataSystemPath(), config_file_) );

// Check for first directory which contains the ini file.
std::vector<std::string> search_directories{ config_file_, cwd_directory_path, ecal_data_path, cmake_data_path, system_data_path };

auto it = std::find_if(search_directories.begin(), search_directories.end(), isValidConfigFilePath);
// We should have encountered a valid path
if (it != search_directories.end())
return (*it);

// Check if user specified complete path, in case all other precedence paths exist
if (isValidConfigFilePath(config_file_))
{
return config_file_;
}

// If valid path is not encountered, throw error
throw std::runtime_error("[CMD Parser] Specified config file: \"" + config_file_ + "\" not found.");
}

bool CmdParser::getDumpConfig() const { return m_dump_config; };
Expand Down
2 changes: 0 additions & 2 deletions ecal/core/src/config/ecal_cmd_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ namespace eCAL
ConfigKey2DMap& getConfigKeysMap();

private:
std::string checkForValidConfigFilePath(std::string config_file_);

std::vector<std::string> m_config_keys;
ConfigKey2DMap m_config_key_map;
bool m_dump_config;
Expand Down
8 changes: 4 additions & 4 deletions ecal/tests/cpp/config_test/src/config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <ecal/ecal_core.h>
#include <ecal/ecal_config.h>
#include <ini_file.h>
#include "ini_file.h"

#include <gtest/gtest.h>

Expand Down Expand Up @@ -237,7 +237,7 @@ TEST(core_cpp_config, config_cmd_parser)

eCAL::Config::CmdParser parser;

std::vector<const char*> arguments;
std::vector<const char*> arguments{};

const std::string set_config_key = "--ecal-set-config-key ";
const std::string sep_slash = "/";
Expand Down Expand Up @@ -289,9 +289,9 @@ TEST(core_cpp_config, config_cmd_parser)

TEST(CmdParserDeathTest, config_cmd_parser_death_test)
{
eCAL::Config::CmdParser parser;
eCAL::Config::CmdParser parser{};

std::vector<const char*> arguments;
std::vector<const char*> arguments{};

arguments.push_back("test_config_cmd_parser_death_test");
arguments.push_back("--ecal-ini-file someNotValidFileName.ini");
Expand Down
2 changes: 1 addition & 1 deletion ecal/tests/cpp/config_test/src/ini_file.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <string>

std::string ini_file_as_string =
static const std::string ini_file_as_string =
"; --------------------------------------------------\n"
"; NETWORK SETTINGS\n"
"; --------------------------------------------------\n"
Expand Down

0 comments on commit 287c24f

Please sign in to comment.