Commit 1a264240 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

utils: Add a parentDirectory helper

parent 60a51cc4
......@@ -44,6 +44,14 @@ std::string directory( const std::string& filePath )
return filePath.substr( 0, pos + 1 );
}
std::string parentDirectory( const std::string& path )
{
auto pos = path.find_last_of( '/' );
if ( pos == path.length() - 1 )
pos = path.find_last_of( '/', pos - 1 );
return path.substr( 0, pos + 1 );
}
std::string fileName(const std::string& filePath)
{
auto pos = filePath.find_last_of( '/' );
......
......@@ -34,6 +34,7 @@ namespace file
std::string fileName( const std::string& filePath );
std::string firstFolder( const std::string& path );
std::string removePath( const std::string& fullPath, const std::string& toRemove );
std::string parentDirectory( const std::string& path );
}
}
......@@ -73,3 +73,10 @@ TEST( FsUtils, removePath )
ASSERT_EQ( "", utils::file::removePath( "bar/", "bar" ) );
ASSERT_EQ( "", utils::file::removePath( "/f00/", "/f00/" ) );
}
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( "" ) );
}
Supports Markdown
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