pixbuf, composite and fezzik mirrors
[melted] / src / framework / mlt_properties.c
1 /*
2 * mlt_properties.c -- base properties class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include "config.h"
22 #include "mlt_properties.h"
23 #include "mlt_property.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 /* ---------------- // Private Implementation // ---------------- */
30
31 /** Private implementation of the property list.
32 */
33
34 typedef struct
35 {
36 char **name;
37 mlt_property *value;
38 int count;
39 int size;
40 }
41 property_list;
42
43 /** Basic implementation.
44 */
45
46 int mlt_properties_init( mlt_properties this, void *child )
47 {
48 // NULL all methods
49 memset( this, 0, sizeof( struct mlt_properties_s ) );
50
51 // Assign the child of the object
52 this->child = child;
53
54 // Allocate the private structure
55 this->private = calloc( sizeof( property_list ), 1 );
56
57 return this->private == NULL;
58 }
59
60 /** Constructor for stand alone object.
61 */
62
63 mlt_properties mlt_properties_new( )
64 {
65 // Construct a standalone properties object
66 mlt_properties this = calloc( sizeof( struct mlt_properties_s ), 1 );
67
68 // Initialise this
69 mlt_properties_init( this, NULL );
70
71 // Return the pointer
72 return this;
73 }
74
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
77 real producer.
78 */
79
80 static void mlt_properties_do_mirror( mlt_properties this, char *name )
81 {
82 mlt_properties mirror = mlt_properties_get_data( this, "mlt_mirror", NULL );
83 if ( mirror != NULL )
84 {
85 char *value = mlt_properties_get( this, name );
86 if ( value != NULL )
87 mlt_properties_set( mirror, name, value );
88 }
89 }
90
91 /** Allow the specification of a mirror.
92 */
93
94 void mlt_properties_mirror( mlt_properties this, mlt_properties that )
95 {
96 mlt_properties_set_data( this, "mlt_mirror", that, 0, NULL, NULL );
97 }
98
99 /** Inherit all serialisable properties from that into this.
100 */
101
102 int mlt_properties_inherit( mlt_properties this, mlt_properties that )
103 {
104 int count = mlt_properties_count( that );
105 while ( count -- )
106 {
107 char *value = mlt_properties_get_value( that, count );
108 if ( value != NULL )
109 {
110 char *name = mlt_properties_get_name( that, count );
111 mlt_properties_set( this, name, value );
112 }
113 }
114 return 0;
115 }
116
117 /** Locate a property by name
118 */
119
120 static mlt_property mlt_properties_find( mlt_properties this, char *name )
121 {
122 mlt_property value = NULL;
123 property_list *list = this->private;
124 int i = 0;
125
126 // Locate the item
127 for ( i = 0; value == NULL && i < list->count; i ++ )
128 if ( !strcmp( list->name[ i ], name ) )
129 value = list->value[ i ];
130
131 return value;
132 }
133
134 /** Add a new property.
135 */
136
137 static mlt_property mlt_properties_add( mlt_properties this, char *name )
138 {
139 property_list *list = this->private;
140
141 // Check that we have space and resize if necessary
142 if ( list->count == list->size )
143 {
144 list->size += 10;
145 list->name = realloc( list->name, list->size * sizeof( char * ) );
146 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
147 }
148
149 // Assign name/value pair
150 list->name[ list->count ] = strdup( name );
151 list->value[ list->count ] = mlt_property_init( );
152
153 // Return and increment count accordingly
154 return list->value[ list->count ++ ];
155 }
156
157 /** Fetch a property by name - this includes add if not found.
158 */
159
160 static mlt_property mlt_properties_fetch( mlt_properties this, char *name )
161 {
162 // Try to find an existing property first
163 mlt_property property = mlt_properties_find( this, name );
164
165 // If it wasn't found, create one
166 if ( property == NULL )
167 property = mlt_properties_add( this, name );
168
169 // Return the property
170 return property;
171 }
172
173 /** Set the property.
174 */
175
176 int mlt_properties_set( mlt_properties this, char *name, char *value )
177 {
178 int error = 1;
179
180 // Fetch the property to work with
181 mlt_property property = mlt_properties_fetch( this, name );
182
183 // Set it if not NULL
184 if ( property != NULL )
185 {
186 error = mlt_property_set_string( property, value );
187 mlt_properties_do_mirror( this, name );
188 }
189
190 return error;
191 }
192
193 /** Get a string value by name.
194 */
195
196 char *mlt_properties_get( mlt_properties this, char *name )
197 {
198 mlt_property value = mlt_properties_find( this, name );
199 return value == NULL ? NULL : mlt_property_get_string( value );
200 }
201
202 /** Get a name by index.
203 */
204
205 char *mlt_properties_get_name( mlt_properties this, int index )
206 {
207 property_list *list = this->private;
208 if ( index >= 0 && index < list->count )
209 return list->name[ index ];
210 return NULL;
211 }
212
213 /** Get a string value by index.
214 */
215
216 char *mlt_properties_get_value( mlt_properties this, int index )
217 {
218 property_list *list = this->private;
219 if ( index >= 0 && index < list->count )
220 return mlt_property_get_string( list->value[ index ] );
221 return NULL;
222 }
223
224 /** Return the number of items in the list.
225 */
226
227 int mlt_properties_count( mlt_properties this )
228 {
229 property_list *list = this->private;
230 return list->count;
231 }
232
233 /** Set a value by parsing a name=value string
234 */
235
236 int mlt_properties_parse( mlt_properties this, char *namevalue )
237 {
238 char *name = strdup( namevalue );
239 char *value = strdup( namevalue );
240 int error = 0;
241
242 if ( strchr( name, '=' ) )
243 {
244 *( strchr( name, '=' ) ) = '\0';
245 strcpy( value, strchr( value, '=' ) + 1 );
246 }
247 else
248 {
249 strcpy( value, "" );
250 }
251
252 error = mlt_properties_set( this, name, value );
253
254 free( name );
255 free( value );
256
257 return error;
258 }
259
260 /** Get a value associated to the name.
261 */
262
263 int mlt_properties_get_int( mlt_properties this, char *name )
264 {
265 mlt_property value = mlt_properties_find( this, name );
266 return value == NULL ? 0 : mlt_property_get_int( value );
267 }
268
269 /** Set a value associated to the name.
270 */
271
272 int mlt_properties_set_int( mlt_properties this, char *name, int value )
273 {
274 int error = 1;
275
276 // Fetch the property to work with
277 mlt_property property = mlt_properties_fetch( this, name );
278
279 // Set it if not NULL
280 if ( property != NULL )
281 {
282 error = mlt_property_set_int( property, value );
283 mlt_properties_do_mirror( this, name );
284 }
285
286 return error;
287 }
288
289 /** Get a value associated to the name.
290 */
291
292 double mlt_properties_get_double( mlt_properties this, char *name )
293 {
294 mlt_property value = mlt_properties_find( this, name );
295 return value == NULL ? 0 : mlt_property_get_double( value );
296 }
297
298 /** Set a value associated to the name.
299 */
300
301 int mlt_properties_set_double( mlt_properties this, char *name, double value )
302 {
303 int error = 1;
304
305 // Fetch the property to work with
306 mlt_property property = mlt_properties_fetch( this, name );
307
308 // Set it if not NULL
309 if ( property != NULL )
310 {
311 error = mlt_property_set_double( property, value );
312 mlt_properties_do_mirror( this, name );
313 }
314
315 return error;
316 }
317
318 /** Get a value associated to the name.
319 */
320
321 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
322 {
323 mlt_property value = mlt_properties_find( this, name );
324 return value == NULL ? 0 : mlt_property_get_position( value );
325 }
326
327 /** Set a value associated to the name.
328 */
329
330 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
331 {
332 int error = 1;
333
334 // Fetch the property to work with
335 mlt_property property = mlt_properties_fetch( this, name );
336
337 // Set it if not NULL
338 if ( property != NULL )
339 {
340 error = mlt_property_set_position( property, value );
341 mlt_properties_do_mirror( this, name );
342 }
343
344 return error;
345 }
346
347 /** Get a value associated to the name.
348 */
349
350 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
351 {
352 mlt_property value = mlt_properties_find( this, name );
353 return value == NULL ? NULL : mlt_property_get_data( value, length );
354 }
355
356 /** Set a value associated to the name.
357 */
358
359 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
360 {
361 int error = 1;
362
363 // Fetch the property to work with
364 mlt_property property = mlt_properties_fetch( this, name );
365
366 // Set it if not NULL
367 if ( property != NULL )
368 error = mlt_property_set_data( property, value, length, destroy, serialise );
369
370 return error;
371 }
372
373 /** Close the list.
374 */
375
376 void mlt_properties_close( mlt_properties this )
377 {
378 property_list *list = this->private;
379 int index = 0;
380
381 // Clean up names and values
382 for ( index = list->count - 1; index >= 0; index -- )
383 {
384 free( list->name[ index ] );
385 mlt_property_close( list->value[ index ] );
386 }
387
388 // Clear up the list
389 free( list->name );
390 free( list->value );
391 free( list );
392
393 // Free this now if this has no child
394 if ( this->child == NULL )
395 free( this );
396 }
397