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 /** Locate a property by name
63 static mlt_property mlt_properties_find( mlt_properties this, char *name )
65 mlt_property value = NULL;
66 property_list *list = this->private;
70 for ( i = 0; value == NULL && i < list->count; i ++ )
71 if ( !strcmp( list->name[ i ], name ) )
72 value = list->value[ i ];
77 /** Add a new property.
80 static mlt_property mlt_properties_add( mlt_properties this, char *name )
82 property_list *list = this->private;
84 // Check that we have space and resize if necessary
85 if ( list->count == list->size )
88 list->name = realloc( list->name, list->size * sizeof( char * ) );
89 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
92 // Assign name/value pair
93 list->name[ list->count ] = strdup( name );
94 list->value[ list->count ] = mlt_property_init( );
96 // Return and increment count accordingly
97 return list->value[ list->count ++ ];
100 /** Fetch a property by name - this includes add if not found.
103 static mlt_property mlt_properties_fetch( mlt_properties this, char *name )
105 // Try to find an existing property first
106 mlt_property property = mlt_properties_find( this, name );
108 // If it wasn't found, create one
109 if ( property == NULL )
110 property = mlt_properties_add( this, name );
112 // Return the property
116 /** Set the property.
119 int mlt_properties_set( mlt_properties this, char *name, char *value )
123 // Fetch the property to work with
124 mlt_property property = mlt_properties_fetch( this, name );
126 // Set it if not NULL
127 if ( property != NULL )
128 error = mlt_property_set_string( property, value );
133 /** Get a string value by name.
136 char *mlt_properties_get( mlt_properties this, char *name )
138 mlt_property value = mlt_properties_find( this, name );
139 return value == NULL ? NULL : mlt_property_get_string( value );
142 /** Get a name by index.
145 char *mlt_properties_get_name( mlt_properties this, int index )
147 property_list *list = this->private;
148 if ( index >= 0 && index < list->count )
149 return list->name[ index ];
153 /** Get a string value by index.
156 char *mlt_properties_get_value( mlt_properties this, int index )
158 property_list *list = this->private;
159 if ( index >= 0 && index < list->count )
160 return mlt_property_get_string( list->value[ index ] );
164 /** Return the number of items in the list.
167 int mlt_properties_count( mlt_properties this )
169 property_list *list = this->private;
173 /** Set a value by parsing a name=value string
176 int mlt_properties_parse( mlt_properties this, char *namevalue )
178 char *name = strdup( namevalue );
179 char *value = strdup( namevalue );
182 if ( strchr( name, '=' ) )
184 *( strchr( name, '=' ) ) = '\0';
185 strcpy( value, strchr( value, '=' ) + 1 );
192 error = mlt_properties_set( this, name, value );
200 /** Get a value associated to the name.
203 int mlt_properties_get_int( mlt_properties this, char *name )
205 mlt_property value = mlt_properties_find( this, name );
206 return value == NULL ? 0 : mlt_property_get_int( value );
209 /** Set a value associated to the name.
212 int mlt_properties_set_int( mlt_properties this, char *name, int value )
216 // Fetch the property to work with
217 mlt_property property = mlt_properties_fetch( this, name );
219 // Set it if not NULL
220 if ( property != NULL )
221 error = mlt_property_set_int( property, value );
226 /** Get a value associated to the name.
229 double mlt_properties_get_double( mlt_properties this, char *name )
231 mlt_property value = mlt_properties_find( this, name );
232 return value == NULL ? 0 : mlt_property_get_double( value );
235 /** Set a value associated to the name.
238 int mlt_properties_set_double( mlt_properties this, char *name, double value )
242 // Fetch the property to work with
243 mlt_property property = mlt_properties_fetch( this, name );
245 // Set it if not NULL
246 if ( property != NULL )
247 error = mlt_property_set_double( property, value );
252 /** Get a value associated to the name.
255 mlt_timecode mlt_properties_get_timecode( mlt_properties this, char *name )
257 mlt_property value = mlt_properties_find( this, name );
258 return value == NULL ? 0 : mlt_property_get_timecode( value );
261 /** Set a value associated to the name.
264 int mlt_properties_set_timecode( mlt_properties this, char *name, mlt_timecode value )
268 // Fetch the property to work with
269 mlt_property property = mlt_properties_fetch( this, name );
271 // Set it if not NULL
272 if ( property != NULL )
273 error = mlt_property_set_timecode( property, value );
278 /** Get a value associated to the name.
281 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
283 mlt_property value = mlt_properties_find( this, name );
284 return value == NULL ? NULL : mlt_property_get_data( value, length );
287 /** Set a value associated to the name.
290 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
294 // Fetch the property to work with
295 mlt_property property = mlt_properties_fetch( this, name );
297 // Set it if not NULL
298 if ( property != NULL )
299 error = mlt_property_set_data( property, value, length, destroy, serialise );
307 void mlt_properties_close( mlt_properties this )
309 property_list *list = this->private;
312 // Clean up names and values
313 for ( index = 0; index < list->count; index ++ )
315 free( list->name[ index ] );
316 mlt_property_close( list->value[ index ] );