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.
44 /** Basic implementation.
47 int mlt_properties_init( mlt_properties this, void *child )
50 memset( this, 0, sizeof( struct mlt_properties_s ) );
52 // Assign the child of the object
55 // Allocate the private structure
56 this->private = calloc( sizeof( property_list ), 1 );
58 return this->private == NULL;
61 /** Constructor for stand alone object.
64 mlt_properties mlt_properties_new( )
66 // Construct a standalone properties object
67 mlt_properties this = calloc( sizeof( struct mlt_properties_s ), 1 );
70 mlt_properties_init( this, NULL );
76 static inline int generate_hash( char *name )
81 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
85 /** Special case - when a container (such as fezzik) is protecting another
86 producer, we need to ensure that properties are passed through to the
90 static void mlt_properties_do_mirror( mlt_properties this, char *name )
92 if ( strcmp( name, "in" ) && strcmp( name, "out" ) )
94 mlt_properties mirror = mlt_properties_get_data( this, "mlt_mirror", NULL );
97 char *value = mlt_properties_get( this, name );
99 mlt_properties_set( mirror, name, value );
104 /** Allow the specification of a mirror.
107 void mlt_properties_mirror( mlt_properties this, mlt_properties that )
109 mlt_properties_set_data( this, "mlt_mirror", that, 0, NULL, NULL );
112 /** Inherit all serialisable properties from that into this.
115 int mlt_properties_inherit( mlt_properties this, mlt_properties that )
117 int count = mlt_properties_count( that );
119 for ( i = 0; i < count; i ++ )
121 char *value = mlt_properties_get_value( that, i );
124 char *name = mlt_properties_get_name( that, i );
125 mlt_properties_set( this, name, value );
131 /** Pass all properties from 'that' that match the prefix to 'this' (excluding the prefix).
134 int mlt_properties_pass( mlt_properties this, mlt_properties that, char *prefix )
136 int count = mlt_properties_count( that );
137 int length = strlen( prefix );
139 for ( i = 0; i < count; i ++ )
141 char *name = mlt_properties_get_name( that, i );
142 if ( !strncmp( name, prefix, length ) )
144 char *value = mlt_properties_get_value( that, i );
146 mlt_properties_set( this, name + length, value );
152 /** Locate a property by name
155 static inline mlt_property mlt_properties_find( mlt_properties this, char *name )
157 property_list *list = this->private;
158 mlt_property value = NULL;
159 int key = generate_hash( name );
160 int i = list->hash[ key ];
162 // Check if we're hashed
163 if ( list->count > 0 &&
164 name[ 0 ] == list->name[ i ][ 0 ] &&
165 !strcmp( list->name[ i ], name ) )
166 value = list->value[ i ];
169 for ( i = 0; value == NULL && i < list->count; i ++ )
170 if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
171 value = list->value[ i ];
176 /** Add a new property.
179 static mlt_property mlt_properties_add( mlt_properties this, char *name )
181 property_list *list = this->private;
182 int key = generate_hash( name );
184 // Check that we have space and resize if necessary
185 if ( list->count == list->size )
188 list->name = realloc( list->name, list->size * sizeof( char * ) );
189 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
192 // Assign name/value pair
193 list->name[ list->count ] = strdup( name );
194 list->value[ list->count ] = mlt_property_init( );
196 // Assign to hash table
197 list->hash[ key ] = list->count;
199 // Return and increment count accordingly
200 return list->value[ list->count ++ ];
203 /** Fetch a property by name - this includes add if not found.
206 static mlt_property mlt_properties_fetch( mlt_properties this, char *name )
208 // Try to find an existing property first
209 mlt_property property = mlt_properties_find( this, name );
211 // If it wasn't found, create one
212 if ( property == NULL )
213 property = mlt_properties_add( this, name );
215 // Return the property
219 /** Set the property.
222 int mlt_properties_set( mlt_properties this, char *name, char *value )
226 // Fetch the property to work with
227 mlt_property property = mlt_properties_fetch( this, name );
229 // Set it if not NULL
230 if ( property != NULL )
232 error = mlt_property_set_string( property, value );
233 mlt_properties_do_mirror( this, name );
239 /** Get a string value by name.
242 char *mlt_properties_get( mlt_properties this, char *name )
244 mlt_property value = mlt_properties_find( this, name );
245 return value == NULL ? NULL : mlt_property_get_string( value );
248 /** Get a name by index.
251 char *mlt_properties_get_name( mlt_properties this, int index )
253 property_list *list = this->private;
254 if ( index >= 0 && index < list->count )
255 return list->name[ index ];
259 /** Get a string value by index.
262 char *mlt_properties_get_value( mlt_properties this, int index )
264 property_list *list = this->private;
265 if ( index >= 0 && index < list->count )
266 return mlt_property_get_string( list->value[ index ] );
270 /** Get a data value by index.
273 void *mlt_properties_get_data_at( mlt_properties this, int index, int *size )
275 property_list *list = this->private;
276 if ( index >= 0 && index < list->count )
277 return mlt_property_get_data( list->value[ index ], size );
281 /** Return the number of items in the list.
284 int mlt_properties_count( mlt_properties this )
286 property_list *list = this->private;
290 /** Set a value by parsing a name=value string
293 int mlt_properties_parse( mlt_properties this, char *namevalue )
295 char *name = strdup( namevalue );
296 char *value = strdup( namevalue );
299 if ( strchr( name, '=' ) )
301 *( strchr( name, '=' ) ) = '\0';
302 strcpy( value, strchr( value, '=' ) + 1 );
309 if ( strlen( value ) > 1 && value[ 0 ] == '\"' )
311 strcpy( value, value + 1 );
312 if ( value[ strlen( value ) - 1 ] == '\"' )
313 value[ strlen( value ) - 1 ] = '\0';
316 error = mlt_properties_set( this, name, value );
324 /** Get a value associated to the name.
327 int mlt_properties_get_int( mlt_properties this, char *name )
329 mlt_property value = mlt_properties_find( this, name );
330 return value == NULL ? 0 : mlt_property_get_int( value );
333 /** Set a value associated to the name.
336 int mlt_properties_set_int( mlt_properties this, char *name, int value )
340 // Fetch the property to work with
341 mlt_property property = mlt_properties_fetch( this, name );
343 // Set it if not NULL
344 if ( property != NULL )
346 error = mlt_property_set_int( property, value );
347 mlt_properties_do_mirror( this, name );
353 /** Get a value associated to the name.
356 double mlt_properties_get_double( mlt_properties this, char *name )
358 mlt_property value = mlt_properties_find( this, name );
359 return value == NULL ? 0 : mlt_property_get_double( value );
362 /** Set a value associated to the name.
365 int mlt_properties_set_double( mlt_properties this, char *name, double value )
369 // Fetch the property to work with
370 mlt_property property = mlt_properties_fetch( this, name );
372 // Set it if not NULL
373 if ( property != NULL )
375 error = mlt_property_set_double( property, value );
376 mlt_properties_do_mirror( this, name );
382 /** Get a value associated to the name.
385 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
387 mlt_property value = mlt_properties_find( this, name );
388 return value == NULL ? 0 : mlt_property_get_position( value );
391 /** Set a value associated to the name.
394 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
398 // Fetch the property to work with
399 mlt_property property = mlt_properties_fetch( this, name );
401 // Set it if not NULL
402 if ( property != NULL )
404 error = mlt_property_set_position( property, value );
405 mlt_properties_do_mirror( this, name );
411 /** Get a value associated to the name.
414 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
416 mlt_property value = mlt_properties_find( this, name );
417 return value == NULL ? NULL : mlt_property_get_data( value, length );
420 /** Set a value associated to the name.
423 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
427 // Fetch the property to work with
428 mlt_property property = mlt_properties_fetch( this, name );
430 // Set it if not NULL
431 if ( property != NULL )
432 error = mlt_property_set_data( property, value, length, destroy, serialise );
437 /** Rename a property.
440 int mlt_properties_rename( mlt_properties this, char *source, char *dest )
442 mlt_property value = mlt_properties_find( this, dest );
446 property_list *list = this->private;
450 for ( i = 0; i < list->count; i ++ )
452 if ( !strcmp( list->name[ i ], source ) )
454 free( list->name[ i ] );
455 list->name[ i ] = strdup( dest );
461 return value != NULL;
464 /** Dump the properties.
467 void mlt_properties_dump( mlt_properties this, FILE *output )
469 property_list *list = this->private;
471 for ( i = 0; i < list->count; i ++ )
472 fprintf( stderr, "%s = %s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
478 void mlt_properties_close( mlt_properties this )
480 property_list *list = this->private;
483 // Clean up names and values
484 for ( index = list->count - 1; index >= 0; index -- )
486 free( list->name[ index ] );
487 mlt_property_close( list->value[ index ] );
495 // Free this now if this has no child
496 if ( this->child == NULL )