Skip to content

Commit

Permalink
[rtefsutils] Add LexicallyNormal function (#663) (#1077)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Brondani <[email protected]>
  • Loading branch information
grasci-arm and brondani authored Jul 28, 2023
1 parent b0ddd9e commit 9b487dd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
7 changes: 7 additions & 0 deletions libs/rtefsutils/include/RteFsUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions libs/rtefsutils/src/RteFsUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
Expand Down
13 changes: 13 additions & 0 deletions libs/rtefsutils/test/src/RteFsUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tools/projmgr/src/ProjMgrYamlParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 9b487dd

Please sign in to comment.