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