Class for cross-platform file processing. More...
#include <system/file_abstraction.h>
Inherited by nitro::File.
Public Member Functions | |
FileAbstraction (void) | |
void | Open (const char *FilePath, const std::size_t theMode) |
ALIAS_FUNCTION_2 (Open, aliasOpen, const char *, std::size_t) | |
void | Open (const std::string &FilePath, const std::size_t Mode) |
void | Seek (std::size_t Offset, std::size_t theMode) |
std::size_t | Tell (void) |
void | Write (const char *Buffer, std::size_t BufferLength) |
std::size_t | Read (char *Buffer, std::size_t BufferLength) |
void | Close (void) |
virtual | ~FileAbstraction () |
Static Public Member Functions | |
static void | DeleteFile (const char *FilePath) |
static void | DeleteFile (const std::string &FilePath) |
static void | RenameFile (const char *FilePathFrom, const char *FilePathTo) |
static void | RenameFile (const std::string &FilePathFrom, const std::string &FilePathTo) |
static int | GetOwner (const std::string &FilePath) |
static int | GetGroup (const std::string &FilePath) |
Private Member Functions | |
FileAbstraction (const FileAbstraction &) | |
FileAbstraction | operator= (const FileAbstraction &) |
Private Attributes | |
std::size_t | Mode |
void * | FileData |
Class for cross-platform file processing.
Definition at line 179 of file file_abstraction.h.
nitro::FileAbstraction::FileAbstraction | ( | void | ) |
Default constructor.
Definition at line 19 of file file_abstraction.cpp.
References FileData.
{ FileData = NULL; }
nitro::FileAbstraction::~FileAbstraction | ( | ) | [virtual] |
Destructor (virtual).
Definition at line 344 of file file_abstraction.cpp.
References Close().
{ try { Close(); } catch( ... ) { } }
nitro::FileAbstraction::FileAbstraction | ( | const FileAbstraction & | ) | [inline, private] |
nitro::FileAbstraction::ALIAS_FUNCTION_2 | ( | Open | , | |
aliasOpen | , | |||
const char * | , | |||
std::size_t | ||||
) |
void nitro::FileAbstraction::Close | ( | void | ) |
Function closes opened file.
nitro::exception | Throws exception wich contains error description. |
Definition at line 315 of file file_abstraction.cpp.
References nitro::exception::code(), FileData, and nitro::exception::what().
Referenced by nitro::UnTARAbstraction::CloseFile(), nitro::CSVFile::CloseFile(), Open(), nitro::LogStream::Release(), and ~FileAbstraction().
{ try { if( FileData ) { #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) CloseHandle( * ( ( HANDLE * )FileData ) ); delete ( HANDLE * ) FileData; #endif #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM ) fclose( ( FILE * )FileData ); #endif FileData = NULL; } } catch( nitro::exception e ) { throw( nitro::exception( std::string( "FileAbstraction::Close( void )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "FileAbstraction::Close( void )::An error occured" ) , 0 ) ); } }
void nitro::FileAbstraction::DeleteFile | ( | const char * | FilePath | ) | [static] |
Function deletes file.
FilePath | - Path to the deleting file. |
nitro::exception | Throws exception wich contains error description. |
Definition at line 355 of file file_abstraction.cpp.
References nitro::exception::code(), and nitro::exception::what().
Referenced by DeleteFile().
{ try { #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) if( ::DeleteFileA( FilePath ) == 0 ) { throw( nitro::exception( std::string( "An error occured while deleting file " ) + FilePath , 0 ) ); } #endif #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM ) if( unlink( FilePath ) != 0 ) { throw( nitro::exception( std::string( "An error occured while deleting file " ) + FilePath , 0 ) ); } #endif } catch( nitro::exception e ) { throw( nitro::exception( std::string( "FileAbstraction::DeleteFile( const char * FilePath )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "FileAbstraction::DeleteFile( const char * FilePath )::An error occured" ) , 0 ) ); } }
void nitro::FileAbstraction::DeleteFile | ( | const std::string & | FilePath | ) | [static] |
Function deletes file.
FilePath | - Path to the deleting file. |
nitro::exception | Throws exception wich contains error description. |
Definition at line 425 of file file_abstraction.cpp.
References nitro::exception::code(), DeleteFile(), and nitro::exception::what().
{ try { DeleteFile( FilePath.c_str() ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "FileAbstraction::DeleteFile( const std::string & FilePath )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "FileAbstraction::DeleteFile( const std::string & FilePath )::An error occured" ) , 0 ) ); } }
int nitro::FileAbstraction::GetGroup | ( | const std::string & | FilePath | ) | [static] |
Function returns file group's id.
FilePath | - Path to the file. |
nitro::exception | Throws an exception of that type with the error description. |
Definition at line 489 of file file_abstraction.cpp.
References nitro::exception::code(), and nitro::exception::what().
{ try { #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) struct _stat s; int Result( _stat( Path.c_str() , & s ) ); #endif #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM ) struct stat s; int Result( stat( Path.c_str() , & s ) ); #endif if( Result == 0 ) { return( s.st_gid ); } else { throw( nitro::exception( "Path does not exists" , 0 ) ); } } catch( nitro::exception e ) { throw( nitro::exception( std::string( "FileAbstraction::GetGroup( const std::string & Path )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "FileAbstraction::GetGroup( const std::string & Path )::An error occured" ) , 0 ) ); } }
int nitro::FileAbstraction::GetOwner | ( | const std::string & | FilePath | ) | [static] |
Function returns file owner's id.
FilePath | - Path to the file. |
nitro::exception | Throws an exception of that type with the error description. |
Definition at line 457 of file file_abstraction.cpp.
References nitro::exception::code(), and nitro::exception::what().
{ try { #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) struct _stat s; int Result( _stat( Path.c_str() , & s ) ); #endif #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM ) struct stat s; int Result( stat( Path.c_str() , & s ) ); #endif if( Result == 0 ) { return( s.st_uid ); } else { throw( nitro::exception( "Path does not exists" , 0 ) ); } } catch( nitro::exception e ) { throw( nitro::exception( std::string( "FileAbstraction::GetOwner( const std::string & Path )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "FileAbstraction::GetOwner( const std::string & Path )::An error occured" ) , 0 ) ); } }
void nitro::FileAbstraction::Open | ( | const char * | FilePath, | |
const std::size_t | theMode | |||
) |
Function opens file FilePath with mode Mode.
FilePath | - Path to the opening file. | |
theMode | - File processing mode. |
nitro::exception | Throws exception wich contains error description. |
Definition at line 24 of file file_abstraction.cpp.
References Close(), nitro::exception::code(), nitro::FA_FILE_APPEND, nitro::FA_FILE_BEGIN, nitro::FA_FILE_END, nitro::FA_FILE_READ, nitro::FA_FILE_TRUNCATE, nitro::FA_FILE_WRITE, FileData, Mode, Seek(), and nitro::exception::what().
Referenced by nitro::File::File(), nitro::File::LoadBinDataFromFile(), Open(), nitro::UnTARAbstraction::OpenFile(), nitro::CSVFile::OpenFile(), nitro::LogStream::Reset(), nitro::File::SaveBinDataToFile(), nitro::XMLFile::SaveXML(), and nitro::UnZIPAbstraction::UnZIPFile().
{ try { Close(); Mode = theMode; if( !( Mode & FA_FILE_READ ) && !( Mode & FA_FILE_WRITE ) ) { // ни чтение ни запись - ошибка. // neither reading nor writing - error throw( nitro::exception( "Neither reading nor writing modes specified - error" , 1 ) ); } #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) FileData = ( void * ) new HANDLE(); unsigned int Access( 0 ); if( Mode & FA_FILE_READ ) Access |= GENERIC_READ; if( Mode & FA_FILE_WRITE ) Access |= GENERIC_WRITE; if( Mode & FA_FILE_APPEND ) Access |= GENERIC_WRITE; unsigned int Creation( 0 ); if( Mode & FA_FILE_READ && Mode & FA_FILE_WRITE ) { Creation |= OPEN_ALWAYS; } else { if( Mode & FA_FILE_READ ) Creation |= OPEN_EXISTING; if( Mode & FA_FILE_WRITE ) Creation |= OPEN_ALWAYS; } if( Mode & FA_FILE_TRUNCATE ) { // file must exist to be truncated std::ifstream file( FilePath ); if( file.good() ) { Creation |= TRUNCATE_EXISTING; } } * ( ( HANDLE * )FileData ) = CreateFile( FilePath , Access , ( DWORD )NULL , NULL , Creation , ( DWORD )NULL , NULL ); if( * ( ( HANDLE * )FileData ) == INVALID_HANDLE_VALUE ) { throw( nitro::exception( std::string( "An error occured while opening file " ) + FilePath , 1 ) ); } #endif #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM ) std::string PlatformMode( "" ); if( Mode & FA_FILE_READ && Mode & FA_FILE_WRITE ) { // чтение и запись одновременно // both reading and writing if( Mode & FA_FILE_TRUNCATE ) { PlatformMode += "w+"; } else { PlatformMode += "a+"; } } if( Mode & FA_FILE_READ && !( Mode & FA_FILE_WRITE ) ) { // только чтение // read only PlatformMode += "r"; } if( !( Mode & FA_FILE_READ ) && Mode & FA_FILE_WRITE ) { // только запись // writing only if( Mode & FA_FILE_TRUNCATE ) { PlatformMode += "w"; } else { std::ifstream file( FilePath ); if( file.good() ) { // открываем уже существующий файл // opening already existing file PlatformMode += "r+"; } else { // открываем несуществующий файл // opening not existing file PlatformMode += "w"; } } } PlatformMode += "b"; FileData = ( void * )fopen( FilePath , PlatformMode.c_str() ); if( FileData == NULL ) { throw( nitro::exception( std::string( "An error occured while opening file " ) + FilePath , 1 ) ); } #endif // типа если открыт в режиме добавления, то устанавливаем курсор в конец if( Mode & FA_FILE_APPEND ) { Seek( 0 , FA_FILE_END ); } else { Seek( 0 , FA_FILE_BEGIN ); } } catch( nitro::exception e ) { throw( nitro::exception( std::string( "FileAbstraction::Open( const char * FilePath , const std::size_t theMode )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "FileAbstraction::Open( const char * FilePath , const std::size_t theMode )::An error occured" ) , 0 ) ); } }
void nitro::FileAbstraction::Open | ( | const std::string & | FilePath, | |
const std::size_t | Mode | |||
) |
Function opens file FilePath in mode Mode.
FilePath | - Path to the opening file. | |
Mode | - File opening mode. |
nitro::exception | Throws an exception of that type with the error description. |
Definition at line 409 of file file_abstraction.cpp.
References nitro::exception::code(), Open(), and nitro::exception::what().
{ try { Open( FilePath.c_str() , theMode ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "FileAbstraction::Open( const std::string & FilePath , const std::size_t theMode )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "FileAbstraction::Open( const std::string & FilePath , const std::size_t theMode )::An error occured" ) , 0 ) ); } }
FileAbstraction nitro::FileAbstraction::operator= | ( | const FileAbstraction & | ) | [inline, private] |
std::size_t nitro::FileAbstraction::Read | ( | char * | Buffer, | |
std::size_t | BufferLength | |||
) |
Function reads binary data in buffer from file.
Buffer | - Buffer for accepting read data. | |
BufferLength | - Length of data buffer(in bytes). |
nitro::exception | Throws exception wich contains error description. |
Definition at line 275 of file file_abstraction.cpp.
References nitro::exception::code(), nitro::FA_FILE_READ, FileData, Mode, and nitro::exception::what().
Referenced by nitro::CSVFile::AppendRecord(), nitro::UnTARAbstraction::ExtractFile(), nitro::File::LoadBinDataFromFile(), nitro::UnTARAbstraction::ReadHeader(), and nitro::CSVFile::ReadRecord().
{ try { if( FileData == NULL ) { throw( nitro::exception( std::string( "File was not opened" ) , 1 ) ); } if( ( Mode & FA_FILE_READ ) == 0 ) { throw( nitro::exception( "File was not opened for reading" , 1 ) ); } #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) DWORD ReadBytes( 0 ); if( !ReadFile( * ( ( HANDLE * )FileData ) , ( LPVOID )Buffer , ( LONG )BufferLength , & ReadBytes , NULL ) ) { throw( nitro::exception( "FileAbstraction::Read( char * Buffer , const std::size_t BufferLength )::An error occured while reading data from file" , 1 ) ); } return( ( std::size_t )ReadBytes ); #endif #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM ) return( fread( Buffer , 1 , BufferLength , ( FILE * )FileData ) ); #endif } catch( nitro::exception e ) { throw( nitro::exception( std::string( "FileAbstraction::Read( char * Buffer , const std::size_t BufferLength )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "FileAbstraction::Read( char * Buffer , const std::size_t BufferLength )::An error occured" ) , 0 ) ); } return( 0 ); }
void nitro::FileAbstraction::RenameFile | ( | const std::string & | FilePathFrom, | |
const std::string & | FilePathTo | |||
) | [static] |
Function renames file.
FilePathFrom | - Path to the renaming file. | |
FilePathTo | - Path to the renaming file with niew file name. |
nitro::exception | Throws an exception of that type with the error description. |
Definition at line 441 of file file_abstraction.cpp.
References nitro::exception::code(), RenameFile(), and nitro::exception::what().
{ try { RenameFile( FilePathFrom.c_str() , FilePathTo.c_str() ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "FileAbstraction::RenameFile( const std::string & FilePathFrom , const std::string & FilePathTo )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "FileAbstraction::RenameFile( const std::string & FilePathFrom , const std::string & FilePathTo )::An error occured" ) , 0 ) ); } }
void nitro::FileAbstraction::RenameFile | ( | const char * | FilePathFrom, | |
const char * | FilePathTo | |||
) | [static] |
Function renames file.
FilePathFrom | - Path to the renaming file. | |
FilePathTo | - Path to the renaming file with niew file name. |
nitro::exception | Throws an exception of that type with the error description. |
Definition at line 382 of file file_abstraction.cpp.
References nitro::exception::code(), and nitro::exception::what().
Referenced by RenameFile().
{ try { #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) if( MoveFile( FilePathFrom , FilePathTo ) == 0 ) { throw( nitro::exception( std::string( "An error cccured while renaming file " ) + FilePathFrom , 0 ) ); } #endif #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM ) if( rename( FilePathFrom , FilePathTo ) != 0 ) { throw( nitro::exception( std::string( "An error cccured while renaming file " ) + FilePathFrom , 0 ) ); } #endif } catch( nitro::exception e ) { throw( nitro::exception( std::string( "FileAbstraction::RenameFile( const char * FilePathFrom , const char * FilePathTo )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "FileAbstraction::RenameFile( const char * FilePathFrom , const char * FilePathTo )::An error occured" ) , 0 ) ); } }
void nitro::FileAbstraction::Seek | ( | std::size_t | Offset, | |
std::size_t | theMode | |||
) |
Setting read/write cursor in file.
Offset | - Cursor offset. | |
theMode | - Cursor setting theMode. |
nitro::exception | Throws exception wich contains error description. |
Definition at line 161 of file file_abstraction.cpp.
References nitro::exception::code(), nitro::FA_FILE_BEGIN, nitro::FA_FILE_CURRENT, nitro::FA_FILE_END, FileData, and nitro::exception::what().
Referenced by nitro::CSVFile::AppendRecord(), nitro::File::FileSize(), nitro::UnTARAbstraction::GetCountOfFiles(), nitro::UnTARAbstraction::GotoFirstFile(), Open(), nitro::CSVFile::ReadRecord(), and nitro::UnTARAbstraction::SetReadCursor().
{ try { if( FileData == NULL ) { throw( nitro::exception( std::string( "File was not opened" ) , 1 ) ); } #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) switch( theMode ) { case( FA_FILE_BEGIN ): SetFilePointer( * ( ( HANDLE * )FileData ) , ( LONG )Offset , NULL , FILE_BEGIN ); break; case( FA_FILE_CURRENT ): SetFilePointer( * ( ( HANDLE * )FileData ) , ( LONG )Offset , NULL , FILE_CURRENT ); break; case( FA_FILE_END ): SetFilePointer( * ( ( HANDLE * )FileData ) , ( LONG )Offset , NULL , FILE_END ); break; } #endif #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM ) switch( theMode ) { case( FA_FILE_BEGIN ): fseek( ( FILE * )FileData , Offset , SEEK_SET ); break; case( FA_FILE_CURRENT ): fseek( ( FILE * )FileData , Offset , SEEK_CUR ); break; case( FA_FILE_END ): fseek( ( FILE * )FileData , Offset , SEEK_END ); break; } #endif } catch( nitro::exception e ) { throw( nitro::exception( std::string( "FileAbstraction::Seek( std::size_t Offset , std::size_t theMode )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "FileAbstraction::Seek( std::size_t Offset , std::size_t theMode )::An error occured" ) , 0 ) ); } }
std::size_t nitro::FileAbstraction::Tell | ( | void | ) |
Function gets current cursor position in file.
nitro::exception | Throws exception wich contains error description. |
Definition at line 214 of file file_abstraction.cpp.
References nitro::exception::code(), FileData, and nitro::exception::what().
Referenced by nitro::CSVFile::AppendRecord(), nitro::File::FileSize(), nitro::UnTARAbstraction::GetCountOfFiles(), and nitro::CSVFile::ReadRecord().
{ try { if( FileData == NULL ) { throw( nitro::exception( std::string( "File was not opened" ) , 1 ) ); } #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) return( ( std::size_t )SetFilePointer( * ( ( HANDLE * )FileData ) , ( LONG )NULL , NULL , FILE_CURRENT ) ); #endif #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM ) return( ( std::size_t )ftell( ( FILE * )FileData ) ); #endif } catch( nitro::exception e ) { throw( nitro::exception( std::string( "FileAbstraction::Tell( void )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "FileAbstraction::Tell( void )::An error occured" ) , 0 ) ); } return( 0 ); }
void nitro::FileAbstraction::Write | ( | const char * | Buffer, | |
std::size_t | BufferLength | |||
) |
Function writes binary data into file.
Buffer | - Writing data. | |
BufferLength | - Length of data buffer (in bytes). |
nitro::exception | Throws exception wich contains error description. |
Definition at line 242 of file file_abstraction.cpp.
References nitro::exception::code(), nitro::FA_FILE_WRITE, FileData, Mode, and nitro::exception::what().
Referenced by nitro::CSVFile::AppendRecord(), nitro::LogStream::operator<<(), nitro::File::SaveBinDataToFile(), nitro::XMLFile::SaveXML(), and nitro::XMLTag::SaveXML().
{ try { if( FileData == NULL ) { throw( nitro::exception( std::string( "File was not opened" ) , 1 ) ); } if( ( Mode & FA_FILE_WRITE ) == 0 ) { throw( nitro::exception( "File was not opened for writing" , 1 ) ); } #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) DWORD WriteBytes; WriteFile( * ( ( HANDLE * )FileData ) , Buffer , ( LONG )BufferLength , & WriteBytes , NULL ); #endif #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM ) fwrite( Buffer , BufferLength , 1 , ( FILE * )FileData ); #endif } catch( nitro::exception e ) { throw( nitro::exception( std::string( "FileAbstraction::Write( const char * Buffer , const std::size_t BufferLength )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "FileAbstraction::Write( const char * Buffer , const std::size_t BufferLength )::An error occured" ) , 0 ) ); } }
void* nitro::FileAbstraction::FileData [private] |
Data for describing opened file.
Definition at line 529 of file file_abstraction.h.
Referenced by Close(), FileAbstraction(), Open(), Read(), Seek(), Tell(), and Write().
std::size_t nitro::FileAbstraction::Mode [private] |