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 /** Load properties from a file.
80 mlt_properties mlt_properties_load( char *filename )
82 // Construct a standalone properties object
83 mlt_properties this = mlt_properties_new( );
88 FILE *file = fopen( filename, "r" );
90 // Load contents of file
96 // Read each string from the file
97 while( fgets( temp, 1024, file ) )
100 temp[ strlen( temp ) - 1 ] = '\0';
102 // Parse and set the property
103 if ( strcmp( temp, "" ) )
104 mlt_properties_parse( this, temp );
112 // Return the pointer
116 static inline int generate_hash( char *name )
121 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
125 /** Special case - when a container (such as fezzik) is protecting another
126 producer, we need to ensure that properties are passed through to the
130 static inline void mlt_properties_do_mirror( mlt_properties this, char *name )
132 property_list *list = this->private;
133 if ( list->mirror != NULL )
135 char *value = mlt_properties_get( this, name );
137 mlt_properties_set( list->mirror, name, value );
141 /** Allow the specification of a mirror.
144 void mlt_properties_mirror( mlt_properties this, mlt_properties that )
146 property_list *list = this->private;
150 /** Inherit all serialisable properties from that into this.
153 int mlt_properties_inherit( mlt_properties this, mlt_properties that )
155 int count = mlt_properties_count( that );
157 for ( i = 0; i < count; i ++ )
159 char *value = mlt_properties_get_value( that, i );
162 char *name = mlt_properties_get_name( that, i );
163 mlt_properties_set( this, name, value );
169 /** Pass all properties from 'that' that match the prefix to 'this' (excluding the prefix).
172 int mlt_properties_pass( mlt_properties this, mlt_properties that, char *prefix )
174 int count = mlt_properties_count( that );
175 int length = strlen( prefix );
177 for ( i = 0; i < count; i ++ )
179 char *name = mlt_properties_get_name( that, i );
180 if ( !strncmp( name, prefix, length ) )
182 char *value = mlt_properties_get_value( that, i );
184 mlt_properties_set( this, name + length, value );
190 /** Locate a property by name
193 static inline mlt_property mlt_properties_find( mlt_properties this, char *name )
195 property_list *list = this->private;
196 mlt_property value = NULL;
197 int key = generate_hash( name );
198 int i = list->hash[ key ] - 1;
202 // Check if we're hashed
203 if ( list->count > 0 &&
204 name[ 0 ] == list->name[ i ][ 0 ] &&
205 !strcmp( list->name[ i ], name ) )
206 value = list->value[ i ];
209 for ( i = list->count - 1; value == NULL && i >= 0; i -- )
210 if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
211 value = list->value[ i ];
217 /** Add a new property.
220 static mlt_property mlt_properties_add( mlt_properties this, char *name )
222 property_list *list = this->private;
223 int key = generate_hash( name );
225 // Check that we have space and resize if necessary
226 if ( list->count == list->size )
229 list->name = realloc( list->name, list->size * sizeof( char * ) );
230 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
233 // Assign name/value pair
234 list->name[ list->count ] = strdup( name );
235 list->value[ list->count ] = mlt_property_init( );
237 // Assign to hash table
238 if ( list->hash[ key ] == 0 )
239 list->hash[ key ] = list->count + 1;
241 // Return and increment count accordingly
242 return list->value[ list->count ++ ];
245 /** Fetch a property by name - this includes add if not found.
248 static mlt_property mlt_properties_fetch( mlt_properties this, char *name )
250 // Try to find an existing property first
251 mlt_property property = mlt_properties_find( this, name );
253 // If it wasn't found, create one
254 if ( property == NULL )
255 property = mlt_properties_add( this, name );
257 // Return the property
261 /** Set the property.
264 int mlt_properties_set( mlt_properties this, char *name, char *value )
268 // Fetch the property to work with
269 mlt_property property = mlt_properties_fetch( this, name );
271 // Set it if not NULL
272 if ( property != NULL )
274 error = mlt_property_set_string( property, value );
275 mlt_properties_do_mirror( this, name );
281 /** Set or default the property.
284 int mlt_properties_set_or_default( mlt_properties this, char *name, char *value, char *def )
286 return mlt_properties_set( this, name, value == NULL ? def : value );
289 /** Get a string value by name.
292 char *mlt_properties_get( mlt_properties this, char *name )
294 mlt_property value = mlt_properties_find( this, name );
295 return value == NULL ? NULL : mlt_property_get_string( value );
298 /** Get a name by index.
301 char *mlt_properties_get_name( mlt_properties this, int index )
303 property_list *list = this->private;
304 if ( index >= 0 && index < list->count )
305 return list->name[ index ];
309 /** Get a string value by index.
312 char *mlt_properties_get_value( mlt_properties this, int index )
314 property_list *list = this->private;
315 if ( index >= 0 && index < list->count )
316 return mlt_property_get_string( list->value[ index ] );
320 /** Get a data value by index.
323 void *mlt_properties_get_data_at( mlt_properties this, int index, int *size )
325 property_list *list = this->private;
326 if ( index >= 0 && index < list->count )
327 return mlt_property_get_data( list->value[ index ], size );
331 /** Return the number of items in the list.
334 int mlt_properties_count( mlt_properties this )
336 property_list *list = this->private;
340 /** Set a value by parsing a name=value string
343 int mlt_properties_parse( mlt_properties this, char *namevalue )
345 char *name = strdup( namevalue );
346 char *value = strdup( namevalue );
349 if ( strchr( name, '=' ) )
351 *( strchr( name, '=' ) ) = '\0';
352 strcpy( value, strchr( value, '=' ) + 1 );
359 if ( strlen( value ) > 1 && value[ 0 ] == '\"' )
361 strcpy( value, value + 1 );
362 if ( value[ strlen( value ) - 1 ] == '\"' )
363 value[ strlen( value ) - 1 ] = '\0';
366 error = mlt_properties_set( this, name, value );
374 /** Get a value associated to the name.
377 int mlt_properties_get_int( mlt_properties this, char *name )
379 mlt_property value = mlt_properties_find( this, name );
380 return value == NULL ? 0 : mlt_property_get_int( value );
383 /** Set a value associated to the name.
386 int mlt_properties_set_int( mlt_properties this, char *name, int value )
390 // Fetch the property to work with
391 mlt_property property = mlt_properties_fetch( this, name );
393 // Set it if not NULL
394 if ( property != NULL )
396 error = mlt_property_set_int( property, value );
397 mlt_properties_do_mirror( this, name );
403 /** Get a value associated to the name.
406 double mlt_properties_get_double( mlt_properties this, char *name )
408 mlt_property value = mlt_properties_find( this, name );
409 return value == NULL ? 0 : mlt_property_get_double( value );
412 /** Set a value associated to the name.
415 int mlt_properties_set_double( mlt_properties this, char *name, double value )
419 // Fetch the property to work with
420 mlt_property property = mlt_properties_fetch( this, name );
422 // Set it if not NULL
423 if ( property != NULL )
425 error = mlt_property_set_double( property, value );
426 mlt_properties_do_mirror( this, name );
432 /** Get a value associated to the name.
435 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
437 mlt_property value = mlt_properties_find( this, name );
438 return value == NULL ? 0 : mlt_property_get_position( value );
441 /** Set a value associated to the name.
444 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
448 // Fetch the property to work with
449 mlt_property property = mlt_properties_fetch( this, name );
451 // Set it if not NULL
452 if ( property != NULL )
454 error = mlt_property_set_position( property, value );
455 mlt_properties_do_mirror( this, name );
461 /** Get a value associated to the name.
464 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
466 mlt_property value = mlt_properties_find( this, name );
467 return value == NULL ? NULL : mlt_property_get_data( value, length );
470 /** Set a value associated to the name.
473 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
477 // Fetch the property to work with
478 mlt_property property = mlt_properties_fetch( this, name );
480 // Set it if not NULL
481 if ( property != NULL )
482 error = mlt_property_set_data( property, value, length, destroy, serialise );
487 /** Rename a property.
490 int mlt_properties_rename( mlt_properties this, char *source, char *dest )
492 mlt_property value = mlt_properties_find( this, dest );
496 property_list *list = this->private;
500 for ( i = 0; i < list->count; i ++ )
502 if ( !strcmp( list->name[ i ], source ) )
504 free( list->name[ i ] );
505 list->name[ i ] = strdup( dest );
506 list->hash[ generate_hash( dest ) ] = i + 1;
512 return value != NULL;
515 /** Dump the properties.
518 void mlt_properties_dump( mlt_properties this, FILE *output )
520 property_list *list = this->private;
522 for ( i = 0; i < list->count; i ++ )
523 fprintf( stderr, "%s = %s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
529 void mlt_properties_close( mlt_properties this )
531 property_list *list = this->private;
534 // Clean up names and values
535 for ( index = list->count - 1; index >= 0; index -- )
537 free( list->name[ index ] );
538 mlt_property_close( list->value[ index ] );
546 // Free this now if this has no child
547 if ( this->child == NULL )