2011-03-29 13:12:20 +02:00
|
|
|
/* base class for all vips operations
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (C) 1991-2005 The National Gallery
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-08-28 13:46:50 +02:00
|
|
|
#define VIPS_DEBUG
|
2011-08-28 14:29:53 +02:00
|
|
|
*/
|
2011-03-29 13:12:20 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif /*HAVE_CONFIG_H*/
|
|
|
|
#include <vips/intl.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include <vips/vips.h>
|
2011-04-04 16:46:57 +02:00
|
|
|
#include <vips/debug.h>
|
2011-03-29 13:12:20 +02:00
|
|
|
|
|
|
|
#include <gobject/gvaluecollector.h>
|
|
|
|
|
|
|
|
#ifdef WITH_DMALLOC
|
|
|
|
#include <dmalloc.h>
|
|
|
|
#endif /*WITH_DMALLOC*/
|
|
|
|
|
|
|
|
/* Abstract base class for operations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
G_DEFINE_ABSTRACT_TYPE( VipsOperation, vips_operation, VIPS_TYPE_OBJECT );
|
|
|
|
|
2011-07-18 18:10:41 +02:00
|
|
|
static void
|
|
|
|
vips_operation_finalize( GObject *gobject )
|
|
|
|
{
|
|
|
|
VIPS_DEBUG_MSG( "vips_operation_finalize: %p\n", gobject );
|
|
|
|
|
|
|
|
G_OBJECT_CLASS( vips_operation_parent_class )->finalize( gobject );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vips_operation_dispose( GObject *gobject )
|
|
|
|
{
|
|
|
|
VIPS_DEBUG_MSG( "vips_operation_dispose: %p\n", gobject );
|
|
|
|
|
|
|
|
G_OBJECT_CLASS( vips_operation_parent_class )->dispose( gobject );
|
|
|
|
}
|
|
|
|
|
2011-03-29 13:12:20 +02:00
|
|
|
/* What to show about the argument.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
2011-05-13 11:08:53 +02:00
|
|
|
char *message; /* header message on first print */
|
2011-03-29 13:12:20 +02:00
|
|
|
gboolean required; /* show required args or optional */
|
|
|
|
gboolean oftype; /* "is of type" message */
|
|
|
|
int n; /* Arg number */
|
|
|
|
} VipsOperationPrint;
|
|
|
|
|
|
|
|
static void *
|
|
|
|
vips_operation_print_arg( VipsObject *object, GParamSpec *pspec,
|
|
|
|
VipsArgumentClass *argument_class,
|
|
|
|
VipsArgumentInstance *argument_instance,
|
|
|
|
VipsBuf *buf, VipsOperationPrint *print )
|
|
|
|
{
|
2011-07-22 18:28:08 +02:00
|
|
|
/* Only show construct args ... others are internal.
|
2011-03-29 13:12:20 +02:00
|
|
|
*/
|
|
|
|
if( print->required ==
|
|
|
|
((argument_class->flags & VIPS_ARGUMENT_REQUIRED) != 0) &&
|
2011-07-22 18:28:08 +02:00
|
|
|
(argument_class->flags & VIPS_ARGUMENT_CONSTRUCT) ) {
|
2011-05-13 11:08:53 +02:00
|
|
|
if( print->message && print->n == 0 )
|
|
|
|
vips_buf_appendf( buf, "%s\n", print->message );
|
|
|
|
|
2011-03-29 13:12:20 +02:00
|
|
|
if( print->oftype )
|
2011-07-22 18:28:08 +02:00
|
|
|
vips_buf_appendf( buf, " %s :: %s (%s)\n",
|
2011-05-16 18:10:08 +02:00
|
|
|
g_param_spec_get_name( pspec ),
|
|
|
|
g_type_name(
|
2011-07-22 18:28:08 +02:00
|
|
|
G_PARAM_SPEC_VALUE_TYPE( pspec ) ),
|
|
|
|
(argument_class->flags & VIPS_ARGUMENT_INPUT) ?
|
|
|
|
_( "input" ) : _( "output" ) );
|
2011-03-29 13:12:20 +02:00
|
|
|
else {
|
|
|
|
if( print->n > 0 )
|
2011-07-22 18:28:08 +02:00
|
|
|
vips_buf_appends( buf, " " );
|
2011-05-16 18:10:08 +02:00
|
|
|
vips_buf_appends( buf, g_param_spec_get_name( pspec ) );
|
2011-03-29 13:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
print->n += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2011-04-04 16:46:57 +02:00
|
|
|
#ifdef VIPS_DEBUG
|
|
|
|
static void *
|
|
|
|
vips_operation_call_argument( VipsObject *object, GParamSpec *pspec,
|
|
|
|
VipsArgumentClass *argument_class,
|
|
|
|
VipsArgumentInstance *argument_instance )
|
|
|
|
{
|
|
|
|
VipsArgument *argument = (VipsArgument *) argument_class;
|
|
|
|
|
|
|
|
printf( " %s: offset = %d ",
|
2011-05-16 18:10:08 +02:00
|
|
|
g_param_spec_get_name( argument->pspec ),
|
|
|
|
argument_class->offset );
|
2011-04-04 16:46:57 +02:00
|
|
|
if( argument_class->flags & VIPS_ARGUMENT_REQUIRED )
|
|
|
|
printf ("required " );
|
|
|
|
if( argument_class->flags & VIPS_ARGUMENT_CONSTRUCT )
|
|
|
|
printf ("construct " );
|
|
|
|
if( argument_class->flags & VIPS_ARGUMENT_SET_ONCE )
|
|
|
|
printf ("set-once " );
|
|
|
|
if( argument_instance->assigned )
|
|
|
|
printf ("assigned " );
|
|
|
|
printf( "\n" );
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
#endif /*VIPS_DEBUG*/
|
|
|
|
|
2011-03-29 13:12:20 +02:00
|
|
|
static void
|
|
|
|
vips_operation_print( VipsObject *object, VipsBuf *buf )
|
|
|
|
{
|
|
|
|
VipsOperation *operation = VIPS_OPERATION( object );
|
|
|
|
VipsObjectClass *object_class = VIPS_OBJECT_GET_CLASS( object );
|
|
|
|
VipsOperationPrint print;
|
|
|
|
|
2011-04-04 16:46:57 +02:00
|
|
|
#ifdef VIPS_DEBUG
|
|
|
|
printf( "%s args:\n", object_class->nickname );
|
|
|
|
vips_argument_map( VIPS_OBJECT( operation ),
|
|
|
|
(VipsArgumentMapFn) vips_operation_call_argument, NULL, NULL );
|
|
|
|
#endif /*VIPS_DEBUG*/
|
|
|
|
|
2011-03-29 13:12:20 +02:00
|
|
|
/* First pass through args: show the required names.
|
|
|
|
*/
|
2011-07-22 18:28:08 +02:00
|
|
|
vips_buf_appendf( buf, " %s ", object_class->nickname );
|
2011-05-13 11:08:53 +02:00
|
|
|
print.message = NULL;
|
2011-03-29 13:12:20 +02:00
|
|
|
print.required = TRUE;
|
|
|
|
print.oftype = FALSE;
|
|
|
|
print.n = 0;
|
|
|
|
vips_argument_map( VIPS_OBJECT( operation ),
|
|
|
|
(VipsArgumentMapFn) vips_operation_print_arg, buf, &print );
|
2011-07-22 18:28:08 +02:00
|
|
|
vips_buf_appends( buf, "\n" );
|
2011-03-29 13:12:20 +02:00
|
|
|
|
|
|
|
/* Show required types.
|
|
|
|
*/
|
2011-05-13 11:08:53 +02:00
|
|
|
print.message = "where:";
|
2011-03-29 13:12:20 +02:00
|
|
|
print.required = TRUE;
|
|
|
|
print.oftype = TRUE;
|
|
|
|
print.n = 0;
|
|
|
|
vips_argument_map( VIPS_OBJECT( operation ),
|
|
|
|
(VipsArgumentMapFn) vips_operation_print_arg, buf, &print );
|
|
|
|
|
|
|
|
/* Show optional args.
|
|
|
|
*/
|
2011-05-13 11:08:53 +02:00
|
|
|
print.message = "optional arguments:";
|
2011-03-29 13:12:20 +02:00
|
|
|
print.required = FALSE;
|
|
|
|
print.oftype = TRUE;
|
|
|
|
print.n = 0;
|
|
|
|
vips_argument_map( VIPS_OBJECT( operation ),
|
|
|
|
(VipsArgumentMapFn) vips_operation_print_arg, buf, &print );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vips_operation_build( VipsObject *object )
|
|
|
|
{
|
|
|
|
if( VIPS_OBJECT_CLASS( vips_operation_parent_class )->build( object ) )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vips_operation_class_init( VipsOperationClass *class )
|
|
|
|
{
|
2011-07-18 18:10:41 +02:00
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
|
2011-03-29 13:12:20 +02:00
|
|
|
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
|
|
|
|
|
2011-07-18 18:10:41 +02:00
|
|
|
gobject_class->finalize = vips_operation_finalize;
|
|
|
|
gobject_class->dispose = vips_operation_dispose;
|
|
|
|
|
2011-07-16 12:26:24 +02:00
|
|
|
vobject_class->nickname = "operation";
|
|
|
|
vobject_class->description = _( "VIPS operations" );
|
2011-03-29 13:12:20 +02:00
|
|
|
vobject_class->print = vips_operation_print;
|
|
|
|
vobject_class->build = vips_operation_build;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vips_operation_init( VipsOperation *operation )
|
|
|
|
{
|
|
|
|
/* Init our instance fields.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2011-05-19 14:48:09 +02:00
|
|
|
VipsOperation *
|
|
|
|
vips_operation_new( const char *name )
|
|
|
|
{
|
|
|
|
GType type;
|
2011-05-22 18:32:57 +02:00
|
|
|
VipsOperation *operation;
|
2011-05-19 14:48:09 +02:00
|
|
|
|
|
|
|
if( !(type = vips_type_find( "VipsOperation", name )) )
|
|
|
|
return( NULL );
|
2011-05-22 18:32:57 +02:00
|
|
|
operation = VIPS_OPERATION( g_object_new( type, NULL ) );
|
2011-05-19 14:48:09 +02:00
|
|
|
|
2011-07-18 18:10:41 +02:00
|
|
|
VIPS_DEBUG_MSG( "vips_operation_new: %s (%p)\n", name, operation );
|
|
|
|
|
2011-05-22 18:32:57 +02:00
|
|
|
return( operation );
|
|
|
|
}
|
2011-03-29 13:12:20 +02:00
|
|
|
|
2011-04-05 18:02:12 +02:00
|
|
|
static int
|
2011-05-22 18:32:57 +02:00
|
|
|
vips_operation_set_valist_required( VipsOperation *operation, va_list ap )
|
2011-04-04 16:46:57 +02:00
|
|
|
{
|
2011-05-22 18:32:57 +02:00
|
|
|
/* Set required input arguments. Can't use vips_argument_map here
|
|
|
|
* :-( because passing va_list by reference is not portable.
|
|
|
|
*/
|
|
|
|
VIPS_ARGUMENT_FOR_ALL( operation,
|
|
|
|
pspec, argument_class, argument_instance ) {
|
|
|
|
|
|
|
|
g_assert( argument_instance );
|
|
|
|
|
|
|
|
if( (argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
|
|
|
|
(argument_class->flags & VIPS_ARGUMENT_INPUT) &&
|
|
|
|
!argument_instance->assigned ) {
|
|
|
|
GValue value = { 0 };
|
|
|
|
char *msg = NULL;
|
|
|
|
|
2011-08-15 14:59:47 +02:00
|
|
|
/* It'd be nice to use G_VALUE_COLLECT_INIT(), but
|
|
|
|
* that's only available in very recent glib.
|
|
|
|
*/
|
|
|
|
g_value_init( &value,
|
|
|
|
G_PARAM_SPEC_VALUE_TYPE( pspec ) );
|
|
|
|
G_VALUE_COLLECT( &value, ap, 0, &msg );
|
2011-05-22 18:32:57 +02:00
|
|
|
|
|
|
|
if( msg ) {
|
|
|
|
VipsObjectClass *class =
|
|
|
|
VIPS_OBJECT_GET_CLASS( operation );
|
|
|
|
|
|
|
|
vips_error( class->description,
|
|
|
|
"%s", _( msg ) );
|
|
|
|
g_value_unset( &value );
|
|
|
|
g_free( msg );
|
|
|
|
|
|
|
|
return( -1 );
|
|
|
|
}
|
2011-04-04 16:46:57 +02:00
|
|
|
|
2011-04-05 18:02:12 +02:00
|
|
|
#ifdef VIPS_DEBUG
|
2011-05-22 18:32:57 +02:00
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
str = g_strdup_value_contents( &value );
|
|
|
|
VIPS_DEBUG_MSG( "\t%s = %s\n",
|
|
|
|
g_param_spec_get_name( pspec ), str );
|
|
|
|
g_free( str );
|
|
|
|
}
|
2011-04-05 18:02:12 +02:00
|
|
|
#endif /*VIPS_DEBUG */
|
|
|
|
|
2011-05-22 18:32:57 +02:00
|
|
|
g_object_set_property( G_OBJECT( operation ),
|
|
|
|
g_param_spec_get_name( pspec ), &value );
|
|
|
|
g_value_unset( &value );
|
2011-03-29 13:12:20 +02:00
|
|
|
}
|
2011-05-22 18:32:57 +02:00
|
|
|
else if( (argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
|
|
|
|
(argument_class->flags & VIPS_ARGUMENT_OUTPUT) &&
|
|
|
|
!argument_instance->assigned ) {
|
|
|
|
void *arg;
|
2011-05-19 14:48:09 +02:00
|
|
|
|
2011-05-22 18:32:57 +02:00
|
|
|
/* Output args are all pointers to places to write
|
|
|
|
* results. Skip here, we use these during the output
|
|
|
|
* phase.
|
|
|
|
*/
|
|
|
|
arg = va_arg( ap, void * );
|
2011-05-19 14:48:09 +02:00
|
|
|
|
|
|
|
#ifdef VIPS_DEBUG
|
2011-05-22 18:32:57 +02:00
|
|
|
printf( "\tskipping arg %p for %s\n",
|
|
|
|
arg, g_param_spec_get_name( pspec ) );
|
2011-05-19 14:48:09 +02:00
|
|
|
#endif /*VIPS_DEBUG */
|
|
|
|
}
|
2011-05-22 18:32:57 +02:00
|
|
|
} VIPS_ARGUMENT_FOR_ALL_END
|
2011-03-29 13:12:20 +02:00
|
|
|
|
2011-05-22 18:32:57 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vips_operation_set_valist_optional( VipsOperation *operation, va_list ap )
|
|
|
|
{
|
|
|
|
char *first_property_name;
|
2011-04-05 15:46:17 +02:00
|
|
|
|
2011-05-22 18:32:57 +02:00
|
|
|
first_property_name = va_arg( ap, char * );
|
|
|
|
g_object_set_valist( G_OBJECT (operation), first_property_name, ap );
|
2011-03-29 13:12:20 +02:00
|
|
|
|
2011-05-22 18:32:57 +02:00
|
|
|
return( 0 );
|
2011-03-29 13:12:20 +02:00
|
|
|
}
|
|
|
|
|
2011-05-19 14:48:09 +02:00
|
|
|
static void
|
2011-05-22 18:32:57 +02:00
|
|
|
vips_operation_get_valist( VipsOperation *operation, va_list ap )
|
2011-03-29 13:12:20 +02:00
|
|
|
{
|
2011-05-22 18:32:57 +02:00
|
|
|
/* Extract output arguments. Can't use vips_argument_map here
|
|
|
|
* :-( because passing va_list by reference is not portable.
|
|
|
|
*/
|
|
|
|
VIPS_ARGUMENT_FOR_ALL( operation,
|
|
|
|
pspec, argument_class, argument_instance ) {
|
|
|
|
if( (argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
|
2011-05-23 19:19:20 +02:00
|
|
|
(argument_class->flags & VIPS_ARGUMENT_INPUT) ) {
|
|
|
|
GValue value = { 0 };
|
|
|
|
char *msg = NULL;
|
|
|
|
|
|
|
|
/* Collect the arg from valist to eat it up, but don't
|
|
|
|
* do anything with it.
|
2011-08-15 14:59:47 +02:00
|
|
|
*
|
|
|
|
* It'd be nice to use G_VALUE_COLLECT_INIT(), but
|
|
|
|
* that's only available in very recent glib.
|
2011-05-23 19:19:20 +02:00
|
|
|
*/
|
2011-08-15 14:59:47 +02:00
|
|
|
g_value_init( &value,
|
|
|
|
G_PARAM_SPEC_VALUE_TYPE( pspec ) );
|
|
|
|
G_VALUE_COLLECT( &value, ap, 0, &msg );
|
2011-05-23 19:19:20 +02:00
|
|
|
g_value_unset( &value );
|
|
|
|
}
|
|
|
|
else if( (argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
|
2011-05-22 18:32:57 +02:00
|
|
|
(argument_class->flags & VIPS_ARGUMENT_OUTPUT) ) {
|
2011-05-23 19:19:20 +02:00
|
|
|
void **arg;
|
2011-05-22 18:32:57 +02:00
|
|
|
|
2011-05-23 19:19:20 +02:00
|
|
|
arg = va_arg( ap, void ** );
|
2011-05-22 18:32:57 +02:00
|
|
|
|
|
|
|
if( !argument_instance->assigned )
|
|
|
|
continue;
|
|
|
|
|
2011-05-19 14:48:09 +02:00
|
|
|
#ifdef VIPS_DEBUG
|
2011-05-23 19:19:20 +02:00
|
|
|
printf( "\twriting %s to %p\n",
|
2011-05-22 18:32:57 +02:00
|
|
|
g_param_spec_get_name( pspec ), arg );
|
2011-05-19 14:48:09 +02:00
|
|
|
#endif /*VIPS_DEBUG */
|
2011-05-19 15:35:51 +02:00
|
|
|
|
2011-05-23 19:19:20 +02:00
|
|
|
g_object_get( G_OBJECT( operation ),
|
|
|
|
g_param_spec_get_name( pspec ), arg, NULL );
|
|
|
|
|
|
|
|
/* If the pspec is an object, that will up the ref
|
|
|
|
* count. We want to hand over the ref, so we have to
|
|
|
|
* knock it down again.
|
2011-05-22 18:32:57 +02:00
|
|
|
*/
|
2011-05-23 19:19:20 +02:00
|
|
|
if( G_IS_PARAM_SPEC_OBJECT( pspec ) ) {
|
|
|
|
GObject *object;
|
|
|
|
|
|
|
|
object = *((GObject **) arg);
|
|
|
|
g_object_unref( object );
|
|
|
|
}
|
2011-05-22 18:32:57 +02:00
|
|
|
}
|
|
|
|
} VIPS_ARGUMENT_FOR_ALL_END
|
2011-03-29 13:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-04-05 18:02:12 +02:00
|
|
|
vips_call( const char *operation_name, ... )
|
2011-03-29 13:12:20 +02:00
|
|
|
{
|
|
|
|
VipsOperation *operation;
|
|
|
|
int result;
|
2011-04-05 18:02:12 +02:00
|
|
|
va_list ap;
|
|
|
|
|
2011-03-29 13:12:20 +02:00
|
|
|
|
2011-04-04 16:46:57 +02:00
|
|
|
VIPS_DEBUG_MSG( "vips_call: starting for %s ...\n", operation_name );
|
2011-03-29 13:12:20 +02:00
|
|
|
|
|
|
|
if( !(operation = vips_operation_new( operation_name ) ) )
|
|
|
|
return( -1 );
|
|
|
|
|
2011-04-04 16:46:57 +02:00
|
|
|
#ifdef VIPS_DEBUG
|
|
|
|
VIPS_DEBUG_MSG( "where:\n" );
|
|
|
|
vips_object_print( VIPS_OBJECT( operation ) );
|
|
|
|
#endif /*VIPS_DEBUG*/
|
|
|
|
|
2011-04-05 18:02:12 +02:00
|
|
|
va_start( ap, operation_name );
|
2011-05-22 18:32:57 +02:00
|
|
|
result = vips_operation_set_valist_required( operation, ap ) ||
|
|
|
|
vips_operation_set_valist_optional( operation, ap ) ||
|
2011-04-05 18:02:12 +02:00
|
|
|
vips_object_build( VIPS_OBJECT( operation ) );
|
|
|
|
va_end( ap );
|
|
|
|
|
2011-05-19 14:48:09 +02:00
|
|
|
/* Build failed: junk args and back out.
|
|
|
|
*/
|
|
|
|
if( result ) {
|
2011-06-16 14:54:13 +02:00
|
|
|
vips_argument_dispose_all( VIPS_OBJECT( operation ) );
|
2011-05-19 14:48:09 +02:00
|
|
|
g_object_unref( operation );
|
|
|
|
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Walk args again writing output.
|
|
|
|
*/
|
|
|
|
va_start( ap, operation_name );
|
|
|
|
vips_operation_get_valist( operation, ap );
|
|
|
|
va_end( ap );
|
|
|
|
|
2011-04-05 18:02:12 +02:00
|
|
|
/* The operation we have built should now have been reffed by one of
|
|
|
|
* its arguments or have finished its work. Either way, we can unref.
|
|
|
|
*/
|
|
|
|
g_object_unref( operation );
|
|
|
|
|
|
|
|
return( result );
|
|
|
|
}
|
|
|
|
|
2011-05-19 14:48:09 +02:00
|
|
|
int
|
|
|
|
vips_call_split( const char *operation_name, va_list optional, ... )
|
2011-04-05 18:02:12 +02:00
|
|
|
{
|
|
|
|
VipsOperation *operation;
|
|
|
|
int result;
|
2011-05-19 14:48:09 +02:00
|
|
|
va_list required;
|
2011-04-05 18:02:12 +02:00
|
|
|
|
2011-05-19 14:48:09 +02:00
|
|
|
VIPS_DEBUG_MSG( "vips_call_split: starting for %s ...\n",
|
|
|
|
operation_name );
|
2011-04-05 18:02:12 +02:00
|
|
|
|
|
|
|
if( !(operation = vips_operation_new( operation_name ) ) )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
#ifdef VIPS_DEBUG
|
|
|
|
VIPS_DEBUG_MSG( "where:\n" );
|
|
|
|
vips_object_print( VIPS_OBJECT( operation ) );
|
|
|
|
#endif /*VIPS_DEBUG*/
|
|
|
|
|
2011-05-19 14:48:09 +02:00
|
|
|
va_start( required, optional );
|
2011-05-22 18:32:57 +02:00
|
|
|
result = vips_operation_set_valist_required( operation, required ) ||
|
|
|
|
vips_operation_set_valist_optional( operation, optional ) ||
|
2011-04-05 15:46:17 +02:00
|
|
|
vips_object_build( VIPS_OBJECT( operation ) );
|
2011-05-19 14:48:09 +02:00
|
|
|
va_end( required );
|
2011-03-29 13:12:20 +02:00
|
|
|
|
2011-05-19 14:48:09 +02:00
|
|
|
/* Build failed: junk args and back out.
|
2011-03-29 13:12:20 +02:00
|
|
|
*/
|
2011-05-19 14:48:09 +02:00
|
|
|
if( result ) {
|
2011-06-16 14:54:13 +02:00
|
|
|
vips_argument_dispose_all( VIPS_OBJECT( operation ) );
|
2011-05-19 14:48:09 +02:00
|
|
|
g_object_unref( operation );
|
2011-04-05 15:46:17 +02:00
|
|
|
|
2011-05-19 14:48:09 +02:00
|
|
|
return( -1 );
|
|
|
|
}
|
2011-04-05 18:02:12 +02:00
|
|
|
|
2011-05-19 14:48:09 +02:00
|
|
|
/* Walk args again writing output.
|
|
|
|
*/
|
2011-04-05 18:02:12 +02:00
|
|
|
va_start( required, optional );
|
2011-05-19 14:48:09 +02:00
|
|
|
vips_operation_get_valist( operation, required );
|
2011-04-05 18:02:12 +02:00
|
|
|
va_end( required );
|
2011-04-05 15:46:17 +02:00
|
|
|
|
2011-05-19 14:48:09 +02:00
|
|
|
/* The operation we have built should now have been reffed by one of
|
|
|
|
* its arguments or have finished its work. Either way, we can unref.
|
|
|
|
*/
|
|
|
|
g_object_unref( operation );
|
|
|
|
|
2011-04-05 15:46:17 +02:00
|
|
|
return( result );
|
2011-03-29 13:12:20 +02:00
|
|
|
}
|
2011-04-20 18:41:55 +02:00
|
|
|
|
2011-05-16 18:10:08 +02:00
|
|
|
static void *
|
|
|
|
vips_call_char_option( VipsObject *object,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
VipsArgumentClass *argument_class,
|
|
|
|
VipsArgumentInstance *argument_instance,
|
|
|
|
void *a, void *b )
|
|
|
|
{
|
|
|
|
const char *name = (const char *) a;
|
|
|
|
const char *value = (const char *) b;
|
|
|
|
|
|
|
|
if( !(argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
|
|
|
|
(argument_class->flags & VIPS_ARGUMENT_CONSTRUCT) &&
|
|
|
|
!argument_instance->assigned &&
|
|
|
|
g_param_spec_get_name( pspec )[0] == name[0] )
|
|
|
|
if( vips_object_set_argument_from_string( object,
|
|
|
|
g_param_spec_get_name( pspec ), value ) )
|
|
|
|
return( object );
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
vips_call_name_option( VipsObject *object,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
VipsArgumentClass *argument_class,
|
|
|
|
VipsArgumentInstance *argument_instance,
|
|
|
|
void *a, void *b )
|
|
|
|
{
|
|
|
|
const char *name = (const char *) a;
|
|
|
|
const char *value = (const char *) b;
|
|
|
|
|
|
|
|
if( !(argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
|
|
|
|
(argument_class->flags & VIPS_ARGUMENT_CONSTRUCT) &&
|
|
|
|
!argument_instance->assigned &&
|
|
|
|
strcmp( g_param_spec_get_name( pspec ), name ) == 0 )
|
|
|
|
if( vips_object_set_argument_from_string( object,
|
|
|
|
g_param_spec_get_name( pspec ), value ) )
|
|
|
|
return( object );
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2011-05-11 15:51:47 +02:00
|
|
|
static gboolean
|
|
|
|
vips_call_options_set( const gchar *option_name, const gchar *value,
|
|
|
|
gpointer data, GError **error )
|
2011-04-20 18:41:55 +02:00
|
|
|
{
|
2011-05-11 15:51:47 +02:00
|
|
|
VipsOperation *operation = (VipsOperation *) data;
|
2011-05-16 18:10:08 +02:00
|
|
|
const char *name;
|
2011-04-20 18:41:55 +02:00
|
|
|
|
2011-05-16 09:31:55 +02:00
|
|
|
VIPS_DEBUG_MSG( "vips_call_options_set: %s = %s\n",
|
|
|
|
option_name, value );
|
|
|
|
|
2011-05-16 18:10:08 +02:00
|
|
|
/* Remove any leading "--" from the option name.
|
|
|
|
*/
|
|
|
|
for( name = option_name; *name == '-'; name++ )
|
|
|
|
;
|
|
|
|
|
|
|
|
/* If this is a single-character name, find the first unset pspec with
|
|
|
|
* that initial. Otherwise, search for a spec of that nmae.
|
|
|
|
*/
|
|
|
|
if( strlen( name ) == 1 ) {
|
|
|
|
if( vips_argument_map( VIPS_OBJECT( operation ),
|
|
|
|
vips_call_char_option,
|
|
|
|
(void *) name, (void *) value ) ) {
|
|
|
|
vips_error_g( error );
|
|
|
|
return( FALSE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if( vips_argument_map( VIPS_OBJECT( operation ),
|
|
|
|
vips_call_name_option,
|
|
|
|
(void *) name, (void *) value ) ) {
|
|
|
|
vips_error_g( error );
|
|
|
|
return( FALSE );
|
|
|
|
}
|
|
|
|
}
|
2011-04-20 18:41:55 +02:00
|
|
|
|
2011-05-11 15:51:47 +02:00
|
|
|
return( TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
vips_call_options_add( VipsObject *object,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
VipsArgumentClass *argument_class,
|
|
|
|
VipsArgumentInstance *argument_instance,
|
|
|
|
void *a, void *b )
|
|
|
|
{
|
|
|
|
GOptionGroup *group = (GOptionGroup *) a;
|
|
|
|
|
|
|
|
if( !(argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
|
|
|
|
(argument_class->flags & VIPS_ARGUMENT_CONSTRUCT) &&
|
|
|
|
!argument_instance->assigned ) {
|
|
|
|
GOptionEntry entry[2];
|
|
|
|
|
2011-05-13 15:47:23 +02:00
|
|
|
entry[0].long_name = g_param_spec_get_name( pspec );
|
|
|
|
entry[0].short_name = g_param_spec_get_name( pspec )[0];
|
2011-05-17 12:35:29 +02:00
|
|
|
if( G_IS_PARAM_SPEC_BOOLEAN( pspec ) )
|
|
|
|
entry[0].flags = G_OPTION_FLAG_NO_ARG;
|
2011-05-22 18:32:57 +02:00
|
|
|
else
|
|
|
|
entry[0].flags = 0;
|
2011-05-11 15:51:47 +02:00
|
|
|
entry[0].arg = G_OPTION_ARG_CALLBACK;
|
|
|
|
entry[0].arg_data = (gpointer) vips_call_options_set;
|
2011-05-13 15:47:23 +02:00
|
|
|
entry[0].description = g_param_spec_get_blurb( pspec );
|
2011-05-17 15:45:51 +02:00
|
|
|
if( G_IS_PARAM_SPEC_BOOLEAN( pspec ) )
|
|
|
|
entry[0].arg_description = NULL;
|
|
|
|
else
|
|
|
|
entry[0].arg_description =
|
|
|
|
g_type_name( G_PARAM_SPEC_VALUE_TYPE( pspec ) );
|
2011-05-11 15:51:47 +02:00
|
|
|
|
|
|
|
entry[1].long_name = NULL;
|
|
|
|
|
2011-05-16 09:31:55 +02:00
|
|
|
VIPS_DEBUG_MSG( "vips_call_options_add: adding %s\n",
|
|
|
|
g_param_spec_get_name( pspec ) );
|
|
|
|
|
2011-05-11 15:51:47 +02:00
|
|
|
g_option_group_add_entries( group, &entry[0] );
|
2011-04-20 18:41:55 +02:00
|
|
|
}
|
|
|
|
|
2011-05-11 15:51:47 +02:00
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2011-05-16 18:10:08 +02:00
|
|
|
void
|
|
|
|
vips_call_options( GOptionGroup *group, VipsOperation *operation )
|
2011-05-11 15:51:47 +02:00
|
|
|
{
|
|
|
|
(void) vips_argument_map( VIPS_OBJECT( operation ),
|
|
|
|
vips_call_options_add, group, NULL );
|
|
|
|
}
|
|
|
|
|
2011-07-22 18:28:08 +02:00
|
|
|
/* What we track during an argv call.
|
|
|
|
*/
|
|
|
|
typedef struct _VipsCall {
|
|
|
|
VipsOperation *operation;
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
int i;
|
|
|
|
} VipsCall;
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
vips_call_get_arg( VipsCall *call, int i )
|
|
|
|
{
|
|
|
|
if( i < 0 || i >= call->argc ) {
|
|
|
|
vips_error( VIPS_OBJECT( call->operation )->nickname,
|
|
|
|
"%s", _( "too few arguments" ) );
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( call->argv[i] );
|
|
|
|
}
|
|
|
|
|
2011-05-22 18:32:57 +02:00
|
|
|
static void *
|
|
|
|
vips_call_argv_input( VipsObject *object,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
VipsArgumentClass *argument_class,
|
|
|
|
VipsArgumentInstance *argument_instance,
|
|
|
|
void *a, void *b )
|
|
|
|
{
|
2011-07-22 18:28:08 +02:00
|
|
|
VipsCall *call = (VipsCall *) a;
|
2011-05-22 18:32:57 +02:00
|
|
|
|
|
|
|
/* Loop over all required construct args.
|
|
|
|
*/
|
|
|
|
if( (argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
|
|
|
|
(argument_class->flags & VIPS_ARGUMENT_CONSTRUCT) ) {
|
2011-05-23 18:12:09 +02:00
|
|
|
if( (argument_class->flags & VIPS_ARGUMENT_INPUT) ) {
|
2011-07-22 18:28:08 +02:00
|
|
|
const char *arg;
|
|
|
|
|
|
|
|
if( !(arg = vips_call_get_arg( call, call->i )) ||
|
|
|
|
vips_object_set_argument_from_string( object,
|
|
|
|
g_param_spec_get_name( pspec ), arg ) )
|
2011-05-22 18:32:57 +02:00
|
|
|
return( pspec );
|
2011-07-22 18:28:08 +02:00
|
|
|
|
|
|
|
call->i += 1;
|
2011-05-23 18:12:09 +02:00
|
|
|
}
|
|
|
|
else if( (argument_class->flags & VIPS_ARGUMENT_OUTPUT) ) {
|
|
|
|
if( vips_object_get_argument_needs_string( object,
|
|
|
|
g_param_spec_get_name( pspec ) ) )
|
2011-07-22 18:28:08 +02:00
|
|
|
call->i += 1;
|
2011-05-23 18:12:09 +02:00
|
|
|
}
|
2011-05-22 18:32:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
vips_call_argv_output( VipsObject *object,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
VipsArgumentClass *argument_class,
|
|
|
|
VipsArgumentInstance *argument_instance,
|
|
|
|
void *a, void *b )
|
|
|
|
{
|
2011-07-22 18:28:08 +02:00
|
|
|
VipsCall *call = (VipsCall *) a;
|
2011-05-22 18:32:57 +02:00
|
|
|
|
|
|
|
/* Loop over all required construct args.
|
|
|
|
*/
|
|
|
|
if( (argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
|
|
|
|
(argument_class->flags & VIPS_ARGUMENT_CONSTRUCT) ) {
|
2011-05-23 18:12:09 +02:00
|
|
|
if( (argument_class->flags & VIPS_ARGUMENT_INPUT) )
|
2011-07-22 18:28:08 +02:00
|
|
|
call->i += 1;
|
2011-05-23 18:12:09 +02:00
|
|
|
else if( (argument_class->flags & VIPS_ARGUMENT_OUTPUT) ) {
|
2011-07-22 18:28:08 +02:00
|
|
|
const char *arg;
|
2011-05-23 18:12:09 +02:00
|
|
|
|
|
|
|
arg = NULL;
|
|
|
|
if( vips_object_get_argument_needs_string( object,
|
|
|
|
g_param_spec_get_name( pspec ) ) ) {
|
2011-07-22 18:28:08 +02:00
|
|
|
arg = vips_call_get_arg( call, call->i );
|
|
|
|
if( !arg )
|
|
|
|
return( pspec );
|
|
|
|
|
|
|
|
call->i += 1;
|
2011-05-23 18:12:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( vips_object_get_argument_to_string( object,
|
|
|
|
g_param_spec_get_name( pspec ), arg ) )
|
|
|
|
return( pspec );
|
|
|
|
}
|
|
|
|
}
|
2011-05-22 18:32:57 +02:00
|
|
|
|
2011-05-23 18:12:09 +02:00
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
vips_call_argv_unref_output( VipsObject *object,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
VipsArgumentClass *argument_class,
|
|
|
|
VipsArgumentInstance *argument_instance,
|
|
|
|
void *a, void *b )
|
|
|
|
{
|
|
|
|
if( (argument_class->flags & VIPS_ARGUMENT_OUTPUT) &&
|
|
|
|
G_IS_PARAM_SPEC_OBJECT( pspec ) ) {
|
|
|
|
GObject *value;
|
|
|
|
|
|
|
|
g_object_get( object,
|
|
|
|
g_param_spec_get_name( pspec ), &value, NULL );
|
|
|
|
|
|
|
|
/* Doing the get refs the object, so unref the get, then unref
|
|
|
|
* again since this an an output object of the operation.
|
|
|
|
*/
|
|
|
|
g_object_unref( value );
|
|
|
|
g_object_unref( value );
|
2011-05-22 18:32:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2011-05-11 15:51:47 +02:00
|
|
|
/* Our main command-line entry point. Optional args should have been set by
|
|
|
|
* the GOption parser already, see above.
|
2011-05-13 11:08:53 +02:00
|
|
|
*
|
|
|
|
* We don't create the operation, so we must not unref it. The caller must
|
|
|
|
* unref on error too.
|
2011-05-11 15:51:47 +02:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
vips_call_argv( VipsOperation *operation, int argc, char **argv )
|
|
|
|
{
|
2011-07-22 18:28:08 +02:00
|
|
|
VipsCall call;
|
2011-05-11 15:51:47 +02:00
|
|
|
|
2011-05-13 11:08:53 +02:00
|
|
|
g_assert( argc >= 0 );
|
2011-05-11 15:51:47 +02:00
|
|
|
|
2011-05-16 09:31:55 +02:00
|
|
|
#ifdef VIPS_DEBUG
|
|
|
|
printf( "vips_call_argv: " );
|
|
|
|
vips_object_print_name( VIPS_OBJECT( operation ) );
|
|
|
|
printf( "\n" );
|
2011-08-28 13:46:50 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-05-16 09:31:55 +02:00
|
|
|
for( i = 0; i < argc; i++ )
|
|
|
|
printf( "%d) %s\n", i, argv[i] );
|
2011-08-28 13:46:50 +02:00
|
|
|
}
|
2011-05-16 09:31:55 +02:00
|
|
|
#endif /*VIPS_DEBUG*/
|
|
|
|
|
2011-07-22 18:28:08 +02:00
|
|
|
call.operation = operation;
|
|
|
|
call.argc = argc;
|
|
|
|
call.argv = argv;
|
|
|
|
|
|
|
|
call.i = 0;
|
2011-05-22 18:32:57 +02:00
|
|
|
(void) vips_argument_map( VIPS_OBJECT( operation ),
|
2011-07-22 18:28:08 +02:00
|
|
|
vips_call_argv_input, &call, NULL );
|
|
|
|
|
|
|
|
if( vips_object_build( VIPS_OBJECT( operation ) ) ) {
|
|
|
|
/* We must unref any output objects, they are holding refs to
|
|
|
|
* the operation.
|
|
|
|
*/
|
|
|
|
(void) vips_argument_map( VIPS_OBJECT( operation ),
|
|
|
|
vips_call_argv_unref_output, NULL, NULL );
|
2011-04-20 18:41:55 +02:00
|
|
|
|
|
|
|
return( -1 );
|
2011-07-22 18:28:08 +02:00
|
|
|
}
|
2011-04-20 18:41:55 +02:00
|
|
|
|
2011-07-22 18:28:08 +02:00
|
|
|
call.i = 0;
|
|
|
|
if( vips_argument_map( VIPS_OBJECT( operation ),
|
|
|
|
vips_call_argv_output, &call, NULL ) ) {
|
|
|
|
(void) vips_argument_map( VIPS_OBJECT( operation ),
|
|
|
|
vips_call_argv_unref_output, NULL, NULL );
|
|
|
|
|
|
|
|
return( -1 );
|
|
|
|
}
|
2011-05-22 18:32:57 +02:00
|
|
|
|
2011-05-23 18:12:09 +02:00
|
|
|
(void) vips_argument_map( VIPS_OBJECT( operation ),
|
|
|
|
vips_call_argv_unref_output, NULL, NULL );
|
|
|
|
|
2011-04-20 18:41:55 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|