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.
43 /** Basic implementation.
46 int mlt_properties_init( mlt_properties this, void *child )
49 memset( this, 0, sizeof( struct mlt_properties_s ) );
51 // Assign the child of the object
54 // Allocate the private structure
55 this->private = calloc( sizeof( property_list ), 1 );
57 return this->private == NULL;
60 /** Constructor for stand alone object.
63 mlt_properties mlt_properties_new( )
65 // Construct a standalone properties object
66 mlt_properties this = calloc( sizeof( struct mlt_properties_s ), 1 );
69 mlt_properties_init( this, NULL );
75 /** Special case - when a container (such as fezzik) is protecting another
76 producer, we need to ensure that properties are passed through to the
80 static void mlt_properties_do_mirror( mlt_properties this, char *name )
82 mlt_properties mirror = mlt_properties_get_data( this, "mlt_mirror", NULL );
85 char *value = mlt_properties_get( this, name );
87 mlt_properties_set( mirror, name, value );
91 /** Allow the specification of a mirror.
94 void mlt_properties_mirror( mlt_properties this, mlt_properties that )
96 mlt_properties_set_data( this, "mlt_mirror", that, 0, NULL, NULL );
99 /** Inherit all serialisable properties from that into this.
102 int mlt_properties_inherit( mlt_properties this, mlt_properties that )
104 int count = mlt_properties_count( that );
107 char *value = mlt_properties_get_value( that, count );
110 char *name = mlt_properties_get_name( that, count );
111 mlt_properties_set( this, name, value );
117 /** Locate a property by name
120 static mlt_property mlt_properties_find( mlt_properties this, char *name )
122 mlt_property value = NULL;
123 property_list *list = this->private;
127 for ( i = 0; value == NULL && i < list->count; i ++ )
128 if ( !strcmp( list->name[ i ], name ) )
129 value = list->value[ i ];
134 /** Add a new property.
137 static mlt_property mlt_properties_add( mlt_properties this, char *name )
139 property_list *list = this->private;
141 // Check that we have space and resize if necessary
142 if ( list->count == list->size )
145 list->name = realloc( list->name, list->size * sizeof( char * ) );
146 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
149 // Assign name/value pair
150 list->name[ list->count ] = strdup( name );
151 list->value[ list->count ] = mlt_property_init( );
153 // Return and increment count accordingly
154 return list->value[ list->count ++ ];
157 /** Fetch a property by name - this includes add if not found.
160 static mlt_property mlt_properties_fetch( mlt_properties this, char *name )
162 // Try to find an existing property first
163 mlt_property property = mlt_properties_find( this, name );
165 // If it wasn't found, create one
166 if ( property == NULL )
167 property = mlt_properties_add( this, name );
169 // Return the property
173 /** Set the property.
176 int mlt_properties_set( mlt_properties this, char *name, char *value )
180 // Fetch the property to work with
181 mlt_property property = mlt_properties_fetch( this, name );
183 // Set it if not NULL
184 if ( property != NULL )
186 error = mlt_property_set_string( property, value );
187 mlt_properties_do_mirror( this, name );
193 /** Get a string value by name.
196 char *mlt_properties_get( mlt_properties this, char *name )
198 mlt_property value = mlt_properties_find( this, name );
199 return value == NULL ? NULL : mlt_property_get_string( value );
202 /** Get a name by index.
205 char *mlt_properties_get_name( mlt_properties this, int index )
207 property_list *list = this->private;
208 if ( index >= 0 && index < list->count )
209 return list->name[ index ];
213 /** Get a string value by index.
216 char *mlt_properties_get_value( mlt_properties this, int index )
218 property_list *list = this->private;
219 if ( index >= 0 && index < list->count )
220 return mlt_property_get_string( list->value[ index ] );
224 /** Return the number of items in the list.
227 int mlt_properties_count( mlt_properties this )
229 property_list *list = this->private;
233 /** Set a value by parsing a name=value string
236 int mlt_properties_parse( mlt_properties this, char *namevalue )
238 char *name = strdup( namevalue );
239 char *value = strdup( namevalue );
242 if ( strchr( name, '=' ) )
244 *( strchr( name, '=' ) ) = '\0';
245 strcpy( value, strchr( value, '=' ) + 1 );
252 error = mlt_properties_set( this, name, value );
260 /** Get a value associated to the name.
263 int mlt_properties_get_int( mlt_properties this, char *name )
265 mlt_property value = mlt_properties_find( this, name );
266 return value == NULL ? 0 : mlt_property_get_int( value );
269 /** Set a value associated to the name.
272 int mlt_properties_set_int( mlt_properties this, char *name, int value )
276 // Fetch the property to work with
277 mlt_property property = mlt_properties_fetch( this, name );
279 // Set it if not NULL
280 if ( property != NULL )
282 error = mlt_property_set_int( property, value );
283 mlt_properties_do_mirror( this, name );
289 /** Get a value associated to the name.
292 double mlt_properties_get_double( mlt_properties this, char *name )
294 mlt_property value = mlt_properties_find( this, name );
295 return value == NULL ? 0 : mlt_property_get_double( value );
298 /** Set a value associated to the name.
301 int mlt_properties_set_double( mlt_properties this, char *name, double value )
305 // Fetch the property to work with
306 mlt_property property = mlt_properties_fetch( this, name );
308 // Set it if not NULL
309 if ( property != NULL )
311 error = mlt_property_set_double( property, value );
312 mlt_properties_do_mirror( this, name );
318 /** Get a value associated to the name.
321 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
323 mlt_property value = mlt_properties_find( this, name );
324 return value == NULL ? 0 : mlt_property_get_position( value );
327 /** Set a value associated to the name.
330 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
334 // Fetch the property to work with
335 mlt_property property = mlt_properties_fetch( this, name );
337 // Set it if not NULL
338 if ( property != NULL )
340 error = mlt_property_set_position( property, value );
341 mlt_properties_do_mirror( this, name );
347 /** Get a value associated to the name.
350 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
352 mlt_property value = mlt_properties_find( this, name );
353 return value == NULL ? NULL : mlt_property_get_data( value, length );
356 /** Set a value associated to the name.
359 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
363 // Fetch the property to work with
364 mlt_property property = mlt_properties_fetch( this, name );
366 // Set it if not NULL
367 if ( property != NULL )
368 error = mlt_property_set_data( property, value, length, destroy, serialise );
376 void mlt_properties_close( mlt_properties this )
378 property_list *list = this->private;
381 // Clean up names and values
382 for ( index = list->count - 1; index >= 0; index -- )
384 free( list->name[ index ] );
385 mlt_property_close( list->value[ index ] );
393 // Free this now if this has no child
394 if ( this->child == NULL )