diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-11-01 12:15:34 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-01-20 10:46:13 +0100 |
commit | 0d1024b64155a05f45f247ad57d0f444db01c1e2 (patch) | |
tree | eedb4ec919e8245dc8c112e1d49a8390d23e7748 | |
parent | aa2613c7037bd12448d58714b4bb317ea939d892 (diff) | |
download | mana-0d1024b64155a05f45f247ad57d0f444db01c1e2.tar.gz mana-0d1024b64155a05f45f247ad57d0f444db01c1e2.tar.bz2 mana-0d1024b64155a05f45f247ad57d0f444db01c1e2.tar.xz mana-0d1024b64155a05f45f247ad57d0f444db01c1e2.zip |
Cleaned up path handling a bit
-rw-r--r-- | src/resources/settingsmanager.cpp | 6 | ||||
-rw-r--r-- | src/utils/path.cpp | 63 | ||||
-rw-r--r-- | src/utils/path.h | 15 |
3 files changed, 37 insertions, 47 deletions
diff --git a/src/resources/settingsmanager.cpp b/src/resources/settingsmanager.cpp index a289c6c1..99798ab0 100644 --- a/src/resources/settingsmanager.cpp +++ b/src/resources/settingsmanager.cpp @@ -139,9 +139,9 @@ namespace SettingsManager if (!includeFile.empty()) { - // build absolute path path - const utils::splittedPath splittedPath = utils::splitFileNameAndPath(filename); - includeFile = utils::cleanPath(utils::joinPaths(splittedPath.path, includeFile)); + // build absolute path + const auto path = utils::path(filename); + includeFile = utils::cleanPath(utils::joinPaths(path, includeFile)); } else { diff --git a/src/utils/path.cpp b/src/utils/path.cpp index c78f0fd7..7d3cf11a 100644 --- a/src/utils/path.cpp +++ b/src/utils/path.cpp @@ -23,30 +23,24 @@ namespace utils { - /** - * Returns the filePath sub-part corresponding to the filename only. - * @return splittedPath: the file path ending with '/' or '\' - * and the file name alone. + * Returns the path without the file name. The path separator is kept. */ - splittedPath splitFileNameAndPath(const std::string &fullFilePath) + std::string_view path(std::string_view fullFilePath) { - // We'll reversed-search for '/' or'\' and extract the substrings - // corresponding to the filename and the path separately. - size_t slashPos = fullFilePath.find_last_of("/\\"); - - splittedPath splittedFilePath; - // Note the last slash is kept in the path name. - splittedFilePath.path = fullFilePath.substr(0, slashPos + 1); - splittedFilePath.file = fullFilePath.substr(slashPos + 1); - - return splittedFilePath; + // We'll reverse-search for '/' or'\' and extract the substring + // corresponding to path. + const auto slashPos = fullFilePath.find_last_of("/\\"); + if (slashPos != std::string::npos) + return fullFilePath.substr(0, slashPos + 1); + else + return std::string_view(); } /** * Join two path elements into one. * - * This function helps build relative paths. + * This function helps handling relative paths. * * Examples: * @@ -58,25 +52,32 @@ namespace utils * * @return Joined paths or path2 if path2 was an absolute path. */ - std::string joinPaths(const std::string &path1, const std::string &path2) + std::string joinPaths(std::string_view path1, std::string_view path2) { - if (path2.empty()) - return path1; + std::string joined; - if (path1.empty()) - return path2; - - // check if path2 is an absolute path that cannot be joined - if (path2[0] == '/' || path2[0] == '\\') - return path2; - - char p1end = path1[path1.size()-1]; - if (p1end == '/' || p1end == '\\') + if (path2.empty()) { - return path1 + path2; + joined.append(path1); + } + else if (path1.empty()) + { + joined.append(path2); + } + else if (path2[0] == '/' || path2[0] == '\\') + { + // return only path2 if it is an absolute path + joined.append(path2); + } + else + { + joined.append(path1); + if (joined.back() != '/' && joined.back() != '\\') + joined.append("/"); + joined.append(path2); } - return path1 + "/" + path2; + return joined; } /** @@ -146,6 +147,4 @@ namespace utils return result; } - - } diff --git a/src/utils/path.h b/src/utils/path.h index 2ad9e987..5338c74e 100644 --- a/src/utils/path.h +++ b/src/utils/path.h @@ -25,18 +25,9 @@ namespace utils { - - struct splittedPath - { - std::string path; - std::string file; - }; - - splittedPath splitFileNameAndPath(const std::string &fullFilePath); - - std::string joinPaths(const std::string &path1, const std::string &path2); - + std::string_view path(std::string_view fullFilePath); + std::string joinPaths(std::string_view path1, std::string_view path2); std::string cleanPath(const std::string &path); - } + #endif // UTILS_PATH_H |