Files - Set path in stream errors

This commit is contained in:
Emmanuel BENOîT 2017-12-28 09:52:47 +01:00
parent 165c19109f
commit 7b97993448

View file

@ -61,20 +61,20 @@ void T_File::open( )
file_ = fopen( &path[ 0 ] , mode ); file_ = fopen( &path[ 0 ] , mode );
#endif #endif
if ( file_ == nullptr ) { if ( file_ == nullptr ) {
throw X_StreamError( errno ); throw X_StreamError( errno , path_ );
} }
// Get initial size // Get initial size
if ( fseek( file_ , 0 , SEEK_END ) != 0 ) { if ( fseek( file_ , 0 , SEEK_END ) != 0 ) {
auto err( errno ); auto err( errno );
close( ); close( );
throw X_StreamError( err ); throw X_StreamError( err , path_ );
} }
off_t sz( ftell( file_ ) ); off_t sz( ftell( file_ ) );
if ( sz < 0 || fseek( file_ , 0 , SEEK_SET ) != 0 ) { if ( sz < 0 || fseek( file_ , 0 , SEEK_SET ) != 0 ) {
auto err( errno ); auto err( errno );
close( ); close( );
throw X_StreamError( err ); throw X_StreamError( err , path_ );
} }
pos_ = 0; pos_ = 0;
size_ = sz; size_ = sz;
@ -97,7 +97,7 @@ void T_File::position( size_t position , bool fromEnd )
? fseek( file_ , -position , SEEK_END ) ? fseek( file_ , -position , SEEK_END )
: fseek( file_ , +position , SEEK_SET ) ); : fseek( file_ , +position , SEEK_SET ) );
if ( rv < 0 ) { if ( rv < 0 ) {
throw X_StreamError( errno ); throw X_StreamError( errno , path_ );
} }
pos_ = rv; pos_ = rv;
} }
@ -107,7 +107,7 @@ void T_File::move( ssize_t offset )
open( ); open( );
const auto rv( fseek( file_ , offset , SEEK_CUR ) ); const auto rv( fseek( file_ , offset , SEEK_CUR ) );
if ( rv < 0 ) { if ( rv < 0 ) {
throw X_StreamError( errno ); throw X_StreamError( errno , path_ );
} }
pos_ = rv; pos_ = rv;
} }
@ -118,12 +118,12 @@ size_t T_File::read( void* data , size_t size )
{ {
open( ); open( );
if ( pos_ >= size_ ) { if ( pos_ >= size_ ) {
throw X_StreamError( E_StreamError::END ); throw X_StreamError( E_StreamError::END , path_ );
} }
const auto rv( fread( data , 1 , size , file_ ) ); const auto rv( fread( data , 1 , size , file_ ) );
if ( ferror( file_ ) ) { if ( ferror( file_ ) ) {
throw X_StreamError( errno ); throw X_StreamError( errno , path_ );
} else { } else {
pos_ += rv; pos_ += rv;
return rv; return rv;
@ -135,13 +135,13 @@ size_t T_File::read( void* data , size_t size )
size_t T_File::write( void const* data , size_t size ) size_t T_File::write( void const* data , size_t size )
{ {
if ( mode_ == E_FileMode::READ_ONLY ) { if ( mode_ == E_FileMode::READ_ONLY ) {
throw X_StreamError( E_StreamError::NOT_SUPPORTED ); throw X_StreamError( E_StreamError::NOT_SUPPORTED , path_ );
} }
open( ); open( );
auto rv( fwrite( data , 1 , size , file_ ) ); auto rv( fwrite( data , 1 , size , file_ ) );
if ( ferror( file_ ) ) { if ( ferror( file_ ) ) {
throw X_StreamError( errno ); throw X_StreamError( errno , path_ );
} else { } else {
pos_ += rv; pos_ += rv;
if ( pos_ > size_ ) { if ( pos_ > size_ ) {
@ -213,11 +213,11 @@ void T_FileInputStream::swap( T_FileInputStream& rhs ) noexcept
size_t T_FileInputStream::read( void* data , size_t size ) size_t T_FileInputStream::read( void* data , size_t size )
{ {
auto& f( file( ) );
if ( position_ >= size_ ) { if ( position_ >= size_ ) {
throw X_StreamError( E_StreamError::END ); throw X_StreamError( E_StreamError::END , f.path( ) );
} }
auto& f( file( ) );
f.open( ); f.open( );
if ( f.position( ) != start_ + position_ ) { if ( f.position( ) != start_ + position_ ) {
f.position( start_ + position_ ); f.position( start_ + position_ );
@ -237,7 +237,8 @@ void T_FileInputStream::init( ssize_t offset , size_t limit )
f.open( ); f.open( );
const ssize_t start( f.position( ) + offset ); const ssize_t start( f.position( ) + offset );
if ( start < 0 || start > ssize_t( f.size( ) ) ) { if ( start < 0 || start > ssize_t( f.size( ) ) ) {
throw X_StreamError( E_StreamError::INVALID_POSITION ); throw X_StreamError( E_StreamError::INVALID_POSITION ,
f.path( ) );
} }
start_ = start; start_ = start;
size_ = std::min( f.size( ) - start , limit ); size_ = std::min( f.size( ) - start , limit );
@ -314,11 +315,13 @@ void T_FileOutputStream::init( ssize_t offset )
{ {
auto& f( file( ) ); auto& f( file( ) );
if ( f.mode( ) == E_FileMode::READ_ONLY ) { if ( f.mode( ) == E_FileMode::READ_ONLY ) {
throw X_StreamError( E_StreamError::NOT_SUPPORTED ); throw X_StreamError( E_StreamError::NOT_SUPPORTED ,
f.path( ) );
} }
const ssize_t start( f.position( ) + offset ); const ssize_t start( f.position( ) + offset );
if ( start < 0 || start > ssize_t( f.size( ) ) ) { if ( start < 0 || start > ssize_t( f.size( ) ) ) {
throw X_StreamError( E_StreamError::INVALID_POSITION ); throw X_StreamError( E_StreamError::INVALID_POSITION ,
f.path( ) );
} }
start_ = start; start_ = start;
} }