Commit 9d1f8e36 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

utils: Replace removeFirstFolder by removePath

This will be used to remove the device mountpoint from full length path
parent 57599e3c
......@@ -63,17 +63,16 @@ std::string firstFolder( const std::string& path )
return path.substr( offset, pos - offset );
}
std::string removeFirstFolder( const std::string& path )
std::string removePath( const std::string& fullPath, const std::string& toRemove )
{
auto f = firstFolder( path );
if ( f.length() == 0 )
return path;
auto pos = path.find( f ) + f.length();
while ( path[pos] == '/' )
if ( toRemove.length() == 0 )
return fullPath;
auto pos = fullPath.find( toRemove ) + toRemove.length();
while ( fullPath[pos] == '/' )
pos++;
if ( pos >= path.length() )
if ( pos >= fullPath.length() )
return {};
return path.substr( pos );
return fullPath.substr( pos );
}
}
......
......@@ -33,7 +33,7 @@ namespace file
std::string directory( const std::string& filePath );
std::string fileName( const std::string& filePath );
std::string firstFolder( const std::string& path );
std::string removeFirstFolder( const std::string& path );
std::string removePath( const std::string& fullPath, const std::string& toRemove );
}
}
......@@ -153,7 +153,7 @@ public:
{
auto it = m_dirs.find( subFolder );
assert( it != end( m_dirs ) );
auto remainingPath = utils::file::removeFirstFolder( filePath );
auto remainingPath = utils::file::removePath( filePath, subFolder );
it->second->addFile( remainingPath );
}
}
......@@ -174,7 +174,7 @@ public:
{
auto it = m_dirs.find( subFolder );
assert( it != end( m_dirs ) );
auto remainingPath = utils::file::removeFirstFolder( folder );
auto remainingPath = utils::file::removePath( folder, subFolder );
it->second->addFolder( remainingPath, lastModif );
}
}
......@@ -194,7 +194,7 @@ public:
{
auto it = m_dirs.find( subFolder );
assert( it != end( m_dirs ) );
auto remainingPath = utils::file::removeFirstFolder( filePath );
auto remainingPath = utils::file::removePath( filePath, subFolder );
it->second->removeFile( remainingPath );
}
}
......@@ -212,7 +212,7 @@ public:
{
auto it = m_dirs.find( subFolder );
assert( it != end( m_dirs ) );
auto remainingPath = utils::file::removeFirstFolder( filePath );
auto remainingPath = utils::file::removePath( filePath, subFolder );
return it->second->file( remainingPath );
}
}
......@@ -230,7 +230,7 @@ public:
{
auto it = m_dirs.find( subFolder );
assert( it != end( m_dirs ) );
auto remainingPath = utils::file::removeFirstFolder( path );
auto remainingPath = utils::file::removePath( path, subFolder );
return it->second->directory( remainingPath );
}
}
......@@ -250,7 +250,7 @@ public:
{
auto it = m_dirs.find( subFolder );
assert( it != end( m_dirs ) );
auto remainingPath = utils::file::removeFirstFolder( path );
auto remainingPath = utils::file::removePath( path, subFolder );
it->second->removeFolder( remainingPath );
}
}
......@@ -266,7 +266,7 @@ public:
{
auto it = m_dirs.find( subFolder );
assert( it != end( m_dirs ) );
auto remainingPath = utils::file::removeFirstFolder( path );
auto remainingPath = utils::file::removePath( path, subFolder );
it->second->addMountpoint( remainingPath );
}
}
......
......@@ -60,15 +60,15 @@ TEST( FsUtils, firstFolder )
ASSERT_EQ( "", utils::file::firstFolder( "/foo.bar" ) );
}
TEST( FsUtils, removeSlice )
TEST( FsUtils, removePath )
{
ASSERT_EQ( "bar/", utils::file::removeFirstFolder( "f00/bar/" ) );
ASSERT_EQ( "bar/", utils::file::removeFirstFolder( "/f00/bar/" ) );
ASSERT_EQ( "bar", utils::file::removeFirstFolder( "f00/bar" ) );
ASSERT_EQ( "bar", utils::file::removeFirstFolder( "/f00/bar" ) );
ASSERT_EQ( "bar", utils::file::removeFirstFolder( "////f00/bar" ) );
ASSERT_EQ( "bar", utils::file::removeFirstFolder( "/f00///bar" ) );
ASSERT_EQ( "bar", utils::file::removeFirstFolder( "bar" ) );
ASSERT_EQ( "", utils::file::removeFirstFolder( "bar/" ) );
ASSERT_EQ( "", utils::file::removeFirstFolder( "/f00/") );
ASSERT_EQ( "bar/", utils::file::removePath( "f00/bar/", "f00" ) );
ASSERT_EQ( "bar/", utils::file::removePath( "/f00/bar/", "/f00" ) );
ASSERT_EQ( "bar", utils::file::removePath( "f00/bar", "f00" ) );
ASSERT_EQ( "bar", utils::file::removePath( "/f00/bar", "/f00" ) );
ASSERT_EQ( "bar", utils::file::removePath( "////f00/bar", "/f00" ) );
ASSERT_EQ( "bar", utils::file::removePath( "/f00///bar", "/f00" ) );
ASSERT_EQ( "bar", utils::file::removePath( "bar", "" ) );
ASSERT_EQ( "", utils::file::removePath( "bar/", "bar" ) );
ASSERT_EQ( "", utils::file::removePath( "/f00/", "/f00/" ) );
}
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