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 /** Memory leak checks.
48 #ifdef _MLT_PROPERTY_CHECKS_
49 static int properties_created = 0;
50 static int properties_destroyed = 0;
53 /** Basic implementation.
56 int mlt_properties_init( mlt_properties this, void *child )
60 #ifdef _MLT_PROPERTY_CHECKS_
61 // Increment number of properties created
62 properties_created ++;
66 memset( this, 0, sizeof( struct mlt_properties_s ) );
68 // Assign the child of the object
71 // Allocate the local structure
72 this->local = calloc( sizeof( property_list ), 1 );
75 // Check that initialisation was successful
76 return this != NULL && this->local == NULL;
79 /** Constructor for stand alone object.
82 mlt_properties mlt_properties_new( )
84 // Construct a standalone properties object
85 mlt_properties this = calloc( sizeof( struct mlt_properties_s ), 1 );
88 mlt_properties_init( this, NULL );
94 /** Load properties from a file.
97 mlt_properties mlt_properties_load( char *filename )
99 // Construct a standalone properties object
100 mlt_properties this = mlt_properties_new( );
105 FILE *file = fopen( filename, "r" );
107 // Load contents of file
113 // Read each string from the file
114 while( fgets( temp, 1024, file ) )
117 temp[ strlen( temp ) - 1 ] = '\0';
119 // Parse and set the property
120 if ( strcmp( temp, "" ) && temp[ 0 ] != '#' )
121 mlt_properties_parse( this, temp );
129 // Return the pointer
133 static inline int generate_hash( char *name )
138 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
142 /** Special case - when a container (such as fezzik) is protecting another
143 producer, we need to ensure that properties are passed through to the
147 static inline void mlt_properties_do_mirror( mlt_properties this, char *name )
149 property_list *list = this->local;
150 if ( list->mirror != NULL )
152 char *value = mlt_properties_get( this, name );
154 mlt_properties_set( list->mirror, name, value );
158 /** Allow the specification of a mirror.
161 void mlt_properties_mirror( mlt_properties this, mlt_properties that )
163 property_list *list = this->local;
167 /** Inherit all serialisable properties from that into this.
170 int mlt_properties_inherit( mlt_properties this, mlt_properties that )
172 int count = mlt_properties_count( that );
174 for ( i = 0; i < count; i ++ )
176 char *value = mlt_properties_get_value( that, i );
179 char *name = mlt_properties_get_name( that, i );
180 mlt_properties_set( this, name, value );
186 /** Pass all properties from 'that' that match the prefix to 'this' (excluding the prefix).
189 int mlt_properties_pass( mlt_properties this, mlt_properties that, char *prefix )
191 int count = mlt_properties_count( that );
192 int length = strlen( prefix );
194 for ( i = 0; i < count; i ++ )
196 char *name = mlt_properties_get_name( that, i );
197 if ( !strncmp( name, prefix, length ) )
199 char *value = mlt_properties_get_value( that, i );
201 mlt_properties_set( this, name + length, value );
207 /** Locate a property by name
210 static inline mlt_property mlt_properties_find( mlt_properties this, char *name )
212 property_list *list = this->local;
213 mlt_property value = NULL;
214 int key = generate_hash( name );
215 int i = list->hash[ key ] - 1;
219 // Check if we're hashed
220 if ( list->count > 0 &&
221 name[ 0 ] == list->name[ i ][ 0 ] &&
222 !strcmp( list->name[ i ], name ) )
223 value = list->value[ i ];
226 for ( i = list->count - 1; value == NULL && i >= 0; i -- )
227 if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
228 value = list->value[ i ];
234 /** Add a new property.
237 static mlt_property mlt_properties_add( mlt_properties this, char *name )
239 property_list *list = this->local;
240 int key = generate_hash( name );
242 // Check that we have space and resize if necessary
243 if ( list->count == list->size )
246 list->name = realloc( list->name, list->size * sizeof( char * ) );
247 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
250 // Assign name/value pair
251 list->name[ list->count ] = strdup( name );
252 list->value[ list->count ] = mlt_property_init( );
254 // Assign to hash table
255 if ( list->hash[ key ] == 0 )
256 list->hash[ key ] = list->count + 1;
258 // Return and increment count accordingly
259 return list->value[ list->count ++ ];
262 /** Fetch a property by name - this includes add if not found.
265 static mlt_property mlt_properties_fetch( mlt_properties this, char *name )
267 // Try to find an existing property first
268 mlt_property property = mlt_properties_find( this, name );
270 // If it wasn't found, create one
271 if ( property == NULL )
272 property = mlt_properties_add( this, name );
274 // Return the property
278 /** Set the property.
281 int mlt_properties_set( mlt_properties this, char *name, char *value )
285 // Fetch the property to work with
286 mlt_property property = mlt_properties_fetch( this, name );
288 // Set it if not NULL
289 if ( property != NULL )
291 error = mlt_property_set_string( property, value );
292 mlt_properties_do_mirror( this, name );
298 /** Set or default the property.
301 int mlt_properties_set_or_default( mlt_properties this, char *name, char *value, char *def )
303 return mlt_properties_set( this, name, value == NULL ? def : value );
306 /** Get a string value by name.
309 char *mlt_properties_get( mlt_properties this, char *name )
311 mlt_property value = mlt_properties_find( this, name );
312 return value == NULL ? NULL : mlt_property_get_string( value );
315 /** Get a name by index.
318 char *mlt_properties_get_name( mlt_properties this, int index )
320 property_list *list = this->local;
321 if ( index >= 0 && index < list->count )
322 return list->name[ index ];
326 /** Get a string value by index.
329 char *mlt_properties_get_value( mlt_properties this, int index )
331 property_list *list = this->local;
332 if ( index >= 0 && index < list->count )
333 return mlt_property_get_string( list->value[ index ] );
337 /** Get a data value by index.
340 void *mlt_properties_get_data_at( mlt_properties this, int index, int *size )
342 property_list *list = this->local;
343 if ( index >= 0 && index < list->count )
344 return mlt_property_get_data( list->value[ index ], size );
348 /** Return the number of items in the list.
351 int mlt_properties_count( mlt_properties this )
353 property_list *list = this->local;
357 /** Set a value by parsing a name=value string
360 int mlt_properties_parse( mlt_properties this, char *namevalue )
362 char *name = strdup( namevalue );
365 char *ptr = strchr( name, '=' );
373 value = strdup( ptr );
378 value = strdup( ptr );
379 if ( value != NULL && value[ strlen( value ) - 1 ] == '\"' )
380 value[ strlen( value ) - 1 ] = '\0';
385 value = strdup( "" );
388 error = mlt_properties_set( this, name, value );
396 /** Get a value associated to the name.
399 int mlt_properties_get_int( mlt_properties this, char *name )
401 mlt_property value = mlt_properties_find( this, name );
402 return value == NULL ? 0 : mlt_property_get_int( value );
405 /** Set a value associated to the name.
408 int mlt_properties_set_int( mlt_properties this, char *name, int value )
412 // Fetch the property to work with
413 mlt_property property = mlt_properties_fetch( this, name );
415 // Set it if not NULL
416 if ( property != NULL )
418 error = mlt_property_set_int( property, value );
419 mlt_properties_do_mirror( this, name );
425 /** Get a value associated to the name.
428 double mlt_properties_get_double( mlt_properties this, char *name )
430 mlt_property value = mlt_properties_find( this, name );
431 return value == NULL ? 0 : mlt_property_get_double( value );
434 /** Set a value associated to the name.
437 int mlt_properties_set_double( mlt_properties this, char *name, double value )
441 // Fetch the property to work with
442 mlt_property property = mlt_properties_fetch( this, name );
444 // Set it if not NULL
445 if ( property != NULL )
447 error = mlt_property_set_double( property, value );
448 mlt_properties_do_mirror( this, name );
454 /** Get a value associated to the name.
457 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
459 mlt_property value = mlt_properties_find( this, name );
460 return value == NULL ? 0 : mlt_property_get_position( value );
463 /** Set a value associated to the name.
466 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
470 // Fetch the property to work with
471 mlt_property property = mlt_properties_fetch( this, name );
473 // Set it if not NULL
474 if ( property != NULL )
476 error = mlt_property_set_position( property, value );
477 mlt_properties_do_mirror( this, name );
483 /** Get a value associated to the name.
486 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
488 mlt_property value = mlt_properties_find( this, name );
489 return value == NULL ? NULL : mlt_property_get_data( value, length );
492 /** Set a value associated to the name.
495 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
499 // Fetch the property to work with
500 mlt_property property = mlt_properties_fetch( this, name );
502 // Set it if not NULL
503 if ( property != NULL )
504 error = mlt_property_set_data( property, value, length, destroy, serialise );
509 /** Rename a property.
512 int mlt_properties_rename( mlt_properties this, char *source, char *dest )
514 mlt_property value = mlt_properties_find( this, dest );
518 property_list *list = this->local;
522 for ( i = 0; i < list->count; i ++ )
524 if ( !strcmp( list->name[ i ], source ) )
526 free( list->name[ i ] );
527 list->name[ i ] = strdup( dest );
528 list->hash[ generate_hash( dest ) ] = i + 1;
534 return value != NULL;
537 /** Dump the properties.
540 void mlt_properties_dump( mlt_properties this, FILE *output )
542 property_list *list = this->local;
544 for ( i = 0; i < list->count; i ++ )
545 if ( mlt_properties_get( this, list->name[ i ] ) != NULL )
546 fprintf( output, "%s=%s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
552 void mlt_properties_close( mlt_properties this )
556 property_list *list = this->local;
559 // Clean up names and values
560 for ( index = list->count - 1; index >= 0; index -- )
562 free( list->name[ index ] );
563 mlt_property_close( list->value[ index ] );
571 // Free this now if this has no child
572 if ( this->child == NULL )
575 #ifdef _MLT_PROPERTY_CHECKS_
576 // Increment destroyed count
577 properties_destroyed ++;
579 // Show current stats - these should match when the app is closed
580 fprintf( stderr, "Created %d, destroyed %d\n", properties_created, properties_destroyed );