1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
|
/* 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
*/
/*
#define VIPS_DEBUG
*/
#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>
#include <vips/debug.h>
#include <gobject/gvaluecollector.h>
/* Abstract base class for operations.
*/
G_DEFINE_ABSTRACT_TYPE( VipsOperation, vips_operation, VIPS_TYPE_OBJECT );
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 );
}
/* What to show about the argument.
*/
typedef struct {
char *message; /* header message on first print */
gboolean required; /* show required args or optional */
gboolean oftype; /* "is of type" message */
int n; /* Arg number */
} VipsOperationClassUsage;
static void *
vips_operation_class_usage_arg( VipsObjectClass *object_class,
GParamSpec *pspec, VipsArgumentClass *argument_class,
VipsBuf *buf, VipsOperationClassUsage *usage )
{
/* Only show construct args ... others are internal.
*/
if( usage->required ==
((argument_class->flags & VIPS_ARGUMENT_REQUIRED) != 0) &&
(argument_class->flags & VIPS_ARGUMENT_CONSTRUCT) ) {
if( usage->message && usage->n == 0 )
vips_buf_appendf( buf, "%s\n", usage->message );
if( usage->oftype ) {
vips_buf_appendf( buf, " %-12s - %s, %s %s\n",
g_param_spec_get_name( pspec ),
g_param_spec_get_blurb( pspec ),
(argument_class->flags & VIPS_ARGUMENT_INPUT) ?
_( "input" ) : _( "output" ),
g_type_name(
G_PARAM_SPEC_VALUE_TYPE( pspec ) ) );
}
else {
if( usage->n > 0 )
vips_buf_appends( buf, " " );
vips_buf_appends( buf, g_param_spec_get_name( pspec ) );
}
usage->n += 1;
}
return( NULL );
}
static void
vips_operation_usage( VipsOperationClass *class, VipsBuf *buf )
{
VipsObjectClass *object_class = VIPS_OBJECT_CLASS( class );
VipsOperationClassUsage usage;
/* First pass through args: show the required names.
*/
vips_buf_appendf( buf, " %s ", object_class->nickname );
usage.message = NULL;
usage.required = TRUE;
usage.oftype = FALSE;
usage.n = 0;
vips_argument_class_map( object_class,
(VipsArgumentClassMapFn) vips_operation_class_usage_arg,
buf, &usage );
vips_buf_appends( buf, "\n" );
/* Show required types.
*/
usage.message = "where:";
usage.required = TRUE;
usage.oftype = TRUE;
usage.n = 0;
vips_argument_class_map( object_class,
(VipsArgumentClassMapFn) vips_operation_class_usage_arg,
buf, &usage );
/* Show optional args.
*/
usage.message = "optional arguments:";
usage.required = FALSE;
usage.oftype = TRUE;
usage.n = 0;
vips_argument_class_map( object_class,
(VipsArgumentClassMapFn) vips_operation_class_usage_arg,
buf, &usage );
}
static void *
vips_operation_call_argument( VipsObject *object, GParamSpec *pspec,
VipsArgumentClass *argument_class,
VipsArgumentInstance *argument_instance,
void *a, void *b )
{
VipsArgument *argument = (VipsArgument *) argument_class;
printf( " %s: offset = %d ",
g_param_spec_get_name( argument->pspec ),
argument_class->offset );
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 );
}
static void
vips_operation_dump( VipsObject *object, VipsBuf *buf )
{
VipsOperation *operation = VIPS_OPERATION( object );
VipsObjectClass *object_class = VIPS_OBJECT_GET_CLASS( object );
printf( "%s args:\n", object_class->nickname );
vips_argument_map( VIPS_OBJECT( operation ),
vips_operation_call_argument, NULL, NULL );
VIPS_OBJECT_CLASS( vips_operation_parent_class )->dump( object, buf );
}
static void *
vips_operation_vips_operation_print_summary_arg( VipsObject *object,
GParamSpec *pspec,
VipsArgumentClass *argument_class,
VipsArgumentInstance *argument_instance,
void *a, void *b )
{
VipsBuf *buf = (VipsBuf *) a;
/* Just assigned required input construct args
*/
if( (argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
(argument_class->flags & VIPS_ARGUMENT_CONSTRUCT) &&
(argument_class->flags & VIPS_ARGUMENT_INPUT) &&
argument_instance->assigned ) {
const char *name = g_param_spec_get_name( pspec );
GType type = G_PARAM_SPEC_VALUE_TYPE( pspec );
GValue gvalue = { 0, };
char *str;
g_value_init( &gvalue, type );
g_object_get_property( G_OBJECT( object ), name, &gvalue );
str = g_strdup_value_contents( &gvalue );
vips_buf_appendf( buf, " %s", str );
g_free( str );
g_value_unset( &gvalue );
}
return( NULL );
}
static void
vips_operation_summary( VipsObject *object, VipsBuf *buf )
{
VipsOperation *operation = VIPS_OPERATION( object );
VipsObjectClass *object_class = VIPS_OBJECT_GET_CLASS( object );
vips_buf_appendf( buf, "%s", object_class->nickname );
vips_argument_map( VIPS_OBJECT( operation ),
vips_operation_vips_operation_print_summary_arg, buf, NULL );
VIPS_OBJECT_CLASS( vips_operation_parent_class )->
summary( object, buf );
}
static void
vips_operation_class_init( VipsOperationClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
gobject_class->finalize = vips_operation_finalize;
gobject_class->dispose = vips_operation_dispose;
vobject_class->nickname = "operation";
vobject_class->description = _( "operations" );
vobject_class->summary = vips_operation_summary;
vobject_class->dump = vips_operation_dump;
class->usage = vips_operation_usage;
}
static void
vips_operation_init( VipsOperation *operation )
{
/* Init our instance fields.
*/
}
/**
* vips_operation_class_print_usage: (skip)
* @operation_class: class to print usage for
*
* Print a usage message for the operation to stdout.
*/
void
vips_operation_class_print_usage( VipsOperationClass *operation_class )
{
char str[2048];
VipsBuf buf = VIPS_BUF_STATIC( str );
operation_class->usage( operation_class, &buf );
printf( "%s", _( "usage:" ) );
printf( "\n%s", vips_buf_all( &buf ) );
}
VipsOperation *
vips_operation_new( const char *name )
{
GType type;
VipsOperation *operation;
vips_check_init();
if( !(type = vips_type_find( "VipsOperation", name )) ) {
vips_error( "VipsOperation",
_( "class \"%s\" not found" ), name );
return( NULL );
}
operation = VIPS_OPERATION( g_object_new( type, NULL ) );
VIPS_DEBUG_MSG( "vips_operation_new: %s (%p)\n", name, operation );
return( operation );
}
/* Some systems do not have va_copy() ... this might work (it does on MSVC),
* apparently.
*
* FIXME ... this should be in configure.in
*/
#ifndef va_copy
#define va_copy(d,s) ((d) = (s))
#endif
static int
vips_operation_set_valist_required( VipsOperation *operation, va_list ap )
{
VIPS_DEBUG_MSG( "vips_operation_set_valist_required:\n" );
/* 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) ) {
VIPS_ARGUMENT_COLLECT_SET( pspec, argument_class, ap );
#ifdef VIPS_DEBUG
{
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 );
}
#endif /*VIPS_DEBUG */
g_object_set_property( G_OBJECT( operation ),
g_param_spec_get_name( pspec ), &value );
VIPS_ARGUMENT_COLLECT_GET( pspec, argument_class, ap );
#ifdef VIPS_DEBUG
printf( "\tskipping arg %p for %s\n",
arg, g_param_spec_get_name( pspec ) );
#endif /*VIPS_DEBUG */
VIPS_ARGUMENT_COLLECT_END
}
} VIPS_ARGUMENT_FOR_ALL_END
return( 0 );
}
static int
vips_operation_set_valist_optional( VipsOperation *operation, va_list ap )
{
char *name;
VIPS_DEBUG_MSG( "vips_operation_set_valist_optional:\n" );
name = va_arg( ap, char * );
while( name ) {
GParamSpec *pspec;
VipsArgumentClass *argument_class;
VipsArgumentInstance *argument_instance;
VIPS_DEBUG_MSG( "\tname = '%s' (%p)\n", name, name );
if( vips_object_get_argument( VIPS_OBJECT( operation ), name,
&pspec, &argument_class, &argument_instance ) )
return( -1 );
VIPS_ARGUMENT_COLLECT_SET( pspec, argument_class, ap );
g_object_set_property( G_OBJECT( operation ),
name, &value );
VIPS_ARGUMENT_COLLECT_GET( pspec, argument_class, ap );
VIPS_ARGUMENT_COLLECT_END
name = va_arg( ap, char * );
}
return( 0 );
}
static int
vips_operation_get_valist_required( VipsOperation *operation, va_list ap )
{
VIPS_DEBUG_MSG( "vips_operation_get_valist_required:\n" );
/* 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) ) {
VIPS_ARGUMENT_COLLECT_SET( pspec, argument_class, ap );
VIPS_ARGUMENT_COLLECT_GET( pspec, argument_class, ap );
if( !argument_instance->assigned )
continue;
#ifdef VIPS_DEBUG
printf( "\twriting %s to %p\n",
g_param_spec_get_name( pspec ), arg );
#endif /*VIPS_DEBUG */
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.
*/
if( G_IS_PARAM_SPEC_OBJECT( pspec ) ) {
GObject *object;
object = *((GObject **) arg);
g_object_unref( object );
}
VIPS_ARGUMENT_COLLECT_END
}
} VIPS_ARGUMENT_FOR_ALL_END
return( 0 );
}
static int
vips_operation_get_valist_optional( VipsOperation *operation, va_list ap )
{
char *name;
VIPS_DEBUG_MSG( "vips_operation_get_valist_optional:\n" );
name = va_arg( ap, char * );
while( name ) {
GParamSpec *pspec;
VipsArgumentClass *argument_class;
VipsArgumentInstance *argument_instance;
VIPS_DEBUG_MSG( "\tname = '%s' (%p)\n", name, name );
if( vips_object_get_argument( VIPS_OBJECT( operation ), name,
&pspec, &argument_class, &argument_instance ) )
return( -1 );
VIPS_ARGUMENT_COLLECT_SET( pspec, argument_class, ap );
/* We must collect input args as we walk the name/value list,
* but we don't do anything with them.
*/
VIPS_ARGUMENT_COLLECT_GET( pspec, argument_class, ap );
/* Here's an output arg.
*/
#ifdef VIPS_DEBUG
printf( "\twriting %s to %p\n",
g_param_spec_get_name( pspec ), arg );
#endif /*VIPS_DEBUG */
/* If the dest pointer is NULL, skip the read.
*/
if( arg ) {
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.
*/
if( G_IS_PARAM_SPEC_OBJECT( pspec ) ) {
GObject *object;
object = *((GObject **) arg);
g_object_unref( object );
}
}
VIPS_ARGUMENT_COLLECT_END
name = va_arg( ap, char * );
}
return( 0 );
}
/* This can change operation to point at an old, cached one.
*/
static int
vips_call_required_optional( VipsOperation **operation,
va_list required, va_list optional )
{
int result;
va_list a;
va_list b;
/* We need to be able to walk required and optional twice. On x64 gcc,
* vips_operation_set_valist_required() etc. will destructively alter
* the passed-in va_list. We make a copy and walk that instead.
*/
va_copy( a, required );
va_copy( b, optional );
result = vips_operation_set_valist_required( *operation, a ) ||
vips_operation_set_valist_optional( *operation, b );
va_end( a );
va_end( b );
/* Build from cache.
*/
if( vips_cache_operation_buildp( operation ) )
return( -1 );
/* Walk args again, writing output.
*/
va_copy( a, required );
va_copy( b, optional );
result = vips_operation_get_valist_required( *operation, required ) ||
vips_operation_get_valist_optional( *operation, optional );
va_end( a );
va_end( b );
return( result );
}
int
vips_call( const char *operation_name, ... )
{
VipsOperation *operation;
int result;
va_list required;
va_list optional;
VIPS_DEBUG_MSG( "vips_call: starting for %s ...\n", operation_name );
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*/
/* We have to break the va_list into separate required and optional
* components.
*
* Note the start, grab the required, then copy and reuse.
*/
va_start( required, operation_name );
va_copy( optional, required );
VIPS_ARGUMENT_FOR_ALL( operation,
pspec, argument_class, argument_instance ) {
g_assert( argument_instance );
if( (argument_class->flags & VIPS_ARGUMENT_REQUIRED) ) {
VIPS_ARGUMENT_COLLECT_SET( pspec, argument_class,
optional );
VIPS_ARGUMENT_COLLECT_GET( pspec, argument_class,
optional );
VIPS_ARGUMENT_COLLECT_END
}
} VIPS_ARGUMENT_FOR_ALL_END
result = vips_call_required_optional( &operation, required, optional );
va_end( required );
va_end( optional );
/* Failed: junk args and back out.
*/
if( result ) {
vips_object_unref_outputs( VIPS_OBJECT( operation ) );
g_object_unref( operation );
return( -1 );
}
/* 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 );
}
int
vips_call_split( const char *operation_name, va_list optional, ... )
{
VipsOperation *operation;
int result;
va_list required;
VIPS_DEBUG_MSG( "vips_call_split: starting for %s ...\n",
operation_name );
if( !(operation = vips_operation_new( operation_name ) ) )
return( -1 );
va_start( required, optional );
result = vips_call_required_optional( &operation, required, optional );
va_end( required );
/* Build failed: junk args and back out.
*/
if( result ) {
vips_object_unref_outputs( VIPS_OBJECT( operation ) );
g_object_unref( operation );
return( -1 );
}
/* 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 );
}
static void *
vips_call_find_pspec( VipsObject *object,
GParamSpec *pspec,
VipsArgumentClass *argument_class,
VipsArgumentInstance *argument_instance,
void *a, void *b )
{
const char *name = (const char *) a;
/* One char names we assume are "-x" style abbreviations, longer names
* we match the whole string.
*/
if( !(argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
(argument_class->flags & VIPS_ARGUMENT_CONSTRUCT) &&
!argument_instance->assigned )
if( (strlen( name ) == 1 &&
g_param_spec_get_name( pspec )[0] == name[0]) ||
strcmp( g_param_spec_get_name( pspec ), name ) == 0 )
return( argument_instance );
return( NULL );
}
/* Keep this stuff around for output args.
*/
typedef struct _VipsCallOptionOutput {
VipsArgumentInstance *argument_instance;
const char *value;
} VipsCallOptionOutput;
static void
vips_call_option_output( VipsObject *object,
VipsCallOptionOutput *output )
{
VipsArgumentInstance *argument_instance = output->argument_instance;
GParamSpec *pspec = ((VipsArgument *) argument_instance)->pspec;
if( vips_object_get_argument_to_string( object,
g_param_spec_get_name( pspec ), output->value ) ) {
/* FIXME .. Hmm what can we do here? If an arg is image
* output, for example, we will lose the error.
*/
}
g_free( output );
}
static gboolean
vips_call_options_set( const gchar *option_name, const gchar *value,
gpointer data, GError **error )
{
VipsOperation *operation = (VipsOperation *) data;
const char *name;
VipsArgumentInstance *argument_instance;
VipsArgumentClass *argument_class;
GParamSpec *pspec;
VIPS_DEBUG_MSG( "vips_call_options_set: %s = %s\n",
option_name, value );
/* Remove any leading "--" from the option name.
*/
for( name = option_name; *name == '-'; name++ )
;
if( !(argument_instance = (VipsArgumentInstance *)
vips_argument_map(
VIPS_OBJECT( operation ),
vips_call_find_pspec, (void *) name, NULL )) ) {
vips_error( VIPS_OBJECT_GET_CLASS( operation )->nickname,
_( "unknown argument '%s'" ), name );
return( FALSE );
}
argument_class = argument_instance->argument_class;
pspec = ((VipsArgument *) argument_instance)->pspec;
if( (argument_class->flags & VIPS_ARGUMENT_INPUT) ) {
if( vips_object_set_argument_from_string(
VIPS_OBJECT( operation ),
g_param_spec_get_name( pspec ), value ) )
return( FALSE );
#ifdef VIPS_DEBUG
{
GType type = G_PARAM_SPEC_VALUE_TYPE( pspec );
GValue gvalue = { 0, };
char *str;
g_value_init( &gvalue, type );
g_object_get_property( G_OBJECT( operation ),
g_param_spec_get_name( pspec ), &gvalue );
str = g_strdup_value_contents( &gvalue );
VIPS_DEBUG_MSG( "\tGValue %s = %s\n",
g_param_spec_get_name( pspec ), str );
g_free( str );
g_value_unset( &gvalue );
}
#endif /*VIPS_DEBUG*/
}
else if( (argument_class->flags & VIPS_ARGUMENT_OUTPUT) ) {
VipsCallOptionOutput *output;
/* We can't do output now, we have to attach a callback to do
* the processing after the operation has run.
*
* FIXME ... something like posteval or postbuild might be
* better for this?
*/
output = g_new( VipsCallOptionOutput, 1 );
output->argument_instance = argument_instance;
output->value = value;
g_signal_connect( operation, "preclose",
G_CALLBACK( vips_call_option_output ),
output );
}
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 ) {
const char *name = g_param_spec_get_name( pspec );
gboolean needs_string =
vips_object_get_argument_needs_string( object, name );
GOptionEntry entry[2];
entry[0].long_name = name;
entry[0].short_name = name[0];
entry[0].flags = 0;
if( !needs_string )
entry[0].flags |= G_OPTION_FLAG_NO_ARG;
entry[0].arg = G_OPTION_ARG_CALLBACK;
entry[0].arg_data = (gpointer) vips_call_options_set;
entry[0].description = g_param_spec_get_blurb( pspec );
if( needs_string )
entry[0].arg_description =
g_type_name( G_PARAM_SPEC_VALUE_TYPE( pspec ) );
else
entry[0].arg_description = NULL;
entry[1].long_name = NULL;
VIPS_DEBUG_MSG( "vips_call_options_add: adding %s\n", name );
g_option_group_add_entries( group, &entry[0] );
}
return( NULL );
}
void
vips_call_options( GOptionGroup *group, VipsOperation *operation )
{
(void) vips_argument_map( VIPS_OBJECT( operation ),
vips_call_options_add, group, NULL );
}
/* 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_GET_CLASS( call->operation )->nickname,
"%s", _( "too few arguments" ) );
return( NULL );
}
return( call->argv[i] );
}
static void *
vips_call_argv_input( VipsObject *object,
GParamSpec *pspec,
VipsArgumentClass *argument_class,
VipsArgumentInstance *argument_instance,
void *a, void *b )
{
VipsCall *call = (VipsCall *) a;
/* Loop over all required construct args.
*/
if( (argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
(argument_class->flags & VIPS_ARGUMENT_CONSTRUCT) ) {
const char *name = g_param_spec_get_name( pspec );
if( (argument_class->flags & VIPS_ARGUMENT_INPUT) ) {
const char *arg;
if( !(arg = vips_call_get_arg( call, call->i )) ||
vips_object_set_argument_from_string( object,
name, arg ) )
return( pspec );
call->i += 1;
}
else if( (argument_class->flags & VIPS_ARGUMENT_OUTPUT) ) {
if( vips_object_get_argument_needs_string( object,
name ) )
call->i += 1;
}
}
return( NULL );
}
static void *
vips_call_argv_output( VipsObject *object,
GParamSpec *pspec,
VipsArgumentClass *argument_class,
VipsArgumentInstance *argument_instance,
void *a, void *b )
{
VipsCall *call = (VipsCall *) a;
/* Loop over all required construct args.
*/
if( (argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
(argument_class->flags & VIPS_ARGUMENT_CONSTRUCT) ) {
if( (argument_class->flags & VIPS_ARGUMENT_INPUT) )
call->i += 1;
else if( (argument_class->flags & VIPS_ARGUMENT_OUTPUT) ) {
const char *name = g_param_spec_get_name( pspec );
const char *arg;
arg = NULL;
if( vips_object_get_argument_needs_string( object,
name ) ) {
arg = vips_call_get_arg( call, call->i );
if( !arg )
return( pspec );
call->i += 1;
}
if( vips_object_get_argument_to_string( object,
name, arg ) )
return( pspec );
}
}
return( NULL );
}
/* Our main command-line entry point. Optional args should have been set by
* the GOption parser already, see above.
*
* We don't create the operation, so we must not unref it. The caller must
* unref on error too. The caller must also call vips_object_unref_outputs() on
* all code paths.
*/
int
vips_call_argv( VipsOperation *operation, int argc, char **argv )
{
VipsCall call;
g_assert( argc >= 0 );
#ifdef VIPS_DEBUG
printf( "vips_call_argv: " );
vips_object_print_name( VIPS_OBJECT( operation ) );
printf( "\n" );
{
int i;
for( i = 0; i < argc; i++ )
printf( "%d) %s\n", i, argv[i] );
}
#endif /*VIPS_DEBUG*/
call.operation = operation;
call.argc = argc;
call.argv = argv;
call.i = 0;
if( vips_argument_map( VIPS_OBJECT( operation ),
vips_call_argv_input, &call, NULL ) )
return( -1 );
/* Any unused arguments? We must fail. Consider eg. "vips bandjoin a b
* c". This would overwrite b with a and ignore c, potentially
* disasterous.
*/
if( argc > call.i ) {
vips_error( VIPS_OBJECT_GET_CLASS( operation )->nickname,
"%s", _( "too many arguments" ) );
return( -1 );
}
/* We can't use the operation cache, we need to be able to change the
* operation pointer. The cache probably wouldn't help anyway.
*/
if( vips_object_build( VIPS_OBJECT( operation ) ) )
return( -1 );
call.i = 0;
if( vips_argument_map( VIPS_OBJECT( operation ),
vips_call_argv_output, &call, NULL ) )
return( -1 );
return( 0 );
}
|