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