2015-03-17 20:59:39 +00:00
# include "filesystemarchive.hpp"
2021-12-14 20:36:11 +00:00
# include <algorithm>
2022-05-24 21:18:21 +00:00
# include <filesystem>
2015-03-17 20:59:39 +00:00
2018-08-14 15:42:41 +00:00
# include <components/debug/debuglog.hpp>
2022-07-16 16:19:56 +00:00
# include <components/files/constrainedfilestream.hpp>
2018-08-14 15:42:41 +00:00
2015-03-17 20:59:39 +00:00
namespace VFS
{
FileSystemArchive : : FileSystemArchive ( const std : : string & path )
: mBuiltIndex ( false )
, mPath ( path )
{
}
void FileSystemArchive : : listResources ( std : : map < std : : string , File * > & out , char ( * normalize_function ) ( char ) )
{
if ( ! mBuiltIndex )
{
2022-05-24 21:18:21 +00:00
typedef std : : filesystem : : recursive_directory_iterator directory_iterator ;
2015-03-17 20:59:39 +00:00
directory_iterator end ;
size_t prefix = mPath . size ( ) ;
if ( mPath . size ( ) > 0 & & mPath [ prefix - 1 ] ! = ' \\ ' & & mPath [ prefix - 1 ] ! = ' / ' )
+ + prefix ;
2022-06-15 23:30:11 +00:00
for ( directory_iterator i ( std : : filesystem : : u8path ( mPath ) ) ; i ! = end ; + + i )
2015-03-17 20:59:39 +00:00
{
2022-05-24 21:18:21 +00:00
if ( std : : filesystem : : is_directory ( * i ) )
2015-03-17 20:59:39 +00:00
continue ;
2022-06-18 21:43:12 +00:00
auto proper = i - > path ( ) . u8string ( ) ;
2015-03-17 20:59:39 +00:00
2022-06-15 23:30:11 +00:00
FileSystemArchiveFile file ( std : : string ( ( char * ) proper . c_str ( ) , proper . size ( ) ) ) ;
2015-03-17 20:59:39 +00:00
std : : string searchable ;
std : : transform ( proper . begin ( ) + prefix , proper . end ( ) , std : : back_inserter ( searchable ) , normalize_function ) ;
2021-06-09 20:25:46 +00:00
const auto inserted = mIndex . insert ( std : : make_pair ( searchable , file ) ) ;
if ( ! inserted . second )
2022-06-15 23:30:11 +00:00
Log ( Debug : : Warning ) < < " Warning: found duplicate file for ' " < < std : : string ( ( char * ) proper . c_str ( ) , proper . size ( ) ) < < " ', please check your file system for two files with the same name in different cases. " ;
2021-06-09 20:25:46 +00:00
else
out [ inserted . first - > first ] = & inserted . first - > second ;
2015-03-17 20:59:39 +00:00
}
mBuiltIndex = true ;
}
2021-06-09 20:25:46 +00:00
else
2015-03-17 20:59:39 +00:00
{
2021-06-09 20:25:46 +00:00
for ( index : : iterator it = mIndex . begin ( ) ; it ! = mIndex . end ( ) ; + + it )
{
out [ it - > first ] = & it - > second ;
}
2015-03-17 20:59:39 +00:00
}
}
2020-12-29 20:45:59 +00:00
bool FileSystemArchive : : contains ( const std : : string & file , char ( * normalize_function ) ( char ) ) const
{
2021-05-01 21:21:21 +00:00
return mIndex . find ( file ) ! = mIndex . end ( ) ;
2020-12-29 20:45:59 +00:00
}
std : : string FileSystemArchive : : getDescription ( ) const
{
return std : : string { " DIR: " } + mPath ;
}
2015-03-17 20:59:39 +00:00
// ----------------------------------------------------------------------------------
FileSystemArchiveFile : : FileSystemArchiveFile ( const std : : string & path )
: mPath ( path )
{
}
Files : : IStreamPtr FileSystemArchiveFile : : open ( )
{
2022-04-10 15:25:26 +00:00
return Files : : openConstrainedFileStream ( mPath ) ;
2015-03-17 20:59:39 +00:00
}
}