transition region
[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 int hash[ 199 ];
37 char **name;
38 mlt_property *value;
39 int count;
40 int size;
41 }
42 property_list;
43
44 /** Basic implementation.
45 */
46
47 int mlt_properties_init( mlt_properties this, void *child )
48 {
49 // NULL all methods
50 memset( this, 0, sizeof( struct mlt_properties_s ) );
51
52 // Assign the child of the object
53 this->child = child;
54
55 // Allocate the private structure
56 this->private = calloc( sizeof( property_list ), 1 );
57
58 return this->private == NULL;
59 }
60
61 /** Constructor for stand alone object.
62 */
63
64 mlt_properties mlt_properties_new( )
65 {
66 // Construct a standalone properties object
67 mlt_properties this = calloc( sizeof( struct mlt_properties_s ), 1 );
68
69 // Initialise this
70 mlt_properties_init( this, NULL );
71
72 // Return the pointer
73 return this;
74 }
75
76 static inline int generate_hash( char *name )
77 {
78 int hash = 0;
79 int i = 1;
80 while ( *name )
81 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
82 return hash;
83 }
84
85 /** Special case - when a container (such as fezzik) is protecting another
86 producer, we need to ensure that properties are passed through to the
87 real producer.
88 */
89
90 static void mlt_properties_do_mirror( mlt_properties this, char *name )
91 {
92 if ( strcmp( name, "in" ) && strcmp( name, "out" ) )
93 {
94 mlt_properties mirror = mlt_properties_get_data( this, "mlt_mirror", NULL );
95 if ( mirror != NULL )
96 {
97 char *value = mlt_properties_get( this, name );
98 if ( value != NULL )
99 mlt_properties_set( mirror, name, value );
100 }
101 }
102 }
103
104 /** Allow the specification of a mirror.
105 */
106
107 void mlt_properties_mirror( mlt_properties this, mlt_properties that )
108 {
109 mlt_properties_set_data( this, "mlt_mirror", that, 0, NULL, NULL );
110 }
111
112 /** Inherit all serialisable properties from that into this.
113 */
114
115 int mlt_properties_inherit( mlt_properties this, mlt_properties that )
116 {
117 int count = mlt_properties_count( that );
118 int i = 0;
119 for ( i = 0; i < count; i ++ )
120 {
121 char *value = mlt_properties_get_value( that, i );
122 if ( value != NULL )
123 {
124 char *name = mlt_properties_get_name( that, i );
125 mlt_properties_set( this, name, value );
126 }
127 }
128 return 0;
129 }
130
131 /** Pass all properties from 'that' that match the prefix to 'this' (excluding the prefix).
132 */
133
134 int mlt_properties_pass( mlt_properties this, mlt_properties that, char *prefix )
135 {
136 int count = mlt_properties_count( that );
137 int length = strlen( prefix );
138 int i = 0;
139 for ( i = 0; i < count; i ++ )
140 {
141 char *name = mlt_properties_get_name( that, i );
142 if ( !strncmp( name, prefix, length ) )
143 {
144 char *value = mlt_properties_get_value( that, i );
145 if ( value != NULL )
146 mlt_properties_set( this, name + length, value );
147 }
148 }
149 return 0;
150 }
151
152 /** Locate a property by name
153 */
154
155 static inline mlt_property mlt_properties_find( mlt_properties this, char *name )
156 {
157 property_list *list = this->private;
158 mlt_property value = NULL;
159 int key = generate_hash( name );
160 int i = list->hash[ key ];
161
162 // Check if we're hashed
163 if ( list->count > 0 &&
164 name[ 0 ] == list->name[ i ][ 0 ] &&
165 !strcmp( list->name[ i ], name ) )
166 value = list->value[ i ];
167
168 // Locate the item
169 for ( i = 0; value == NULL && i < list->count; i ++ )
170 if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
171 value = list->value[ i ];
172
173 return value;
174 }
175
176 /** Add a new property.
177 */
178
179 static mlt_property mlt_properties_add( mlt_properties this, char *name )
180 {
181 property_list *list = this->private;
182 int key = generate_hash( name );
183
184 // Check that we have space and resize if necessary
185 if ( list->count == list->size )
186 {
187 list->size += 50;
188 list->name = realloc( list->name, list->size * sizeof( char * ) );
189 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
190 }
191
192 // Assign name/value pair
193 list->name[ list->count ] = strdup( name );
194 list->value[ list->count ] = mlt_property_init( );
195
196 // Assign to hash table
197 list->hash[ key ] = list->count;
198
199 // Return and increment count accordingly
200 return list->value[ list->count ++ ];
201 }
202
203 /** Fetch a property by name - this includes add if not found.
204 */
205
206 static mlt_property mlt_properties_fetch( mlt_properties this, char *name )
207 {
208 // Try to find an existing property first
209 mlt_property property = mlt_properties_find( this, name );
210
211 // If it wasn't found, create one
212 if ( property == NULL )
213 property = mlt_properties_add( this, name );
214
215 // Return the property
216 return property;
217 }
218
219 /** Set the property.
220 */
221
222 int mlt_properties_set( mlt_properties this, char *name, char *value )
223 {
224 int error = 1;
225
226 // Fetch the property to work with
227 mlt_property property = mlt_properties_fetch( this, name );
228
229 // Set it if not NULL
230 if ( property != NULL )
231 {
232 error = mlt_property_set_string( property, value );
233 mlt_properties_do_mirror( this, name );
234 }
235
236 return error;
237 }
238
239 /** Get a string value by name.
240 */
241
242 char *mlt_properties_get( mlt_properties this, char *name )
243 {
244 mlt_property value = mlt_properties_find( this, name );
245 return value == NULL ? NULL : mlt_property_get_string( value );
246 }
247
248 /** Get a name by index.
249 */
250
251 char *mlt_properties_get_name( mlt_properties this, int index )
252 {
253 property_list *list = this->private;
254 if ( index >= 0 && index < list->count )
255 return list->name[ index ];
256 return NULL;
257 }
258
259 /** Get a string value by index.
260 */
261
262 char *mlt_properties_get_value( mlt_properties this, int index )
263 {
264 property_list *list = this->private;
265 if ( index >= 0 && index < list->count )
266 return mlt_property_get_string( list->value[ index ] );
267 return NULL;
268 }
269
270 /** Get a data value by index.
271 */
272
273 void *mlt_properties_get_data_at( mlt_properties this, int index, int *size )
274 {
275 property_list *list = this->private;
276 if ( index >= 0 && index < list->count )
277 return mlt_property_get_data( list->value[ index ], size );
278 return NULL;
279 }
280
281 /** Return the number of items in the list.
282 */
283
284 int mlt_properties_count( mlt_properties this )
285 {
286 property_list *list = this->private;
287 return list->count;
288 }
289
290 /** Set a value by parsing a name=value string
291 */
292
293 int mlt_properties_parse( mlt_properties this, char *namevalue )
294 {
295 char *name = strdup( namevalue );
296 char *value = strdup( namevalue );
297 int error = 0;
298
299 if ( strchr( name, '=' ) )
300 {
301 *( strchr( name, '=' ) ) = '\0';
302 strcpy( value, strchr( value, '=' ) + 1 );
303 }
304 else
305 {
306 strcpy( value, "" );
307 }
308
309 if ( strlen( value ) > 1 && value[ 0 ] == '\"' )
310 {
311 strcpy( value, value + 1 );
312 if ( value[ strlen( value ) - 1 ] == '\"' )
313 value[ strlen( value ) - 1 ] = '\0';
314 }
315
316 error = mlt_properties_set( this, name, value );
317
318 free( name );
319 free( value );
320
321 return error;
322 }
323
324 /** Get a value associated to the name.
325 */
326
327 int mlt_properties_get_int( mlt_properties this, char *name )
328 {
329 mlt_property value = mlt_properties_find( this, name );
330 return value == NULL ? 0 : mlt_property_get_int( value );
331 }
332
333 /** Set a value associated to the name.
334 */
335
336 int mlt_properties_set_int( mlt_properties this, char *name, int value )
337 {
338 int error = 1;
339
340 // Fetch the property to work with
341 mlt_property property = mlt_properties_fetch( this, name );
342
343 // Set it if not NULL
344 if ( property != NULL )
345 {
346 error = mlt_property_set_int( property, value );
347 mlt_properties_do_mirror( this, name );
348 }
349
350 return error;
351 }
352
353 /** Get a value associated to the name.
354 */
355
356 double mlt_properties_get_double( mlt_properties this, char *name )
357 {
358 mlt_property value = mlt_properties_find( this, name );
359 return value == NULL ? 0 : mlt_property_get_double( value );
360 }
361
362 /** Set a value associated to the name.
363 */
364
365 int mlt_properties_set_double( mlt_properties this, char *name, double value )
366 {
367 int error = 1;
368
369 // Fetch the property to work with
370 mlt_property property = mlt_properties_fetch( this, name );
371
372 // Set it if not NULL
373 if ( property != NULL )
374 {
375 error = mlt_property_set_double( property, value );
376 mlt_properties_do_mirror( this, name );
377 }
378
379 return error;
380 }
381
382 /** Get a value associated to the name.
383 */
384
385 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
386 {
387 mlt_property value = mlt_properties_find( this, name );
388 return value == NULL ? 0 : mlt_property_get_position( value );
389 }
390
391 /** Set a value associated to the name.
392 */
393
394 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
395 {
396 int error = 1;
397
398 // Fetch the property to work with
399 mlt_property property = mlt_properties_fetch( this, name );
400
401 // Set it if not NULL
402 if ( property != NULL )
403 {
404 error = mlt_property_set_position( property, value );
405 mlt_properties_do_mirror( this, name );
406 }
407
408 return error;
409 }
410
411 /** Get a value associated to the name.
412 */
413
414 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
415 {
416 mlt_property value = mlt_properties_find( this, name );
417 return value == NULL ? NULL : mlt_property_get_data( value, length );
418 }
419
420 /** Set a value associated to the name.
421 */
422
423 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
424 {
425 int error = 1;
426
427 // Fetch the property to work with
428 mlt_property property = mlt_properties_fetch( this, name );
429
430 // Set it if not NULL
431 if ( property != NULL )
432 error = mlt_property_set_data( property, value, length, destroy, serialise );
433
434 return error;
435 }
436
437 /** Rename a property.
438 */
439
440 int mlt_properties_rename( mlt_properties this, char *source, char *dest )
441 {
442 mlt_property value = mlt_properties_find( this, dest );
443
444 if ( value == NULL )
445 {
446 property_list *list = this->private;
447 int i = 0;
448
449 // Locate the item
450 for ( i = 0; i < list->count; i ++ )
451 {
452 if ( !strcmp( list->name[ i ], source ) )
453 {
454 free( list->name[ i ] );
455 list->name[ i ] = strdup( dest );
456 break;
457 }
458 }
459 }
460
461 return value != NULL;
462 }
463
464 /** Dump the properties.
465 */
466
467 void mlt_properties_dump( mlt_properties this, FILE *output )
468 {
469 property_list *list = this->private;
470 int i = 0;
471 for ( i = 0; i < list->count; i ++ )
472 fprintf( stderr, "%s = %s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
473 }
474
475 /** Close the list.
476 */
477
478 void mlt_properties_close( mlt_properties this )
479 {
480 property_list *list = this->private;
481 int index = 0;
482
483 // Clean up names and values
484 for ( index = list->count - 1; index >= 0; index -- )
485 {
486 free( list->name[ index ] );
487 mlt_property_close( list->value[ index ] );
488 }
489
490 // Clear up the list
491 free( list->name );
492 free( list->value );
493 free( list );
494
495 // Free this now if this has no child
496 if ( this->child == NULL )
497 free( this );
498 }
499