Skip to content

Commit

Permalink
Match common5 separator/basename/parentPath
Browse files Browse the repository at this point in the history
* Separator with an emptry string argument should return the preferred
separator for that platform
* Basename should return basename in the case with multiple trailing
slashes

Signed-off-by: Michael Carroll <michael@openrobotics.org>
  • Loading branch information
mjcarroll committed Feb 10, 2022
1 parent 64b00a9 commit fb40333
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/Filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ bool ignition::common::createDirectories(const std::string &_path)
/////////////////////////////////////////////////
std::string const ignition::common::separator(std::string const &_s)
{
fs::path path(_s);
return (_s / fs::path("")).string();
return _s + std::string{fs::path::preferred_separator};
}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -138,6 +137,11 @@ std::string ignition::common::joinPaths(
fs::path p1{_path1};
fs::path p2{_path2};

if (p1.empty())
{
p1 = ignition::common::separator("");
}

bool is_url = false;

if (_path1.find("://") == std::string::npos)
Expand Down Expand Up @@ -195,25 +199,24 @@ bool ignition::common::chdir(const std::string &_dir)
/////////////////////////////////////////////////
std::string ignition::common::basename(const std::string &_path)
{
fs::path p(_path);
// Maintain compatibility with ign-common
if (*_path.rbegin() == fs::path::preferred_separator)
p = fs::path(_path.substr(0, _path.size()-1));
return p.filename().string();
fs::path p = fs::path(_path);
p /= "FOO.TXT";
p = p.lexically_normal();
p = p.parent_path();
return p.filename().string().empty() ?
std::string{fs::path::preferred_separator} : p.filename().string();
}

/////////////////////////////////////////////////
std::string ignition::common::parentPath(const std::string &_path)
{
fs::path p(_path);
// Maintain compatibility with ign-common
if (*_path.rbegin() == fs::path::preferred_separator)
p = fs::path(_path.substr(0, _path.size()-1));

if (!p.has_parent_path())
return _path;

return p.parent_path().string();
std::string result;
size_t last_sep = _path.find_last_of(separator(""));
// If slash is the last character, find its parent directory
if (last_sep == _path.length() - 1)
last_sep = _path.substr(0, last_sep).find_last_of(separator(""));
result = _path.substr(0, last_sep);
return result;
}

/////////////////////////////////////////////////
Expand Down
10 changes: 10 additions & 0 deletions src/Filesystem_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,16 @@ TEST_F(FilesystemTest, uniquePaths)
EXPECT_TRUE(removeDirectory(dirExistingRt)) << dirExistingRt;
}

/////////////////////////////////////////////////
TEST_F(FilesystemTest, separator)
{
#ifndef _WIN32
EXPECT_EQ("/", ignition::common::separator(""));
#else
EXPECT_EQ("\\", ignition::common::separator(""));
#endif
}

/////////////////////////////////////////////////
/// Main
int main(int argc, char **argv)
Expand Down

0 comments on commit fb40333

Please sign in to comment.