Commit da8a9e65 authored by Shawn Waldon's avatar Shawn Waldon

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.
parent 288961c3
......@@ -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 {
......
......@@ -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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment