Memory pooling part 2 and other optimisations
[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 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 += 50;
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 /** Get a data value by index.
228 */
229
230 void *mlt_properties_get_data_at( mlt_properties this, int index, int *size )
231 {
232 property_list *list = this->private;
233 if ( index >= 0 && index < list->count )
234 return mlt_property_get_data( list->value[ index ], size );
235 return NULL;
236 }
237
238 /** Return the number of items in the list.
239 */
240
241 int mlt_properties_count( mlt_properties this )
242 {
243 property_list *list = this->private;
244 return list->count;
245 }
246
247 /** Set a value by parsing a name=value string
248 */
249
250 int mlt_properties_parse( mlt_properties this, char *namevalue )
251 {
252 char *name = strdup( namevalue );
253 char *value = strdup( namevalue );
254 int error = 0;
255
256 if ( strchr( name, '=' ) )
257 {
258 *( strchr( name, '=' ) ) = '\0';
259 strcpy( value, strchr( value, '=' ) + 1 );
260 }
261 else
262 {
263 strcpy( value, "" );
264 }
265
266 if ( strlen( value ) > 1 && value[ 0 ] == '\"' )
267 {
268 strcpy( value, value + 1 );
269 if ( value[ strlen( value ) - 1 ] == '\"' )
270 value[ strlen( value ) - 1 ] = '\0';
271 }
272
273 error = mlt_properties_set( this, name, value );
274
275 free( name );
276 free( value );
277
278 return error;
279 }
280
281 /** Get a value associated to the name.
282 */
283
284 int mlt_properties_get_int( mlt_properties this, char *name )
285 {
286 mlt_property value = mlt_properties_find( this, name );
287 return value == NULL ? 0 : mlt_property_get_int( value );
288 }
289
290 /** Set a value associated to the name.
291 */
292
293 int mlt_properties_set_int( mlt_properties this, char *name, int value )
294 {
295 int error = 1;
296
297 // Fetch the property to work with
298 mlt_property property = mlt_properties_fetch( this, name );
299
300 // Set it if not NULL
301 if ( property != NULL )
302 {
303 error = mlt_property_set_int( property, value );
304 mlt_properties_do_mirror( this, name );
305 }
306
307 return error;
308 }
309
310 /** Get a value associated to the name.
311 */
312
313 double mlt_properties_get_double( mlt_properties this, char *name )
314 {
315 mlt_property value = mlt_properties_find( this, name );
316 return value == NULL ? 0 : mlt_property_get_double( value );
317 }
318
319 /** Set a value associated to the name.
320 */
321
322 int mlt_properties_set_double( mlt_properties this, char *name, double value )
323 {
324 int error = 1;
325
326 // Fetch the property to work with
327 mlt_property property = mlt_properties_fetch( this, name );
328
329 // Set it if not NULL
330 if ( property != NULL )
331 {
332 error = mlt_property_set_double( property, value );
333 mlt_properties_do_mirror( this, name );
334 }
335
336 return error;
337 }
338
339 /** Get a value associated to the name.
340 */
341
342 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
343 {
344 mlt_property value = mlt_properties_find( this, name );
345 return value == NULL ? 0 : mlt_property_get_position( value );
346 }
347
348 /** Set a value associated to the name.
349 */
350
351 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
352 {
353 int error = 1;
354
355 // Fetch the property to work with
356 mlt_property property = mlt_properties_fetch( this, name );
357
358 // Set it if not NULL
359 if ( property != NULL )
360 {
361 error = mlt_property_set_position( property, value );
362 mlt_properties_do_mirror( this, name );
363 }
364
365 return error;
366 }
367
368 /** Get a value associated to the name.
369 */
370
371 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
372 {
373 mlt_property value = mlt_properties_find( this, name );
374 return value == NULL ? NULL : mlt_property_get_data( value, length );
375 }
376
377 /** Set a value associated to the name.
378 */
379
380 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
381 {
382 int error = 1;
383
384 // Fetch the property to work with
385 mlt_property property = mlt_properties_fetch( this, name );
386
387 // Set it if not NULL
388 if ( property != NULL )
389 error = mlt_property_set_data( property, value, length, destroy, serialise );
390
391 return error;
392 }
393
394 /** Rename a property.
395 */
396
397 int mlt_properties_rename( mlt_properties this, char *source, char *dest )
398 {
399 mlt_property value = mlt_properties_find( this, dest );
400
401 if ( value == NULL )
402 {
403 property_list *list = this->private;
404 int i = 0;
405
406 // Locate the item
407 for ( i = 0; i < list->count; i ++ )
408 {
409 if ( !strcmp( list->name[ i ], source ) )
410 {
411 free( list->name[ i ] );
412 list->name[ i ] = strdup( dest );
413 break;
414 }
415 }
416 }
417
418 return value != NULL;
419 }
420
421 /** Dump the properties.
422 */
423
424 void mlt_properties_dump( mlt_properties this, FILE *output )
425 {
426 property_list *list = this->private;
427 int i = 0;
428 for ( i = 0; i < list->count; i ++ )
429 fprintf( stderr, "%s = %s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
430 }
431
432 /** Close the list.
433 */
434
435 void mlt_properties_close( mlt_properties this )
436 {
437 property_list *list = this->private;
438 int index = 0;
439
440 // Clean up names and values
441 for ( index = list->count - 1; index >= 0; index -- )
442 {
443 free( list->name[ index ] );
444 mlt_property_close( list->value[ index ] );
445 }
446
447 // Clear up the list
448 free( list->name );
449 free( list->value );
450 free( list );
451
452 // Free this now if this has no child
453 if ( this->child == NULL )
454 free( this );
455 }
456