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.
This commit is contained in:
John Cupitt 2011-01-31 16:00:35 +00:00
parent 5764182fe3
commit 233cbf1f15
8 changed files with 35 additions and 58 deletions

View File

@ -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

11
TODO
View File

@ -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

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );

View File

@ -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 ) ) {