From f822234c4e393747266dc4d9dd6d8e742dcf43c7 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Thu, 29 Jul 2010 10:18:44 +0000 Subject: [PATCH] use GetTempPath() on WIndows --- ChangeLog | 1 + TODO | 5 ----- libvips/conversion/im_copy_file.c | 6 ++---- libvips/conversion/im_system.c | 4 ++-- libvips/conversion/im_system_image.c | 4 ++++ libvips/iofuncs/util.c | 32 +++++++++++++++++++++++----- libvips/morphology/im_rank_image.c | 4 ++-- 7 files changed, 38 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index bc3c642d..efaa7348 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,7 @@ - im_vips2bufjpeg() writes to a linked list, so it will work for any size image and header - added im_vips2bufpng() +- use GetTempPath() to pick a temp dir on Windows 12/5/10 started 7.22.2 - the conditional image of ifthenelse can be any format, a (!=0) is added if diff --git a/TODO b/TODO index 4378df4b..f2e8fb26 100644 --- a/TODO +++ b/TODO @@ -1,8 +1,3 @@ -- use GetTempPath - - http://msdn.microsoft.com/en-us/library/aa364992%28VS.85%29.aspx - - instead of $TMPDIR on win - lcms2 needs testing diff --git a/libvips/conversion/im_copy_file.c b/libvips/conversion/im_copy_file.c index 118ced37..402eda84 100644 --- a/libvips/conversion/im_copy_file.c +++ b/libvips/conversion/im_copy_file.c @@ -59,10 +59,8 @@ * Copy an image to a disc file, then copy again to output. If the image is * already a disc file, just copy straight through. * - * The disc file is allocated in the same way as im_system_image(). A disc - * file is created in /tmp (change this with the TMPDIR environment - * variable) named something like "vips-12-34985.v" and the image is written - * to that file. The file is automatically deleted when @out is closed. + * The disc file is allocated in the same way as im_system_image(). + * The file is automatically deleted when @out is closed. * * See also: im_copy(), im_system_image(). * diff --git a/libvips/conversion/im_system.c b/libvips/conversion/im_system.c index 44fa3d56..a159c9e6 100644 --- a/libvips/conversion/im_system.c +++ b/libvips/conversion/im_system.c @@ -81,8 +81,8 @@ * "vips_XXXXXX.v", and that filename given to the command. The file is * deleted when the command finishes. * - * The environment variable TMPDIR can be used to set the temporary - * directory. If it is not set, it defaults to "/tmp". + * See im_system_image() for details on how VIPS selects a temporary + * directory. * * In all cases, @log must be freed with im_free(). * diff --git a/libvips/conversion/im_system_image.c b/libvips/conversion/im_system_image.c index 958897e8..f1cfeca6 100644 --- a/libvips/conversion/im_system_image.c +++ b/libvips/conversion/im_system_image.c @@ -100,6 +100,10 @@ system_image( IMAGE *im, * can be used to set a different temporary directory. If @in_format is * something like "%%s.png", the file will be written in PNG format. * + * On Windows, if the environment variable TMPDIR is not defined, VIPS calls + * GetTempPath() to get the user's preferred temporary area. If that fails, it + * defaults to C:\temp. + * * Next an output filename is created in the same way using @out_format. The * command string to run is made by substituting the first %%s in @cmd_format * for the name of the input file and the second %%s for the output filename. diff --git a/libvips/iofuncs/util.c b/libvips/iofuncs/util.c index d3b3f28c..059f21c3 100644 --- a/libvips/iofuncs/util.c +++ b/libvips/iofuncs/util.c @@ -1528,6 +1528,32 @@ im_amiMSBfirst( void ) return( 1 ); } +/* Return the tmp dir. On Windows, GetTempPath() will also check the values of + * TMP, TEMP and USERPROFILE. + */ +static const char * +im__temp_dir( void ) +{ + const char *tmpd; + + if( !(tmpd = g_getenv( "TMPDIR" )) ) { +#ifdef OS_WIN32 + static gboolean done = FALSE; + static char buf[256]; + + if( !done ) { + if( !GetTempPath( 256, buf ) ) + strcpy( buf, "C:\\temp" ); + } + tmpd = buf; +#else /*!OS_WIN32*/ + tmpd = "/tmp"; +#endif /*!OS_WIN32*/ + } + + return( tmpd ); +} + /* Make a temporary file name. The format parameter is something like "%s.jpg" * and will be expanded to something like "/tmp/vips-12-34587.jpg". * @@ -1539,19 +1565,15 @@ im__temp_name( const char *format ) { static int serial = 1; - const char *tmpd; char file[FILENAME_MAX]; char file2[FILENAME_MAX]; char *name; int fd; - if( !(tmpd = g_getenv( "TMPDIR" )) ) - tmpd = "/tmp"; - im_snprintf( file, FILENAME_MAX, "vips-%d-XXXXXX", serial++ ); im_snprintf( file2, FILENAME_MAX, format, file ); - name = g_build_filename( tmpd, file2, NULL ); + name = g_build_filename( im__temp_dir(), file2, NULL ); if( (fd = g_mkstemp( name )) == -1 ) { im_error( "tempfile", diff --git a/libvips/morphology/im_rank_image.c b/libvips/morphology/im_rank_image.c index 4d792bf5..a4b9fcbd 100644 --- a/libvips/morphology/im_rank_image.c +++ b/libvips/morphology/im_rank_image.c @@ -288,13 +288,13 @@ im_rank_image( IMAGE **in, IMAGE *out, int n, int index ) } if( im_poutcheck( out ) ) return( -1 ); - for( i = 0; i < n; i++ ) { + for( i = 0; i < n; i++ ) if( im_pincheck( in[i] ) || im_check_uncoded( "im_rank_image", in[i] ) || im_check_noncomplex( "im_rank_image", in[i] ) || im_check_size_same( "im_rank_image", in[i], in[0] ) || im_check_format_same( "im_rank_image", in[i], in[0] ) || - im_check_bands( "im_rank_image", in[i], 3 ) ) + im_check_bands_same( "im_rank_image", in[i], in[0] ) ) return( -1 ); if( !(rank = rank_new( in, out, n, index )) ||