close infile on error condition. Make failure to read not be fatal.

This commit is contained in:
Isaac Connor 2017-12-12 13:35:19 -05:00
parent 7278448b01
commit 599848346e
1 changed files with 17 additions and 19 deletions

View File

@ -746,37 +746,35 @@ Image *Image::HighlightEdges( Rgb colour, unsigned int p_colours, unsigned int p
return( high_image ); return( high_image );
} }
bool Image::ReadRaw( const char *filename ) bool Image::ReadRaw( const char *filename ) {
{
FILE *infile; FILE *infile;
if ( (infile = fopen( filename, "rb" )) == NULL ) if ( (infile = fopen( filename, "rb" )) == NULL ) {
{ Error("Can't open %s: %s", filename, strerror(errno));
Error( "Can't open %s: %s", filename, strerror(errno) ); return false;
return( false );
} }
struct stat statbuf; struct stat statbuf;
if ( fstat( fileno(infile), &statbuf ) < 0 ) if ( fstat( fileno(infile), &statbuf ) < 0 ) {
{ fclose(infile);
Error( "Can't fstat %s: %s", filename, strerror(errno) ); Error("Can't fstat %s: %s", filename, strerror(errno));
return( false ); return false;
} }
if ( statbuf.st_size != size ) if ( statbuf.st_size != size ) {
{ fclose(infile);
Error( "Raw file size mismatch, expected %d bytes, found %ld", size, statbuf.st_size ); Error("Raw file size mismatch, expected %d bytes, found %ld", size, statbuf.st_size);
return( false ); return false;
} }
if ( fread( buffer, size, 1, infile ) < 1 ) if ( fread(buffer, size, 1, infile) < 1 ) {
{ fclose(infile);
Fatal( "Unable to read from '%s': %s", filename, strerror(errno) ); Error("Unable to read from '%s': %s", filename, strerror(errno));
return( false ); return false;
} }
fclose( infile ); fclose( infile );
return( true ); return true;
} }
bool Image::WriteRaw( const char *filename ) const { bool Image::WriteRaw( const char *filename ) const {