Commit 75f9d65b authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

utils: Filename: Better win32 path handling

parent 366585eb
......@@ -30,9 +30,11 @@
#include <stdexcept>
#ifdef _WIN32
# define DIR_SEPARATOR '\\'
# define DIR_SEPARATOR "\\/"
# define DIR_SEPARATOR_CHAR '\\'
#else
# define DIR_SEPARATOR '/'
# define DIR_SEPARATOR "/"
# define DIR_SEPARATOR_CHAR '/'
#endif
namespace medialibrary
......@@ -64,15 +66,7 @@ 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 );
}
......@@ -94,35 +88,16 @@ std::string directoryName( const std::string& directoryPath )
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
}
if ( pos == std::string::npos )
return {};
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 );
......@@ -130,18 +105,8 @@ std::string fileName(const std::string& filePath)
std::string firstFolder( const std::string& path )
{
size_t offset = 0;
while ( path[offset] == DIR_SEPARATOR
#ifdef _WIN32
|| path[offset] == '/'
#endif
)
offset++;
size_t offset = path.find_first_not_of( DIR_SEPARATOR );
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 );
......@@ -155,11 +120,12 @@ std::string removePath( const std::string& fullPath, const std::string& toRemove
if ( pos == std::string::npos )
return fullPath;
pos += toRemove.length();
while ( fullPath[pos] == DIR_SEPARATOR
// Skip over potentially duplicated DIR_SEPARATOR
while ( ( fullPath[pos] == DIR_SEPARATOR_CHAR
#ifdef _WIN32
|| fullPath[pos] == '/'
#endif
)
) && pos < fullPath.length() )
pos++;
if ( pos >= fullPath.length() )
return {};
......@@ -168,24 +134,24 @@ 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_CHAR
#ifdef _WIN32
&& *path.crbegin() != '/'
#endif
)
path += DIR_SEPARATOR;
path += DIR_SEPARATOR_CHAR;
return path;
}
std::string toFolderPath( const std::string& path )
{
auto p = path;
if ( *p.crbegin() != DIR_SEPARATOR
if ( *p.crbegin() != DIR_SEPARATOR_CHAR
#ifdef _WIN32
&& *p.crbegin() != '/'
#endif
)
p += DIR_SEPARATOR;
p += DIR_SEPARATOR_CHAR;
return p;
}
......
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