From 9bcb97fee18bad5916bbc015b4af9fc44ea1828d Mon Sep 17 00:00:00 2001 From: Alex Dewar Date: Wed, 9 Aug 2023 17:43:38 +0100 Subject: [PATCH] Rename some functions and mark noexcept --- .../configuration_parsing.cpp | 24 +++++++++++-------- .../configuration_parsing_helpers.h | 19 ++++++++------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/HealthGPS.Console/configuration_parsing.cpp b/src/HealthGPS.Console/configuration_parsing.cpp index f3f024664..8b92e5d7c 100644 --- a/src/HealthGPS.Console/configuration_parsing.cpp +++ b/src/HealthGPS.Console/configuration_parsing.cpp @@ -20,7 +20,11 @@ nlohmann::json get(const json &j, const std::string &key) { void rebase_valid_path(std::filesystem::path &path, const std::filesystem::path &base_dir) { if (path.is_relative()) { - path = std::filesystem::weakly_canonical(base_dir / path); + try { + path = std::filesystem::weakly_canonical(base_dir / path); + } catch (const std::filesystem::filesystem_error &e) { + throw ConfigurationError{fmt::format("OS error while reading {}", path)}; + } } if (!std::filesystem::exists(path)) { @@ -28,25 +32,25 @@ void rebase_valid_path(std::filesystem::path &path, const std::filesystem::path } } -bool get_valid_path_to(const json &j, const std::string &key, const std::filesystem::path &base_dir, - std::filesystem::path &out) { - if (!get_to(j, key, out)) { +bool rebase_valid_path_to(const json &j, const std::string &key, std::filesystem::path &path, + const std::filesystem::path &base_dir) noexcept { + if (!get_to(j, key, path)) { return false; } try { - rebase_valid_path(out, base_dir); + rebase_valid_path(path, base_dir); } catch (const ConfigurationError &) { - fmt::print(fg(fmt::color::red), "Could not find file {}", out.string()); + fmt::print(fg(fmt::color::red), "Could not find file {}", path.string()); return false; } return true; } -void get_valid_path_to(const json &j, const std::string &key, const std::filesystem::path &base_dir, - std::filesystem::path &out, bool &success) { - if (!get_valid_path_to(j, key, base_dir, out)) { +void rebase_valid_path_to(const json &j, const std::string &key, std::filesystem::path &path, + const std::filesystem::path &base_dir, bool &success) noexcept { + if (!rebase_valid_path_to(j, key, path, base_dir)) { success = false; } } @@ -56,7 +60,7 @@ poco::FileInfo get_file_info(const json &j, const std::filesystem::path &base_di bool success = true; poco::FileInfo info; - get_valid_path_to(dataset, "name", base_dir, info.name, success); + rebase_valid_path_to(dataset, "name", info.name, base_dir, success); get_to(dataset, "format", info.format, success); get_to(dataset, "delimiter", info.delimiter, success); get_to(dataset, "columns", info.columns, success); diff --git a/src/HealthGPS.Console/configuration_parsing_helpers.h b/src/HealthGPS.Console/configuration_parsing_helpers.h index b11cbe9e8..438ff1031 100644 --- a/src/HealthGPS.Console/configuration_parsing_helpers.h +++ b/src/HealthGPS.Console/configuration_parsing_helpers.h @@ -22,7 +22,7 @@ nlohmann::json get(const nlohmann::json &j, const std::string &key); /// @param key Key to value /// @param out Output object /// @return True if value was retrieved successfully, false otherwise -template bool get_to(const nlohmann::json &j, const std::string &key, T &out) { +template bool get_to(const nlohmann::json &j, const std::string &key, T &out) noexcept { try { out = j.at(key).get(); return true; @@ -43,7 +43,7 @@ template bool get_to(const nlohmann::json &j, const std::string &key, /// @param success Success flag, set to false in case of failure /// @return True if value was retrieved successfully, false otherwise template -bool get_to(const nlohmann::json &j, const std::string &key, T &out, bool &success) { +bool get_to(const nlohmann::json &j, const std::string &key, T &out, bool &success) noexcept { const bool ret = get_to(j, key, out); if (!ret) { success = false; @@ -61,20 +61,21 @@ void rebase_valid_path(std::filesystem::path &path, const std::filesystem::path /// @param j JSON object /// @param key Key to value /// @param base_dir Base directory for relative path -/// @param out Output variable +/// @param path Output variable /// @return True if value was retrieved successfully and is valid path, false otherwise -bool get_valid_path_to(const nlohmann::json &j, const std::string &key, - const std::filesystem::path &base_dir, std::filesystem::path &out); +bool rebase_valid_path_to(const nlohmann::json &j, const std::string &key, + std::filesystem::path &path, + const std::filesystem::path &base_dir) noexcept; /// @brief Get a valid path from a JSON object /// @param j JSON object /// @param key Key to value /// @param base_dir Base directory for relative path -/// @param out Output variable +/// @param path Output variable /// @param success Success flag, set to false in case of failure -void get_valid_path_to(const nlohmann::json &j, const std::string &key, - const std::filesystem::path &base_dir, std::filesystem::path &out, - bool &success); +void rebase_valid_path_to(const nlohmann::json &j, const std::string &key, + std::filesystem::path &out, const std::filesystem::path &base_dir, + bool &success) noexcept; /// @brief Load FileInfo from JSON /// @param j Input JSON