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;
47 /** Memory leak checks.
50 //#define _MLT_PROPERTY_CHECKS_
52 #ifdef _MLT_PROPERTY_CHECKS_
53 static int properties_created = 0;
54 static int properties_destroyed = 0;
57 /** Basic implementation.
60 int mlt_properties_init( mlt_properties this, void *child )
64 #ifdef _MLT_PROPERTY_CHECKS_
65 // Increment number of properties created
66 properties_created ++;
70 memset( this, 0, sizeof( struct mlt_properties_s ) );
72 // Assign the child of the object
75 // Allocate the local structure
76 this->local = calloc( sizeof( property_list ), 1 );
78 // Increment the ref count
79 ( ( property_list * )this->local )->ref_count = 1;
82 // Check that initialisation was successful
83 return this != NULL && this->local == NULL;
86 /** Constructor for stand alone object.
89 mlt_properties mlt_properties_new( )
91 // Construct a standalone properties object
92 mlt_properties this = calloc( sizeof( struct mlt_properties_s ), 1 );
95 mlt_properties_init( this, NULL );
101 /** Load properties from a file.
104 mlt_properties mlt_properties_load( char *filename )
106 // Construct a standalone properties object
107 mlt_properties this = mlt_properties_new( );
112 FILE *file = fopen( filename, "r" );
114 // Load contents of file
120 // Read each string from the file
121 while( fgets( temp, 1024, file ) )
124 temp[ strlen( temp ) - 1 ] = '\0';
126 // Parse and set the property
127 if ( strcmp( temp, "" ) && temp[ 0 ] != '#' )
128 mlt_properties_parse( this, temp );
136 // Return the pointer
140 static inline int generate_hash( char *name )
145 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
149 /** Special case - when a container (such as fezzik) is protecting another
150 producer, we need to ensure that properties are passed through to the
154 static inline void mlt_properties_do_mirror( mlt_properties this, char *name )
156 property_list *list = this->local;
157 if ( list->mirror != NULL )
159 char *value = mlt_properties_get( this, name );
161 mlt_properties_set( list->mirror, name, value );
165 /** Maintain ref count to allow multiple uses of an mlt object.
168 int mlt_properties_inc_ref( mlt_properties this )
172 property_list *list = this->local;
173 return ++ list->ref_count;
178 /** Maintain ref count to allow multiple uses of an mlt object.
181 int mlt_properties_dec_ref( mlt_properties this )
185 property_list *list = this->local;
186 return -- list->ref_count;
191 /** Allow the specification of a mirror.
194 void mlt_properties_mirror( mlt_properties this, mlt_properties that )
196 property_list *list = this->local;
200 /** Inherit all serialisable properties from that into this.
203 int mlt_properties_inherit( mlt_properties this, mlt_properties that )
205 int count = mlt_properties_count( that );
207 for ( i = 0; i < count; i ++ )
209 char *value = mlt_properties_get_value( that, i );
212 char *name = mlt_properties_get_name( that, i );
213 mlt_properties_set( this, name, value );
219 /** Pass all properties from 'that' that match the prefix to 'this' (excluding the prefix).
222 int mlt_properties_pass( mlt_properties this, mlt_properties that, char *prefix )
224 int count = mlt_properties_count( that );
225 int length = strlen( prefix );
227 for ( i = 0; i < count; i ++ )
229 char *name = mlt_properties_get_name( that, i );
230 if ( !strncmp( name, prefix, length ) )
232 char *value = mlt_properties_get_value( that, i );
234 mlt_properties_set( this, name + length, value );
240 /** Locate a property by name
243 static inline mlt_property mlt_properties_find( mlt_properties this, char *name )
245 property_list *list = this->local;
246 mlt_property value = NULL;
247 int key = generate_hash( name );
248 int i = list->hash[ key ] - 1;
252 // Check if we're hashed
253 if ( list->count > 0 &&
254 name[ 0 ] == list->name[ i ][ 0 ] &&
255 !strcmp( list->name[ i ], name ) )
256 value = list->value[ i ];
259 for ( i = list->count - 1; value == NULL && i >= 0; i -- )
260 if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
261 value = list->value[ i ];
267 /** Add a new property.
270 static mlt_property mlt_properties_add( mlt_properties this, char *name )
272 property_list *list = this->local;
273 int key = generate_hash( name );
275 // Check that we have space and resize if necessary
276 if ( list->count == list->size )
279 list->name = realloc( list->name, list->size * sizeof( char * ) );
280 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
283 // Assign name/value pair
284 list->name[ list->count ] = strdup( name );
285 list->value[ list->count ] = mlt_property_init( );
287 // Assign to hash table
288 if ( list->hash[ key ] == 0 )
289 list->hash[ key ] = list->count + 1;
291 // Return and increment count accordingly
292 return list->value[ list->count ++ ];
295 /** Fetch a property by name - this includes add if not found.
298 static mlt_property mlt_properties_fetch( mlt_properties this, char *name )
300 // Try to find an existing property first
301 mlt_property property = mlt_properties_find( this, name );
303 // If it wasn't found, create one
304 if ( property == NULL )
305 property = mlt_properties_add( this, name );
307 // Return the property
311 /** Set the property.
314 int mlt_properties_set( mlt_properties this, char *name, char *value )
318 // Fetch the property to work with
319 mlt_property property = mlt_properties_fetch( this, name );
321 // Set it if not NULL
322 if ( property != NULL && ( value == NULL || value[ 0 ] != '@' ) )
324 error = mlt_property_set_string( property, value );
325 mlt_properties_do_mirror( this, name );
327 else if ( property != NULL && value[ 0 ] == '@' )
336 while ( *value != '\0' )
338 int length = strcspn( value, "+-*/" );
340 // Get the identifier
341 strncpy( id, value, length );
345 // Determine the value
346 if ( isdigit( id[ 0 ] ) )
347 current = atof( id );
349 current = mlt_properties_get_int( this, id );
351 // Apply the operation
369 op = *value != '\0' ? *value ++ : ' ';
372 error = mlt_property_set_int( property, total );
373 mlt_properties_do_mirror( this, name );
379 /** Set or default the property.
382 int mlt_properties_set_or_default( mlt_properties this, char *name, char *value, char *def )
384 return mlt_properties_set( this, name, value == NULL ? def : value );
387 /** Get a string value by name.
390 char *mlt_properties_get( mlt_properties this, char *name )
392 mlt_property value = mlt_properties_find( this, name );
393 return value == NULL ? NULL : mlt_property_get_string( value );
396 /** Get a name by index.
399 char *mlt_properties_get_name( mlt_properties this, int index )
401 property_list *list = this->local;
402 if ( index >= 0 && index < list->count )
403 return list->name[ index ];
407 /** Get a string value by index.
410 char *mlt_properties_get_value( mlt_properties this, int index )
412 property_list *list = this->local;
413 if ( index >= 0 && index < list->count )
414 return mlt_property_get_string( list->value[ index ] );
418 /** Get a data value by index.
421 void *mlt_properties_get_data_at( mlt_properties this, int index, int *size )
423 property_list *list = this->local;
424 if ( index >= 0 && index < list->count )
425 return mlt_property_get_data( list->value[ index ], size );
429 /** Return the number of items in the list.
432 int mlt_properties_count( mlt_properties this )
434 property_list *list = this->local;
438 /** Set a value by parsing a name=value string
441 int mlt_properties_parse( mlt_properties this, char *namevalue )
443 char *name = strdup( namevalue );
446 char *ptr = strchr( name, '=' );
454 value = strdup( ptr );
459 value = strdup( ptr );
460 if ( value != NULL && value[ strlen( value ) - 1 ] == '\"' )
461 value[ strlen( value ) - 1 ] = '\0';
466 value = strdup( "" );
469 error = mlt_properties_set( this, name, value );
477 /** Get a value associated to the name.
480 int mlt_properties_get_int( mlt_properties this, char *name )
482 mlt_property value = mlt_properties_find( this, name );
483 return value == NULL ? 0 : mlt_property_get_int( value );
486 /** Set a value associated to the name.
489 int mlt_properties_set_int( mlt_properties this, char *name, int value )
493 // Fetch the property to work with
494 mlt_property property = mlt_properties_fetch( this, name );
496 // Set it if not NULL
497 if ( property != NULL )
499 error = mlt_property_set_int( property, value );
500 mlt_properties_do_mirror( this, name );
506 /** Get a value associated to the name.
509 double mlt_properties_get_double( mlt_properties this, char *name )
511 mlt_property value = mlt_properties_find( this, name );
512 return value == NULL ? 0 : mlt_property_get_double( value );
515 /** Set a value associated to the name.
518 int mlt_properties_set_double( mlt_properties this, char *name, double value )
522 // Fetch the property to work with
523 mlt_property property = mlt_properties_fetch( this, name );
525 // Set it if not NULL
526 if ( property != NULL )
528 error = mlt_property_set_double( property, value );
529 mlt_properties_do_mirror( this, name );
535 /** Get a value associated to the name.
538 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
540 mlt_property value = mlt_properties_find( this, name );
541 return value == NULL ? 0 : mlt_property_get_position( value );
544 /** Set a value associated to the name.
547 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
551 // Fetch the property to work with
552 mlt_property property = mlt_properties_fetch( this, name );
554 // Set it if not NULL
555 if ( property != NULL )
557 error = mlt_property_set_position( property, value );
558 mlt_properties_do_mirror( this, name );
564 /** Get a value associated to the name.
567 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
569 mlt_property value = mlt_properties_find( this, name );
570 return value == NULL ? NULL : mlt_property_get_data( value, length );
573 /** Set a value associated to the name.
576 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
580 // Fetch the property to work with
581 mlt_property property = mlt_properties_fetch( this, name );
583 // Set it if not NULL
584 if ( property != NULL )
585 error = mlt_property_set_data( property, value, length, destroy, serialise );
590 /** Rename a property.
593 int mlt_properties_rename( mlt_properties this, char *source, char *dest )
595 mlt_property value = mlt_properties_find( this, dest );
599 property_list *list = this->local;
603 for ( i = 0; i < list->count; i ++ )
605 if ( !strcmp( list->name[ i ], source ) )
607 free( list->name[ i ] );
608 list->name[ i ] = strdup( dest );
609 list->hash[ generate_hash( dest ) ] = i + 1;
615 return value != NULL;
618 /** Dump the properties.
621 void mlt_properties_dump( mlt_properties this, FILE *output )
623 property_list *list = this->local;
625 for ( i = 0; i < list->count; i ++ )
626 if ( mlt_properties_get( this, list->name[ i ] ) != NULL )
627 fprintf( output, "%s=%s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
630 void mlt_properties_debug( mlt_properties this, char *title, FILE *output )
632 fprintf( stderr, "%s: ", title );
635 property_list *list = this->local;
637 fprintf( output, "[ ref=%d", list->ref_count );
638 for ( i = 0; i < list->count; i ++ )
639 if ( mlt_properties_get( this, list->name[ i ] ) != NULL )
640 fprintf( output, ", %s=%s", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
641 fprintf( output, " ]" );
643 fprintf( stderr, "\n" );
649 void mlt_properties_close( mlt_properties this )
651 if ( this != NULL && mlt_properties_dec_ref( this ) <= 0 )
653 if ( this->close != NULL )
655 this->close( this->close_object );
659 property_list *list = this->local;
662 #ifdef _MLT_PROPERTY_CHECKS_
664 mlt_properties_debug( this, "Closing", stderr );
666 // Increment destroyed count
667 properties_destroyed ++;
669 // Show current stats - these should match when the app is closed
670 fprintf( stderr, "Created %d, destroyed %d\n", properties_created, properties_destroyed );
673 // Clean up names and values
674 for ( index = list->count - 1; index >= 0; index -- )
676 free( list->name[ index ] );
677 mlt_property_close( list->value[ index ] );
685 // Free this now if this has no child
686 if ( this->child == NULL )