Corrections after valgrinding
[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 #include <ctype.h>
29
30 /* ---------------- // Private Implementation // ---------------- */
31
32 /** Private implementation of the property list.
33 */
34
35 typedef struct
36 {
37 int hash[ 199 ];
38 char **name;
39 mlt_property *value;
40 int count;
41 int size;
42 mlt_properties mirror;
43 int ref_count;
44 }
45 property_list;
46
47 /** Memory leak checks.
48 */
49
50 //#define _MLT_PROPERTY_CHECKS_ 2
51
52 #ifdef _MLT_PROPERTY_CHECKS_
53 static int properties_created = 0;
54 static int properties_destroyed = 0;
55 #endif
56
57 /** Basic implementation.
58 */
59
60 int mlt_properties_init( mlt_properties this, void *child )
61 {
62 if ( this != NULL )
63 {
64 #ifdef _MLT_PROPERTY_CHECKS_
65 // Increment number of properties created
66 properties_created ++;
67 #endif
68
69 // NULL all methods
70 memset( this, 0, sizeof( struct mlt_properties_s ) );
71
72 // Assign the child of the object
73 this->child = child;
74
75 // Allocate the local structure
76 this->local = calloc( sizeof( property_list ), 1 );
77
78 // Increment the ref count
79 ( ( property_list * )this->local )->ref_count = 1;
80 }
81
82 // Check that initialisation was successful
83 return this != NULL && this->local == NULL;
84 }
85
86 /** Constructor for stand alone object.
87 */
88
89 mlt_properties mlt_properties_new( )
90 {
91 // Construct a standalone properties object
92 mlt_properties this = calloc( sizeof( struct mlt_properties_s ), 1 );
93
94 // Initialise this
95 mlt_properties_init( this, NULL );
96
97 // Return the pointer
98 return this;
99 }
100
101 /** Load properties from a file.
102 */
103
104 mlt_properties mlt_properties_load( char *filename )
105 {
106 // Construct a standalone properties object
107 mlt_properties this = mlt_properties_new( );
108
109 if ( this != NULL )
110 {
111 // Open the file
112 FILE *file = fopen( filename, "r" );
113
114 // Load contents of file
115 if ( file != NULL )
116 {
117 // Temp string
118 char temp[ 1024 ];
119 char last[ 1024 ] = "";
120
121 // Read each string from the file
122 while( fgets( temp, 1024, file ) )
123 {
124 // Chomp the string
125 temp[ strlen( temp ) - 1 ] = '\0';
126
127 // Check if the line starts with a .
128 if ( temp[ 0 ] == '.' )
129 {
130 char temp2[ 1024 ];
131 sprintf( temp2, "%s%s", last, temp );
132 strcpy( temp, temp2 );
133 }
134 else if ( strchr( temp, '=' ) )
135 {
136 strcpy( last, temp );
137 *( strchr( last, '=' ) ) = '\0';
138 }
139
140 // Parse and set the property
141 if ( strcmp( temp, "" ) && temp[ 0 ] != '#' )
142 mlt_properties_parse( this, temp );
143 }
144
145 // Close the file
146 fclose( file );
147 }
148 }
149
150 // Return the pointer
151 return this;
152 }
153
154 static inline int generate_hash( char *name )
155 {
156 int hash = 0;
157 int i = 1;
158 while ( *name )
159 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
160 return hash;
161 }
162
163 /** Special case - when a container (such as fezzik) is protecting another
164 producer, we need to ensure that properties are passed through to the
165 real producer.
166 */
167
168 static inline void mlt_properties_do_mirror( mlt_properties this, char *name )
169 {
170 property_list *list = this->local;
171 if ( list->mirror != NULL )
172 {
173 char *value = mlt_properties_get( this, name );
174 if ( value != NULL )
175 mlt_properties_set( list->mirror, name, value );
176 }
177 }
178
179 /** Maintain ref count to allow multiple uses of an mlt object.
180 */
181
182 int mlt_properties_inc_ref( mlt_properties this )
183 {
184 if ( this != NULL )
185 {
186 property_list *list = this->local;
187 return ++ list->ref_count;
188 }
189 return 0;
190 }
191
192 /** Maintain ref count to allow multiple uses of an mlt object.
193 */
194
195 int mlt_properties_dec_ref( mlt_properties this )
196 {
197 if ( this != NULL )
198 {
199 property_list *list = this->local;
200 return -- list->ref_count;
201 }
202 return 0;
203 }
204
205 /** Return the ref count of this object.
206 */
207
208 int mlt_properties_ref_count( mlt_properties this )
209 {
210 if ( this != NULL )
211 {
212 property_list *list = this->local;
213 return list->ref_count;
214 }
215 return 0;
216 }
217
218 /** Allow the specification of a mirror.
219 */
220
221 void mlt_properties_mirror( mlt_properties this, mlt_properties that )
222 {
223 property_list *list = this->local;
224 list->mirror = that;
225 }
226
227 /** Inherit all serialisable properties from that into this.
228 */
229
230 int mlt_properties_inherit( mlt_properties this, mlt_properties that )
231 {
232 int count = mlt_properties_count( that );
233 int i = 0;
234 for ( i = 0; i < count; i ++ )
235 {
236 char *value = mlt_properties_get_value( that, i );
237 if ( value != NULL )
238 {
239 char *name = mlt_properties_get_name( that, i );
240 mlt_properties_set( this, name, value );
241 }
242 }
243 return 0;
244 }
245
246 /** Pass all properties from 'that' that match the prefix to 'this' (excluding the prefix).
247 */
248
249 int mlt_properties_pass( mlt_properties this, mlt_properties that, char *prefix )
250 {
251 int count = mlt_properties_count( that );
252 int length = strlen( prefix );
253 int i = 0;
254 for ( i = 0; i < count; i ++ )
255 {
256 char *name = mlt_properties_get_name( that, i );
257 if ( !strncmp( name, prefix, length ) )
258 {
259 char *value = mlt_properties_get_value( that, i );
260 if ( value != NULL )
261 mlt_properties_set( this, name + length, value );
262 }
263 }
264 return 0;
265 }
266
267 /** Locate a property by name
268 */
269
270 static inline mlt_property mlt_properties_find( mlt_properties this, char *name )
271 {
272 property_list *list = this->local;
273 mlt_property value = NULL;
274 int key = generate_hash( name );
275 int i = list->hash[ key ] - 1;
276
277 if ( i >= 0 )
278 {
279 // Check if we're hashed
280 if ( list->count > 0 &&
281 name[ 0 ] == list->name[ i ][ 0 ] &&
282 !strcmp( list->name[ i ], name ) )
283 value = list->value[ i ];
284
285 // Locate the item
286 for ( i = list->count - 1; value == NULL && i >= 0; i -- )
287 if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
288 value = list->value[ i ];
289 }
290
291 return value;
292 }
293
294 /** Add a new property.
295 */
296
297 static mlt_property mlt_properties_add( mlt_properties this, char *name )
298 {
299 property_list *list = this->local;
300 int key = generate_hash( name );
301
302 // Check that we have space and resize if necessary
303 if ( list->count == list->size )
304 {
305 list->size += 50;
306 list->name = realloc( list->name, list->size * sizeof( char * ) );
307 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
308 }
309
310 // Assign name/value pair
311 list->name[ list->count ] = strdup( name );
312 list->value[ list->count ] = mlt_property_init( );
313
314 // Assign to hash table
315 if ( list->hash[ key ] == 0 )
316 list->hash[ key ] = list->count + 1;
317
318 // Return and increment count accordingly
319 return list->value[ list->count ++ ];
320 }
321
322 /** Fetch a property by name - this includes add if not found.
323 */
324
325 static mlt_property mlt_properties_fetch( mlt_properties this, char *name )
326 {
327 // Try to find an existing property first
328 mlt_property property = mlt_properties_find( this, name );
329
330 // If it wasn't found, create one
331 if ( property == NULL )
332 property = mlt_properties_add( this, name );
333
334 // Return the property
335 return property;
336 }
337
338 /** Set the property.
339 */
340
341 int mlt_properties_set( mlt_properties this, char *name, char *value )
342 {
343 int error = 1;
344
345 // Fetch the property to work with
346 mlt_property property = mlt_properties_fetch( this, name );
347
348 // Set it if not NULL
349 if ( property == NULL )
350 {
351 fprintf( stderr, "Whoops\n" );
352 }
353 else if ( value == NULL )
354 {
355 error = mlt_property_set_string( property, value );
356 mlt_properties_do_mirror( this, name );
357 }
358 else if ( *value != '@' )
359 {
360 error = mlt_property_set_string( property, value );
361 mlt_properties_do_mirror( this, name );
362 }
363 else if ( property != NULL && value[ 0 ] == '@' )
364 {
365 int total = 0;
366 int current = 0;
367 char id[ 255 ];
368 char op = '+';
369
370 value ++;
371
372 while ( *value != '\0' )
373 {
374 int length = strcspn( value, "+-*/" );
375
376 // Get the identifier
377 strncpy( id, value, length );
378 id[ length ] = '\0';
379 value += length;
380
381 // Determine the value
382 if ( isdigit( id[ 0 ] ) )
383 current = atof( id );
384 else
385 current = mlt_properties_get_int( this, id );
386
387 // Apply the operation
388 switch( op )
389 {
390 case '+':
391 total += current;
392 break;
393 case '-':
394 total -= current;
395 break;
396 case '*':
397 total *= current;
398 break;
399 case '/':
400 total /= current;
401 break;
402 }
403
404 // Get the next op
405 op = *value != '\0' ? *value ++ : ' ';
406 }
407
408 error = mlt_property_set_int( property, total );
409 mlt_properties_do_mirror( this, name );
410 }
411
412 mlt_events_fire( this, "property-changed", name, NULL );
413
414 return error;
415 }
416
417 /** Set or default the property.
418 */
419
420 int mlt_properties_set_or_default( mlt_properties this, char *name, char *value, char *def )
421 {
422 return mlt_properties_set( this, name, value == NULL ? def : value );
423 }
424
425 /** Get a string value by name.
426 */
427
428 char *mlt_properties_get( mlt_properties this, char *name )
429 {
430 mlt_property value = mlt_properties_find( this, name );
431 return value == NULL ? NULL : mlt_property_get_string( value );
432 }
433
434 /** Get a name by index.
435 */
436
437 char *mlt_properties_get_name( mlt_properties this, int index )
438 {
439 property_list *list = this->local;
440 if ( index >= 0 && index < list->count )
441 return list->name[ index ];
442 return NULL;
443 }
444
445 /** Get a string value by index.
446 */
447
448 char *mlt_properties_get_value( mlt_properties this, int index )
449 {
450 property_list *list = this->local;
451 if ( index >= 0 && index < list->count )
452 return mlt_property_get_string( list->value[ index ] );
453 return NULL;
454 }
455
456 /** Get a data value by index.
457 */
458
459 void *mlt_properties_get_data_at( mlt_properties this, int index, int *size )
460 {
461 property_list *list = this->local;
462 if ( index >= 0 && index < list->count )
463 return mlt_property_get_data( list->value[ index ], size );
464 return NULL;
465 }
466
467 /** Return the number of items in the list.
468 */
469
470 int mlt_properties_count( mlt_properties this )
471 {
472 property_list *list = this->local;
473 return list->count;
474 }
475
476 /** Set a value by parsing a name=value string
477 */
478
479 int mlt_properties_parse( mlt_properties this, char *namevalue )
480 {
481 char *name = strdup( namevalue );
482 char *value = NULL;
483 int error = 0;
484 char *ptr = strchr( name, '=' );
485
486 if ( ptr )
487 {
488 *( ptr ++ ) = '\0';
489
490 if ( *ptr != '\"' )
491 {
492 value = strdup( ptr );
493 }
494 else
495 {
496 ptr ++;
497 value = strdup( ptr );
498 if ( value != NULL && value[ strlen( value ) - 1 ] == '\"' )
499 value[ strlen( value ) - 1 ] = '\0';
500 }
501 }
502 else
503 {
504 value = strdup( "" );
505 }
506
507 error = mlt_properties_set( this, name, value );
508
509 free( name );
510 free( value );
511
512 return error;
513 }
514
515 /** Get a value associated to the name.
516 */
517
518 int mlt_properties_get_int( mlt_properties this, char *name )
519 {
520 mlt_property value = mlt_properties_find( this, name );
521 return value == NULL ? 0 : mlt_property_get_int( value );
522 }
523
524 /** Set a value associated to the name.
525 */
526
527 int mlt_properties_set_int( mlt_properties this, char *name, int value )
528 {
529 int error = 1;
530
531 // Fetch the property to work with
532 mlt_property property = mlt_properties_fetch( this, name );
533
534 // Set it if not NULL
535 if ( property != NULL )
536 {
537 error = mlt_property_set_int( property, value );
538 mlt_properties_do_mirror( this, name );
539 }
540
541 mlt_events_fire( this, "property-changed", name, NULL );
542
543 return error;
544 }
545
546 /** Get a value associated to the name.
547 */
548
549 double mlt_properties_get_double( mlt_properties this, char *name )
550 {
551 mlt_property value = mlt_properties_find( this, name );
552 return value == NULL ? 0 : mlt_property_get_double( value );
553 }
554
555 /** Set a value associated to the name.
556 */
557
558 int mlt_properties_set_double( mlt_properties this, char *name, double value )
559 {
560 int error = 1;
561
562 // Fetch the property to work with
563 mlt_property property = mlt_properties_fetch( this, name );
564
565 // Set it if not NULL
566 if ( property != NULL )
567 {
568 error = mlt_property_set_double( property, value );
569 mlt_properties_do_mirror( this, name );
570 }
571
572 mlt_events_fire( this, "property-changed", name, NULL );
573
574 return error;
575 }
576
577 /** Get a value associated to the name.
578 */
579
580 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
581 {
582 mlt_property value = mlt_properties_find( this, name );
583 return value == NULL ? 0 : mlt_property_get_position( value );
584 }
585
586 /** Set a value associated to the name.
587 */
588
589 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
590 {
591 int error = 1;
592
593 // Fetch the property to work with
594 mlt_property property = mlt_properties_fetch( this, name );
595
596 // Set it if not NULL
597 if ( property != NULL )
598 {
599 error = mlt_property_set_position( property, value );
600 mlt_properties_do_mirror( this, name );
601 }
602
603 mlt_events_fire( this, "property-changed", name, NULL );
604
605 return error;
606 }
607
608 /** Get a value associated to the name.
609 */
610
611 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
612 {
613 mlt_property value = mlt_properties_find( this, name );
614 return value == NULL ? NULL : mlt_property_get_data( value, length );
615 }
616
617 /** Set a value associated to the name.
618 */
619
620 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
621 {
622 int error = 1;
623
624 // Fetch the property to work with
625 mlt_property property = mlt_properties_fetch( this, name );
626
627 // Set it if not NULL
628 if ( property != NULL )
629 error = mlt_property_set_data( property, value, length, destroy, serialise );
630
631 mlt_events_fire( this, "property-changed", name, NULL );
632
633 return error;
634 }
635
636 /** Rename a property.
637 */
638
639 int mlt_properties_rename( mlt_properties this, char *source, char *dest )
640 {
641 mlt_property value = mlt_properties_find( this, dest );
642
643 if ( value == NULL )
644 {
645 property_list *list = this->local;
646 int i = 0;
647
648 // Locate the item
649 for ( i = 0; i < list->count; i ++ )
650 {
651 if ( !strcmp( list->name[ i ], source ) )
652 {
653 free( list->name[ i ] );
654 list->name[ i ] = strdup( dest );
655 list->hash[ generate_hash( dest ) ] = i + 1;
656 break;
657 }
658 }
659 }
660
661 return value != NULL;
662 }
663
664 /** Dump the properties.
665 */
666
667 void mlt_properties_dump( mlt_properties this, FILE *output )
668 {
669 property_list *list = this->local;
670 int i = 0;
671 for ( i = 0; i < list->count; i ++ )
672 if ( mlt_properties_get( this, list->name[ i ] ) != NULL )
673 fprintf( output, "%s=%s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
674 }
675
676 void mlt_properties_debug( mlt_properties this, char *title, FILE *output )
677 {
678 fprintf( output, "%s: ", title );
679 if ( this != NULL )
680 {
681 property_list *list = this->local;
682 int i = 0;
683 fprintf( output, "[ ref=%d", list->ref_count );
684 for ( i = 0; i < list->count; i ++ )
685 if ( mlt_properties_get( this, list->name[ i ] ) != NULL )
686 fprintf( output, ", %s=%s", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
687 else
688 fprintf( output, ", %s=%p", list->name[ i ], mlt_properties_get_data( this, list->name[ i ], NULL ) );
689 fprintf( output, " ]" );
690 }
691 fprintf( output, "\n" );
692 }
693
694 /** Close the list.
695 */
696
697 void mlt_properties_close( mlt_properties this )
698 {
699 if ( this != NULL && mlt_properties_dec_ref( this ) <= 0 )
700 {
701 if ( this->close != NULL )
702 {
703 this->close( this->close_object );
704 }
705 else
706 {
707 property_list *list = this->local;
708 int index = 0;
709
710 #if _MLT_PROPERTY_CHECKS_ == 1
711 // Show debug info
712 mlt_properties_debug( this, "Closing", stderr );
713 #endif
714
715 #ifdef _MLT_PROPERTY_CHECKS_
716 // Increment destroyed count
717 properties_destroyed ++;
718
719 // Show current stats - these should match when the app is closed
720 fprintf( stderr, "Created %d, destroyed %d\n", properties_created, properties_destroyed );
721 #endif
722
723 // Clean up names and values
724 for ( index = list->count - 1; index >= 0; index -- )
725 {
726 free( list->name[ index ] );
727 mlt_property_close( list->value[ index ] );
728 }
729
730 // Clear up the list
731 free( list->name );
732 free( list->value );
733 free( list );
734
735 // Free this now if this has no child
736 if ( this->child == NULL )
737 free( this );
738 }
739 }
740 }
741