From 7b97993448d086a92d9875db2c2815afa249ba65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Thu, 28 Dec 2017 09:52:47 +0100 Subject: [PATCH] Files - Set path in stream errors --- src/Files.cc | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/Files.cc b/src/Files.cc index 2965aa6..5a6aed1 100644 --- a/src/Files.cc +++ b/src/Files.cc @@ -61,20 +61,20 @@ void T_File::open( ) file_ = fopen( &path[ 0 ] , mode ); #endif if ( file_ == nullptr ) { - throw X_StreamError( errno ); + throw X_StreamError( errno , path_ ); } // Get initial size if ( fseek( file_ , 0 , SEEK_END ) != 0 ) { auto err( errno ); close( ); - throw X_StreamError( err ); + throw X_StreamError( err , path_ ); } off_t sz( ftell( file_ ) ); if ( sz < 0 || fseek( file_ , 0 , SEEK_SET ) != 0 ) { auto err( errno ); close( ); - throw X_StreamError( err ); + throw X_StreamError( err , path_ ); } pos_ = 0; size_ = sz; @@ -97,7 +97,7 @@ void T_File::position( size_t position , bool fromEnd ) ? fseek( file_ , -position , SEEK_END ) : fseek( file_ , +position , SEEK_SET ) ); if ( rv < 0 ) { - throw X_StreamError( errno ); + throw X_StreamError( errno , path_ ); } pos_ = rv; } @@ -107,7 +107,7 @@ void T_File::move( ssize_t offset ) open( ); const auto rv( fseek( file_ , offset , SEEK_CUR ) ); if ( rv < 0 ) { - throw X_StreamError( errno ); + throw X_StreamError( errno , path_ ); } pos_ = rv; } @@ -118,12 +118,12 @@ size_t T_File::read( void* data , size_t size ) { open( ); if ( pos_ >= size_ ) { - throw X_StreamError( E_StreamError::END ); + throw X_StreamError( E_StreamError::END , path_ ); } const auto rv( fread( data , 1 , size , file_ ) ); if ( ferror( file_ ) ) { - throw X_StreamError( errno ); + throw X_StreamError( errno , path_ ); } else { pos_ += 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 ) { if ( mode_ == E_FileMode::READ_ONLY ) { - throw X_StreamError( E_StreamError::NOT_SUPPORTED ); + throw X_StreamError( E_StreamError::NOT_SUPPORTED , path_ ); } open( ); auto rv( fwrite( data , 1 , size , file_ ) ); if ( ferror( file_ ) ) { - throw X_StreamError( errno ); + throw X_StreamError( errno , path_ ); } else { pos_ += rv; 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 ) { + auto& f( file( ) ); if ( position_ >= size_ ) { - throw X_StreamError( E_StreamError::END ); + throw X_StreamError( E_StreamError::END , f.path( ) ); } - auto& f( file( ) ); f.open( ); if ( f.position( ) != start_ + position_ ) { f.position( start_ + position_ ); @@ -237,7 +237,8 @@ void T_FileInputStream::init( ssize_t offset , size_t limit ) f.open( ); const ssize_t start( f.position( ) + offset ); 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; size_ = std::min( f.size( ) - start , limit ); @@ -314,11 +315,13 @@ void T_FileOutputStream::init( ssize_t offset ) { auto& f( file( ) ); 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 ); 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; }