From 72c9f6c79f27a0adccc22c743c51624252f3316f Mon Sep 17 00:00:00 2001 From: Michael Carroll Date: Wed, 9 Feb 2022 08:25:07 -0600 Subject: [PATCH] Bugfix Signed-off-by: Michael Carroll --- src/Filesystem.cc | 19 ++++++++++++------- src/Filesystem_TEST.cc | 10 ++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Filesystem.cc b/src/Filesystem.cc index ce66d6d6f..c89830d65 100644 --- a/src/Filesystem.cc +++ b/src/Filesystem.cc @@ -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}; } ///////////////////////////////////////////////// @@ -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) @@ -195,11 +199,12 @@ 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(); } ///////////////////////////////////////////////// diff --git a/src/Filesystem_TEST.cc b/src/Filesystem_TEST.cc index 24b9aff4e..23482d8e7 100644 --- a/src/Filesystem_TEST.cc +++ b/src/Filesystem_TEST.cc @@ -604,6 +604,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)