Commit 625d4025 authored by Simon Spannagel's avatar Simon Spannagel
Browse files

Configuration: improve path_to_absolute using std::filesystem methods

parent 2ee0c546
Loading
Loading
Loading
Loading
+5 −8
Original line number Diff line number Diff line
@@ -130,14 +130,11 @@ std::vector<std::filesystem::path> Configuration::getPathArray(const std::string
/**
 * @throws std::invalid_argument If the path does not exists
 */
std::filesystem::path Configuration::path_to_absolute(std::string path, bool canonicalize_path) const {
std::filesystem::path Configuration::path_to_absolute(std::filesystem::path path, bool canonicalize_path) const {
    // If not a absolute path, make it an absolute path
    if(path.front() != '/') {
        // Get base directory of config file
        std::string directory = path_.substr(0, path_.find_last_of('/'));

        // Set new path
        path = directory + "/" + path;
    if(!path.is_absolute()) {
        // Get base directory of config file and append the relative path
        path = path_.parent_path() / path;
    }

    // Normalize path only if we have to check if it exists
@@ -146,7 +143,7 @@ std::filesystem::path Configuration::path_to_absolute(std::string path, bool can
        try {
            path = std::filesystem::canonical(path);
        } catch(std::filesystem::filesystem_error&) {
            throw std::invalid_argument("path " + path + " not found");
            throw std::invalid_argument("path " + path.string() + " not found");
        }
    }
    return path;
+2 −2
Original line number Diff line number Diff line
@@ -324,7 +324,7 @@ namespace allpix {
         * @param path Path to make absolute (if it is not already absolute)
         * @param canonicalize_path If the path should be canonicalized (throws an error if the path does not exist)
         */
        std::filesystem::path path_to_absolute(std::string path, bool canonicalize_path) const;
        std::filesystem::path path_to_absolute(std::filesystem::path path, bool canonicalize_path) const;

        /**
         * @brief Node in a parse tree
@@ -342,7 +342,7 @@ namespace allpix {
        static std::unique_ptr<parse_node> parse_value(std::string str, int depth = 0);

        std::string name_;
        std::string path_;
        std::filesystem::path path_;

        using ConfigMap = std::map<std::string, std::string>;
        ConfigMap config_;