Skip to content

Commit

Permalink
Unfinished config files support
Browse files Browse the repository at this point in the history
  • Loading branch information
mwestphal committed Aug 26, 2024
1 parent e60ae11 commit ae494db
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 123 deletions.
27 changes: 11 additions & 16 deletions application/F3DConfigFileTools.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ std::vector<fs::path> F3DConfigFileTools::GetConfigPaths(const std::string& conf
}

//----------------------------------------------------------------------------
// TODO exceptions
F3DConfigFileTools::ConfigEntries F3DConfigFileTools::ReadConfigFiles(const std::string& userConfig)
void F3DConfigFileTools::ReadConfigFiles(const std::string& userConfig, F3DConfigFileTools::ConfigEntries& confEntries)
{
std::string configSearch = "config";
if (!userConfig.empty())
Expand Down Expand Up @@ -148,14 +147,14 @@ F3DConfigFileTools::ConfigEntries F3DConfigFileTools::ReadConfigFiles(const std:
}
catch (const fs::filesystem_error&)
{
f3d::log::error("Configuration file does not exist: ", configPath.string());
// return false;
f3d::log::error("Configuration file does not exist: ", configPath.string(), " , ignoring it");
continue;
}

// Recover all config files if needed
if (fs::is_directory(configPath))
{
// TODO add a ordche check test if not alrady
// TODO add a order check test if not alrady

Check failure on line 157 in application/F3DConfigFileTools.cxx

View workflow job for this annotation

GitHub Actions / Codespell Check

alrady ==> already
f3d::log::debug("Using config directory ", configPath.string());
for (auto& entry : std::filesystem::directory_iterator(configPath))
{
Expand All @@ -175,20 +174,18 @@ F3DConfigFileTools::ConfigEntries F3DConfigFileTools::ReadConfigFiles(const std:
{
f3d::log::warn("Configuration file for \"", configSearch, "\" could not be found");
}
// return false;
}

// Read config files
ConfigEntries entries;
for (auto& configFilePath : actualConfigFilePaths)
{
std::ifstream file;
file.open(configFilePath.string().c_str());

if (!file.is_open())
{
f3d::log::error("Unable to open the configuration file: ", configFilePath.string());
// return false;
f3d::log::warn("Unable to open the configuration file: ", configFilePath.string(), " , ignoring it");
continue;
}

nlohmann::ordered_json json;
Expand All @@ -198,9 +195,9 @@ F3DConfigFileTools::ConfigEntries F3DConfigFileTools::ReadConfigFiles(const std:
}
catch (const std::exception& ex)
{
f3d::log::error("Unable to parse the configuration file ", configFilePath.string());
f3d::log::error("Unable to parse the configuration file ", configFilePath.string(), " , ignoring it");
f3d::log::error(ex.what());
// return false;
continue;
}

for (const auto& configBlock : json.items())
Expand All @@ -210,7 +207,6 @@ F3DConfigFileTools::ConfigEntries F3DConfigFileTools::ReadConfigFiles(const std:
{
if (item.value().is_number() || item.value().is_boolean())
{
// TODO options::format ?
entry[item.key()] = ::ToString(item.value());
}
else if (item.value().is_string())
Expand All @@ -219,13 +215,12 @@ F3DConfigFileTools::ConfigEntries F3DConfigFileTools::ReadConfigFiles(const std:
}
else
{
f3d::log::error(item.key(), " must be a string, a boolean or a number");
// return false;
f3d::log::error(item.key(), " from ", configFilePath.string(), " must be a string, a boolean or a number, ignoring entry");
continue;
}
}
entries.emplace_back(entry, configFilePath, configBlock.key());
confEntries.emplace_back(entry, configFilePath, configBlock.key());
}
}
return entries;
}

2 changes: 1 addition & 1 deletion application/F3DConfigFileTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using ConfigDict = std::map<std::string, std::string>;
using ConfigEntry = std::tuple<ConfigDict, std::filesystem::path, std::string>;
using ConfigEntries = std::vector<ConfigEntry>;

ConfigEntries ReadConfigFiles(const std::string& userConfig);
void ReadConfigFiles(const std::string& userConfig, ConfigEntries& conEntries);
}

#endif
Loading

0 comments on commit ae494db

Please sign in to comment.