@ -205,34 +205,46 @@ int Filesystem::listdir (std::string dir, std::vector<std::string> &files)
return 0 ;
return 0 ;
}
}
int Filesystem : : listdir_recursive ( std : : string dir , std : : map < std : : string , bool > & files ,
// prefix is the top-level dir where we started recursing
int depth /* = 10 */ , std : : string prefix /* = "" */ )
// path is the relative path under the prefix; must be empty or end in a '/'
// files is the output list of files and directories (bool == true for dir)
// depth is the remaining dir depth to recurse into. function returns -1 if
// we haven't finished recursing when we run out of depth.
// include_prefix controls whether the directory where we started recursing is
// included in the filenames returned in files.
static int listdir_recursive_impl ( std : : string prefix , std : : string path ,
std : : map < std : : string , bool > & files , int depth , bool include_prefix )
{
{
int err ;
if ( depth < 0 )
if ( depth < 0 )
return - 1 ;
return - 1 ;
if ( prefix = = " " )
std : : string prefixed_path = prefix + " / " + path ;
prefix = dir ;
std : : vector < std : : string > curdir_files ;
std : : vector < std : : string > tmp ;
int err = Filesystem : : listdir ( prefixed_path , curdir_files ) ;
err = listdir ( dir , tmp ) ;
if ( err )
if ( err )
return err ;
return err ;
for ( auto file = tmp. begin ( ) ; file ! = tmp . end ( ) ; + + file )
for ( auto file = curdir_files. begin ( ) ; file ! = curdir_files . end ( ) ; + + file )
{
{
if ( * file = = " . " | | * file = = " .. " )
if ( * file = = " . " | | * file = = " .. " )
continue ;
continue ;
std : : string rel_path = prefix + " / " + * file ;
std : : string prefixed_file = prefixed_path + * file ;
if ( isdir ( rel_path ) )
std : : string path_file = path + * file ;
if ( Filesystem : : isdir ( prefixed_file ) )
{
{
files . insert ( std : : pair < std : : string , bool > ( rel_path , true ) ) ;
files . insert ( std : : pair < std : : string , bool > ( include_prefix ? prefixed_file : path_file , true ) ) ;
err = listdir_recursive ( dir + " / " + * file , files , depth - 1 , rel_path ) ;
err = listdir_recursive _impl( prefix , path_file + " / " , files , depth - 1 , include_prefix ) ;
if ( err )
if ( err )
return err ;
return err ;
}
}
else
else
{
{
files . insert ( std : : pair < std : : string , bool > ( rel_path , false ) ) ;
files . insert ( std : : pair < std : : string , bool > ( include_prefix ? prefixed_file : path_file , false ) ) ;
}
}
}
}
return 0 ;
return 0 ;
}
}
int Filesystem : : listdir_recursive ( std : : string dir , std : : map < std : : string , bool > & files ,
int depth /* = 10 */ , bool include_prefix /* = true */ )
{
return listdir_recursive_impl ( dir , " " , files , depth , include_prefix ) ;
}