more robust enum printing

This commit is contained in:
John Cupitt 2012-01-27 17:14:59 +00:00
parent 7330c244a4
commit c197e2f90e
11 changed files with 63 additions and 42 deletions

View File

@ -466,7 +466,7 @@ vips_arithmetic_compile( VipsArithmeticClass *class )
for( i = 0; i < VIPS_FORMAT_LAST; i++ )
if( class->vector_program[i] )
printf( "%s ",
VIPS_ENUM_NICK( VIPS_TYPE_BAND_FORMAT, i ) );
vips_enum_nick( VIPS_TYPE_BAND_FORMAT, i ) );
printf( "\n" );
#endif /*DEBUG*/
}

View File

@ -211,18 +211,18 @@ im_init( const char *filename )
}
/* Prettyprint various header fields. Just for vips7 compat, use
* VIPS_ENUM_VALUE() instead.
* vips_enum_value() instead.
*/
const char *im_Type2char( VipsInterpretation type )
{ return( VIPS_ENUM_STRING( VIPS_TYPE_INTERPRETATION, type ) ); }
{ return( vips_enum_string( VIPS_TYPE_INTERPRETATION, type ) ); }
const char *im_BandFmt2char( VipsBandFormat format )
{ return( VIPS_ENUM_STRING( VIPS_TYPE_BAND_FORMAT, format ) ); }
{ return( vips_enum_string( VIPS_TYPE_BAND_FORMAT, format ) ); }
const char *im_Coding2char( VipsCoding coding )
{ return( VIPS_ENUM_STRING( VIPS_TYPE_CODING, coding ) ); }
{ return( vips_enum_string( VIPS_TYPE_CODING, coding ) ); }
const char *im_dtype2char( VipsImageType n )
{ return( VIPS_ENUM_STRING( VIPS_TYPE_IMAGE_TYPE, n ) ); }
{ return( vips_enum_string( VIPS_TYPE_IMAGE_TYPE, n ) ); }
const char *im_dhint2char( VipsDemandStyle style )
{ return( VIPS_ENUM_STRING( VIPS_TYPE_DEMAND_STYLE, style ) ); }
{ return( vips_enum_string( VIPS_TYPE_DEMAND_STYLE, style ) ); }
/* Old names for enums, for compat.
*/

View File

@ -814,7 +814,7 @@ vips_foreign_save_summary_class( VipsObjectClass *object_class, VipsBuf *buf )
summary_class( object_class, buf );
vips_buf_appendf( buf, ", %s",
VIPS_ENUM_NICK( VIPS_TYPE_SAVEABLE, class->saveable ) );
vips_enum_nick( VIPS_TYPE_SAVEABLE, class->saveable ) );
}
/* Can we write this filename with this file?

View File

@ -148,12 +148,8 @@ G_STMT_START { \
#define VIPS_CLIP_NONE( V, SEQ ) {}
/* Look up the const char * for an enum value.
*/
#define VIPS_ENUM_STRING( ENUM, VALUE ) \
(g_enum_get_value( g_type_class_ref( ENUM ), VALUE )->value_name)
#define VIPS_ENUM_NICK( ENUM, VALUE ) \
(g_enum_get_value( g_type_class_ref( ENUM ), VALUE )->value_nick)
const char *vips_enum_string( GType enm, int value );
const char *vips_enum_nick( GType enm, int value );
gboolean vips_slist_equal( GSList *l1, GSList *l2 );
void *vips_slist_map2( GSList *list, VipsSListMap2Fn fn, void *a, void *b );
@ -166,7 +162,8 @@ GSList *vips_slist_filter( GSList *list, VipsSListMap2Fn fn, void *a, void *b );
void vips_slist_free_all( GSList *list );
void *vips_map_equal( void *a, void *b );
void *vips_hash_table_map( GHashTable *hash, VipsSListMap2Fn fn, void *a, void *b );
void *vips_hash_table_map( GHashTable *hash,
VipsSListMap2Fn fn, void *a, void *b );
char *vips_strncpy( char *dest, const char *src, int n );
char *vips_strrstr( const char *haystack, const char *needle );

View File

@ -747,7 +747,7 @@ vips_check_format( const char *domain, VipsImage *im, VipsBandFormat fmt )
if( im->BandFmt != fmt ) {
vips_error( domain,
_( "image must be %s" ),
VIPS_ENUM_STRING( VIPS_TYPE_BAND_FORMAT, fmt ) );
vips_enum_string( VIPS_TYPE_BAND_FORMAT, fmt ) );
return( -1 );
}

View File

@ -295,13 +295,13 @@ vips_demand_hint_array( VipsImage *image, VipsDemandStyle hint, VipsImage **in )
#ifdef DEBUG
printf( "vips_demand_hint_array: set dhint for \"%s\" to %s\n",
im->filename,
VIPS_ENUM_NICK( VIPS_TYPE_DEMAND_STYLE, image->dhint ) );
vips_enum_nick( VIPS_TYPE_DEMAND_STYLE, image->dhint ) );
printf( "\toperation requested %s\n",
VIPS_ENUM_NICK( VIPS_TYPE_DEMAND_STYLE, hint ) );
vips_enum_nick( VIPS_TYPE_DEMAND_STYLE, hint ) );
printf( "\tinputs were:\n" );
printf( "\t" );
for( i = 0; in[i]; i++ )
printf( "%s ", VIPS_ENUM_NICK( VIPS_TYPE_DEMAND_STYLE,
printf( "%s ", vips_enum_nick( VIPS_TYPE_DEMAND_STYLE,
in[i]->dhint ) );
printf( "\n" );
#endif /*DEBUG*/
@ -680,7 +680,7 @@ vips_image_generate( VipsImage *image,
*/
vips_error( "VipsImage",
_( "unable to output to a %s image" ),
VIPS_ENUM_NICK( VIPS_TYPE_IMAGE_TYPE,
vips_enum_nick( VIPS_TYPE_IMAGE_TYPE,
image->dtype ) );
return( -1 );
}

View File

@ -342,13 +342,13 @@ print_field_fn( VipsImage *image, const char *field, GValue *value, void *a )
*/
extra = NULL;
if( strcmp( field, "coding" ) == 0 )
extra = VIPS_ENUM_NICK(
extra = vips_enum_nick(
VIPS_TYPE_CODING, g_value_get_int( value ) );
else if( strcmp( field, "format" ) == 0 )
extra = VIPS_ENUM_NICK(
extra = vips_enum_nick(
VIPS_TYPE_BAND_FORMAT, g_value_get_int( value ) );
else if( strcmp( field, "interpretation" ) == 0 )
extra = VIPS_ENUM_NICK(
extra = vips_enum_nick(
VIPS_TYPE_INTERPRETATION, g_value_get_int( value ) );
str_value = g_strdup_value_contents( value );
@ -375,10 +375,10 @@ vips_image_dump( VipsObject *object, VipsBuf *buf )
vips_image_get_bands( image ) ),
vips_image_get_width( image ),
vips_image_get_height( image ),
VIPS_ENUM_NICK( VIPS_TYPE_BAND_FORMAT,
vips_enum_nick( VIPS_TYPE_BAND_FORMAT,
vips_image_get_format( image ) ),
vips_image_get_bands( image ),
VIPS_ENUM_NICK( VIPS_TYPE_INTERPRETATION,
vips_enum_nick( VIPS_TYPE_INTERPRETATION,
vips_image_get_interpretation( image ) ) );
VIPS_OBJECT_CLASS( vips_image_parent_class )->dump( object, buf );
@ -403,15 +403,15 @@ vips_image_summary( VipsObject *object, VipsBuf *buf )
" %s, %d band, %s",
" %s, %d bands, %s",
vips_image_get_bands( image ) ),
VIPS_ENUM_NICK( VIPS_TYPE_BAND_FORMAT,
vips_enum_nick( VIPS_TYPE_BAND_FORMAT,
vips_image_get_format( image ) ),
vips_image_get_bands( image ),
VIPS_ENUM_NICK( VIPS_TYPE_INTERPRETATION,
vips_enum_nick( VIPS_TYPE_INTERPRETATION,
vips_image_get_interpretation( image ) ) );
}
else {
vips_buf_appendf( buf, ", %s",
VIPS_ENUM_NICK( VIPS_TYPE_CODING,
vips_enum_nick( VIPS_TYPE_CODING,
vips_image_get_coding( image ) ) );
}
@ -1872,7 +1872,7 @@ vips_image_write_line( VipsImage *image, int ypos, VipsPel *linebuffer )
default:
vips_error( "VipsImage",
_( "unable to output to a %s image" ),
VIPS_ENUM_STRING( VIPS_TYPE_IMAGE_TYPE,
vips_enum_string( VIPS_TYPE_IMAGE_TYPE,
image->dtype ) );
return( -1 );
}

View File

@ -1093,7 +1093,7 @@ vips_region_prepare( VipsRegion *reg, VipsRect *r )
default:
vips_error( "vips_region_prepare",
_( "unable to input from a %s image" ),
VIPS_ENUM_STRING( VIPS_TYPE_DEMAND_STYLE, im->dtype ) );
vips_enum_string( VIPS_TYPE_DEMAND_STYLE, im->dtype ) );
return( -1 );
}
@ -1285,7 +1285,7 @@ vips_region_prepare_to( VipsRegion *reg,
default:
vips_error( "vips_region_prepare_to",
_( "unable to input from a %s image" ),
VIPS_ENUM_NICK( VIPS_TYPE_DEMAND_STYLE, im->dtype ) );
vips_enum_nick( VIPS_TYPE_DEMAND_STYLE, im->dtype ) );
return( -1 );
}

View File

@ -1313,8 +1313,8 @@ vips__token_need( const char *p, VipsToken need_token,
return( NULL );
if( token != need_token ) {
vips_error( "get_token", _( "expected %s, saw %s" ),
VIPS_ENUM_NICK( VIPS_TYPE_TOKEN, need_token ),
VIPS_ENUM_NICK( VIPS_TYPE_TOKEN, token ) );
vips_enum_nick( VIPS_TYPE_TOKEN, need_token ),
vips_enum_nick( VIPS_TYPE_TOKEN, token ) );
return( NULL );
}
@ -1570,3 +1570,27 @@ vips__parse_size( const char *size_string )
return( size );
}
/* Look up the const char * for an enum value.
*/
const char *
vips_enum_string( GType enm, int v )
{
GEnumValue *value;
if( !(value = g_enum_get_value( g_type_class_ref( enm ), v )) )
return( "(null)" );
return( value->value_name );
}
const char *
vips_enum_nick( GType enm, int v )
{
GEnumValue *value;
if( !(value = g_enum_get_value( g_type_class_ref( enm ), v )) )
return( "(null)" );
return( value->value_nick );
}

View File

@ -68,7 +68,7 @@ static char *xsize = NULL;
static char *ysize = NULL;
static char *bands = NULL;
static char *format = NULL;
static char *type = NULL;
static char *interpretation = NULL;
static char *coding = NULL;
static char *xres = NULL;
static char *yres = NULL;
@ -88,7 +88,7 @@ static GOptionEntry entries[] = {
N_( "set Bands to N" ), "N" },
{ "format", 'f', 0, G_OPTION_ARG_STRING, &format,
N_( "set BandFmt to F (eg. uchar, float)" ), "F" },
{ "interpretation", 'i', 0, G_OPTION_ARG_STRING, &type,
{ "interpretation", 'i', 0, G_OPTION_ARG_STRING, &interpretation,
N_( "set interpretation to I (eg. xyz)" ), "I" },
{ "coding", 'c', 0, G_OPTION_ARG_STRING, &coding,
N_( "set Coding to C (eg. labq)" ), "C" },
@ -106,7 +106,7 @@ static GOptionEntry entries[] = {
N_( "set Xsize to N (deprecated, use width)" ), "N" },
{ "ysize", 'y', 0, G_OPTION_ARG_STRING, &ysize,
N_( "set Ysize to N (deprecated, use height)" ), "N" },
{ "type", 't', 0, G_OPTION_ARG_STRING, &type,
{ "type", 't', 0, G_OPTION_ARG_STRING, &interpretation,
N_( "set Type to T (deprecated, use interpretation)" ), "T" },
{ NULL }
};
@ -180,9 +180,9 @@ main( int argc, char **argv )
error_exit( _( "bad format %s" ), format );
im->Bbits = im_bits_of_fmt( im->BandFmt );
}
if( type ) {
if( (im->Type = im_char2Type( type )) < 0 )
error_exit( _( "bad type %s" ), type );
if( interpretation ) {
if( (im->Type = im_char2Type( interpretation )) < 0 )
error_exit( _( "bad interpretation %s" ), interpretation );
}
if( coding ) {
if( (im->Coding = im_char2Coding( coding )) < 0 )

View File

@ -110,13 +110,13 @@ print_field_fn( VipsImage *image, const char *field, GValue *value, void *a )
*/
extra = NULL;
if( strcmp( field, "coding" ) == 0 )
extra = VIPS_ENUM_NICK(
extra = vips_enum_nick(
VIPS_TYPE_CODING, g_value_get_int( value ) );
else if( strcmp( field, "format" ) == 0 )
extra = VIPS_ENUM_NICK(
extra = vips_enum_nick(
VIPS_TYPE_BAND_FORMAT, g_value_get_int( value ) );
else if( strcmp( field, "interpretation" ) == 0 )
extra = VIPS_ENUM_NICK(
extra = vips_enum_nick(
VIPS_TYPE_INTERPRETATION, g_value_get_int( value ) );
if( *many )