2 * mlt_properties.c -- base properties class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "mlt_properties.h"
23 #include "mlt_property.h"
29 /* ---------------- // Private Implementation // ---------------- */
31 /** Private implementation of the property list.
41 mlt_properties mirror;
45 /** Basic implementation.
48 int mlt_properties_init( mlt_properties this, void *child )
51 memset( this, 0, sizeof( struct mlt_properties_s ) );
53 // Assign the child of the object
56 // Allocate the private structure
57 this->private = calloc( sizeof( property_list ), 1 );
59 return this->private == NULL;
62 /** Constructor for stand alone object.
65 mlt_properties mlt_properties_new( )
67 // Construct a standalone properties object
68 mlt_properties this = calloc( sizeof( struct mlt_properties_s ), 1 );
71 mlt_properties_init( this, NULL );
77 static inline int generate_hash( char *name )
82 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
86 /** Special case - when a container (such as fezzik) is protecting another
87 producer, we need to ensure that properties are passed through to the
91 static inline void mlt_properties_do_mirror( mlt_properties this, char *name )
93 property_list *list = this->private;
94 if ( list->mirror != NULL && strcmp( name, "in" ) && strcmp( name, "out" ) )
96 char *value = mlt_properties_get( this, name );
98 mlt_properties_set( list->mirror, name, value );
102 /** Allow the specification of a mirror.
105 void mlt_properties_mirror( mlt_properties this, mlt_properties that )
107 property_list *list = this->private;
111 /** Inherit all serialisable properties from that into this.
114 int mlt_properties_inherit( mlt_properties this, mlt_properties that )
116 int count = mlt_properties_count( that );
118 for ( i = 0; i < count; i ++ )
120 char *value = mlt_properties_get_value( that, i );
123 char *name = mlt_properties_get_name( that, i );
124 mlt_properties_set( this, name, value );
130 /** Pass all properties from 'that' that match the prefix to 'this' (excluding the prefix).
133 int mlt_properties_pass( mlt_properties this, mlt_properties that, char *prefix )
135 int count = mlt_properties_count( that );
136 int length = strlen( prefix );
138 for ( i = 0; i < count; i ++ )
140 char *name = mlt_properties_get_name( that, i );
141 if ( !strncmp( name, prefix, length ) )
143 char *value = mlt_properties_get_value( that, i );
145 mlt_properties_set( this, name + length, value );
151 /** Locate a property by name
154 static inline mlt_property mlt_properties_find( mlt_properties this, char *name )
156 property_list *list = this->private;
157 mlt_property value = NULL;
158 int key = generate_hash( name );
159 int i = list->hash[ key ] - 1;
163 // Check if we're hashed
164 if ( list->count > 0 &&
165 name[ 0 ] == list->name[ i ][ 0 ] &&
166 !strcmp( list->name[ i ], name ) )
167 value = list->value[ i ];
170 for ( i = list->count - 1; value == NULL && i >= 0; i -- )
171 if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
172 value = list->value[ i ];
178 /** Add a new property.
181 static mlt_property mlt_properties_add( mlt_properties this, char *name )
183 property_list *list = this->private;
184 int key = generate_hash( name );
186 // Check that we have space and resize if necessary
187 if ( list->count == list->size )
190 list->name = realloc( list->name, list->size * sizeof( char * ) );
191 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
194 // Assign name/value pair
195 list->name[ list->count ] = strdup( name );
196 list->value[ list->count ] = mlt_property_init( );
198 // Assign to hash table
199 if ( list->hash[ key ] == 0 )
200 list->hash[ key ] = list->count + 1;
202 // Return and increment count accordingly
203 return list->value[ list->count ++ ];
206 /** Fetch a property by name - this includes add if not found.
209 static mlt_property mlt_properties_fetch( mlt_properties this, char *name )
211 // Try to find an existing property first
212 mlt_property property = mlt_properties_find( this, name );
214 // If it wasn't found, create one
215 if ( property == NULL )
216 property = mlt_properties_add( this, name );
218 // Return the property
222 /** Set the property.
225 int mlt_properties_set( mlt_properties this, char *name, char *value )
229 // Fetch the property to work with
230 mlt_property property = mlt_properties_fetch( this, name );
232 // Set it if not NULL
233 if ( property != NULL )
235 error = mlt_property_set_string( property, value );
236 mlt_properties_do_mirror( this, name );
242 /** Get a string value by name.
245 char *mlt_properties_get( mlt_properties this, char *name )
247 mlt_property value = mlt_properties_find( this, name );
248 return value == NULL ? NULL : mlt_property_get_string( value );
251 /** Get a name by index.
254 char *mlt_properties_get_name( mlt_properties this, int index )
256 property_list *list = this->private;
257 if ( index >= 0 && index < list->count )
258 return list->name[ index ];
262 /** Get a string value by index.
265 char *mlt_properties_get_value( mlt_properties this, int index )
267 property_list *list = this->private;
268 if ( index >= 0 && index < list->count )
269 return mlt_property_get_string( list->value[ index ] );
273 /** Get a data value by index.
276 void *mlt_properties_get_data_at( mlt_properties this, int index, int *size )
278 property_list *list = this->private;
279 if ( index >= 0 && index < list->count )
280 return mlt_property_get_data( list->value[ index ], size );
284 /** Return the number of items in the list.
287 int mlt_properties_count( mlt_properties this )
289 property_list *list = this->private;
293 /** Set a value by parsing a name=value string
296 int mlt_properties_parse( mlt_properties this, char *namevalue )
298 char *name = strdup( namevalue );
299 char *value = strdup( namevalue );
302 if ( strchr( name, '=' ) )
304 *( strchr( name, '=' ) ) = '\0';
305 strcpy( value, strchr( value, '=' ) + 1 );
312 if ( strlen( value ) > 1 && value[ 0 ] == '\"' )
314 strcpy( value, value + 1 );
315 if ( value[ strlen( value ) - 1 ] == '\"' )
316 value[ strlen( value ) - 1 ] = '\0';
319 error = mlt_properties_set( this, name, value );
327 /** Get a value associated to the name.
330 int mlt_properties_get_int( mlt_properties this, char *name )
332 mlt_property value = mlt_properties_find( this, name );
333 return value == NULL ? 0 : mlt_property_get_int( value );
336 /** Set a value associated to the name.
339 int mlt_properties_set_int( mlt_properties this, char *name, int value )
343 // Fetch the property to work with
344 mlt_property property = mlt_properties_fetch( this, name );
346 // Set it if not NULL
347 if ( property != NULL )
349 error = mlt_property_set_int( property, value );
350 mlt_properties_do_mirror( this, name );
356 /** Get a value associated to the name.
359 double mlt_properties_get_double( mlt_properties this, char *name )
361 mlt_property value = mlt_properties_find( this, name );
362 return value == NULL ? 0 : mlt_property_get_double( value );
365 /** Set a value associated to the name.
368 int mlt_properties_set_double( mlt_properties this, char *name, double value )
372 // Fetch the property to work with
373 mlt_property property = mlt_properties_fetch( this, name );
375 // Set it if not NULL
376 if ( property != NULL )
378 error = mlt_property_set_double( property, value );
379 mlt_properties_do_mirror( this, name );
385 /** Get a value associated to the name.
388 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
390 mlt_property value = mlt_properties_find( this, name );
391 return value == NULL ? 0 : mlt_property_get_position( value );
394 /** Set a value associated to the name.
397 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
401 // Fetch the property to work with
402 mlt_property property = mlt_properties_fetch( this, name );
404 // Set it if not NULL
405 if ( property != NULL )
407 error = mlt_property_set_position( property, value );
408 mlt_properties_do_mirror( this, name );
414 /** Get a value associated to the name.
417 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
419 mlt_property value = mlt_properties_find( this, name );
420 return value == NULL ? NULL : mlt_property_get_data( value, length );
423 /** Set a value associated to the name.
426 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
430 // Fetch the property to work with
431 mlt_property property = mlt_properties_fetch( this, name );
433 // Set it if not NULL
434 if ( property != NULL )
435 error = mlt_property_set_data( property, value, length, destroy, serialise );
440 /** Rename a property.
443 int mlt_properties_rename( mlt_properties this, char *source, char *dest )
445 mlt_property value = mlt_properties_find( this, dest );
449 property_list *list = this->private;
453 for ( i = 0; i < list->count; i ++ )
455 if ( !strcmp( list->name[ i ], source ) )
457 free( list->name[ i ] );
458 list->name[ i ] = strdup( dest );
459 list->hash[ generate_hash( dest ) ] = i + 1;
465 return value != NULL;
468 /** Dump the properties.
471 void mlt_properties_dump( mlt_properties this, FILE *output )
473 property_list *list = this->private;
475 for ( i = 0; i < list->count; i ++ )
476 fprintf( stderr, "%s = %s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
482 void mlt_properties_close( mlt_properties this )
484 property_list *list = this->private;
487 // Clean up names and values
488 for ( index = list->count - 1; index >= 0; index -- )
490 free( list->name[ index ] );
491 mlt_property_close( list->value[ index ] );
499 // Free this now if this has no child
500 if ( this->child == NULL )