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"
30 /* ---------------- // Private Implementation // ---------------- */
32 /** Private implementation of the property list.
42 mlt_properties mirror;
46 /** Memory leak checks.
49 #ifdef _MLT_PROPERTY_CHECKS_
50 static int properties_created = 0;
51 static int properties_destroyed = 0;
54 /** Basic implementation.
57 int mlt_properties_init( mlt_properties this, void *child )
61 #ifdef _MLT_PROPERTY_CHECKS_
62 // Increment number of properties created
63 properties_created ++;
67 memset( this, 0, sizeof( struct mlt_properties_s ) );
69 // Assign the child of the object
72 // Allocate the local structure
73 this->local = calloc( sizeof( property_list ), 1 );
76 // Check that initialisation was successful
77 return this != NULL && this->local == NULL;
80 /** Constructor for stand alone object.
83 mlt_properties mlt_properties_new( )
85 // Construct a standalone properties object
86 mlt_properties this = calloc( sizeof( struct mlt_properties_s ), 1 );
89 mlt_properties_init( this, NULL );
95 /** Load properties from a file.
98 mlt_properties mlt_properties_load( char *filename )
100 // Construct a standalone properties object
101 mlt_properties this = mlt_properties_new( );
106 FILE *file = fopen( filename, "r" );
108 // Load contents of file
114 // Read each string from the file
115 while( fgets( temp, 1024, file ) )
118 temp[ strlen( temp ) - 1 ] = '\0';
120 // Parse and set the property
121 if ( strcmp( temp, "" ) && temp[ 0 ] != '#' )
122 mlt_properties_parse( this, temp );
130 // Return the pointer
134 static inline int generate_hash( char *name )
139 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
143 /** Special case - when a container (such as fezzik) is protecting another
144 producer, we need to ensure that properties are passed through to the
148 static inline void mlt_properties_do_mirror( mlt_properties this, char *name )
150 property_list *list = this->local;
151 if ( list->mirror != NULL )
153 char *value = mlt_properties_get( this, name );
155 mlt_properties_set( list->mirror, name, value );
159 /** Allow the specification of a mirror.
162 void mlt_properties_mirror( mlt_properties this, mlt_properties that )
164 property_list *list = this->local;
168 /** Inherit all serialisable properties from that into this.
171 int mlt_properties_inherit( mlt_properties this, mlt_properties that )
173 int count = mlt_properties_count( that );
175 for ( i = 0; i < count; i ++ )
177 char *value = mlt_properties_get_value( that, i );
180 char *name = mlt_properties_get_name( that, i );
181 mlt_properties_set( this, name, value );
187 /** Pass all properties from 'that' that match the prefix to 'this' (excluding the prefix).
190 int mlt_properties_pass( mlt_properties this, mlt_properties that, char *prefix )
192 int count = mlt_properties_count( that );
193 int length = strlen( prefix );
195 for ( i = 0; i < count; i ++ )
197 char *name = mlt_properties_get_name( that, i );
198 if ( !strncmp( name, prefix, length ) )
200 char *value = mlt_properties_get_value( that, i );
202 mlt_properties_set( this, name + length, value );
208 /** Locate a property by name
211 static inline mlt_property mlt_properties_find( mlt_properties this, char *name )
213 property_list *list = this->local;
214 mlt_property value = NULL;
215 int key = generate_hash( name );
216 int i = list->hash[ key ] - 1;
220 // Check if we're hashed
221 if ( list->count > 0 &&
222 name[ 0 ] == list->name[ i ][ 0 ] &&
223 !strcmp( list->name[ i ], name ) )
224 value = list->value[ i ];
227 for ( i = list->count - 1; value == NULL && i >= 0; i -- )
228 if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
229 value = list->value[ i ];
235 /** Add a new property.
238 static mlt_property mlt_properties_add( mlt_properties this, char *name )
240 property_list *list = this->local;
241 int key = generate_hash( name );
243 // Check that we have space and resize if necessary
244 if ( list->count == list->size )
247 list->name = realloc( list->name, list->size * sizeof( char * ) );
248 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
251 // Assign name/value pair
252 list->name[ list->count ] = strdup( name );
253 list->value[ list->count ] = mlt_property_init( );
255 // Assign to hash table
256 if ( list->hash[ key ] == 0 )
257 list->hash[ key ] = list->count + 1;
259 // Return and increment count accordingly
260 return list->value[ list->count ++ ];
263 /** Fetch a property by name - this includes add if not found.
266 static mlt_property mlt_properties_fetch( mlt_properties this, char *name )
268 // Try to find an existing property first
269 mlt_property property = mlt_properties_find( this, name );
271 // If it wasn't found, create one
272 if ( property == NULL )
273 property = mlt_properties_add( this, name );
275 // Return the property
279 /** Set the property.
282 int mlt_properties_set( mlt_properties this, char *name, char *value )
286 // Fetch the property to work with
287 mlt_property property = mlt_properties_fetch( this, name );
289 // Set it if not NULL
290 if ( property != NULL && ( value == NULL || value[ 0 ] != '@' ) )
292 error = mlt_property_set_string( property, value );
293 mlt_properties_do_mirror( this, name );
295 else if ( property != NULL && value[ 0 ] == '@' )
304 while ( *value != '\0' )
306 int length = strcspn( value, "+-*/" );
308 // Get the identifier
309 strncpy( id, value, length );
313 // Determine the value
314 if ( isdigit( id[ 0 ] ) )
315 current = atof( id );
317 current = mlt_properties_get_int( this, id );
319 // Apply the operation
337 op = *value != '\0' ? *value ++ : ' ';
340 error = mlt_property_set_int( property, total );
341 mlt_properties_do_mirror( this, name );
347 /** Set or default the property.
350 int mlt_properties_set_or_default( mlt_properties this, char *name, char *value, char *def )
352 return mlt_properties_set( this, name, value == NULL ? def : value );
355 /** Get a string value by name.
358 char *mlt_properties_get( mlt_properties this, char *name )
360 mlt_property value = mlt_properties_find( this, name );
361 return value == NULL ? NULL : mlt_property_get_string( value );
364 /** Get a name by index.
367 char *mlt_properties_get_name( mlt_properties this, int index )
369 property_list *list = this->local;
370 if ( index >= 0 && index < list->count )
371 return list->name[ index ];
375 /** Get a string value by index.
378 char *mlt_properties_get_value( mlt_properties this, int index )
380 property_list *list = this->local;
381 if ( index >= 0 && index < list->count )
382 return mlt_property_get_string( list->value[ index ] );
386 /** Get a data value by index.
389 void *mlt_properties_get_data_at( mlt_properties this, int index, int *size )
391 property_list *list = this->local;
392 if ( index >= 0 && index < list->count )
393 return mlt_property_get_data( list->value[ index ], size );
397 /** Return the number of items in the list.
400 int mlt_properties_count( mlt_properties this )
402 property_list *list = this->local;
406 /** Set a value by parsing a name=value string
409 int mlt_properties_parse( mlt_properties this, char *namevalue )
411 char *name = strdup( namevalue );
414 char *ptr = strchr( name, '=' );
422 value = strdup( ptr );
427 value = strdup( ptr );
428 if ( value != NULL && value[ strlen( value ) - 1 ] == '\"' )
429 value[ strlen( value ) - 1 ] = '\0';
434 value = strdup( "" );
437 error = mlt_properties_set( this, name, value );
445 /** Get a value associated to the name.
448 int mlt_properties_get_int( mlt_properties this, char *name )
450 mlt_property value = mlt_properties_find( this, name );
451 return value == NULL ? 0 : mlt_property_get_int( value );
454 /** Set a value associated to the name.
457 int mlt_properties_set_int( mlt_properties this, char *name, int value )
461 // Fetch the property to work with
462 mlt_property property = mlt_properties_fetch( this, name );
464 // Set it if not NULL
465 if ( property != NULL )
467 error = mlt_property_set_int( property, value );
468 mlt_properties_do_mirror( this, name );
474 /** Get a value associated to the name.
477 double mlt_properties_get_double( mlt_properties this, char *name )
479 mlt_property value = mlt_properties_find( this, name );
480 return value == NULL ? 0 : mlt_property_get_double( value );
483 /** Set a value associated to the name.
486 int mlt_properties_set_double( mlt_properties this, char *name, double value )
490 // Fetch the property to work with
491 mlt_property property = mlt_properties_fetch( this, name );
493 // Set it if not NULL
494 if ( property != NULL )
496 error = mlt_property_set_double( property, value );
497 mlt_properties_do_mirror( this, name );
503 /** Get a value associated to the name.
506 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
508 mlt_property value = mlt_properties_find( this, name );
509 return value == NULL ? 0 : mlt_property_get_position( value );
512 /** Set a value associated to the name.
515 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
519 // Fetch the property to work with
520 mlt_property property = mlt_properties_fetch( this, name );
522 // Set it if not NULL
523 if ( property != NULL )
525 error = mlt_property_set_position( property, value );
526 mlt_properties_do_mirror( this, name );
532 /** Get a value associated to the name.
535 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
537 mlt_property value = mlt_properties_find( this, name );
538 return value == NULL ? NULL : mlt_property_get_data( value, length );
541 /** Set a value associated to the name.
544 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
548 // Fetch the property to work with
549 mlt_property property = mlt_properties_fetch( this, name );
551 // Set it if not NULL
552 if ( property != NULL )
553 error = mlt_property_set_data( property, value, length, destroy, serialise );
558 /** Rename a property.
561 int mlt_properties_rename( mlt_properties this, char *source, char *dest )
563 mlt_property value = mlt_properties_find( this, dest );
567 property_list *list = this->local;
571 for ( i = 0; i < list->count; i ++ )
573 if ( !strcmp( list->name[ i ], source ) )
575 free( list->name[ i ] );
576 list->name[ i ] = strdup( dest );
577 list->hash[ generate_hash( dest ) ] = i + 1;
583 return value != NULL;
586 /** Dump the properties.
589 void mlt_properties_dump( mlt_properties this, FILE *output )
591 property_list *list = this->local;
593 for ( i = 0; i < list->count; i ++ )
594 if ( mlt_properties_get( this, list->name[ i ] ) != NULL )
595 fprintf( output, "%s=%s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
601 void mlt_properties_close( mlt_properties this )
605 property_list *list = this->local;
608 // Clean up names and values
609 for ( index = list->count - 1; index >= 0; index -- )
611 free( list->name[ index ] );
612 mlt_property_close( list->value[ index ] );
620 // Free this now if this has no child
621 if ( this->child == NULL )
624 #ifdef _MLT_PROPERTY_CHECKS_
625 // Increment destroyed count
626 properties_destroyed ++;
628 // Show current stats - these should match when the app is closed
629 fprintf( stderr, "Created %d, destroyed %d\n", properties_created, properties_destroyed );