]> git.sesse.net Git - mlt/blob - src/framework/mlt_properties.c
Miracle mods
[mlt] / 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         if ( strcmp( name, "in" ) && strcmp( name, "out" ) )
83         {
84                 mlt_properties mirror = mlt_properties_get_data( this, "mlt_mirror", NULL );
85                 if ( mirror != NULL )
86                 {
87                         char *value = mlt_properties_get( this, name );
88                         if ( value != NULL )
89                                 mlt_properties_set( mirror, name, value );
90                 }
91         }
92 }
93
94 /** Allow the specification of a mirror.
95 */
96
97 void mlt_properties_mirror( mlt_properties this, mlt_properties that )
98 {
99         mlt_properties_set_data( this, "mlt_mirror", that, 0, NULL, NULL );
100 }
101
102 /** Inherit all serialisable properties from that into this.
103 */
104
105 int mlt_properties_inherit( mlt_properties this, mlt_properties that )
106 {
107         int count = mlt_properties_count( that );
108         while ( count -- )
109         {
110                 char *value = mlt_properties_get_value( that, count );
111                 if ( value != NULL )
112                 {
113                         char *name = mlt_properties_get_name( that, count );
114                         mlt_properties_set( this, name, value );
115                 }
116         }
117         return 0;
118 }
119
120 /** Locate a property by name
121 */
122
123 static mlt_property mlt_properties_find( mlt_properties this, char *name )
124 {
125         mlt_property value = NULL;
126         property_list *list = this->private;
127         int i = 0;
128
129         // Locate the item 
130         for ( i = 0; value == NULL && i < list->count; i ++ )
131                 if ( !strcmp( list->name[ i ], name ) )
132                         value = list->value[ i ];
133
134         return value;
135 }
136
137 /** Add a new property.
138 */
139
140 static mlt_property mlt_properties_add( mlt_properties this, char *name )
141 {
142         property_list *list = this->private;
143
144         // Check that we have space and resize if necessary
145         if ( list->count == list->size )
146         {
147                 list->size += 10;
148                 list->name = realloc( list->name, list->size * sizeof( char * ) );
149                 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
150         }
151
152         // Assign name/value pair
153         list->name[ list->count ] = strdup( name );
154         list->value[ list->count ] = mlt_property_init( );
155
156         // Return and increment count accordingly
157         return list->value[ list->count ++ ];
158 }
159
160 /** Fetch a property by name - this includes add if not found.
161 */
162
163 static mlt_property mlt_properties_fetch( mlt_properties this, char *name )
164 {
165         // Try to find an existing property first
166         mlt_property property = mlt_properties_find( this, name );
167
168         // If it wasn't found, create one
169         if ( property == NULL )
170                 property = mlt_properties_add( this, name );
171
172         // Return the property
173         return property;
174 }
175
176 /** Set the property.
177 */
178
179 int mlt_properties_set( mlt_properties this, char *name, char *value )
180 {
181         int error = 1;
182
183         // Fetch the property to work with
184         mlt_property property = mlt_properties_fetch( this, name );
185
186         // Set it if not NULL
187         if ( property != NULL )
188         {
189                 error = mlt_property_set_string( property, value );
190                 mlt_properties_do_mirror( this, name );
191         }
192
193         return error;
194 }
195
196 /** Get a string value by name.
197 */
198
199 char *mlt_properties_get( mlt_properties this, char *name )
200 {
201         mlt_property value = mlt_properties_find( this, name );
202         return value == NULL ? NULL : mlt_property_get_string( value );
203 }
204
205 /** Get a name by index.
206 */
207
208 char *mlt_properties_get_name( mlt_properties this, int index )
209 {
210         property_list *list = this->private;
211         if ( index >= 0 && index < list->count )
212                 return list->name[ index ];
213         return NULL;
214 }
215
216 /** Get a string value by index.
217 */
218
219 char *mlt_properties_get_value( mlt_properties this, int index )
220 {
221         property_list *list = this->private;
222         if ( index >= 0 && index < list->count )
223                 return mlt_property_get_string( list->value[ index ] );
224         return NULL;
225 }
226
227 /** Return the number of items in the list.
228 */
229
230 int mlt_properties_count( mlt_properties this )
231 {
232         property_list *list = this->private;
233         return list->count;
234 }
235
236 /** Set a value by parsing a name=value string
237 */
238
239 int mlt_properties_parse( mlt_properties this, char *namevalue )
240 {
241         char *name = strdup( namevalue );
242         char *value = strdup( namevalue );
243         int error = 0;
244
245         if ( strchr( name, '=' ) )
246         {
247                 *( strchr( name, '=' ) ) = '\0';
248                 strcpy( value, strchr( value, '=' ) + 1 );
249         }
250         else
251         {
252                 strcpy( value, "" );
253         }
254
255         error = mlt_properties_set( this, name, value );
256
257         free( name );
258         free( value );
259
260         return error;
261 }
262
263 /** Get a value associated to the name.
264 */
265
266 int mlt_properties_get_int( mlt_properties this, char *name )
267 {
268         mlt_property value = mlt_properties_find( this, name );
269         return value == NULL ? 0 : mlt_property_get_int( value );
270 }
271
272 /** Set a value associated to the name.
273 */
274
275 int mlt_properties_set_int( mlt_properties this, char *name, int value )
276 {
277         int error = 1;
278
279         // Fetch the property to work with
280         mlt_property property = mlt_properties_fetch( this, name );
281
282         // Set it if not NULL
283         if ( property != NULL )
284         {
285                 error = mlt_property_set_int( property, value );
286                 mlt_properties_do_mirror( this, name );
287         }
288
289         return error;
290 }
291
292 /** Get a value associated to the name.
293 */
294
295 double mlt_properties_get_double( mlt_properties this, char *name )
296 {
297         mlt_property value = mlt_properties_find( this, name );
298         return value == NULL ? 0 : mlt_property_get_double( value );
299 }
300
301 /** Set a value associated to the name.
302 */
303
304 int mlt_properties_set_double( mlt_properties this, char *name, double value )
305 {
306         int error = 1;
307
308         // Fetch the property to work with
309         mlt_property property = mlt_properties_fetch( this, name );
310
311         // Set it if not NULL
312         if ( property != NULL )
313         {
314                 error = mlt_property_set_double( property, value );
315                 mlt_properties_do_mirror( this, name );
316         }
317
318         return error;
319 }
320
321 /** Get a value associated to the name.
322 */
323
324 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
325 {
326         mlt_property value = mlt_properties_find( this, name );
327         return value == NULL ? 0 : mlt_property_get_position( value );
328 }
329
330 /** Set a value associated to the name.
331 */
332
333 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
334 {
335         int error = 1;
336
337         // Fetch the property to work with
338         mlt_property property = mlt_properties_fetch( this, name );
339
340         // Set it if not NULL
341         if ( property != NULL )
342         {
343                 error = mlt_property_set_position( property, value );
344                 mlt_properties_do_mirror( this, name );
345         }
346
347         return error;
348 }
349
350 /** Get a value associated to the name.
351 */
352
353 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
354 {
355         mlt_property value = mlt_properties_find( this, name );
356         return value == NULL ? NULL : mlt_property_get_data( value, length );
357 }
358
359 /** Set a value associated to the name.
360 */
361
362 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
363 {
364         int error = 1;
365
366         // Fetch the property to work with
367         mlt_property property = mlt_properties_fetch( this, name );
368
369         // Set it if not NULL
370         if ( property != NULL )
371                 error = mlt_property_set_data( property, value, length, destroy, serialise );
372
373         return error;
374 }
375
376 /** Close the list.
377 */
378
379 void mlt_properties_close( mlt_properties this )
380 {
381         property_list *list = this->private;
382         int index = 0;
383
384         // Clean up names and values
385         for ( index = list->count - 1; index >= 0; index -- )
386         {
387                 free( list->name[ index ] );
388                 mlt_property_close( list->value[ index ] );
389         }
390
391         // Clear up the list
392         free( list->name );
393         free( list->value );
394         free( list );
395
396         // Free this now if this has no child
397         if ( this->child == NULL )
398                 free( this );
399 }
400