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