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 if ( strcmp( name
, "in" ) && strcmp( name
, "out" ) )
84 mlt_properties mirror
= mlt_properties_get_data( this, "mlt_mirror", NULL
);
87 char *value
= mlt_properties_get( this, name
);
89 mlt_properties_set( mirror
, name
, value
);
94 /** Allow the specification of a mirror.
97 void mlt_properties_mirror( mlt_properties
this, mlt_properties that
)
99 mlt_properties_set_data( this, "mlt_mirror", that
, 0, NULL
, NULL
);
102 /** Inherit all serialisable properties from that into this.
105 int mlt_properties_inherit( mlt_properties
this, mlt_properties that
)
107 int count
= mlt_properties_count( that
);
110 char *value
= mlt_properties_get_value( that
, count
);
113 char *name
= mlt_properties_get_name( that
, count
);
114 mlt_properties_set( this, name
, value
);
120 /** Locate a property by name
123 static mlt_property
mlt_properties_find( mlt_properties
this, char *name
)
125 mlt_property value
= NULL
;
126 property_list
*list
= this->private;
130 for ( i
= 0; value
== NULL
&& i
< list
->count
; i
++ )
131 if ( !strcmp( list
->name
[ i
], name
) )
132 value
= list
->value
[ i
];
137 /** Add a new property.
140 static mlt_property
mlt_properties_add( mlt_properties
this, char *name
)
142 property_list
*list
= this->private;
144 // Check that we have space and resize if necessary
145 if ( list
->count
== list
->size
)
148 list
->name
= realloc( list
->name
, list
->size
* sizeof( char * ) );
149 list
->value
= realloc( list
->value
, list
->size
* sizeof( mlt_property
) );
152 // Assign name/value pair
153 list
->name
[ list
->count
] = strdup( name
);
154 list
->value
[ list
->count
] = mlt_property_init( );
156 // Return and increment count accordingly
157 return list
->value
[ list
->count
++ ];
160 /** Fetch a property by name - this includes add if not found.
163 static mlt_property
mlt_properties_fetch( mlt_properties
this, char *name
)
165 // Try to find an existing property first
166 mlt_property property
= mlt_properties_find( this, name
);
168 // If it wasn't found, create one
169 if ( property
== NULL
)
170 property
= mlt_properties_add( this, name
);
172 // Return the property
176 /** Set the property.
179 int mlt_properties_set( mlt_properties
this, char *name
, char *value
)
183 // Fetch the property to work with
184 mlt_property property
= mlt_properties_fetch( this, name
);
186 // Set it if not NULL
187 if ( property
!= NULL
)
189 error
= mlt_property_set_string( property
, value
);
190 mlt_properties_do_mirror( this, name
);
196 /** Get a string value by name.
199 char *mlt_properties_get( mlt_properties
this, char *name
)
201 mlt_property value
= mlt_properties_find( this, name
);
202 return value
== NULL ? NULL
: mlt_property_get_string( value
);
205 /** Get a name by index.
208 char *mlt_properties_get_name( mlt_properties
this, int index
)
210 property_list
*list
= this->private;
211 if ( index
>= 0 && index
< list
->count
)
212 return list
->name
[ index
];
216 /** Get a string value by index.
219 char *mlt_properties_get_value( mlt_properties
this, int index
)
221 property_list
*list
= this->private;
222 if ( index
>= 0 && index
< list
->count
)
223 return mlt_property_get_string( list
->value
[ index
] );
227 /** Get a data value by index.
230 void *mlt_properties_get_data_at( mlt_properties
this, int index
, int *size
)
232 property_list
*list
= this->private;
233 if ( index
>= 0 && index
< list
->count
)
234 return mlt_property_get_data( list
->value
[ index
], size
);
238 /** Return the number of items in the list.
241 int mlt_properties_count( mlt_properties
this )
243 property_list
*list
= this->private;
247 /** Set a value by parsing a name=value string
250 int mlt_properties_parse( mlt_properties
this, char *namevalue
)
252 char *name
= strdup( namevalue
);
253 char *value
= strdup( namevalue
);
256 if ( strchr( name
, '=' ) )
258 *( strchr( name
, '=' ) ) = '\0';
259 strcpy( value
, strchr( value
, '=' ) + 1 );
266 if ( strlen( value
) > 1 && value
[ 0 ] == '\"' )
268 strcpy( value
, value
+ 1 );
269 if ( value
[ strlen( value
) - 1 ] == '\"' )
270 value
[ strlen( value
) - 1 ] = '\0';
273 error
= mlt_properties_set( this, name
, value
);
281 /** Get a value associated to the name.
284 int mlt_properties_get_int( mlt_properties
this, char *name
)
286 mlt_property value
= mlt_properties_find( this, name
);
287 return value
== NULL ?
0 : mlt_property_get_int( value
);
290 /** Set a value associated to the name.
293 int mlt_properties_set_int( mlt_properties
this, char *name
, int value
)
297 // Fetch the property to work with
298 mlt_property property
= mlt_properties_fetch( this, name
);
300 // Set it if not NULL
301 if ( property
!= NULL
)
303 error
= mlt_property_set_int( property
, value
);
304 mlt_properties_do_mirror( this, name
);
310 /** Get a value associated to the name.
313 double mlt_properties_get_double( mlt_properties
this, char *name
)
315 mlt_property value
= mlt_properties_find( this, name
);
316 return value
== NULL ?
0 : mlt_property_get_double( value
);
319 /** Set a value associated to the name.
322 int mlt_properties_set_double( mlt_properties
this, char *name
, double value
)
326 // Fetch the property to work with
327 mlt_property property
= mlt_properties_fetch( this, name
);
329 // Set it if not NULL
330 if ( property
!= NULL
)
332 error
= mlt_property_set_double( property
, value
);
333 mlt_properties_do_mirror( this, name
);
339 /** Get a value associated to the name.
342 mlt_position
mlt_properties_get_position( mlt_properties
this, char *name
)
344 mlt_property value
= mlt_properties_find( this, name
);
345 return value
== NULL ?
0 : mlt_property_get_position( value
);
348 /** Set a value associated to the name.
351 int mlt_properties_set_position( mlt_properties
this, char *name
, mlt_position value
)
355 // Fetch the property to work with
356 mlt_property property
= mlt_properties_fetch( this, name
);
358 // Set it if not NULL
359 if ( property
!= NULL
)
361 error
= mlt_property_set_position( property
, value
);
362 mlt_properties_do_mirror( this, name
);
368 /** Get a value associated to the name.
371 void *mlt_properties_get_data( mlt_properties
this, char *name
, int *length
)
373 mlt_property value
= mlt_properties_find( this, name
);
374 return value
== NULL ? NULL
: mlt_property_get_data( value
, length
);
377 /** Set a value associated to the name.
380 int mlt_properties_set_data( mlt_properties
this, char *name
, void *value
, int length
, mlt_destructor destroy
, mlt_serialiser serialise
)
384 // Fetch the property to work with
385 mlt_property property
= mlt_properties_fetch( this, name
);
387 // Set it if not NULL
388 if ( property
!= NULL
)
389 error
= mlt_property_set_data( property
, value
, length
, destroy
, serialise
);
394 /** Rename a property.
397 int mlt_properties_rename( mlt_properties
this, char *source
, char *dest
)
399 mlt_property value
= mlt_properties_find( this, dest
);
403 property_list
*list
= this->private;
407 for ( i
= 0; i
< list
->count
; i
++ )
409 if ( !strcmp( list
->name
[ i
], source
) )
411 free( list
->name
[ i
] );
412 list
->name
[ i
] = strdup( dest
);
418 return value
!= NULL
;
421 /** Dump the properties.
424 void mlt_properties_dump( mlt_properties
this, FILE *output
)
426 property_list
*list
= this->private;
428 for ( i
= 0; i
< list
->count
; i
++ )
429 fprintf( stderr
, "%s = %s\n", list
->name
[ i
], mlt_properties_get( this, list
->name
[ i
] ) );
435 void mlt_properties_close( mlt_properties
this )
437 property_list
*list
= this->private;
440 // Clean up names and values
441 for ( index
= list
->count
- 1; index
>= 0; index
-- )
443 free( list
->name
[ index
] );
444 mlt_property_close( list
->value
[ index
] );
452 // Free this now if this has no child
453 if ( this->child
== NULL
)