Skip to content

Commit

Permalink
Removed asserts and replaced by exceptions for checking of runtime er…
Browse files Browse the repository at this point in the history
…rors (#369)

The runtime errors such as zero-length file or disk full.
Unified error message wording.
  • Loading branch information
xjhouska authored and mloskot committed Aug 9, 2019
1 parent 64ceb37 commit 48e35c1
Showing 1 changed file with 55 additions and 71 deletions.
126 changes: 55 additions & 71 deletions include/boost/gil/io/device.hpp
Expand Up @@ -11,9 +11,6 @@
#include <boost/gil/detail/mp11.hpp>
#include <boost/gil/io/base.hpp>

#include <boost/assert.hpp>
#include <boost/core/ignore_unused.hpp>

#include <cstdio>
#include <memory>
#include <type_traits>
Expand Down Expand Up @@ -64,19 +61,10 @@ class file_stream_device
/// Constructor
///
file_stream_device( const std::string& file_name
, read_tag = read_tag()
, read_tag tag = read_tag()
)
{
FILE* file = nullptr;

io_error_if( ( file = fopen( file_name.c_str(), "rb" )) == nullptr
, "file_stream_device: failed to open file"
);

_file = file_ptr_t( file
, file_deleter
);
}
: file_stream_device(file_name.c_str(), tag)
{}

///
/// Constructor
Expand All @@ -88,7 +76,7 @@ class file_stream_device
FILE* file = nullptr;

io_error_if( ( file = fopen( file_name, "rb" )) == nullptr
, "file_stream_device: failed to open file"
, "file_stream_device: failed to open file for reading"
);

_file = file_ptr_t( file
Expand All @@ -100,19 +88,10 @@ class file_stream_device
/// Constructor
///
file_stream_device( const std::string& file_name
, write_tag
, write_tag tag
)
{
FILE* file = nullptr;

io_error_if( ( file = fopen( file_name.c_str(), "wb" )) == nullptr
, "file_stream_device: failed to open file"
);

_file = file_ptr_t( file
, file_deleter
);
}
: file_stream_device(file_name.c_str(), tag)
{}

///
/// Constructor
Expand All @@ -124,7 +103,7 @@ class file_stream_device
FILE* file = nullptr;

io_error_if( ( file = fopen( file_name, "wb" )) == nullptr
, "file_stream_device: failed to open file"
, "file_stream_device: failed to open file for writing"
);

_file = file_ptr_t( file
Expand Down Expand Up @@ -153,8 +132,9 @@ class file_stream_device
{
int ch;

if(( ch = std::getc( get() )) == EOF )
io_error( "file_stream_device: unexpected EOF" );
io_error_if( ( ch = std::getc( get() )) == EOF
, "file_stream_device: unexpected EOF"
);

return ( char ) ch;
}
Expand All @@ -171,29 +151,29 @@ class file_stream_device
);

///@todo: add compiler symbol to turn error checking on and off.
if(ferror( get() ))
{
BOOST_ASSERT(false);
}
io_error_if( ferror( get() )
, "file_stream_device: file read error"
);

//libjpeg sometimes reads blocks in 4096 bytes even when the file is smaller than that.
//assert( num_elements == count );
BOOST_ASSERT(num_elements > 0 );

//return value indicates how much was actually read
//returning less than "count" is not an error
return num_elements;
}

/// Reads array
template< typename T
, int N
>
std::size_t read( T (&buf)[N] )
void read( T (&buf)[N] )
{
return read( buf, N );
io_error_if( read( buf, N ) < N
, "file_stream_device: file read error"
);
}

/// Reads byte
uint8_t read_uint8() throw()
uint8_t read_uint8()
{
byte_t m[1];

Expand All @@ -202,7 +182,7 @@ class file_stream_device
}

/// Reads 16 bit little endian integer
uint16_t read_uint16() throw()
uint16_t read_uint16()
{
byte_t m[2];

Expand All @@ -211,7 +191,7 @@ class file_stream_device
}

/// Reads 32 bit little endian integer
uint32_t read_uint32() throw()
uint32_t read_uint32()
{
byte_t m[4];

Expand All @@ -224,36 +204,39 @@ class file_stream_device
std::size_t write( const T* buf
, std::size_t count
)
throw()
{
std::size_t num_elements = fwrite( buf
, buff_item<T>::size
, count
, get()
);

BOOST_ASSERT(num_elements == count);
//return value indicates how much was actually written
//returning less than "count" is not an error
return num_elements;
}

/// Writes array
template < typename T
, std::size_t N
>
std::size_t write( const T (&buf)[N] ) throw()
void write( const T (&buf)[N] )
{
return write( buf, N );
io_error_if( write( buf, N ) < N
, "file_stream_device: file write error"
);
return ;
}

/// Writes byte
void write_uint8( uint8_t x ) throw()
void write_uint8( uint8_t x )
{
byte_t m[1] = { x };
write(m);
}

/// Writes 16 bit little endian integer
void write_uint16( uint16_t x ) throw()
void write_uint16( uint16_t x )
{
byte_t m[2];

Expand All @@ -264,7 +247,7 @@ class file_stream_device
}

/// Writes 32 bit little endian integer
void write_uint32( uint32_t x ) throw()
void write_uint32( uint32_t x )
{
byte_t m[4];

Expand All @@ -282,7 +265,7 @@ class file_stream_device
, count
, whence
) != 0
, "file read error"
, "file_stream_device: file seek error"
);
}

Expand All @@ -291,7 +274,7 @@ class file_stream_device
long int pos = ftell( get() );

io_error_if( pos == -1L
, "file read error"
, "file_stream_device: file position error"
);

return pos;
Expand All @@ -311,8 +294,9 @@ class file_stream_device
, get()
);

BOOST_ASSERT(num_elements == line.size());
boost::ignore_unused(num_elements);
io_error_if( num_elements < line.size()
, "file_stream_device: line print error"
);
}

int error()
Expand Down Expand Up @@ -346,11 +330,10 @@ class istream_device
istream_device( std::istream& in )
: _in( in )
{
if (!in)
{
// does the file exists?
io_error("Stream is not valid.");
}
// does the file exists?
io_error_if( !in
, "istream_device: Stream is not valid."
);
}

int getc_unchecked()
Expand All @@ -362,8 +345,9 @@ class istream_device
{
int ch;

if(( ch = _in.get() ) == EOF )
io_error( "file_stream_device: unexpected EOF" );
io_error_if( ( ch = _in.get() ) == EOF
, "istream_device: unexpected EOF"
);

return ( char ) ch;
}
Expand Down Expand Up @@ -392,13 +376,13 @@ class istream_device
template< typename T
, int N
>
size_t read( T (&buf)[N] )
void read( T (&buf)[N] )
{
return read( buf, N );
}

/// Reads byte
uint8_t read_uint8() throw()
uint8_t read_uint8()
{
byte_t m[1];

Expand All @@ -407,7 +391,7 @@ class istream_device
}

/// Reads 16 bit little endian integer
uint16_t read_uint16() throw()
uint16_t read_uint16()
{
byte_t m[2];

Expand All @@ -416,7 +400,7 @@ class istream_device
}

/// Reads 32 bit little endian integer
uint32_t read_uint32() throw()
uint32_t read_uint32()
{
byte_t m[4];

Expand All @@ -435,7 +419,7 @@ class istream_device

void write(const byte_t*, std::size_t)
{
io_error( "Bad io error." );
io_error( "istream_device: Bad io error." );
}

void flush() {}
Expand All @@ -459,7 +443,7 @@ class ostream_device

std::size_t read(byte_t *, std::size_t)
{
io_error( "Bad io error." );
io_error( "ostream_device: Bad io error." );
return 0;
}

Expand All @@ -486,20 +470,20 @@ class ostream_device
template < typename T
, std::size_t N
>
void write( const T (&buf)[N] ) throw()
void write( const T (&buf)[N] )
{
write( buf, N );
}

/// Writes byte
void write_uint8( uint8_t x ) throw()
void write_uint8( uint8_t x )
{
byte_t m[1] = { x };
write(m);
}

/// Writes 16 bit little endian integer
void write_uint16( uint16_t x ) throw()
void write_uint16( uint16_t x )
{
byte_t m[2];

Expand All @@ -510,7 +494,7 @@ class ostream_device
}

/// Writes 32 bit little endian integer
void write_uint32( uint32_t x ) throw()
void write_uint32( uint32_t x )
{
byte_t m[4];

Expand Down

0 comments on commit 48e35c1

Please sign in to comment.