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 /** Return the number of items in the list.
230 int mlt_properties_count( mlt_properties
this )
232 property_list
*list
= this->private;
236 /** Set a value by parsing a name=value string
239 int mlt_properties_parse( mlt_properties
this, char *namevalue
)
241 char *name
= strdup( namevalue
);
242 char *value
= strdup( namevalue
);
245 if ( strchr( name
, '=' ) )
247 *( strchr( name
, '=' ) ) = '\0';
248 strcpy( value
, strchr( value
, '=' ) + 1 );
255 error
= mlt_properties_set( this, name
, value
);
263 /** Get a value associated to the name.
266 int mlt_properties_get_int( mlt_properties
this, char *name
)
268 mlt_property value
= mlt_properties_find( this, name
);
269 return value
== NULL ?
0 : mlt_property_get_int( value
);
272 /** Set a value associated to the name.
275 int mlt_properties_set_int( mlt_properties
this, char *name
, int value
)
279 // Fetch the property to work with
280 mlt_property property
= mlt_properties_fetch( this, name
);
282 // Set it if not NULL
283 if ( property
!= NULL
)
285 error
= mlt_property_set_int( property
, value
);
286 mlt_properties_do_mirror( this, name
);
292 /** Get a value associated to the name.
295 double mlt_properties_get_double( mlt_properties
this, char *name
)
297 mlt_property value
= mlt_properties_find( this, name
);
298 return value
== NULL ?
0 : mlt_property_get_double( value
);
301 /** Set a value associated to the name.
304 int mlt_properties_set_double( mlt_properties
this, char *name
, double value
)
308 // Fetch the property to work with
309 mlt_property property
= mlt_properties_fetch( this, name
);
311 // Set it if not NULL
312 if ( property
!= NULL
)
314 error
= mlt_property_set_double( property
, value
);
315 mlt_properties_do_mirror( this, name
);
321 /** Get a value associated to the name.
324 mlt_position
mlt_properties_get_position( mlt_properties
this, char *name
)
326 mlt_property value
= mlt_properties_find( this, name
);
327 return value
== NULL ?
0 : mlt_property_get_position( value
);
330 /** Set a value associated to the name.
333 int mlt_properties_set_position( mlt_properties
this, char *name
, mlt_position value
)
337 // Fetch the property to work with
338 mlt_property property
= mlt_properties_fetch( this, name
);
340 // Set it if not NULL
341 if ( property
!= NULL
)
343 error
= mlt_property_set_position( property
, value
);
344 mlt_properties_do_mirror( this, name
);
350 /** Get a value associated to the name.
353 void *mlt_properties_get_data( mlt_properties
this, char *name
, int *length
)
355 mlt_property value
= mlt_properties_find( this, name
);
356 return value
== NULL ? NULL
: mlt_property_get_data( value
, length
);
359 /** Set a value associated to the name.
362 int mlt_properties_set_data( mlt_properties
this, char *name
, void *value
, int length
, mlt_destructor destroy
, mlt_serialiser serialise
)
366 // Fetch the property to work with
367 mlt_property property
= mlt_properties_fetch( this, name
);
369 // Set it if not NULL
370 if ( property
!= NULL
)
371 error
= mlt_property_set_data( property
, value
, length
, destroy
, serialise
);
379 void mlt_properties_close( mlt_properties
this )
381 property_list
*list
= this->private;
384 // Clean up names and values
385 for ( index
= list
->count
- 1; index
>= 0; index
-- )
387 free( list
->name
[ index
] );
388 mlt_property_close( list
->value
[ index
] );
396 // Free this now if this has no child
397 if ( this->child
== NULL
)