From 233cbf1f15414b78d0a2bc2b1296d99e05ddd310 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Mon, 31 Jan 2011 16:00:35 +0000 Subject: [PATCH] get all fopen()s going through util.c Add a text_mode flag to im__file_open_write() and _read() so we can route all fopen() though these two functions. --- ChangeLog | 2 ++ TODO | 11 ----------- libvips/format/im_csv2vips.c | 6 +----- libvips/format/im_vips2csv.c | 6 +----- libvips/include/vips/util.h | 6 ++++-- libvips/iofuncs/threadpool.c | 4 ++-- libvips/iofuncs/util.c | 38 ++++++++++++++++++++++-------------- libvips/mask/rw_mask.c | 20 ++----------------- 8 files changed, 35 insertions(+), 58 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2ed4e96f..4674c99e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -15,6 +15,8 @@ - gtk-doc for mosaicing - add im_fits2vips() to the operation database - im_fits2vips() is lazy and much faster +- im__file_open_write() / _read() has a flag for text_mode, get rid of all the + remaining fopen()s 30/11/10 started 7.24.0 - bump for new stable diff --git a/TODO b/TODO index 19e21888..e1dbb060 100644 --- a/TODO +++ b/TODO @@ -1,16 +1,5 @@ -- half-way thorough making fits read lazy - - - -- im__open_write() should take a mode (text / binary) - - rw_mask.c has copies of im__open_write() etc. which use text mode, remove - them - - - - use |_O_TEMPORARY to open()'s mode on Windows for tmpfiles to increase the chances we actually delete diff --git a/libvips/format/im_csv2vips.c b/libvips/format/im_csv2vips.c index 86f20b07..01de0a6d 100644 --- a/libvips/format/im_csv2vips.c +++ b/libvips/format/im_csv2vips.c @@ -358,12 +358,8 @@ im_csv2vips( const char *filename, IMAGE *out ) lines = atoi( r ); } - if( !(fp = fopen( name, "r" )) ) { - im_error( "im_csv2vips", - _( "unable to open \"%s\"" ), name ); + if( !(fp = im__file_open_read( name, NULL, TRUE )) ) return( -1 ); - } - if( read_csv( fp, out, start_skip, whitespace, separator, lines ) ) { fclose( fp ); return( -1 ); diff --git a/libvips/format/im_vips2csv.c b/libvips/format/im_vips2csv.c index 94022e71..a0631ee4 100644 --- a/libvips/format/im_vips2csv.c +++ b/libvips/format/im_vips2csv.c @@ -162,12 +162,8 @@ im_vips2csv( IMAGE *in, const char *filename ) im_check_uncoded( "im_vips2csv", in ) ) return( -1 ); - if( !(fp = fopen( name, "w" )) ) { - im_error( "im_cvips2csv", _( "unable to open \"%s\"" ), - name ); + if( !(fp = im__file_open_write( name, TRUE )) ) return( -1 ); - } - if( vips2csv( in, fp, separator ) ) { fclose( fp ); return( -1 ); diff --git a/libvips/include/vips/util.h b/libvips/include/vips/util.h index ec01d2f3..4bc76e91 100644 --- a/libvips/include/vips/util.h +++ b/libvips/include/vips/util.h @@ -240,8 +240,10 @@ char *im_getsuboption( const char *buf ); gint64 im_file_length( int fd ); int im__write( int fd, const void *buf, size_t count ); -FILE *im__file_open_read( const char *filename, const char *fallback_dir ); -FILE *im__file_open_write( const char *filename ); +FILE *im__file_open_read( const char *filename, + const char *fallback_dir, gboolean text_mode ); +FILE *im__file_open_write( const char *filename, + gboolean text_mode ); char *im__file_read( FILE *fp, const char *name, unsigned int *length_out ); char *im__file_read_name( const char *name, const char *fallback_dir, unsigned int *length_out ); diff --git a/libvips/iofuncs/threadpool.c b/libvips/iofuncs/threadpool.c index e247e83f..0f4c7c71 100644 --- a/libvips/iofuncs/threadpool.c +++ b/libvips/iofuncs/threadpool.c @@ -403,8 +403,8 @@ vips_thread_save_time_buffers( VipsThread *thr ) im_snprintf( name, 256, "time%d", rn++ ); printf( "vips_thread_save_time_buffers: " "saving buffer to \"%s\"\n", name ); - if( !(fp = fopen( name, "w" )) ) - error_exit( "unable to write to \"%s\"", name ); + if( !(fp = im__file_open_write( name, TRUE )) ) + return( -1 ); for( i = 0; i < thr->tpos; i++ ) fprintf( fp, "%g, %g\n", thr->btime[i], thr->etime[i] ); fclose( fp ); diff --git a/libvips/iofuncs/util.c b/libvips/iofuncs/util.c index 059f21c3..0b8dfdbe 100644 --- a/libvips/iofuncs/util.c +++ b/libvips/iofuncs/util.c @@ -910,27 +910,29 @@ filename_hasdir( const char *filename ) * directory separator, we try looking in the fallback dir. */ FILE * -im__file_open_read( const char *filename, const char *fallback_dir ) +im__file_open_read( const char *filename, const char *fallback_dir, + gboolean text_mode ) { + char *mode; FILE *fp; #ifdef BINARY_OPEN - fp = fopen( filename, "rb" ); -#else /*!BINARY_OPEN*/ - fp = fopen( filename, "r" ); + if( text_mode ) + mode = "r"; + else + mode = "rb"; +#else /*BINARY_OPEN*/ + mode = "r"; #endif /*BINARY_OPEN*/ - if( fp ) + + if( (fp = fopen( filename, mode )) ) return( fp ); if( fallback_dir && !filename_hasdir( filename ) ) { char *path; path = g_build_filename( fallback_dir, filename, NULL ); -#ifdef BINARY_OPEN - fp = fopen( path, "rb" ); -#else /*!BINARY_OPEN*/ - fp = fopen( path, "r" ); -#endif /*BINARY_OPEN*/ + fp = fopen( path, mode ); g_free( path ); if( fp ) @@ -944,15 +946,21 @@ im__file_open_read( const char *filename, const char *fallback_dir ) } FILE * -im__file_open_write( const char *filename ) +im__file_open_write( const char *filename, gboolean text_mode ) { + char *mode; FILE *fp; #ifdef BINARY_OPEN - if( !(fp = fopen( filename, "wb" )) ) { + if( text_mode ) + mode = "w"; + else + mode = "wb"; #else /*BINARY_OPEN*/ - if( !(fp = fopen( filename, "w" )) ) { + mode = "w"; #endif /*BINARY_OPEN*/ + + if( !(fp = fopen( filename, mode )) ) { im_error( "im__file_open_write", _( "unable to open file \"%s\" for writing" ), filename ); @@ -963,7 +971,7 @@ im__file_open_write( const char *filename ) } /* Load from a filename as a string. Used for things like reading in ICC - * profiles. + * profiles, ie. binary objects. */ char * im__file_read_name( const char *filename, const char *fallback_dir, @@ -972,7 +980,7 @@ im__file_read_name( const char *filename, const char *fallback_dir, FILE *fp; char *buffer; - if( !(fp = im__file_open_read( filename, fallback_dir )) ) + if( !(fp = im__file_open_read( filename, fallback_dir, FALSE )) ) return( NULL ); if( !(buffer = im__file_read( fp, filename, length_out )) ) { fclose( fp ); diff --git a/libvips/mask/rw_mask.c b/libvips/mask/rw_mask.c index eddabafd..a0cd43c0 100644 --- a/libvips/mask/rw_mask.c +++ b/libvips/mask/rw_mask.c @@ -784,22 +784,6 @@ im_dup_dmask( DOUBLEMASK *in, const char *filename ) return( out ); } -/* Open for write. We can't use im__open_write(), we don't want binary mode. - */ -static FILE * -open_write( const char *name ) -{ - FILE *fp; - - if( !(fp = fopen( name, "w" )) ) { - im_error( "write_mask", _( "unable to open \"%s\" for output" ), - name ); - return( NULL ); - } - - return( fp ); -} - /* Write to file. */ static int @@ -846,7 +830,7 @@ im_write_imask_name( INTMASK *in, const char *filename ) int x, y, i; if( im_check_imask( "im_write_imask_name", in ) || - !(fp = open_write( filename )) ) + !(fp = im__file_open_write( filename, TRUE )) ) return( -1 ); if( write_line( fp, "%d %d", in->xsize, in->ysize ) ) { @@ -911,7 +895,7 @@ im_write_dmask_name( DOUBLEMASK *in, const char *filename ) int x, y, i; if( im_check_dmask( "im_write_dmask_name", in ) || - !(fp = open_write( filename )) ) + !(fp = im__file_open_write( filename, TRUE )) ) return( -1 ); if( write_line( fp, "%d %d", in->xsize, in->ysize ) ) {