00001 #ifndef __FILE_UTILITIES_CPP__
00002 #define __FILE_UTILITIES_CPP__
00003
00004 #include <utilities/directory_utilities.h>
00005 #include <utilities/file_utilities.h>
00006 #include <utilities/string_utilities.h>
00007
00008 namespace nitro
00009 {
00010 File::File( void )
00011 {
00012 }
00013
00014 File::File( const char * FilePath , const std::size_t Mode )
00015 {
00016 try
00017 {
00018 nitro::FileAbstraction::Open( FilePath , Mode );
00019 }
00020 catch( nitro::exception e )
00021 {
00022 throw( nitro::exception( std::string( "File::File( const char * FilePath , const std::size_t Mode )::" ) + e.what() , e.code() ) );
00023 }
00024 catch( ... )
00025 {
00026 throw( nitro::exception( std::string( "File::File( const char * FilePath , const std::size_t Mode )::An error occured" ) , 0 ) );
00027 }
00028 }
00029
00030 void File::SaveBinDataToFile( const BinaryData & BinData , const char * Path )
00031 {
00032 try
00033 {
00034 if( Path == NULL || std::string( Path ).size() == 0 )
00035 {
00036 throw( nitro::exception( std::string( "File path was not specified" ) , 1 ) );
00037 }
00038
00039 nitro::FileAbstraction File;
00040
00041 File::ForceCreateFile( Path );
00042
00043 File.Open( Path , nitro::FA_FILE_WRITE | nitro::FA_FILE_BINARY | nitro::FA_FILE_TRUNCATE );
00044
00045 File.Write( BinData.GetBuffer() , ( unsigned int )BinData.GetBufferLength() );
00046 }
00047 catch( nitro::exception e )
00048 {
00049 throw( nitro::exception( std::string( "void File::SaveBinDataToFile( const BinaryData & BinData , const char * Path )::" ) + e.what() , e.code() ) );
00050 }
00051 catch( ... )
00052 {
00053 throw( nitro::exception( std::string( "void File::SaveBinDataToFile( const BinaryData & BinData , const char * Path )::An error occured" ) , 0 ) );
00054 }
00055 }
00056
00057 void File::LoadBinDataFromFile( BinaryData & BinData , const char * Path )
00058 {
00059 try
00060 {
00061 nitro::FileAbstraction File;
00062
00063 File.Open( Path , nitro::FA_FILE_READ );
00064
00065 char Buffer[ 1024 ];
00066
00067 std::size_t ReadBytes( 0 );
00068
00069 do
00070 {
00071 ReadBytes = File.Read( Buffer , 1024 );
00072 BinData.AppendData( Buffer , ReadBytes );
00073 }
00074 while( ReadBytes == 1024 );
00075 }
00076 catch( nitro::exception e )
00077 {
00078 throw( nitro::exception( std::string( "void File::LoadBinDataFromFile( BinaryData & BinData , const char * Path )::" ) + e.what() , e.code() ) );
00079 }
00080 catch( ... )
00081 {
00082 throw( nitro::exception( std::string( "void File::LoadBinDataFromFile( BinaryData & BinData , const char * Path )::An error occured" ) , 0 ) );
00083 }
00084 }
00085
00086 tm File::GetLastModified( const char * Path )
00087 {
00088 try
00089 {
00090 struct stat FileInfo;
00091
00092 if( stat( Path , & FileInfo ) != 0 )
00093 {
00094 throw( nitro::exception( std::string( "GetLastModified( const char * Path )::An error occured while getting info for file/folder" ) , 0 ) );
00095 }
00096
00097 struct tm ModificationDate;
00098
00099 ModificationDate = * localtime( & FileInfo.st_mtime );
00100
00101 return( ModificationDate );
00102 }
00103 catch( nitro::exception e )
00104 {
00105 throw( nitro::exception( std::string( "tm File::GetLastModified( const char * Path )::" ) + e.what() , e.code() ) );
00106 }
00107 catch( ... )
00108 {
00109 throw( nitro::exception( std::string( "tm File::GetLastModified( const char * Path )::An error occured" ) , 0 ) );
00110 }
00111 }
00112
00113 bool File::FileExists( const char * FilePath )
00114 {
00115 try
00116 {
00117 if( strlen( FilePath ) == 0 )return( false );
00118
00119 std::ifstream file( FilePath );
00120
00121 return( file.good() );
00122 }
00123 catch( nitro::exception e )
00124 {
00125 throw( nitro::exception( std::string( "bool File::FileExists( const char * FilePath )::" ) + e.what() , e.code() ) );
00126 }
00127 catch( ... )
00128 {
00129 throw( nitro::exception( std::string( "bool File::FileExists( const char * FilePath )::An error occured" ) , 0 ) );
00130 }
00131 }
00132
00133 void File::Write( const char * Data )
00134 {
00135 try
00136 {
00137 nitro::FileAbstraction::Write( Data , strlen( Data ) );
00138 }
00139 catch( nitro::exception e )
00140 {
00141 throw( nitro::exception( std::string( "void File::Write( const char * Data )::" ) + e.what() , e.code() ) );
00142 }
00143 catch( ... )
00144 {
00145 throw( nitro::exception( std::string( "void File::Write( const char * Data )::An error occured" ) , 0 ) );
00146 }
00147 }
00148
00149 void File::ForceCreateFile( const char * FilePath )
00150 {
00151 try
00152 {
00153 std::string ClearPath( nitro::FSPath::ExtractFilePath( std::string( FilePath ) ) );
00154
00155
00156 Directory::ForceCreateDirectory( ClearPath.c_str() );
00157
00158
00159 File TmpFile( FilePath , nitro::FA_FILE_BINARY | nitro::FA_FILE_WRITE );
00160 }
00161 catch( nitro::exception e )
00162 {
00163 throw( nitro::exception( std::string( "void File::ForceCreateFile( const char * FilePath )::" ) + e.what() , e.code() ) );
00164 }
00165 catch( ... )
00166 {
00167 throw( nitro::exception( std::string( "void File::ForceCreateFile( const char * FilePath )::An error occured" ) , 0 ) );
00168 }
00169 }
00170
00171 std::size_t File::FileSize( void )
00172 {
00173 try
00174 {
00175 std::size_t CurrentPosition( nitro::FileAbstraction::Tell() );
00176
00177 nitro::FileAbstraction::Seek( 0 , nitro::FA_FILE_END );
00178
00179 std::size_t EndPosition( nitro::FileAbstraction::Tell() );
00180
00181 FileAbstraction::Seek( CurrentPosition , nitro::FA_FILE_BEGIN );
00182
00183 return( EndPosition );
00184 }
00185 catch( nitro::exception e )
00186 {
00187 throw( nitro::exception( std::string( "std::size_t File::FileSize( void )::" ) + e.what() , e.code() ) );
00188 }
00189 catch( ... )
00190 {
00191 throw( nitro::exception( std::string( "std::size_t File::FileSize( void )::An error occured" ) , 0 ) );
00192 }
00193 }
00194
00195 File::File( const std::string & FilePath , const std::size_t Mode )
00196 {
00197 try
00198 {
00199 FileAbstraction::Open( FilePath.c_str() , Mode );
00200 }
00201 catch( nitro::exception e )
00202 {
00203 throw( nitro::exception( std::string( "File::File( const std::string & FilePath , const std::size_t Mode )::" ) + e.what() , e.code() ) );
00204 }
00205 catch( ... )
00206 {
00207 throw( nitro::exception( std::string( "File::File( const std::string & FilePath , const std::size_t Mode )::An error occured" ) , 0 ) );
00208 }
00209 }
00210
00211 void File::SaveBinDataToFile( const BinaryData & BinData , const std::string & Path )
00212 {
00213 try
00214 {
00215 if( Path.size() == 0 )
00216 {
00217 throw( nitro::exception( std::string( "File path was not specified" ) , 1 ) );
00218 }
00219
00220 SaveBinDataToFile( BinData , Path.c_str() );
00221 }
00222 catch( nitro::exception e )
00223 {
00224 throw( nitro::exception( std::string( "File::SaveBinDataToFile( const BinaryData & BinData , const std::string & Path )::" ) + e.what() , e.code() ) );
00225 }
00226 catch( ... )
00227 {
00228 throw( nitro::exception( std::string( "File::SaveBinDataToFile( const BinaryData & BinData , const std::string & Path )::An error occured" ) , 0 ) );
00229 }
00230 }
00231
00232 void File::LoadBinDataFromFile( BinaryData & BinData , const std::string & Path )
00233 {
00234 try
00235 {
00236 LoadBinDataFromFile( BinData , Path.c_str() );
00237 }
00238 catch( nitro::exception e )
00239 {
00240 throw( nitro::exception( std::string( "File::LoadBinDataFromFile( BinaryData & BinData , const std::string & Path )::" ) + e.what() , e.code() ) );
00241 }
00242 catch( ... )
00243 {
00244 throw( nitro::exception( std::string( "File::LoadBinDataFromFile( BinaryData & BinData , const std::string & Path )::An error occured" ) , 0 ) );
00245 }
00246 }
00247
00248 tm File::GetLastModified( const std::string & Path )
00249 {
00250 try
00251 {
00252 return( GetLastModified( Path.c_str() ) );
00253 }
00254 catch( nitro::exception e )
00255 {
00256 throw( nitro::exception( std::string( "File::GetLastModified( const std::string & Path )::" ) + e.what() , e.code() ) );
00257 }
00258 catch( ... )
00259 {
00260 throw( nitro::exception( std::string( "File::GetLastModified( const std::string & Path )::An error occured" ) , 0 ) );
00261 }
00262 }
00263
00264 bool File::FileExists( const std::string & FilePath )
00265 {
00266 try
00267 {
00268 return( FileExists( FilePath.c_str() ) );
00269 }
00270 catch( nitro::exception e )
00271 {
00272 throw( nitro::exception( std::string( "File::FileExists( const std::string & FilePath )::" ) + e.what() , e.code() ) );
00273 }
00274 catch( ... )
00275 {
00276 throw( nitro::exception( std::string( "File::FileExists( const std::string & FilePath )::An error occured" ) , 0 ) );
00277 }
00278 }
00279
00280 void File::Write( const std::string & Data )
00281 {
00282 try
00283 {
00284 Write( Data.c_str() );
00285 }
00286 catch( nitro::exception e )
00287 {
00288 throw( nitro::exception( std::string( "File::Write( const std::string & Data )::" ) + e.what() , e.code() ) );
00289 }
00290 catch( ... )
00291 {
00292 throw( nitro::exception( std::string( "File::Write( const std::string & Data )::An error occured" ) , 0 ) );
00293 }
00294 }
00295
00296 void File::ForceCreateFile( const std::string & FilePath )
00297 {
00298 try
00299 {
00300 ForceCreateFile( FilePath.c_str() );
00301 }
00302 catch( nitro::exception e )
00303 {
00304 throw( nitro::exception( std::string( "File::ForceCreateFile( const std::string & FilePath )::" ) + e.what() , e.code() ) );
00305 }
00306 catch( ... )
00307 {
00308 throw( nitro::exception( std::string( "File::ForceCreateFile( const std::string & FilePath )::An error occured" ) , 0 ) );
00309 }
00310 }
00311
00312
00313
00314
00315 BEGIN_TESTING_SECTION()
00316
00317 ENABLE_CLASS_TESTING( File )
00318
00319 CLASS_MEMBER_FUNCTION_TESTING_2( File , tstOpen , tstOpen , const char * , std::size_t , void , NO_RET )
00320 CLASS_MEMBER_FUNCTION_TESTING_0( File , tstClose , tstClose , void , NO_RET )
00321
00322 CLASS_MEMBER_FUNCTION_TESTING_1( File , tstWrite , tstWrite , const char * , void , NO_RET )
00323 CLASS_MEMBER_FUNCTION_TESTING_0( File , FileSize , tstFileSize , std::size_t , std::size_t )
00324
00325
00326
00327 #ifdef ENABLE_AUTOTESTING
00328 char StrTime[ 1024 ];
00329 const char * tmToStr( tm t )
00330 {
00331 memset( ( void * )StrTime, 0 , 1024 );
00332 sprintf( ( char * )StrTime , "%d%d%d%d%d%d" , 1900 + t.tm_year , t.tm_mon , t.tm_mday , t.tm_hour , t.tm_min , t.tm_sec );
00333 return( StrTime );
00334 }
00335 #endif
00336
00337 FUNCTION_TESTING_1( aliasForceCreateFile , tstForceCreateFile , const char * , void , NO_RET )
00338 FUNCTION_TESTING_1( aliasFileExists , tstFileExists , const char * , bool , bool )
00339 FUNCTION_TESTING_1( aliasGetLastModified , _tstGetLastModified , const char * , tm , tm )
00340 FUNCTION_RESULT_MAP_1( _tstGetLastModified , tstGetLastModified , tmToStr , const char * , tm , const char * )
00341
00342 END_TESTING_SECTION()
00343 }
00344
00345 #endif