src/framework/mlt_properties.c
[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( const 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( const 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, const 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 /** Mirror properties set on 'this' to 'that'.
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, const 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
268 /** Locate a property by name
269 */
270
271 static inline mlt_property mlt_properties_find( mlt_properties this, const char *name )
272 {
273 property_list *list = this->local;
274 mlt_property value = NULL;
275 int key = generate_hash( name );
276 int i = list->hash[ key ] - 1;
277
278 if ( i >= 0 )
279 {
280 // Check if we're hashed
281 if ( list->count > 0 &&
282 name[ 0 ] == list->name[ i ][ 0 ] &&
283 !strcmp( list->name[ i ], name ) )
284 value = list->value[ i ];
285
286 // Locate the item
287 for ( i = list->count - 1; value == NULL && i >= 0; i -- )
288 if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
289 value = list->value[ i ];
290 }
291
292 return value;
293 }
294
295 /** Add a new property.
296 */
297
298 static mlt_property mlt_properties_add( mlt_properties this, const char *name )
299 {
300 property_list *list = this->local;
301 int key = generate_hash( name );
302
303 // Check that we have space and resize if necessary
304 if ( list->count == list->size )
305 {
306 list->size += 50;
307 list->name = realloc( list->name, list->size * sizeof( const char * ) );
308 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
309 }
310
311 // Assign name/value pair
312 list->name[ list->count ] = strdup( name );
313 list->value[ list->count ] = mlt_property_init( );
314
315 // Assign to hash table
316 if ( list->hash[ key ] == 0 )
317 list->hash[ key ] = list->count + 1;
318
319 // Return and increment count accordingly
320 return list->value[ list->count ++ ];
321 }
322
323 /** Fetch a property by name - this includes add if not found.
324 */
325
326 static mlt_property mlt_properties_fetch( mlt_properties this, const char *name )
327 {
328 // Try to find an existing property first
329 mlt_property property = mlt_properties_find( this, name );
330
331 // If it wasn't found, create one
332 if ( property == NULL )
333 property = mlt_properties_add( this, name );
334
335 // Return the property
336 return property;
337 }
338
339 /** Pass property 'name' from 'that' to 'this'
340 * Who to blame: Zach <zachary.drew@gmail.com>
341 */
342
343 void mlt_properties_pass_property( mlt_properties this, mlt_properties that, const char *name )
344 {
345 // Make sure the source property isn't null.
346 mlt_property that_prop = mlt_properties_find( that, name );
347 if( that_prop == NULL )
348 return;
349
350 mlt_property_pass( mlt_properties_fetch( this, name ), that_prop );
351 }
352
353 /** Pass all properties from 'that' to 'this' as found in comma seperated 'list'.
354 * Who to blame: Zach <zachary.drew@gmail.com>
355 */
356
357 int mlt_properties_pass_list( mlt_properties this, mlt_properties that, const char *list )
358 {
359 char *props = strdup( list );
360 char *ptr = props;
361 char *delim = " ,\t\n"; // Any combination of spaces, commas, tabs, and newlines
362 int count, done = 0;
363
364 while( !done )
365 {
366 count = strcspn( ptr, delim );
367
368 if( ptr[count] == '\0' )
369 done = 1;
370 else
371 ptr[count] = '\0'; // Make it a real string
372
373 mlt_properties_pass_property( this, that, ptr );
374
375 ptr += count + 1;
376 ptr += strspn( ptr, delim );
377 }
378
379 free( props );
380
381 return 0;
382 }
383
384
385 /** Set the property.
386 */
387
388 int mlt_properties_set( mlt_properties this, const char *name, const char *value )
389 {
390 int error = 1;
391
392 // Fetch the property to work with
393 mlt_property property = mlt_properties_fetch( this, name );
394
395 // Set it if not NULL
396 if ( property == NULL )
397 {
398 fprintf( stderr, "Whoops - %s not found (should never occur)\n", name );
399 }
400 else if ( value == NULL )
401 {
402 error = mlt_property_set_string( property, value );
403 mlt_properties_do_mirror( this, name );
404 }
405 else if ( *value != '@' )
406 {
407 error = mlt_property_set_string( property, value );
408 mlt_properties_do_mirror( this, name );
409 }
410 else if ( value[ 0 ] == '@' )
411 {
412 int total = 0;
413 int current = 0;
414 char id[ 255 ];
415 char op = '+';
416
417 value ++;
418
419 while ( *value != '\0' )
420 {
421 int length = strcspn( value, "+-*/" );
422
423 // Get the identifier
424 strncpy( id, value, length );
425 id[ length ] = '\0';
426 value += length;
427
428 // Determine the value
429 if ( isdigit( id[ 0 ] ) )
430 current = atof( id );
431 else
432 current = mlt_properties_get_int( this, id );
433
434 // Apply the operation
435 switch( op )
436 {
437 case '+':
438 total += current;
439 break;
440 case '-':
441 total -= current;
442 break;
443 case '*':
444 total *= current;
445 break;
446 case '/':
447 total /= current;
448 break;
449 }
450
451 // Get the next op
452 op = *value != '\0' ? *value ++ : ' ';
453 }
454
455 error = mlt_property_set_int( property, total );
456 mlt_properties_do_mirror( this, name );
457 }
458
459 mlt_events_fire( this, "property-changed", name, NULL );
460
461 return error;
462 }
463
464 /** Set or default the property.
465 */
466
467 int mlt_properties_set_or_default( mlt_properties this, const char *name, const char *value, const char *def )
468 {
469 return mlt_properties_set( this, name, value == NULL ? def : value );
470 }
471
472 /** Get a string value by name.
473 */
474
475 char *mlt_properties_get( mlt_properties this, const char *name )
476 {
477 mlt_property value = mlt_properties_find( this, name );
478 return value == NULL ? NULL : mlt_property_get_string( value );
479 }
480
481 /** Get a name by index.
482 */
483
484 char *mlt_properties_get_name( mlt_properties this, int index )
485 {
486 property_list *list = this->local;
487 if ( index >= 0 && index < list->count )
488 return list->name[ index ];
489 return NULL;
490 }
491
492 /** Get a string value by index.
493 */
494
495 char *mlt_properties_get_value( mlt_properties this, int index )
496 {
497 property_list *list = this->local;
498 if ( index >= 0 && index < list->count )
499 return mlt_property_get_string( list->value[ index ] );
500 return NULL;
501 }
502
503 /** Get a data value by index.
504 */
505
506 void *mlt_properties_get_data_at( mlt_properties this, int index, int *size )
507 {
508 property_list *list = this->local;
509 if ( index >= 0 && index < list->count )
510 return mlt_property_get_data( list->value[ index ], size );
511 return NULL;
512 }
513
514 /** Return the number of items in the list.
515 */
516
517 int mlt_properties_count( mlt_properties this )
518 {
519 property_list *list = this->local;
520 return list->count;
521 }
522
523 /** Set a value by parsing a name=value string
524 */
525
526 int mlt_properties_parse( mlt_properties this, const char *namevalue )
527 {
528 char *name = strdup( namevalue );
529 char *value = NULL;
530 int error = 0;
531 char *ptr = strchr( name, '=' );
532
533 if ( ptr )
534 {
535 *( ptr ++ ) = '\0';
536
537 if ( *ptr != '\"' )
538 {
539 value = strdup( ptr );
540 }
541 else
542 {
543 ptr ++;
544 value = strdup( ptr );
545 if ( value != NULL && value[ strlen( value ) - 1 ] == '\"' )
546 value[ strlen( value ) - 1 ] = '\0';
547 }
548 }
549 else
550 {
551 value = strdup( "" );
552 }
553
554 error = mlt_properties_set( this, name, value );
555
556 free( name );
557 free( value );
558
559 return error;
560 }
561
562 /** Get a value associated to the name.
563 */
564
565 int mlt_properties_get_int( mlt_properties this, const char *name )
566 {
567 mlt_property value = mlt_properties_find( this, name );
568 return value == NULL ? 0 : mlt_property_get_int( value );
569 }
570
571 /** Set a value associated to the name.
572 */
573
574 int mlt_properties_set_int( mlt_properties this, const char *name, int value )
575 {
576 int error = 1;
577
578 // Fetch the property to work with
579 mlt_property property = mlt_properties_fetch( this, name );
580
581 // Set it if not NULL
582 if ( property != NULL )
583 {
584 error = mlt_property_set_int( property, value );
585 mlt_properties_do_mirror( this, name );
586 }
587
588 mlt_events_fire( this, "property-changed", name, NULL );
589
590 return error;
591 }
592
593 /** Get a value associated to the name.
594 */
595
596 int64_t mlt_properties_get_int64( mlt_properties this, const char *name )
597 {
598 mlt_property value = mlt_properties_find( this, name );
599 return value == NULL ? 0 : mlt_property_get_int64( value );
600 }
601
602 /** Set a value associated to the name.
603 */
604
605 int mlt_properties_set_int64( mlt_properties this, const char *name, int64_t value )
606 {
607 int error = 1;
608
609 // Fetch the property to work with
610 mlt_property property = mlt_properties_fetch( this, name );
611
612 // Set it if not NULL
613 if ( property != NULL )
614 {
615 error = mlt_property_set_int64( property, value );
616 mlt_properties_do_mirror( this, name );
617 }
618
619 mlt_events_fire( this, "property-changed", name, NULL );
620
621 return error;
622 }
623
624 /** Get a value associated to the name.
625 */
626
627 double mlt_properties_get_double( mlt_properties this, const char *name )
628 {
629 mlt_property value = mlt_properties_find( this, name );
630 return value == NULL ? 0 : mlt_property_get_double( value );
631 }
632
633 /** Set a value associated to the name.
634 */
635
636 int mlt_properties_set_double( mlt_properties this, const char *name, double value )
637 {
638 int error = 1;
639
640 // Fetch the property to work with
641 mlt_property property = mlt_properties_fetch( this, name );
642
643 // Set it if not NULL
644 if ( property != NULL )
645 {
646 error = mlt_property_set_double( property, value );
647 mlt_properties_do_mirror( this, name );
648 }
649
650 mlt_events_fire( this, "property-changed", name, NULL );
651
652 return error;
653 }
654
655 /** Get a value associated to the name.
656 */
657
658 mlt_position mlt_properties_get_position( mlt_properties this, const char *name )
659 {
660 mlt_property value = mlt_properties_find( this, name );
661 return value == NULL ? 0 : mlt_property_get_position( value );
662 }
663
664 /** Set a value associated to the name.
665 */
666
667 int mlt_properties_set_position( mlt_properties this, const char *name, mlt_position value )
668 {
669 int error = 1;
670
671 // Fetch the property to work with
672 mlt_property property = mlt_properties_fetch( this, name );
673
674 // Set it if not NULL
675 if ( property != NULL )
676 {
677 error = mlt_property_set_position( property, value );
678 mlt_properties_do_mirror( this, name );
679 }
680
681 mlt_events_fire( this, "property-changed", name, NULL );
682
683 return error;
684 }
685
686 /** Get a value associated to the name.
687 */
688
689 void *mlt_properties_get_data( mlt_properties this, const char *name, int *length )
690 {
691 mlt_property value = mlt_properties_find( this, name );
692 return value == NULL ? NULL : mlt_property_get_data( value, length );
693 }
694
695 /** Set a value associated to the name.
696 */
697
698 int mlt_properties_set_data( mlt_properties this, const char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
699 {
700 int error = 1;
701
702 // Fetch the property to work with
703 mlt_property property = mlt_properties_fetch( this, name );
704
705 // Set it if not NULL
706 if ( property != NULL )
707 error = mlt_property_set_data( property, value, length, destroy, serialise );
708
709 mlt_events_fire( this, "property-changed", name, NULL );
710
711 return error;
712 }
713
714 /** Rename a property.
715 */
716
717 int mlt_properties_rename( mlt_properties this, const char *source, const char *dest )
718 {
719 mlt_property value = mlt_properties_find( this, dest );
720
721 if ( value == NULL )
722 {
723 property_list *list = this->local;
724 int i = 0;
725
726 // Locate the item
727 for ( i = 0; i < list->count; i ++ )
728 {
729 if ( !strcmp( list->name[ i ], source ) )
730 {
731 free( list->name[ i ] );
732 list->name[ i ] = strdup( dest );
733 list->hash[ generate_hash( dest ) ] = i + 1;
734 break;
735 }
736 }
737 }
738
739 return value != NULL;
740 }
741
742 /** Dump the properties.
743 */
744
745 void mlt_properties_dump( mlt_properties this, FILE *output )
746 {
747 property_list *list = this->local;
748 int i = 0;
749 for ( i = 0; i < list->count; i ++ )
750 if ( mlt_properties_get( this, list->name[ i ] ) != NULL )
751 fprintf( output, "%s=%s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
752 }
753
754 void mlt_properties_debug( mlt_properties this, const char *title, FILE *output )
755 {
756 fprintf( output, "%s: ", title );
757 if ( this != NULL )
758 {
759 property_list *list = this->local;
760 int i = 0;
761 fprintf( output, "[ ref=%d", list->ref_count );
762 for ( i = 0; i < list->count; i ++ )
763 if ( mlt_properties_get( this, list->name[ i ] ) != NULL )
764 fprintf( output, ", %s=%s", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
765 else
766 fprintf( output, ", %s=%p", list->name[ i ], mlt_properties_get_data( this, list->name[ i ], NULL ) );
767 fprintf( output, " ]" );
768 }
769 fprintf( output, "\n" );
770 }
771
772 /** Close the list.
773 */
774
775 void mlt_properties_close( mlt_properties this )
776 {
777 if ( this != NULL && mlt_properties_dec_ref( this ) <= 0 )
778 {
779 if ( this->close != NULL )
780 {
781 this->close( this->close_object );
782 }
783 else
784 {
785 property_list *list = this->local;
786 int index = 0;
787
788 #if _MLT_PROPERTY_CHECKS_ == 1
789 // Show debug info
790 mlt_properties_debug( this, "Closing", stderr );
791 #endif
792
793 #ifdef _MLT_PROPERTY_CHECKS_
794 // Increment destroyed count
795 properties_destroyed ++;
796
797 // Show current stats - these should match when the app is closed
798 fprintf( stderr, "Created %d, destroyed %d\n", properties_created, properties_destroyed );
799 #endif
800
801 // Clean up names and values
802 for ( index = list->count - 1; index >= 0; index -- )
803 {
804 free( list->name[ index ] );
805 mlt_property_close( list->value[ index ] );
806 }
807
808 // Clear up the list
809 free( list->name );
810 free( list->value );
811 free( list );
812
813 // Free this now if this has no child
814 if ( this->child == NULL )
815 free( this );
816 }
817 }
818 }
819