vips and error done

more files compile
This commit is contained in:
John Cupitt 2011-02-26 14:46:38 +00:00
parent d203195fff
commit 2a2fef64f3
5 changed files with 94 additions and 171 deletions

View File

@ -51,6 +51,9 @@ void vips_diag( const char *domain, const char *fmt, ... )
__attribute__((format(printf, 2, 3)));
void vips_vdiag( const char *domain, const char *fmt, va_list ap );
void vips_error_exit( const char *fmt, ... )
__attribute__((format(printf, 1, 2)));
#ifdef __cplusplus
}
#endif /*__cplusplus*/

View File

@ -345,6 +345,7 @@ double vips_image_get_xres( VipsImage *image );
double vips_image_get_yres( VipsImage *image );
int vips_image_get_xoffset( VipsImage *image );
int vips_image_get_yoffset( VipsImage *image );
size_t vips_image_size( VipsImage *image );
void vips_image_written( VipsImage *image );
void vips_image_preeval( VipsImage *image );

View File

@ -94,7 +94,7 @@
*
* if( im->Xsize < 100 ) {
* // we have detected an error, we must set a message
* im_error( "myprogram", "%s", _( "XSize too small" ) );
* vips_error( "myprogram", "%s", _( "XSize too small" ) );
* return( -1 );
* }
* ]|
@ -106,76 +106,76 @@
/* Make global array to keep the error message buffer.
*/
#define IM_MAX_ERROR (10240)
static char im_error_text[IM_MAX_ERROR] = "";
static VipsBuf im_error_buf = VIPS_BUF_STATIC( im_error_text );
static char vips_error_text[IM_MAX_ERROR] = "";
static VipsBuf vips_error_buf = VIPS_BUF_STATIC( vips_error_text );
#define IM_DIAGNOSTICS "IM_DIAGNOSTICS"
#define IM_WARNING "IM_WARNING"
/**
* im_error_buffer:
* vips_error_buffer:
*
* Get a pointer to the start of the error buffer as a C string.
* The string is owned by the error system and must not be freed.
*
* See also: im_error_clear().
* See also: vips_error_clear().
*
* Returns: the error buffer as a C string which must not be freed
*/
const char *
im_error_buffer( void )
vips_error_buffer( void )
{
const char *msg;
g_mutex_lock( im__global_lock );
msg = vips_buf_all( &im_error_buf );
msg = vips_buf_all( &vips_error_buf );
g_mutex_unlock( im__global_lock );
return( msg );
}
/**
* im_verror:
* vips_verror:
* @domain: the source of the error
* @fmt: printf()-style format string for the error
* @ap: arguments to the format string
*
* Append a message to the error buffer.
*
* See also: im_error().
* See also: vips_error().
*/
void
im_verror( const char *domain, const char *fmt, va_list ap )
vips_verror( const char *domain, const char *fmt, va_list ap )
{
g_mutex_lock( im__global_lock );
vips_buf_appendf( &im_error_buf, "%s: ", domain );
vips_buf_vappendf( &im_error_buf, fmt, ap );
vips_buf_appends( &im_error_buf, "\n" );
vips_buf_appendf( &vips_error_buf, "%s: ", domain );
vips_buf_vappendf( &vips_error_buf, fmt, ap );
vips_buf_appends( &vips_error_buf, "\n" );
g_mutex_unlock( im__global_lock );
}
/**
* im_error:
* vips_error:
* @domain: the source of the error
* @fmt: printf()-style format string for the error
* @Varargs: arguments to the format string
*
* Format the string in the style of printf() and append to the error buffer.
*
* See also: im_error_system(), im_verror().
* See also: vips_error_system(), vips_verror().
*/
void
im_error( const char *domain, const char *fmt, ... )
vips_error( const char *domain, const char *fmt, ... )
{
va_list ap;
va_start( ap, fmt );
im_verror( domain, fmt, ap );
vips_verror( domain, fmt, ap );
va_end( ap );
}
/**
* im_verror_system:
* vips_verror_system:
* @err: the system error code
* @domain: the source of the error
* @fmt: printf()-style format string for the error
@ -185,12 +185,12 @@ im_error( const char *domain, const char *fmt, ... )
* Then create and append a localised message based on the system error code,
* usually the value of errno.
*
* See also: im_error_system().
* See also: vips_error_system().
*/
void
im_verror_system( int err, const char *domain, const char *fmt, va_list ap )
vips_verror_system( int err, const char *domain, const char *fmt, va_list ap )
{
im_verror( domain, fmt, ap );
vips_verror( domain, fmt, ap );
#ifdef OS_WIN32
{
@ -213,14 +213,14 @@ im_verror_system( int err, const char *domain, const char *fmt, va_list ap )
char *buf;
buf = g_locale_to_utf8( strerror( err ), -1, NULL, NULL, NULL );
im_error( _( "unix error" ), "%s", buf );
vips_error( _( "unix error" ), "%s", buf );
g_free( buf );
}
#endif /*OS_WIN32*/
}
/**
* im_error_system:
* vips_error_system:
* @err: the system error code
* @domain: the source of the error
* @fmt: printf()-style format string for the error
@ -230,36 +230,36 @@ im_verror_system( int err, const char *domain, const char *fmt, va_list ap )
* Then create and append a localised message based on the system error code,
* usually the value of errno.
*
* See also: im_verror_system().
* See also: vips_verror_system().
*/
void
im_error_system( int err, const char *domain, const char *fmt, ... )
vips_error_system( int err, const char *domain, const char *fmt, ... )
{
va_list ap;
va_start( ap, fmt );
im_verror_system( err, domain, fmt, ap );
vips_verror_system( err, domain, fmt, ap );
va_end( ap );
}
/**
* im_error_clear:
* vips_error_clear:
*
* Clear and reset the error buffer. This is typically called after presentng
* an error to the user.
*
* See also: im_error_buffer().
* See also: vips_error_buffer().
*/
void
im_error_clear( void )
vips_error_clear( void )
{
g_mutex_lock( im__global_lock );
vips_buf_rewind( &im_error_buf );
vips_buf_rewind( &vips_error_buf );
g_mutex_unlock( im__global_lock );
}
/**
* im_vdiag:
* vips_vdiag:
* @domain: the source of the diagnostic message
* @fmt: printf()-style format string for the message
* @ap: arguments to the format string
@ -270,10 +270,10 @@ im_error_clear( void )
* Diagnostic messages are used to report details about the operation of
* functions.
*
* See also: im_diag(), im_warn().
* See also: vips_diag(), vips_warn().
*/
void
im_vdiag( const char *domain, const char *fmt, va_list ap )
vips_vdiag( const char *domain, const char *fmt, va_list ap )
{
if( !g_getenv( IM_DIAGNOSTICS ) ) {
g_mutex_lock( im__global_lock );
@ -286,7 +286,7 @@ im_vdiag( const char *domain, const char *fmt, va_list ap )
}
/**
* im_diag:
* vips_diag:
* @domain: the source of the diagnostic message
* @fmt: printf()-style format string for the message
* @Varargs: arguments to the format string
@ -297,20 +297,20 @@ im_vdiag( const char *domain, const char *fmt, va_list ap )
* Diagnostic messages are used to report details about the operation of
* functions.
*
* See also: im_vdiag(), im_warn().
* See also: vips_vdiag(), vips_warn().
*/
void
im_diag( const char *domain, const char *fmt, ... )
vips_diag( const char *domain, const char *fmt, ... )
{
va_list ap;
va_start( ap, fmt );
im_vdiag( domain, fmt, ap );
vips_vdiag( domain, fmt, ap );
va_end( ap );
}
/**
* im_vwarn:
* vips_vwarn:
* @domain: the source of the warning message
* @fmt: printf()-style format string for the message
* @ap: arguments to the format string
@ -320,10 +320,10 @@ im_diag( const char *domain, const char *fmt, ... )
*
* Warning messages are used to report things like overflow counts.
*
* See also: im_diag(), im_warn().
* See also: vips_diag(), vips_warn().
*/
void
im_vwarn( const char *domain, const char *fmt, va_list ap )
vips_vwarn( const char *domain, const char *fmt, va_list ap )
{
if( !g_getenv( IM_WARNING ) ) {
g_mutex_lock( im__global_lock );
@ -336,7 +336,7 @@ im_vwarn( const char *domain, const char *fmt, va_list ap )
}
/**
* im_warn:
* vips_warn:
* @domain: the source of the warning message
* @fmt: printf()-style format string for the message
* @Varargs: arguments to the format string
@ -346,20 +346,20 @@ im_vwarn( const char *domain, const char *fmt, va_list ap )
*
* Warning messages are used to report things like overflow counts.
*
* See also: im_diag(), im_vwarn().
* See also: vips_diag(), vips_vwarn().
*/
void
im_warn( const char *domain, const char *fmt, ... )
vips_warn( const char *domain, const char *fmt, ... )
{
va_list ap;
va_start( ap, fmt );
im_vwarn( domain, fmt, ap );
vips_vwarn( domain, fmt, ap );
va_end( ap );
}
/**
* error_exit:
* vips_error_exit:
* @fmt: printf()-style format string for the message
* @Varargs: arguments to the format string
*
@ -369,10 +369,10 @@ im_warn( const char *domain, const char *fmt, ... )
* @fmt may be %NULL, in which case only the error buffer is printed before
* exiting.
*
* See also: im_error().
* See also: vips_error().
*/
void
error_exit( const char *fmt, ... )
vips_error_exit( const char *fmt, ... )
{
if( fmt ) {
va_list ap;
@ -386,7 +386,7 @@ error_exit( const char *fmt, ... )
fprintf( stderr, "\n" );
}
fprintf( stderr, "%s", im_error_buffer() );
fprintf( stderr, "%s", vips_error_buffer() );
exit( 1 );
}

View File

@ -565,12 +565,6 @@ disc_threshold( void )
return( threshold );
}
size_t
vips_image_size( VipsImage *image )
{
return( VIPS_IMAGE_SIZEOF_LINE( image ) * image->Ysize );
}
/* Make the real underlying image: either a direct disc file, or a temp file
* somewhere.
*/
@ -1102,6 +1096,12 @@ vips_image_get_yoffset( VipsImage *image )
return( image->Yoffset );
}
size_t
vips_image_size( VipsImage *image )
{
return( VIPS_IMAGE_SIZEOF_LINE( image ) * image->Ysize );
}
void
vips_image_written( VipsImage *image )
{

View File

@ -137,7 +137,7 @@ im__open_image_file( const char *filename )
/* Open read-write failed. Fall back to open read-only.
*/
if( (fd = open( filename, MODE_READONLY )) == -1 ) {
im_error( "im__open_image_file",
vips_error( "im__open_image_file",
_( "unable to open \"%s\", %s" ),
filename, strerror( errno ) );
return( -1 );
@ -152,23 +152,23 @@ im__open_image_file( const char *filename )
* want to always be 64 bit.
*/
gint64
im__image_pixel_length( IMAGE *im )
im__image_pixel_length( VipsImage *image )
{
gint64 psize;
switch( im->Coding ) {
case IM_CODING_LABQ:
case IM_CODING_RAD:
case IM_CODING_NONE:
psize = (gint64) IM_IMAGE_SIZEOF_LINE( im ) * im->Ysize;
switch( image->Coding ) {
case VIPS_CODING_LABQ:
case VIPS_CODING_RAD:
case VIPS_CODING_NONE:
psize = vips_image_size( image );
break;
default:
psize = im->Length;
psize = image->Length;
break;
}
return( psize + im->sizeof_header );
return( psize + image->sizeof_header );
}
/* Read short/int/float LSB and MSB first.
@ -265,14 +265,15 @@ im__read_header_bytes( IMAGE *im, unsigned char *from )
int i;
im__read_4byte( 1, (unsigned char *) &im->magic, &from );
if( im->magic != IM_MAGIC_INTEL && im->magic != IM_MAGIC_SPARC ) {
im_error( "im_open", _( "\"%s\" is not a VIPS image" ),
if( im->magic != VIPS_MAGIC_INTEL &&
im->magic != VIPS_MAGIC_SPARC ) {
vips_error( "VipsImage", _( "\"%s\" is not a VIPS image" ),
im->filename );
return( -1 );
}
msb_first = im->magic == IM_MAGIC_SPARC;
msb_first = im->magic == VIPS_MAGIC_SPARC;
for( i = 0; i < IM_NUMBER( fields ); i++ )
for( i = 0; i < VIPS_NUMBER( fields ); i++ )
fields[i].read( msb_first,
&G_STRUCT_MEMBER( unsigned char, im, fields[i].offset ),
&from );
@ -293,14 +294,14 @@ im__write_header_bytes( IMAGE *im, unsigned char *to )
/* Always write the magic number MSB first.
*/
magic = im_amiMSBfirst() ? IM_MAGIC_SPARC : IM_MAGIC_INTEL;
magic = im_amiMSBfirst() ? VIPS_MAGIC_SPARC : VIPS_MAGIC_INTEL;
to[0] = magic >> 24;
to[1] = magic >> 16;
to[2] = magic >> 8;
to[3] = magic;
q = to + 4;
for( i = 0; i < IM_NUMBER( fields ); i++ )
for( i = 0; i < VIPS_NUMBER( fields ); i++ )
fields[i].write( &q,
&G_STRUCT_MEMBER( unsigned char, im,
fields[i].offset ) );
@ -326,7 +327,7 @@ read_chunk( int fd, gint64 offset, size_t length )
return( NULL );
if( read( fd, buf, length ) != (ssize_t) length ) {
im_free( buf );
im_error( "im_readhist", "%s", _( "unable to read history" ) );
vips_error( "im_readhist", "%s", _( "unable to read history" ) );
return( NULL );
}
buf[length] = '\0';
@ -358,7 +359,7 @@ im__read_extension_block( IMAGE *im, int *size )
psize = im__image_pixel_length( im );
g_assert( im->file_length > 0 );
if( im->file_length - psize > 10 * 1024 * 1024 ) {
im_error( "im_readhist",
vips_error( "im_readhist",
"%s", _( "more than a 10 megabytes of XML? "
"sufferin' succotash!" ) );
return( NULL );
@ -406,7 +407,7 @@ read_xml( IMAGE *im )
if( !(node = xmlDocGetRootElement( doc )) ||
!node->nsDef ||
!im_isprefix( NAMESPACE, (char *) node->nsDef->href ) ) {
im_error( "im__readhist",
vips_error( "im__readhist",
"%s", _( "incorrect namespace in XML" ) );
xmlFreeDoc( doc );
return( NULL );
@ -444,7 +445,7 @@ get_sprop( xmlNode *xnode, const char *name, char *buf, int sz )
return( 0 );
im_strncpy( buf, value, sz );
IM_FREEF( xmlFree, value );
VIPS_FREEF( xmlFree, value );
return( 1 );
}
@ -459,7 +460,7 @@ set_history( IMAGE *im, char *history )
/* There can be history there already if we're rewinding.
*/
IM_FREEF( im__gslist_gvalue_free, im->history_list );
VIPS_FREEF( im__gslist_gvalue_free, im->history_list );
history_list = NULL;
@ -530,7 +531,7 @@ rebuild_header_meta( IMAGE *im, xmlNode *i )
g_value_init( &value, gtype );
if( !g_value_transform( &save_value, &value ) ) {
g_value_unset( &save_value );
im_error( "im__readhist",
vips_error( "im__readhist",
"%s", _( "error transforming from "
"save format" ) );
return( -1 );
@ -639,7 +640,7 @@ set_prop( xmlNode *node, const char *name, const char *fmt, ... )
va_end( ap );
if( !xmlSetProp( node, (xmlChar *) name, (xmlChar *) value ) ) {
im_error( "im_writehist", _( "unable to set property \"%s\" "
vips_error( "im_writehist", _( "unable to set property \"%s\" "
"to value \"%s\"." ),
name, value );
return( -1 );
@ -686,7 +687,7 @@ save_fields_meta( Meta *meta, xmlNode *node )
g_value_init( &save_value, IM_TYPE_SAVE_STRING );
if( !g_value_transform( &meta->value, &save_value ) ) {
im_error( "im__writehist", "%s",
vips_error( "im__writehist", "%s",
_( "error transforming to save format" ) );
return( node );
}
@ -734,7 +735,7 @@ im__write_extension_block( IMAGE *im, void *buf, int size )
if( (length = im_file_length( im->fd )) == -1 )
return( -1 );
if( length - psize < 0 ) {
im_error( "im__write_extension_block",
vips_error( "im__write_extension_block",
"%s", _( "file has been truncated" ) );
return( -1 );
}
@ -836,7 +837,7 @@ im__writehist( IMAGE *im )
char *dump;
int dump_size;
assert( im->dtype == IM_OPENOUT );
assert( im->dtype == VIPS_IMAGE_OPENOUT );
assert( im->fd != -1 );
if( !(doc = xmlNewDoc( (xmlChar *) "1.0" )) )
@ -849,7 +850,7 @@ im__writehist( IMAGE *im )
NULL, (xmlChar *) "root", NULL )) ||
set_sprop( doc->children, "xmlns", namespace ) ||
save_fields( im, doc->children ) ) {
im_error( "im__writehist", "%s", _( "xml save error" ) );
vips_error( "im__writehist", "%s", _( "xml save error" ) );
xmlFreeDoc( doc );
return( -1 );
}
@ -858,7 +859,7 @@ im__writehist( IMAGE *im )
*/
xmlDocDumpMemory( doc, (xmlChar **) ((char *) &dump), &dump_size );
if( !dump ) {
im_error( "im__writehist", "%s", _( "xml save error" ) );
vips_error( "im__writehist", "%s", _( "xml save error" ) );
xmlFreeDoc( doc );
return( -1 );
}
@ -881,7 +882,7 @@ im__writehist( IMAGE *im )
xmlDocDumpMemory( doc, (xmlChar **) &dump2, &dump_size2 );
if( !dump2 ) {
im_error( "im__writehist", "%s", _( "xml save error" ) );
vips_error( "im__writehist", "%s", _( "xml save error" ) );
xmlFreeDoc( doc );
xmlFree( dump );
return( -1 );
@ -911,12 +912,12 @@ vips_open_input( VipsImage *image )
gint64 psize;
gint64 rsize;
image->dtype = IM_OPENIN;
image->dtype = VIPS_IMAGE_OPENIN;
if( (image->fd = im__open_image_file( image->filename )) == -1 )
return( -1 );
if( read( image->fd, header, IM_SIZEOF_HEADER ) != IM_SIZEOF_HEADER ||
im__read_header_bytes( image, header ) ) {
im_error( "vips_open_input",
vips_error( "vips_open_input",
_( "unable to read header for \"%s\", %s" ),
image->filename, strerror( errno ) );
return( -1 );
@ -929,105 +930,23 @@ vips_open_input( VipsImage *image )
return( -1 );
image->file_length = rsize;
if( psize > rsize )
im_warn( "vips_open_input",
vips_warn( "vips_open_input",
_( "unable to read data for \"%s\", %s" ),
image->filename, _( "file has been truncated" ) );
/* Set demand style. This suits a disc file we read sequentially.
*/
image->dhint = IM_THINSTRIP;
image->dhint = VIPS_DEMAND_STYLE_THINSTRIP;
/* Set the history part of im descriptor. Don't return an error if this
* fails (due to eg. corrupted XML) because it's probably mostly
* harmless.
*/
if( im__readhist( image ) ) {
im_warn( "vips_open_input", _( "error reading XML: %s" ),
im_error_buffer() );
im_error_clear();
vips_warn( "vips_open_input", _( "error reading XML: %s" ),
vips_error_buffer() );
vips_error_clear();
}
return( 0 );
}
/* Open, then mmap() read/write. This is old and deprecated API, use
* im_vips_open() in preference.
*/
int
vips_open_input_rw( VipsImage *image )
{
if( vips_open_input( image ) ||
im_mapfilerw( image ) )
return( -1 );
image->data = image->baseaddr + image->sizeof_header;
image->dtype = IM_MMAPINRW;
#ifdef DEBUG
printf( "im_openinrw: completely mmap()ing \"%s\" read-write\n",
image->filename );
#endif /*DEBUG*/
return( 0 );
}
/* Open a VIPS image for reading and byte-swap the image data if necessary. A
* ":w" at the end of the filename means we open read-write.
*/
IMAGE *
im_open_vips( const char *filename )
{
char name[FILENAME_MAX];
char mode[FILENAME_MAX];
IMAGE *im;
im_filename_split( filename, name, mode );
if( !(im = im_init( name )) )
return( NULL );
if( mode[0] == 'w' ) {
if( im_openinrw( im ) ) {
im_close( im );
return( NULL );
}
if( im_isMSBfirst( im ) != im_amiMSBfirst() ) {
im_close( im );
im_error( "im_open_vips", "%s",
_( "open for read-write for "
"native format images only" ) );
return( NULL );
}
}
else {
if( im_openin( im ) ) {
im_close( im );
return( NULL );
}
}
/* Not in native format? And needs swapping?
*/
if( im_isMSBfirst( im ) != im_amiMSBfirst() &&
im->Coding == IM_CODING_NONE &&
im->BandFmt != IM_BANDFMT_CHAR &&
im->BandFmt != IM_BANDFMT_UCHAR ) {
IMAGE *im2;
if( !(im2 = im_open( filename, "p" )) ) {
im_close( im );
return( NULL );
}
if( im_add_close_callback( im2,
(im_callback_fn) im_close, im, NULL ) ) {
im_close( im );
im_close( im2 );
return( NULL );
}
if( im_copy_swap( im, im2 ) ) {
im_close( im2 );
return( NULL );
}
im = im2;
}
return( im );
}