From da8a9e65270ea4b2af4ef7ee94cf6ee88c552d2b Mon Sep 17 00:00:00 2001 From: Shawn Waldon <shawn.waldon@kitware.com> Date: Thu, 3 Aug 2017 09:42:35 -0400 Subject: [PATCH] SystemTools: make GetFilenameName handle either kind of slash Previously trying to parse a windows-style (backslash separated) path on Linux would fail. Make this function handle either kind of separator on both platforms. --- SystemTools.cxx | 4 ---- testSystemTools.cxx | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/SystemTools.cxx b/SystemTools.cxx index b87a002..11f3b81 100644 --- a/SystemTools.cxx +++ b/SystemTools.cxx @@ -3797,11 +3797,7 @@ std::string SystemTools::GetFilenamePath(const std::string& filename) */ std::string SystemTools::GetFilenameName(const std::string& filename) { -#if defined(_WIN32) std::string::size_type slash_pos = filename.find_last_of("/\\"); -#else - std::string::size_type slash_pos = filename.rfind('/'); -#endif if (slash_pos != std::string::npos) { return filename.substr(slash_pos + 1); } else { diff --git a/testSystemTools.cxx b/testSystemTools.cxx index e6fbf6c..1871f5d 100644 --- a/testSystemTools.cxx +++ b/testSystemTools.cxx @@ -758,6 +758,30 @@ static bool CheckGetPath() return res; } +static bool CheckGetFilenameName() +{ + const char* windowsFilepath = "C:\\somewhere\\something"; + const char* unixFilepath = "/somewhere/something"; + + std::string expectedFilename = "something"; + + bool res = true; + std::string filename = kwsys::SystemTools::GetFilenameName(windowsFilepath); + if (filename != expectedFilename) { + std::cerr << "GetFilenameName(" << windowsFilepath << ") yielded " + << filename << " instead of " << expectedFilename << std::endl; + res = false; + } + + filename = kwsys::SystemTools::GetFilenameName(unixFilepath); + if (filename != expectedFilename) { + std::cerr << "GetFilenameName(" << unixFilepath << ") yielded " << filename + << " instead of " << expectedFilename << std::endl; + res = false; + } + return res; +} + static bool CheckFind() { bool res = true; @@ -875,5 +899,7 @@ int testSystemTools(int, char* []) res &= CheckGetLineFromStream(); + res &= CheckGetFilenameName(); + return res ? 0 : 1; } -- GitLab