Commit 2d69edad authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

utils: Filename: Handle both backward & forward slashes on win32

parent 505f48f6
......@@ -53,21 +53,50 @@ std::string directory( const std::string& filePath )
{
auto pos = filePath.find_last_of( DIR_SEPARATOR );
if ( pos == std::string::npos )
#ifdef _WIN32
{
pos = filePath.find_last_of( '/' );
if ( pos == std::string::npos )
return {};
}
#else
return {};
#endif
return filePath.substr( 0, pos + 1 );
}
std::string parentDirectory( const std::string& path )
{
auto pos = path.find_last_of( DIR_SEPARATOR );
#ifdef _WIN32
// If the \ appears right after the drive letter, or there's no backward /
if ( ( pos == 2 && path[1] == ':' ) || pos == std::string::npos )
{
// Fall back to forward slashes
pos = path.find_last_of( '/' );
}
#endif
if ( pos == path.length() - 1 )
{
#ifdef _WIN32
auto oldPos = pos;
#endif
pos = path.find_last_of( DIR_SEPARATOR, pos - 1 );
#ifdef _WIN32
if ( ( pos == 2 && path[1] == ':' ) || pos == std::string::npos )
pos = path.find_last_of( '/', oldPos - 1 );
#endif
}
return path.substr( 0, pos + 1 );
}
std::string fileName(const std::string& filePath)
{
auto pos = filePath.find_last_of( DIR_SEPARATOR );
#ifdef _WIN32
if ( pos == std::string::npos )
pos = filePath.find_last_of( '/' );
#endif
if ( pos == std::string::npos )
return filePath;
return filePath.substr( pos + 1 );
......@@ -76,9 +105,17 @@ std::string fileName(const std::string& filePath)
std::string firstFolder( const std::string& path )
{
size_t offset = 0;
while ( path[offset] == DIR_SEPARATOR )
while ( path[offset] == DIR_SEPARATOR
#ifdef _WIN32
|| path[offset] == '/'
#endif
)
offset++;
auto pos = path.find_first_of( DIR_SEPARATOR, offset );
#ifdef _WIN32
if ( pos == std::string::npos )
pos = path.find_first_of( '/', offset );
#endif
if ( pos == std::string::npos )
return {};
return path.substr( offset, pos - offset );
......@@ -89,7 +126,11 @@ std::string removePath( const std::string& fullPath, const std::string& toRemove
if ( toRemove.length() == 0 )
return fullPath;
auto pos = fullPath.find( toRemove ) + toRemove.length();
while ( fullPath[pos] == DIR_SEPARATOR )
while ( fullPath[pos] == DIR_SEPARATOR
#ifdef _WIN32
|| fullPath[pos] == '/'
#endif
)
pos++;
if ( pos >= fullPath.length() )
return {};
......@@ -98,7 +139,11 @@ std::string removePath( const std::string& fullPath, const std::string& toRemove
std::string& toFolderPath( std::string& path )
{
if ( *path.crbegin() != DIR_SEPARATOR )
if ( *path.crbegin() != DIR_SEPARATOR
#ifdef _WIN32
&& *path.crbegin() != '/'
#endif
)
path += DIR_SEPARATOR;
return path;
}
......@@ -106,7 +151,11 @@ std::string& toFolderPath( std::string& path )
std::string toFolderPath( const std::string& path )
{
auto p = path;
if ( *p.crbegin() != DIR_SEPARATOR )
if ( *p.crbegin() != DIR_SEPARATOR
#ifdef _WIN32
&& *p.crbegin() != '/'
#endif
)
p += DIR_SEPARATOR;
return p;
}
......
......@@ -85,4 +85,10 @@ TEST( FsUtils, parentFolder )
ASSERT_EQ( "/a/b/", utils::file::parentDirectory( "/a/b/c/" ) );
ASSERT_EQ( "/a/b/", utils::file::parentDirectory( "/a/b/c" ) );
ASSERT_EQ( "", utils::file::parentDirectory( "" ) );
#ifdef _WIN32
ASSERT_EQ( "C:\\a/b/", utils::file::parentDirectory( "C:\\a/b/c" ) );
ASSERT_EQ( "C:/a/b/", utils::file::parentDirectory( "C:/a/b/c\\" ) );
ASSERT_EQ( "C:\\a\\b\\", utils::file::parentDirectory( "C:\\a\\b\\c\\" ) );
ASSERT_EQ( "C:\\a\\b\\", utils::file::parentDirectory( "C:\\a\\b\\c" ) );
#endif
}
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