From 9b487ddaf546bb3fff736408655a5e675ef78691 Mon Sep 17 00:00:00 2001 From: grasci <86058054+grasci-arm@users.noreply.github.com> Date: Fri, 28 Jul 2023 08:43:37 +0100 Subject: [PATCH] [rtefsutils] Add LexicallyNormal function (#663) (#1077) Co-authored-by: Daniel Brondani --- libs/rtefsutils/include/RteFsUtils.h | 7 +++++++ libs/rtefsutils/src/RteFsUtils.cpp | 9 +++++++++ libs/rtefsutils/test/src/RteFsUtilsTest.cpp | 13 +++++++++++++ tools/projmgr/src/ProjMgrYamlParser.cpp | 2 +- 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/libs/rtefsutils/include/RteFsUtils.h b/libs/rtefsutils/include/RteFsUtils.h index 8055ca76f..1fac98326 100644 --- a/libs/rtefsutils/include/RteFsUtils.h +++ b/libs/rtefsutils/include/RteFsUtils.h @@ -223,6 +223,13 @@ class RteFsUtils */ static std::string ParentPath(const std::string& path); + /** + * @brief get lexically normalized path + * @param path path to be processed + * @return string containing the lexically normalized path + */ + static std::string LexicallyNormal(const std::string& path); + /** * @brief determine relative path in respect to base directory * @param path path to be processed diff --git a/libs/rtefsutils/src/RteFsUtils.cpp b/libs/rtefsutils/src/RteFsUtils.cpp index 4da2d874d..d6e177944 100644 --- a/libs/rtefsutils/src/RteFsUtils.cpp +++ b/libs/rtefsutils/src/RteFsUtils.cpp @@ -562,6 +562,15 @@ string RteFsUtils::ParentPath(const string& path) { return fs::path(path).parent_path().generic_string(); } +string RteFsUtils::LexicallyNormal(const string& path) { + string lexicallyNormal = fs::path(path).lexically_normal().generic_string(); + if ((lexicallyNormal.length() > 1) && (lexicallyNormal.back() == '/')) { + // remove trailing separator for alignment with canonical behaviour + lexicallyNormal.pop_back(); + } + return lexicallyNormal; +} + string RteFsUtils::RelativePath(const string& path, const string& base, bool withHeadingDot) { if (path.empty() || base.empty()) { return ""; diff --git a/libs/rtefsutils/test/src/RteFsUtilsTest.cpp b/libs/rtefsutils/test/src/RteFsUtilsTest.cpp index 17b15d202..06f4e095f 100644 --- a/libs/rtefsutils/test/src/RteFsUtilsTest.cpp +++ b/libs/rtefsutils/test/src/RteFsUtilsTest.cpp @@ -920,6 +920,19 @@ TEST_F(RteFsUtilsTest, ParentPath) { EXPECT_EQ(filePath, dirnameSubdir); } +TEST_F(RteFsUtilsTest, LexicallyNormal) { + string dirPath; + + dirPath = RteFsUtils::LexicallyNormal(dirnameDotDotSubdir); + EXPECT_EQ(dirPath, dirnameSubdir); + + dirPath = RteFsUtils::LexicallyNormal(dirnameDotSubdir); + EXPECT_EQ(dirPath, dirnameSubdir); + + dirPath = RteFsUtils::LexicallyNormal(dirnameSubdirWithTrailing); + EXPECT_EQ(dirPath, dirnameSubdir); +} + TEST_F(RteFsUtilsTest, RelativePath) { string relPath; string absSubdir = RteFsUtils::AbsolutePath(dirnameSubdir).generic_string(); diff --git a/tools/projmgr/src/ProjMgrYamlParser.cpp b/tools/projmgr/src/ProjMgrYamlParser.cpp index f665cc1e5..e2f18625a 100644 --- a/tools/projmgr/src/ProjMgrYamlParser.cpp +++ b/tools/projmgr/src/ProjMgrYamlParser.cpp @@ -237,7 +237,7 @@ void ProjMgrYamlParser::EnsurePortability(const string& file, const YAML::Mark& value.clear(); } else { const string parentDir = RteFsUtils::ParentPath(file); - const string original = fs::path(parentDir).append(value).lexically_normal().generic_string(); + const string original = RteFsUtils::LexicallyNormal(fs::path(parentDir).append(value).generic_string()); if (RteFsUtils::Exists(original)) { error_code ec; string canonical = fs::canonical(original, ec).generic_string();