00001 #ifndef __FILE_UTILITIES_H__
00002 #define __FILE_UTILITIES_H__
00003
00004 #include <sys/stat.h>
00005 #include <time.h>
00006
00007 #include <fstream>
00008 #include <functional>
00009
00010 #include <system/directory_abstraction.h>
00011 #include <system/file_abstraction.h>
00012 #include <utilities/binary_data.h>
00013 #include <utilities/exception.h>
00014 #include <utilities/string_utilities.h>
00015 #include <utilities/testing_utilities.h>
00016
00017 #if !defined( WIN32_PLATFORM ) && !defined( NIX_PLATFORM ) && !defined( MINGW_PLATFORM ) && !defined( CYGWIN_PLATFORM )
00018 #define WIN32_PLATFORM
00019 #endif
00020
00021 #ifdef FILE_UTILITIES
00022 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) || defined( CYGWIN_PLATFORM )
00023 #define FILE_UTILITIES_DLL_ENTITY __declspec( dllexport )
00024 #endif
00025
00026 #if defined( NIX_PLATFORM )
00027 #define FILE_UTILITIES_DLL_ENTITY
00028 #endif
00029 #else
00030 #ifdef WIN32_PLATFORM
00031 #define FILE_UTILITIES_DLL_ENTITY __declspec( dllimport )
00032 #pragma comment( lib , "file_utilities.lib" )
00033 #endif
00034
00035 #if defined( CYGWIN_PLATFORM ) || defined( MINGW_PLATFORM )
00036 #define FILE_UTILITIES_DLL_ENTITY __declspec( dllimport )
00037 #endif
00038
00039 #if defined( NIX_PLATFORM )
00040 #define FILE_UTILITIES_DLL_ENTITY
00041 #endif
00042 #endif
00043
00044 namespace nitro
00045 {
00058 class FILE_UTILITIES_DLL_ENTITY File: public nitro::FileAbstraction{
00059
00060 public:
00061
00076 File( void );
00077
00100 File( const char * FilePath , const std::size_t Mode );
00101
00124 File( const std::string & FilePath , const std::size_t Mode );
00125
00126 ALIAS_FUNCTION_2( Open , tstOpen , const char * , std::size_t );
00127 ALIAS_FUNCTION_0( Close , tstClose );
00128
00151 static void SaveBinDataToFile( const BinaryData & BinData , const char * Path );
00152
00175 static void SaveBinDataToFile( const BinaryData & BinData , const std::string & Path );
00176
00199 static void LoadBinDataFromFile( nitro::BinaryData & BinData , const char * Path );
00200
00223 static void LoadBinDataFromFile( nitro::BinaryData & BinData , const std::string & Path );
00224
00247 static tm GetLastModified( const char * Path );
00248
00271 static tm GetLastModified( const std::string & Path );
00272
00295 static bool FileExists( const char * FilePath );
00296
00319 static bool FileExists( const std::string & FilePath );
00320
00339 void Write( const char * Data );
00340
00341 ALIAS_FUNCTION_1( Write , tstWrite , const char * )
00342
00343
00361 void Write( const std::string & Data );
00362
00381 static void ForceCreateFile( const char * FilePath );
00382
00401 static void ForceCreateFile( const std::string & FilePath );
00402
00421 std::size_t FileSize( void );
00422
00434 using FileAbstraction::Write;
00435
00436 private:
00437
00456 File( const File & File ){}
00457
00480 File operator=( const File & File ){return( *this );}
00481 };
00482
00483 ALIAS_FUNCTION_2( File::SaveBinDataToFile , tstSaveBinDataToFile , BinaryData , const char * );
00484
00485 ALIAS_FUNCTION_2( File::LoadBinDataFromFile , tstLoadBinDataFromFile , BinaryData , const char * );
00486
00487 ALIAS_FUNCTION_1R( File::FileExists , aliasFileExists , const char * , bool );
00488
00489 ALIAS_FUNCTION_1( File::ForceCreateFile , aliasForceCreateFile , const char * );
00490
00491 ALIAS_FUNCTION_1R( File::GetLastModified , aliasGetLastModified , const char * , tm );
00492
00501 template< class type , class cont >struct CollectFiles : public std::binary_function< type , cont * , void >{
00502
00525 void operator()( const type & FilePath , cont * PathsContainer ) const
00526 {
00527 PathsContainer->push_back( FilePath );
00528 }
00529 };
00530
00539 template< class type , class cont >struct CollectNone : public std::binary_function< type , cont * , void >{
00540
00563 void operator()( const type & FilePath , cont * PathsContainer ) const
00564 {
00565 }
00566 };
00567
00598 template< class file_func_type , class directory_func_type >void WalkThroughDirectory( const std::string & FolderPath , file_func_type FileFunc , directory_func_type DirectoryFunc , const bool Recursive = true )
00599 {
00600 try
00601 {
00602 nitro::DirectoryAbstraction FileFinder;
00603
00604 std::size_t Again( FileFinder.FindFirst( FolderPath ) );
00605
00606 while( Again == 0 )
00607 {
00608 if( FileFinder.IsDots() )
00609 {
00610 Again = FileFinder.FindNext();
00611 continue;
00612 }
00613
00614 if( FileFinder.IsDirectory() )
00615 {
00616 DirectoryFunc( nitro::FSPath::AddEndSlash( FolderPath ) + FileFinder.GetName() );
00617
00618 if( Recursive )
00619 {
00620 WalkThroughDirectory( nitro::FSPath::AddEndSlash( FolderPath ) + FileFinder.GetName() , FileFunc , DirectoryFunc , Recursive );
00621 }
00622 }
00623 else
00624 {
00625 FileFunc( nitro::FSPath::AddEndSlash( FolderPath ) + FileFinder.GetName() );
00626 }
00627
00628 Again = FileFinder.FindNext();
00629 }
00630 }
00631 catch( nitro::exception e )
00632 {
00633 throw( nitro::exception( std::string( "template< class file_func_type , class directory_func_type >void WalkThroughDirectory( const std::string & FolderPath , file_func_type FileFunc , directory_func_type DirectoryFunc , const bool Recursive = true )::" ) + e.what() , e.code() ) );
00634 }
00635 catch( ... )
00636 {
00637 throw( nitro::exception( std::string( "template< class file_func_type , class directory_func_type >void WalkThroughDirectory( const std::string & FolderPath , file_func_type FileFunc , directory_func_type DirectoryFunc , const bool Recursive = true )::An error occured" ) , 0 ) );
00638 }
00639 }
00640
00667 template< class cont >void CollectFilesFromDirectory( const std::string & FolderPath , cont & c , const bool Recursive = true )
00668 {
00669 try
00670 {
00671 nitro::WalkThroughDirectory(
00672 FolderPath ,
00673 std::bind2nd(
00674 nitro::CollectFiles< std::string , std::vector< std::string > >() , & c
00675 ) ,
00676 std::bind2nd(
00677 nitro::CollectNone< std::string , std::vector< std::string > >() , & c
00678 ) ,
00679 Recursive
00680 );
00681 }
00682 catch( nitro::exception e )
00683 {
00684 throw( nitro::exception( std::string( "template< class cont >void CollectFilesFromDirectory( const std::string & FolderPath , cont & c , const bool Recursive = true )::" ) + e.what() , e.code() ) );
00685 }
00686 catch( ... )
00687 {
00688 throw( nitro::exception( std::string( "template< class cont >void CollectFilesFromDirectory( const std::string & FolderPath , cont & c , const bool Recursive = true )::An error occured" ) , 0 ) );
00689 }
00690 }
00691
00718 template< class cont >void CollectDirectoriesFromDirectory( const std::string & FolderPath , cont & c , const bool Recursive = true )
00719 {
00720 try
00721 {
00722 nitro::WalkThroughDirectory(
00723 FolderPath ,
00724 std::bind2nd(
00725 nitro::CollectNone< std::string , std::vector< std::string > >() , & c
00726 ) ,
00727 std::bind2nd(
00728 nitro::CollectFiles< std::string , std::vector< std::string > >() , & c
00729 ) ,
00730 Recursive
00731 );
00732 }
00733 catch( nitro::exception e )
00734 {
00735 throw( nitro::exception( std::string( "template< class cont >void CollectDirectoriesFromDirectory( const std::string & FolderPath , cont & c , const bool Recursive = true )::" ) + e.what() , e.code() ) );
00736 }
00737 catch( ... )
00738 {
00739 throw( nitro::exception( std::string( "template< class cont >void CollectDirectoriesFromDirectory( const std::string & FolderPath , cont & c , const bool Recursive = true )::An error occured" ) , 0 ) );
00740 }
00741 }
00742 }
00743
00744 #endif