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