improve file testing

and add some checks that the load subclasses have been defined
correctly
This commit is contained in:
John Cupitt 2021-01-01 11:12:15 +00:00
parent 6fb7ca73a4
commit 0e7f2124c7

View File

@ -381,10 +381,13 @@ vips_foreign_init( VipsForeign *object )
static void * static void *
file_add_class( VipsForeignClass *class, GSList **files ) file_add_class( VipsForeignClass *class, GSList **files )
{ {
/* Append so we don't reverse the list of files. Sort will not reorder /* We exclude "rawload" as it has a different API.
* items of equal priority.
*/ */
*files = g_slist_append( *files, class ); if( !vips_isprefix( "rawload", VIPS_OBJECT_CLASS( class )->nickname ) )
/* Append so we don't reverse the list of files. Sort will
* not reorder items of equal priority.
*/
*files = g_slist_append( *files, class );
return( NULL ); return( NULL );
} }
@ -437,6 +440,7 @@ vips_foreign_map( const char *base, VipsSListMap2Fn fn, void *a, void *b )
} }
#endif /*DEBUG*/ #endif /*DEBUG*/
result = vips_slist_map2( files, fn, a, b ); result = vips_slist_map2( files, fn, a, b );
g_slist_free( files ); g_slist_free( files );
return( result ); return( result );
@ -496,14 +500,21 @@ vips_foreign_find_load_sub( VipsForeignLoadClass *load_class,
VipsObjectClass *object_class = VIPS_OBJECT_CLASS( load_class ); VipsObjectClass *object_class = VIPS_OBJECT_CLASS( load_class );
VipsForeignClass *class = VIPS_FOREIGN_CLASS( load_class ); VipsForeignClass *class = VIPS_FOREIGN_CLASS( load_class );
/* Ignore the buffer and source loaders.
*/
if( vips_ispostfix( object_class->nickname, "_buffer" ) ||
vips_ispostfix( object_class->nickname, "_source" ) )
return( NULL );
#ifdef DEBUG #ifdef DEBUG
printf( "vips_foreign_find_load_sub: %s\n", printf( "vips_foreign_find_load_sub: %s\n",
VIPS_OBJECT_CLASS( class )->nickname ); VIPS_OBJECT_CLASS( class )->nickname );
#endif /*DEBUG*/ #endif /*DEBUG*/
if( load_class->is_a && /* Try to sniff the filetype from the first few bytes, if we can,
!vips_ispostfix( object_class->nickname, "_buffer" ) && * otherwise fall back to checking the filename suffix.
!vips_ispostfix( object_class->nickname, "_source" ) ) { */
if( load_class->is_a ) {
if( load_class->is_a( filename ) ) if( load_class->is_a( filename ) )
return( load_class ); return( load_class );
@ -511,14 +522,13 @@ vips_foreign_find_load_sub( VipsForeignLoadClass *load_class,
printf( "vips_foreign_find_load_sub: is_a failed\n" ); printf( "vips_foreign_find_load_sub: is_a failed\n" );
#endif /*DEBUG*/ #endif /*DEBUG*/
} }
else if( class->suffs && else if( class->suffs ) {
vips_filename_suffix_match( filename, class->suffs ) ) if( vips_filename_suffix_match( filename, class->suffs ) )
return( load_class ); return( load_class );
else {
#ifdef DEBUG
printf( "vips_foreign_find_load_sub: suffix match failed\n" );
#endif /*DEBUG*/
} }
else
g_warning( "loader %s has no is_a method and no suffix list",
object_class->nickname );
return( NULL ); return( NULL );
} }
@ -606,10 +616,18 @@ vips_foreign_find_load_buffer_sub( VipsForeignLoadClass *load_class,
{ {
VipsObjectClass *object_class = VIPS_OBJECT_CLASS( load_class ); VipsObjectClass *object_class = VIPS_OBJECT_CLASS( load_class );
if( load_class->is_a_buffer && /* Skip non-buffer loaders.
vips_ispostfix( object_class->nickname, "_buffer" ) && */
load_class->is_a_buffer( *buf, *len ) ) if( !vips_ispostfix( object_class->nickname, "_buffer" ) )
return( load_class ); return( NULL );
if( load_class->is_a_buffer ) {
if( load_class->is_a_buffer( *buf, *len ) )
return( load_class );
}
else
g_warning( "loader %s has no is_a_buffer method",
object_class->nickname );
return( NULL ); return( NULL );
} }
@ -656,9 +674,13 @@ vips_foreign_find_load_source_sub( void *item, void *a, void *b )
VipsForeignLoadClass *load_class = VIPS_FOREIGN_LOAD_CLASS( item ); VipsForeignLoadClass *load_class = VIPS_FOREIGN_LOAD_CLASS( item );
VipsSource *source = VIPS_SOURCE( a ); VipsSource *source = VIPS_SOURCE( a );
if( load_class->is_a_source && /* Skip non-source loaders.
vips_ispostfix( object_class->nickname, "_source" ) ) { */
/* We may have done a read() rather than a sniff() in one of if( !vips_ispostfix( object_class->nickname, "_source" ) )
return( NULL );
if( load_class->is_a_source ) {
/* We may have done a _read() rather than a _sniff() in one of
* the is_a testers. Always rewind. * the is_a testers. Always rewind.
*/ */
(void) vips_source_rewind( source ); (void) vips_source_rewind( source );
@ -666,6 +688,9 @@ vips_foreign_find_load_source_sub( void *item, void *a, void *b )
if( load_class->is_a_source( source ) ) if( load_class->is_a_source( source ) )
return( load_class ); return( load_class );
} }
else
g_warning( "loader %s has no is_a_source method",
object_class->nickname );
return( NULL ); return( NULL );
} }