2 * mlt_properties.c -- base properties class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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.
22 #include "mlt_properties.h"
23 #include "mlt_property.h"
30 /* ---------------- // Private Implementation // ---------------- */
32 /** Private implementation of the property list.
42 mlt_properties mirror
;
47 /** Memory leak checks.
50 //#define _MLT_PROPERTY_CHECKS_ 2
52 #ifdef _MLT_PROPERTY_CHECKS_
53 static int properties_created
= 0;
54 static int properties_destroyed
= 0;
57 /** Basic implementation.
60 int mlt_properties_init( mlt_properties
this, void *child
)
64 #ifdef _MLT_PROPERTY_CHECKS_
65 // Increment number of properties created
66 properties_created
++;
70 memset( this, 0, sizeof( struct mlt_properties_s
) );
72 // Assign the child of the object
75 // Allocate the local structure
76 this->local
= calloc( sizeof( property_list
), 1 );
78 // Increment the ref count
79 ( ( property_list
* )this->local
)->ref_count
= 1;
82 // Check that initialisation was successful
83 return this != NULL
&& this->local
== NULL
;
86 /** Constructor for stand alone object.
89 mlt_properties
mlt_properties_new( )
91 // Construct a standalone properties object
92 mlt_properties
this = calloc( sizeof( struct mlt_properties_s
), 1 );
95 mlt_properties_init( this, NULL
);
101 /** Load properties from a file.
104 mlt_properties
mlt_properties_load( const char *filename
)
106 // Construct a standalone properties object
107 mlt_properties
this = mlt_properties_new( );
112 FILE *file
= fopen( filename
, "r" );
114 // Load contents of file
119 char last
[ 1024 ] = "";
121 // Read each string from the file
122 while( fgets( temp
, 1024, file
) )
125 temp
[ strlen( temp
) - 1 ] = '\0';
127 // Check if the line starts with a .
128 if ( temp
[ 0 ] == '.' )
131 sprintf( temp2
, "%s%s", last
, temp
);
132 strcpy( temp
, temp2
);
134 else if ( strchr( temp
, '=' ) )
136 strcpy( last
, temp
);
137 *( strchr( last
, '=' ) ) = '\0';
140 // Parse and set the property
141 if ( strcmp( temp
, "" ) && temp
[ 0 ] != '#' )
142 mlt_properties_parse( this, temp
);
150 // Return the pointer
154 static inline int generate_hash( const char *name
)
159 hash
= ( hash
+ ( i
++ * ( *name
++ & 31 ) ) ) % 199;
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
168 static inline void mlt_properties_do_mirror( mlt_properties
this, const char *name
)
170 property_list
*list
= this->local
;
171 if ( list
->mirror
!= NULL
)
173 char *value
= mlt_properties_get( this, name
);
175 mlt_properties_set( list
->mirror
, name
, value
);
179 /** Maintain ref count to allow multiple uses of an mlt object.
182 int mlt_properties_inc_ref( mlt_properties
this )
186 property_list
*list
= this->local
;
187 return ++ list
->ref_count
;
192 /** Maintain ref count to allow multiple uses of an mlt object.
195 int mlt_properties_dec_ref( mlt_properties
this )
199 property_list
*list
= this->local
;
200 return -- list
->ref_count
;
205 /** Return the ref count of this object.
208 int mlt_properties_ref_count( mlt_properties
this )
212 property_list
*list
= this->local
;
213 return list
->ref_count
;
218 /** Mirror properties set on 'this' to 'that'.
221 void mlt_properties_mirror( mlt_properties
this, mlt_properties that
)
223 property_list
*list
= this->local
;
227 /** Inherit all serialisable properties from that into this.
230 int mlt_properties_inherit( mlt_properties
this, mlt_properties that
)
232 int count
= mlt_properties_count( that
);
234 for ( i
= 0; i
< count
; i
++ )
236 char *value
= mlt_properties_get_value( that
, i
);
239 char *name
= mlt_properties_get_name( that
, i
);
240 mlt_properties_set( this, name
, value
);
246 /** Pass all properties from 'that' that match the prefix to 'this' (excluding the prefix).
249 int mlt_properties_pass( mlt_properties
this, mlt_properties that
, const char *prefix
)
251 int count
= mlt_properties_count( that
);
252 int length
= strlen( prefix
);
254 for ( i
= 0; i
< count
; i
++ )
256 char *name
= mlt_properties_get_name( that
, i
);
257 if ( !strncmp( name
, prefix
, length
) )
259 char *value
= mlt_properties_get_value( that
, i
);
261 mlt_properties_set( this, name
+ length
, value
);
268 /** Locate a property by name
271 static inline mlt_property
mlt_properties_find( mlt_properties
this, const char *name
)
273 property_list
*list
= this->local
;
274 mlt_property value
= NULL
;
275 int key
= generate_hash( name
);
276 int i
= list
->hash
[ key
] - 1;
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
];
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
];
295 /** Add a new property.
298 static mlt_property
mlt_properties_add( mlt_properties
this, const char *name
)
300 property_list
*list
= this->local
;
301 int key
= generate_hash( name
);
303 // Check that we have space and resize if necessary
304 if ( list
->count
== list
->size
)
307 list
->name
= realloc( list
->name
, list
->size
* sizeof( const char * ) );
308 list
->value
= realloc( list
->value
, list
->size
* sizeof( mlt_property
) );
311 // Assign name/value pair
312 list
->name
[ list
->count
] = strdup( name
);
313 list
->value
[ list
->count
] = mlt_property_init( );
315 // Assign to hash table
316 if ( list
->hash
[ key
] == 0 )
317 list
->hash
[ key
] = list
->count
+ 1;
319 // Return and increment count accordingly
320 return list
->value
[ list
->count
++ ];
323 /** Fetch a property by name - this includes add if not found.
326 static mlt_property
mlt_properties_fetch( mlt_properties
this, const char *name
)
328 // Try to find an existing property first
329 mlt_property property
= mlt_properties_find( this, name
);
331 // If it wasn't found, create one
332 if ( property
== NULL
)
333 property
= mlt_properties_add( this, name
);
335 // Return the property
339 /** Pass property 'name' from 'that' to 'this'
340 * Who to blame: Zach <zachary.drew@gmail.com>
343 void mlt_properties_pass_property( mlt_properties
this, mlt_properties that
, const char *name
)
345 // Make sure the source property isn't null.
346 mlt_property that_prop
= mlt_properties_find( that
, name
);
347 if( that_prop
== NULL
)
350 mlt_property_pass( mlt_properties_fetch( this, name
), that_prop
);
353 /** Pass all properties from 'that' to 'this' as found in comma seperated 'list'.
354 * Who to blame: Zach <zachary.drew@gmail.com>
357 int mlt_properties_pass_list( mlt_properties
this, mlt_properties that
, const char *list
)
359 char *props
= strdup( list
);
361 char *delim
= " ,\t\n"; // Any combination of spaces, commas, tabs, and newlines
366 count
= strcspn( ptr
, delim
);
368 if( ptr
[count
] == '\0' )
371 ptr
[count
] = '\0'; // Make it a real string
373 mlt_properties_pass_property( this, that
, ptr
);
376 ptr
+= strspn( ptr
, delim
);
385 /** Set the property.
388 int mlt_properties_set( mlt_properties
this, const char *name
, const char *value
)
392 // Fetch the property to work with
393 mlt_property property
= mlt_properties_fetch( this, name
);
395 // Set it if not NULL
396 if ( property
== NULL
)
398 fprintf( stderr
, "Whoops - %s not found (should never occur)\n", name
);
400 else if ( value
== NULL
)
402 error
= mlt_property_set_string( property
, value
);
403 mlt_properties_do_mirror( this, name
);
405 else if ( *value
!= '@' )
407 error
= mlt_property_set_string( property
, value
);
408 mlt_properties_do_mirror( this, name
);
410 else if ( value
[ 0 ] == '@' )
419 while ( *value
!= '\0' )
421 int length
= strcspn( value
, "+-*/" );
423 // Get the identifier
424 strncpy( id
, value
, length
);
428 // Determine the value
429 if ( isdigit( id
[ 0 ] ) )
430 current
= atof( id
);
432 current
= mlt_properties_get_int( this, id
);
434 // Apply the operation
452 op
= *value
!= '\0' ?
*value
++ : ' ';
455 error
= mlt_property_set_int( property
, total
);
456 mlt_properties_do_mirror( this, name
);
459 mlt_events_fire( this, "property-changed", name
, NULL
);
464 /** Set or default the property.
467 int mlt_properties_set_or_default( mlt_properties
this, const char *name
, const char *value
, const char *def
)
469 return mlt_properties_set( this, name
, value
== NULL ? def
: value
);
472 /** Get a string value by name.
475 char *mlt_properties_get( mlt_properties
this, const char *name
)
477 mlt_property value
= mlt_properties_find( this, name
);
478 return value
== NULL ? NULL
: mlt_property_get_string( value
);
481 /** Get a name by index.
484 char *mlt_properties_get_name( mlt_properties
this, int index
)
486 property_list
*list
= this->local
;
487 if ( index
>= 0 && index
< list
->count
)
488 return list
->name
[ index
];
492 /** Get a string value by index.
495 char *mlt_properties_get_value( mlt_properties
this, int index
)
497 property_list
*list
= this->local
;
498 if ( index
>= 0 && index
< list
->count
)
499 return mlt_property_get_string( list
->value
[ index
] );
503 /** Get a data value by index.
506 void *mlt_properties_get_data_at( mlt_properties
this, int index
, int *size
)
508 property_list
*list
= this->local
;
509 if ( index
>= 0 && index
< list
->count
)
510 return mlt_property_get_data( list
->value
[ index
], size
);
514 /** Return the number of items in the list.
517 int mlt_properties_count( mlt_properties
this )
519 property_list
*list
= this->local
;
523 /** Set a value by parsing a name=value string
526 int mlt_properties_parse( mlt_properties
this, const char *namevalue
)
528 char *name
= strdup( namevalue
);
531 char *ptr
= strchr( name
, '=' );
539 value
= strdup( ptr
);
544 value
= strdup( ptr
);
545 if ( value
!= NULL
&& value
[ strlen( value
) - 1 ] == '\"' )
546 value
[ strlen( value
) - 1 ] = '\0';
551 value
= strdup( "" );
554 error
= mlt_properties_set( this, name
, value
);
562 /** Get a value associated to the name.
565 int mlt_properties_get_int( mlt_properties
this, const char *name
)
567 mlt_property value
= mlt_properties_find( this, name
);
568 return value
== NULL ?
0 : mlt_property_get_int( value
);
571 /** Set a value associated to the name.
574 int mlt_properties_set_int( mlt_properties
this, const char *name
, int value
)
578 // Fetch the property to work with
579 mlt_property property
= mlt_properties_fetch( this, name
);
581 // Set it if not NULL
582 if ( property
!= NULL
)
584 error
= mlt_property_set_int( property
, value
);
585 mlt_properties_do_mirror( this, name
);
588 mlt_events_fire( this, "property-changed", name
, NULL
);
593 /** Get a value associated to the name.
596 int64_t mlt_properties_get_int64( mlt_properties
this, const char *name
)
598 mlt_property value
= mlt_properties_find( this, name
);
599 return value
== NULL ?
0 : mlt_property_get_int64( value
);
602 /** Set a value associated to the name.
605 int mlt_properties_set_int64( mlt_properties
this, const char *name
, int64_t value
)
609 // Fetch the property to work with
610 mlt_property property
= mlt_properties_fetch( this, name
);
612 // Set it if not NULL
613 if ( property
!= NULL
)
615 error
= mlt_property_set_int64( property
, value
);
616 mlt_properties_do_mirror( this, name
);
619 mlt_events_fire( this, "property-changed", name
, NULL
);
624 /** Get a value associated to the name.
627 double mlt_properties_get_double( mlt_properties
this, const char *name
)
629 mlt_property value
= mlt_properties_find( this, name
);
630 return value
== NULL ?
0 : mlt_property_get_double( value
);
633 /** Set a value associated to the name.
636 int mlt_properties_set_double( mlt_properties
this, const char *name
, double value
)
640 // Fetch the property to work with
641 mlt_property property
= mlt_properties_fetch( this, name
);
643 // Set it if not NULL
644 if ( property
!= NULL
)
646 error
= mlt_property_set_double( property
, value
);
647 mlt_properties_do_mirror( this, name
);
650 mlt_events_fire( this, "property-changed", name
, NULL
);
655 /** Get a value associated to the name.
658 mlt_position
mlt_properties_get_position( mlt_properties
this, const char *name
)
660 mlt_property value
= mlt_properties_find( this, name
);
661 return value
== NULL ?
0 : mlt_property_get_position( value
);
664 /** Set a value associated to the name.
667 int mlt_properties_set_position( mlt_properties
this, const char *name
, mlt_position value
)
671 // Fetch the property to work with
672 mlt_property property
= mlt_properties_fetch( this, name
);
674 // Set it if not NULL
675 if ( property
!= NULL
)
677 error
= mlt_property_set_position( property
, value
);
678 mlt_properties_do_mirror( this, name
);
681 mlt_events_fire( this, "property-changed", name
, NULL
);
686 /** Get a value associated to the name.
689 void *mlt_properties_get_data( mlt_properties
this, const char *name
, int *length
)
691 mlt_property value
= mlt_properties_find( this, name
);
692 return value
== NULL ? NULL
: mlt_property_get_data( value
, length
);
695 /** Set a value associated to the name.
698 int mlt_properties_set_data( mlt_properties
this, const char *name
, void *value
, int length
, mlt_destructor destroy
, mlt_serialiser serialise
)
702 // Fetch the property to work with
703 mlt_property property
= mlt_properties_fetch( this, name
);
705 // Set it if not NULL
706 if ( property
!= NULL
)
707 error
= mlt_property_set_data( property
, value
, length
, destroy
, serialise
);
709 mlt_events_fire( this, "property-changed", name
, NULL
);
714 /** Rename a property.
717 int mlt_properties_rename( mlt_properties
this, const char *source
, const char *dest
)
719 mlt_property value
= mlt_properties_find( this, dest
);
723 property_list
*list
= this->local
;
727 for ( i
= 0; i
< list
->count
; i
++ )
729 if ( !strcmp( list
->name
[ i
], source
) )
731 free( list
->name
[ i
] );
732 list
->name
[ i
] = strdup( dest
);
733 list
->hash
[ generate_hash( dest
) ] = i
+ 1;
739 return value
!= NULL
;
742 /** Dump the properties.
745 void mlt_properties_dump( mlt_properties
this, FILE *output
)
747 property_list
*list
= this->local
;
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
] ) );
754 void mlt_properties_debug( mlt_properties
this, const char *title
, FILE *output
)
756 fprintf( output
, "%s: ", title
);
759 property_list
*list
= this->local
;
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
] ) );
766 fprintf( output
, ", %s=%p", list
->name
[ i
], mlt_properties_get_data( this, list
->name
[ i
], NULL
) );
767 fprintf( output
, " ]" );
769 fprintf( output
, "\n" );
775 void mlt_properties_close( mlt_properties
this )
777 if ( this != NULL
&& mlt_properties_dec_ref( this ) <= 0 )
779 if ( this->close
!= NULL
)
781 this->close( this->close_object
);
785 property_list
*list
= this->local
;
788 #if _MLT_PROPERTY_CHECKS_ == 1
790 mlt_properties_debug( this, "Closing", stderr
);
793 #ifdef _MLT_PROPERTY_CHECKS_
794 // Increment destroyed count
795 properties_destroyed
++;
797 // Show current stats - these should match when the app is closed
798 fprintf( stderr
, "Created %d, destroyed %d\n", properties_created
, properties_destroyed
);
801 // Clean up names and values
802 for ( index
= list
->count
- 1; index
>= 0; index
-- )
804 free( list
->name
[ index
] );
805 mlt_property_close( list
->value
[ index
] );
813 // Free this now if this has no child
814 if ( this->child
== NULL
)