Commit 6770cb3e authored by Ben Boeckel's avatar Ben Boeckel Committed by Brad King

GetFilenameName: optionally don't search for \ on non-Windows

Non-Windows platforms don't have backslash as a separator, however some
projects may want to support Windows paths on non-Windows platforms
(e.g., paths from data files). For those which don't need this, this
allows the search to be faster since it's searching for a single
character.
parent cfd0f1e6
......@@ -496,6 +496,16 @@ IF(KWSYS_USE_SystemTools)
KWSYS_CXX_STAT_HAS_ST_MTIM=${KWSYS_CXX_STAT_HAS_ST_MTIM}
KWSYS_CXX_STAT_HAS_ST_MTIMESPEC=${KWSYS_CXX_STAT_HAS_ST_MTIMESPEC}
)
IF(NOT WIN32)
IF(KWSYS_STANDALONE)
OPTION(KWSYS_SYSTEMTOOLS_SUPPORT_WINDOWS_SLASHES "If true, Windows paths will be supported on Unix as well" ON)
ENDIF()
IF(KWSYS_SYSTEMTOOLS_SUPPORT_WINDOWS_SLASHES)
SET_PROPERTY(SOURCE SystemTools.cxx testSystemTools.cxx APPEND PROPERTY COMPILE_DEFINITIONS
KWSYS_SYSTEMTOOLS_SUPPORT_WINDOWS_SLASHES
)
ENDIF()
ENDIF()
# Disable getpwnam for static linux builds since it depends on shared glibc
GET_PROPERTY(SHARED_LIBS_SUPPORTED GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS)
......
......@@ -3719,7 +3719,12 @@ std::string SystemTools::GetFilenamePath(const std::string& filename)
*/
std::string SystemTools::GetFilenameName(const std::string& filename)
{
std::string::size_type slash_pos = filename.find_last_of("/\\");
#if defined(_WIN32) || defined(KWSYS_SYSTEMTOOLS_SUPPORT_WINDOWS_SLASHES)
const char* separators = "/\\";
#else
char separators = '/';
#endif
std::string::size_type slash_pos = filename.find_last_of(separators);
if (slash_pos != std::string::npos) {
return filename.substr(slash_pos + 1);
} else {
......
......@@ -764,20 +764,26 @@ static bool CheckGetFilenameName()
const char* windowsFilepath = "C:\\somewhere\\something";
const char* unixFilepath = "/somewhere/something";
std::string expectedFilename = "something";
#if defined(_WIN32) || defined(KWSYS_SYSTEMTOOLS_SUPPORT_WINDOWS_SLASHES)
std::string expectedWindowsFilename = "something";
#else
std::string expectedWindowsFilename = "C:\\somewhere\\something";
#endif
std::string expectedUnixFilename = "something";
bool res = true;
std::string filename = kwsys::SystemTools::GetFilenameName(windowsFilepath);
if (filename != expectedFilename) {
if (filename != expectedWindowsFilename) {
std::cerr << "GetFilenameName(" << windowsFilepath << ") yielded "
<< filename << " instead of " << expectedFilename << std::endl;
<< filename << " instead of " << expectedWindowsFilename
<< std::endl;
res = false;
}
filename = kwsys::SystemTools::GetFilenameName(unixFilepath);
if (filename != expectedFilename) {
if (filename != expectedUnixFilename) {
std::cerr << "GetFilenameName(" << unixFilepath << ") yielded " << filename
<< " instead of " << expectedFilename << std::endl;
<< " instead of " << expectedUnixFilename << std::endl;
res = false;
}
return res;
......
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