From c03d9440cb2156cfeb437477123f847becf296ba Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Wed, 23 Apr 2014 12:57:49 +0100 Subject: [PATCH] added vips_object_set_from_string() --- ChangeLog | 1 + TODO | 15 +++++------- libvips/include/vips/object.h | 1 + libvips/iofuncs/object.c | 44 ++++++++++++++++++++++++++++++++--- 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index 60095594..e28eef50 100644 --- a/ChangeLog +++ b/ChangeLog @@ -24,6 +24,7 @@ - add ".vips" as an alternative suffix for vips files - added vips_tiffload_buffer() - added vips_foreign_load_buffer(), vips_foreign_save_buffer() +- added vips_object_set_from_string() 6/3/14 started 7.38.6 - grey ramp minimum was wrong diff --git a/TODO b/TODO index f86cd8b7..bd957857 100644 --- a/TODO +++ b/TODO @@ -6,22 +6,19 @@ - set options from suffix - no. 2 needs something like: + use vips__find_rightmost_brackets() and vips_object_set_from_string() - vips_object_set_options_from_string( object, "[a=12,b=13]" ); +- use vips_object_set_from_string() for vips_foreign_load_options() and + friends - perhaps with the outermost brackets optional? - - maybe vips_object_set_from_string()? - - read object.c and see how close we are to this model now - -- clean up foreign.c, there seems to be some cruft - use this for dzsave_buffer + +- clean up foreign.c, there seems to be some cruft + - vips_filename_suffix_match() is used by vips_foreign_load_new_from_foreign_sub(), but it splits on ':' ... argh! diff --git a/libvips/include/vips/object.h b/libvips/include/vips/object.h index a23af7ec..2489c439 100644 --- a/libvips/include/vips/object.h +++ b/libvips/include/vips/object.h @@ -587,6 +587,7 @@ VipsObject *vips_object_new( GType type, int vips_object_set_valist( VipsObject *object, va_list ap ); int vips_object_set( VipsObject *object, ... ) __attribute__((sentinel)); +int vips_object_set_from_string( VipsObject *object, const char *string ); VipsObject *vips_object_new_from_string( VipsObjectClass *object_class, const char *p ); diff --git a/libvips/iofuncs/object.c b/libvips/iofuncs/object.c index dd07472c..ad538d31 100644 --- a/libvips/iofuncs/object.c +++ b/libvips/iofuncs/object.c @@ -2054,7 +2054,7 @@ vips_object_set_valist( VipsObject *object, va_list ap ) * Input arguments are given in-line, output arguments are given as pointers * to where the output value should be written. * - * See also: vips_object_set_valist(). + * See also: vips_object_set_valist(), vips_object_set_from_string(). * * Returns: 0 on success, -1 on error */ @@ -2072,7 +2072,7 @@ vips_object_set( VipsObject *object, ... ) } /* Set object args from a string. @p should be the initial left bracket and - * there should be no tokens after the matching right bracket. + * there should be no tokens after the matching right bracket. @p is modified. */ static int vips_object_set_args( VipsObject *object, const char *p ) @@ -2154,6 +2154,44 @@ vips_object_set_args( VipsObject *object, const char *p ) return( 0 ); } +/** + * vips_object_set_from_string: + * @object: object to set arguments on + * @string: arguments as a string + * + * Set object arguments from a string. The string can be something like + * "a=12", or "a = 12, b = 13", or "fred". The string can optionally be + * enclosed in brackets. + * + * You'd typically use this between creating the object and building it. + * + * See also: vips_object_set(), vips_object_build(), + * vips_cache_operation_buildp(). + * + * Returns: 0 on success, -1 on error + */ +int +vips_object_set_from_string( VipsObject *object, const char *string ) +{ + const char *q; + VipsToken token; + char buffer[VIPS_PATH_MAX]; + char str[VIPS_PATH_MAX]; + + vips_strncpy( buffer, string, VIPS_PATH_MAX ); + + /* Does string start with a bracket? If it doesn't, enclose the whole + * thing in []. + */ + if( !(q = vips__token_get( buffer, &token, str, VIPS_PATH_MAX )) !! + token != VIPS_TOKEN_LEFT ) + vips_snprintf( buffer, VIPS_PATH_MAX, "[%s]", string ); + else + vips_strncpy( buffer, string, VIPS_PATH_MAX ); + + return( vips_object_set_args( object, buffer ) ); +} + VipsObject * vips_object_new_from_string( VipsObjectClass *object_class, const char *p ) { @@ -2177,7 +2215,7 @@ vips_object_new_from_string( VipsObjectClass *object_class, const char *p ) /* More tokens there? Set any other args. */ if( q && - vips_object_set_args( object, q ) ) { + vips_object_set_from_string( object, q ) ) { g_object_unref( object ); return( NULL ); }