]> git.sesse.net Git - mlt/blob - src/framework/mlt_properties.c
Big modification - switch to macros for parent class access
[mlt] / 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 && ( value == NULL || value[ 0 ] != '@' ) )
350         {
351                 error = mlt_property_set_string( property, value );
352                 mlt_properties_do_mirror( this, name );
353         }
354         else if ( property != NULL && value[ 0 ] == '@' )
355         {
356                 int total = 0;
357                 int current = 0;
358                 char id[ 255 ];
359                 char op = '+';
360
361                 value ++;
362
363                 while ( *value != '\0' )
364                 {
365                         int length = strcspn( value, "+-*/" );
366
367                         // Get the identifier
368                         strncpy( id, value, length );
369                         id[ length ] = '\0';
370                         value += length;
371
372                         // Determine the value
373                         if ( isdigit( id[ 0 ] ) )
374                                 current = atof( id );
375                         else
376                                 current = mlt_properties_get_int( this, id );
377
378                         // Apply the operation
379                         switch( op )
380                         {
381                                 case '+':
382                                         total += current;
383                                         break;
384                                 case '-':
385                                         total -= current;
386                                         break;
387                                 case '*':
388                                         total *= current;
389                                         break;
390                                 case '/':
391                                         total /= current;
392                                         break;
393                         }
394
395                         // Get the next op
396                         op = *value != '\0' ? *value ++ : ' ';
397                 }
398
399                 error = mlt_property_set_int( property, total );
400                 mlt_properties_do_mirror( this, name );
401         }
402
403         mlt_events_fire( this, "property-changed", name, NULL );
404
405         return error;
406 }
407
408 /** Set or default the property.
409 */
410
411 int mlt_properties_set_or_default( mlt_properties this, char *name, char *value, char *def )
412 {
413         return mlt_properties_set( this, name, value == NULL ? def : value );
414 }
415
416 /** Get a string value by name.
417 */
418
419 char *mlt_properties_get( mlt_properties this, char *name )
420 {
421         mlt_property value = mlt_properties_find( this, name );
422         return value == NULL ? NULL : mlt_property_get_string( value );
423 }
424
425 /** Get a name by index.
426 */
427
428 char *mlt_properties_get_name( mlt_properties this, int index )
429 {
430         property_list *list = this->local;
431         if ( index >= 0 && index < list->count )
432                 return list->name[ index ];
433         return NULL;
434 }
435
436 /** Get a string value by index.
437 */
438
439 char *mlt_properties_get_value( mlt_properties this, int index )
440 {
441         property_list *list = this->local;
442         if ( index >= 0 && index < list->count )
443                 return mlt_property_get_string( list->value[ index ] );
444         return NULL;
445 }
446
447 /** Get a data value by index.
448 */
449
450 void *mlt_properties_get_data_at( mlt_properties this, int index, int *size )
451 {
452         property_list *list = this->local;
453         if ( index >= 0 && index < list->count )
454                 return mlt_property_get_data( list->value[ index ], size );
455         return NULL;
456 }
457
458 /** Return the number of items in the list.
459 */
460
461 int mlt_properties_count( mlt_properties this )
462 {
463         property_list *list = this->local;
464         return list->count;
465 }
466
467 /** Set a value by parsing a name=value string
468 */
469
470 int mlt_properties_parse( mlt_properties this, char *namevalue )
471 {
472         char *name = strdup( namevalue );
473         char *value = NULL;
474         int error = 0;
475         char *ptr = strchr( name, '=' );
476
477         if ( ptr )
478         {
479                 *( ptr ++ ) = '\0';
480
481                 if ( *ptr != '\"' )
482                 {
483                         value = strdup( ptr );
484                 }
485                 else
486                 {
487                         ptr ++;
488                         value = strdup( ptr );
489                         if ( value != NULL && value[ strlen( value ) - 1 ] == '\"' )
490                                 value[ strlen( value ) - 1 ] = '\0';
491                 }
492         }
493         else
494         {
495                 value = strdup( "" );
496         }
497
498         error = mlt_properties_set( this, name, value );
499
500         free( name );
501         free( value );
502
503         return error;
504 }
505
506 /** Get a value associated to the name.
507 */
508
509 int mlt_properties_get_int( mlt_properties this, char *name )
510 {
511         mlt_property value = mlt_properties_find( this, name );
512         return value == NULL ? 0 : mlt_property_get_int( value );
513 }
514
515 /** Set a value associated to the name.
516 */
517
518 int mlt_properties_set_int( mlt_properties this, char *name, int value )
519 {
520         int error = 1;
521
522         // Fetch the property to work with
523         mlt_property property = mlt_properties_fetch( this, name );
524
525         // Set it if not NULL
526         if ( property != NULL )
527         {
528                 error = mlt_property_set_int( property, value );
529                 mlt_properties_do_mirror( this, name );
530         }
531
532         mlt_events_fire( this, "property-changed", name, NULL );
533
534         return error;
535 }
536
537 /** Get a value associated to the name.
538 */
539
540 double mlt_properties_get_double( mlt_properties this, char *name )
541 {
542         mlt_property value = mlt_properties_find( this, name );
543         return value == NULL ? 0 : mlt_property_get_double( value );
544 }
545
546 /** Set a value associated to the name.
547 */
548
549 int mlt_properties_set_double( mlt_properties this, char *name, double value )
550 {
551         int error = 1;
552
553         // Fetch the property to work with
554         mlt_property property = mlt_properties_fetch( this, name );
555
556         // Set it if not NULL
557         if ( property != NULL )
558         {
559                 error = mlt_property_set_double( property, value );
560                 mlt_properties_do_mirror( this, name );
561         }
562
563         mlt_events_fire( this, "property-changed", name, NULL );
564
565         return error;
566 }
567
568 /** Get a value associated to the name.
569 */
570
571 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
572 {
573         mlt_property value = mlt_properties_find( this, name );
574         return value == NULL ? 0 : mlt_property_get_position( value );
575 }
576
577 /** Set a value associated to the name.
578 */
579
580 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
581 {
582         int error = 1;
583
584         // Fetch the property to work with
585         mlt_property property = mlt_properties_fetch( this, name );
586
587         // Set it if not NULL
588         if ( property != NULL )
589         {
590                 error = mlt_property_set_position( property, value );
591                 mlt_properties_do_mirror( this, name );
592         }
593
594         mlt_events_fire( this, "property-changed", name, NULL );
595
596         return error;
597 }
598
599 /** Get a value associated to the name.
600 */
601
602 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
603 {
604         mlt_property value = mlt_properties_find( this, name );
605         return value == NULL ? NULL : mlt_property_get_data( value, length );
606 }
607
608 /** Set a value associated to the name.
609 */
610
611 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
612 {
613         int error = 1;
614
615         // Fetch the property to work with
616         mlt_property property = mlt_properties_fetch( this, name );
617
618         // Set it if not NULL
619         if ( property != NULL )
620                 error = mlt_property_set_data( property, value, length, destroy, serialise );
621
622         mlt_events_fire( this, "property-changed", name, NULL );
623
624         return error;
625 }
626
627 /** Rename a property.
628 */
629
630 int mlt_properties_rename( mlt_properties this, char *source, char *dest )
631 {
632         mlt_property value = mlt_properties_find( this, dest );
633
634         if ( value == NULL )
635         {
636                 property_list *list = this->local;
637                 int i = 0;
638
639                 // Locate the item 
640                 for ( i = 0; i < list->count; i ++ )
641                 {
642                         if ( !strcmp( list->name[ i ], source ) )
643                         {
644                                 free( list->name[ i ] );
645                                 list->name[ i ] = strdup( dest );
646                                 list->hash[ generate_hash( dest ) ] = i + 1;
647                                 break;
648                         }
649                 }
650         }
651
652         return value != NULL;
653 }
654
655 /** Dump the properties.
656 */
657
658 void mlt_properties_dump( mlt_properties this, FILE *output )
659 {
660         property_list *list = this->local;
661         int i = 0;
662         for ( i = 0; i < list->count; i ++ )
663                 if ( mlt_properties_get( this, list->name[ i ] ) != NULL )
664                         fprintf( output, "%s=%s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
665 }
666
667 void mlt_properties_debug( mlt_properties this, char *title, FILE *output )
668 {
669         fprintf( output, "%s: ", title );
670         if ( this != NULL )
671         {
672                 property_list *list = this->local;
673                 int i = 0;
674                 fprintf( output, "[ ref=%d", list->ref_count );
675                 for ( i = 0; i < list->count; i ++ )
676                         if ( mlt_properties_get( this, list->name[ i ] ) != NULL )
677                                 fprintf( output, ", %s=%s", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
678                         else
679                                 fprintf( output, ", %s=%p", list->name[ i ], mlt_properties_get_data( this, list->name[ i ], NULL ) );
680                 fprintf( output, " ]" );
681         }
682         fprintf( output, "\n" );
683 }
684
685 /** Close the list.
686 */
687
688 void mlt_properties_close( mlt_properties this )
689 {
690         if ( this != NULL && mlt_properties_dec_ref( this ) <= 0 )
691         {
692                 if ( this->close != NULL )
693                 {
694                         this->close( this->close_object );
695                 }
696                 else
697                 {
698                         property_list *list = this->local;
699                         int index = 0;
700
701 #if _MLT_PROPERTY_CHECKS_ == 1
702                         // Show debug info
703                         mlt_properties_debug( this, "Closing", stderr );
704 #endif
705
706 #ifdef _MLT_PROPERTY_CHECKS_
707                         // Increment destroyed count
708                         properties_destroyed ++;
709
710                         // Show current stats - these should match when the app is closed
711                         fprintf( stderr, "Created %d, destroyed %d\n", properties_created, properties_destroyed );
712 #endif
713
714                         // Clean up names and values
715                         for ( index = list->count - 1; index >= 0; index -- )
716                         {
717                                 free( list->name[ index ] );
718                                 mlt_property_close( list->value[ index ] );
719                         }
720         
721                         // Clear up the list
722                         free( list->name );
723                         free( list->value );
724                         free( list );
725         
726                         // Free this now if this has no child
727                         if ( this->child == NULL )
728                                 free( this );
729                 }
730         }
731 }
732