Go to the documentation of this file.00001 #ifndef __TAR_ABSTRACTION_H__
00002 #define __TAR_ABSTRACTION_H__
00003
00004 #include <utilities/file_utilities.h>
00005 #include <utilities/string_utilities.h>
00006
00007 #if !defined( WIN32_PLATFORM ) && !defined( NIX_PLATFORM ) && !defined( MINGW_PLATFORM ) && !defined( CYGWIN_PLATFORM )
00008 #define WIN32_PLATFORM
00009 #endif
00010
00011 #ifdef TAR_ABSTRACTION
00012 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) || defined( CYGWIN_PLATFORM )
00013 #define TAR_ABSTRACTION_DLL_ENTITY __declspec( dllexport )
00014 #endif
00015
00016 #if defined( NIX_PLATFORM )
00017 #define TAR_ABSTRACTION_DLL_ENTITY
00018 #endif
00019 #else
00020 #if defined( WIN32_PLATFORM )
00021 #define TAR_ABSTRACTION_DLL_ENTITY __declspec( dllimport )
00022 #pragma comment( lib , "tar_abstraction.lib" )
00023 #endif
00024
00025 #if defined( CYGWIN_PLATFORM ) || defined( MINGW_PLATFORM )
00026 #define TAR_ABSTRACTION_DLL_ENTITY __declspec( dllimport )
00027 #endif
00028
00029 #if defined( NIX_PLATFORM )
00030 #define TAR_ABSTRACTION_DLL_ENTITY
00031 #endif
00032 #endif
00033
00034 namespace nitro
00035 {
00046 class TARFileHeader{
00047 public:
00048 char FileName[ 100 ];
00049 char FileMode[ 8 ];
00050 char OwnerId[ 8 ];
00051 char GroupId[ 8 ];
00052 char FileSize[ 12 ];
00053 char LastModificationTime[ 12 ];
00054 char CheckSum[ 8 ];
00055 char LinkType;
00056 char LinkedFileName[ 100 ];
00057 char Format[ 6 ];
00058 char Version[ 2 ];
00059 char OwnerUserName[ 32 ];
00060 char OwnerGroupName[ 32 ];
00061 char DevMajor[ 8 ];
00062 char DevMinor[ 8 ];
00063 char Appendix[ 167 ];
00064
00087 static std::size_t GetCheckSum( const TARFileHeader & Header );
00088
00107 static void SetCheckSum( TARFileHeader & Header );
00108 };
00109
00122 class TAR_ABSTRACTION_DLL_ENTITY UnTARAbstraction{
00123 public:
00138 UnTARAbstraction( void );
00139
00158 UnTARAbstraction( const std::string & FilePath );
00159
00178 void OpenFile( const std::string & FilePath );
00179
00198 std::size_t GetCountOfFiles( void );
00199
00226 void ExtractFile( const std::size_t & FileCursor , const std::string & Folder , bool IgnoreFilePath );
00227
00250 template< class type >void ExtractFile( const std::size_t & FileCursor , type & Storage );
00251
00270 void CloseFile( void );
00271
00282 virtual ~UnTARAbstraction();
00283
00284 private:
00285
00296 std::size_t Cursor;
00297
00308 TARFileHeader Header;
00309
00324 void GotoFirstFile( void );
00325
00340 void ReadHeader( void );
00341
00360 void SetReadCursor( const std::size_t & ReadCursor );
00361
00372 nitro::File LoadedFile;
00373 };
00374
00375 template< class type >void UnTARAbstraction::ExtractFile( const std::size_t & FileCursor , type & Storage )
00376 {
00377 try
00378 {
00379 SetReadCursor( FileCursor );
00380
00381 if( Header.LinkType == '0' )
00382 {
00383 char Buffer[ 512 ];
00384 int FileSize( nitro::Converters::atoi( Header.FileSize , nitro::OCTAL ) );
00385 int ReadBytes( 0 ) , TotalReadBytes( 0 );
00386
00387 do
00388 {
00389 LoadedFile.Read( Buffer , 512 );
00390 ReadBytes = TotalReadBytes + 512 < FileSize ? 512 : FileSize % 512;
00391 if( ReadBytes > 0 )
00392 {
00393 Storage.AppendData( Buffer , ReadBytes );
00394 }
00395 TotalReadBytes += ReadBytes;
00396 }
00397 while( ReadBytes == 512 );
00398 }
00399 }
00400 catch( nitro::exception e )
00401 {
00402 throw( nitro::exception( std::string( "UnTARAbstraction::ExtractFile( const std::size_t & FileCursor , type & Storage )::" ) + e.what() , e.code() ) );
00403 }
00404 catch( ... )
00405 {
00406 throw( nitro::exception( std::string( "UnTARAbstraction::ExtractFile( const std::size_t & FileCursor , type & Storage )::An error occured while extracting file" ) , 1 ) );
00407 }
00408 }
00409 }
00410
00411 #endif