+++ /dev/null
-#include <cpPlugins/OS/FileSystem.h>
-#include <cpPlugins/OS/tinydir.h>
-#include <fstream>
-#include <sstream>
-
-// -------------------------------------------------------------------------
-std::string cpPlugins::OS::FileSystem::
-CanonicalPath( const std::string& path )
-{
- std::string ret = "";
-#ifdef cpExtensions_OS_Windows
- TCHAR buffer[ 4096 ] = TEXT( "" );
- TCHAR** lppPart = { NULL };
- GetFullPathName( path.c_str( ), 4096, buffer, lppPart );
- ret = std::string( buffer );
-#else // cpExtensions_OS_Windows
- char* canonical_path = realpath( path.c_str( ), NULL );
- if( canonical_path != NULL )
- {
- ret = canonical_path;
- free( canonical_path );
-
- } // fi
-#endif // cpExtensions_OS_Windows
- return( ret );
-}
-
-// -------------------------------------------------------------------------
-std::pair< std::string, std::string > cpPlugins::OS::FileSystem::
-SplitPath( const std::string& path )
-{
- size_t found = path.find_last_of( "/\\" );
- return(
- std::pair< std::string, std::string >(
- path.substr( 0, found + 1 ), path.substr( found + 1 )
- )
- );
-}
-
-// -------------------------------------------------------------------------
-std::set< std::string > cpPlugins::OS::FileSystem::
-LoadDirContents(
- const std::string& path, bool recursive,
- const std::string& pattern
- )
-{
- // Prepare inputs and outputs
- std::stringstream path_stream;
- path_stream << path << cpPlugins_PATH_SEPARATOR;
- std::string path_str = path_stream.str( );
- std::set< std::string > output;
-
- // Process data
- tinydir_dir dir;
- if( tinydir_open_sorted( &dir, path.c_str( ) ) != -1 )
- {
- for( auto i = 0; i < dir.n_files; i++ )
- {
- tinydir_file file;
- if( tinydir_readfile_n( &dir, &file , i ) != -1 )
- {
- std::string fname = path_str + file.name;
- if( file.is_dir )
- {
- if( recursive )
- {
- if( fname != "." && fname != ".." )
- {
- auto tmp = Self::LoadDirContents( fname, true );
- output.insert( tmp.begin( ), tmp.end( ) );
-
- } // fi
-
- } // fi
- }
- else
- {
- if( pattern != "" )
- {
- if( Self::Glob( fname, path_str + pattern ) )
- output.insert( fname );
- }
- else
- output.insert( fname );
-
- } // fi
-
- } // fi
-
- } // rof
-
- } // fi
- tinydir_close( &dir );
- return( output );
-}
-
-// -------------------------------------------------------------------------
-int cpPlugins::OS::FileSystem::
-GlobMatch( const char* target, const char* pattern )
-{
- char* pat = const_cast< char* >( pattern );
- char* end = strchr( pat, ']' );
- char* ptr;
- if( *pat == '[' && end != NULL )
- {
- if( end == pat + 1 )
- {
- end = strchr( pat + 2, ']' );
- if( end == NULL )
- return( 0 );
-
- } // fi
-
- if( end - pat == 4 && pat[ 2 ] == '-' && pat[ 1 ] <= pattern[ 3 ] )
- {
- if( *target >= pat[ 1 ] && *target <= pat[ 3 ] )
- return( 5 );
- else
- return( 0 );
-
- } // fi
-
- ptr = strchr( pat + 1, *target );
- if( ptr != NULL && ptr < end )
- return( end - pat + 1 );
- else
- return( 0 );
-
- } // fi
-
- if( *pat == '?' && *target != 0 )
- return( 1 );
- if( *pat == '*' )
- return( 0 );
- if( *target == 0 || *pat == 0 )
- return( 0 );
- if( *target == *pat )
- return( 1 );
- return( 0 );
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::OS::FileSystem::
-Glob( const std::string& line, const std::string& pattern )
-{
- const char* target = line.c_str( );
- const char* pat = pattern.c_str( );
- int gobble;
-
- while( ( gobble = GlobMatch( target, pat ) ) )
- {
- target++;
- pat += gobble;
-
- } // elihw
- if( *target == 0 && *pat == 0 )
- return( true );
- else if( *pat == '*' )
- {
- while( pat[ 1 ] == '*' )
- pat++;
- if( pat[ 1 ] == 0 )
- return( true );
- while( *target )
- if( Glob( target++, pat + 1 ) )
- return( 1 );
-
- } // fi
- return( false );
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::OS::FileSystem::
-Read( std::string& buffer, const std::string& fname )
-{
- buffer = "";
- std::ifstream file_stream( fname.c_str( ) );
- if( !file_stream )
- return( false );
- file_stream.seekg( 0, std::ios::end );
- buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
- file_stream.seekg( 0, std::ios::beg );
- buffer.assign(
- ( std::istreambuf_iterator< char >( file_stream ) ),
- std::istreambuf_iterator< char >( )
- );
- file_stream.close( );
- return( true );
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::OS::FileSystem::
-Write( const std::string& buffer, const std::string& fname )
-{
- std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
- if( !file_stream )
- return( false );
- file_stream.write( buffer.c_str( ), buffer.size( ) );
- return( true );
-}
-
-// eof - $RCSfile$