01c4ba2d9a736a1ed04565d238abe14281ec0b27
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_
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( 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( 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, 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 /** Allow the specification of a mirror.
208 void mlt_properties_mirror( mlt_properties
this, mlt_properties that
)
210 property_list
*list
= this->local
;
214 /** Inherit all serialisable properties from that into this.
217 int mlt_properties_inherit( mlt_properties
this, mlt_properties that
)
219 int count
= mlt_properties_count( that
);
221 for ( i
= 0; i
< count
; i
++ )
223 char *value
= mlt_properties_get_value( that
, i
);
226 char *name
= mlt_properties_get_name( that
, i
);
227 mlt_properties_set( this, name
, value
);
233 /** Pass all properties from 'that' that match the prefix to 'this' (excluding the prefix).
236 int mlt_properties_pass( mlt_properties
this, mlt_properties that
, char *prefix
)
238 int count
= mlt_properties_count( that
);
239 int length
= strlen( prefix
);
241 for ( i
= 0; i
< count
; i
++ )
243 char *name
= mlt_properties_get_name( that
, i
);
244 if ( !strncmp( name
, prefix
, length
) )
246 char *value
= mlt_properties_get_value( that
, i
);
248 mlt_properties_set( this, name
+ length
, value
);
254 /** Locate a property by name
257 static inline mlt_property
mlt_properties_find( mlt_properties
this, char *name
)
259 property_list
*list
= this->local
;
260 mlt_property value
= NULL
;
261 int key
= generate_hash( name
);
262 int i
= list
->hash
[ key
] - 1;
266 // Check if we're hashed
267 if ( list
->count
> 0 &&
268 name
[ 0 ] == list
->name
[ i
][ 0 ] &&
269 !strcmp( list
->name
[ i
], name
) )
270 value
= list
->value
[ i
];
273 for ( i
= list
->count
- 1; value
== NULL
&& i
>= 0; i
-- )
274 if ( name
[ 0 ] == list
->name
[ i
][ 0 ] && !strcmp( list
->name
[ i
], name
) )
275 value
= list
->value
[ i
];
281 /** Add a new property.
284 static mlt_property
mlt_properties_add( mlt_properties
this, char *name
)
286 property_list
*list
= this->local
;
287 int key
= generate_hash( name
);
289 // Check that we have space and resize if necessary
290 if ( list
->count
== list
->size
)
293 list
->name
= realloc( list
->name
, list
->size
* sizeof( char * ) );
294 list
->value
= realloc( list
->value
, list
->size
* sizeof( mlt_property
) );
297 // Assign name/value pair
298 list
->name
[ list
->count
] = strdup( name
);
299 list
->value
[ list
->count
] = mlt_property_init( );
301 // Assign to hash table
302 if ( list
->hash
[ key
] == 0 )
303 list
->hash
[ key
] = list
->count
+ 1;
305 // Return and increment count accordingly
306 return list
->value
[ list
->count
++ ];
309 /** Fetch a property by name - this includes add if not found.
312 static mlt_property
mlt_properties_fetch( mlt_properties
this, char *name
)
314 // Try to find an existing property first
315 mlt_property property
= mlt_properties_find( this, name
);
317 // If it wasn't found, create one
318 if ( property
== NULL
)
319 property
= mlt_properties_add( this, name
);
321 // Return the property
325 /** Set the property.
328 int mlt_properties_set( mlt_properties
this, char *name
, char *value
)
332 // Fetch the property to work with
333 mlt_property property
= mlt_properties_fetch( this, name
);
335 // Set it if not NULL
336 if ( property
!= NULL
&& ( value
== NULL
|| value
[ 0 ] != '@' ) )
338 error
= mlt_property_set_string( property
, value
);
339 mlt_properties_do_mirror( this, name
);
341 else if ( property
!= NULL
&& value
[ 0 ] == '@' )
350 while ( *value
!= '\0' )
352 int length
= strcspn( value
, "+-*/" );
354 // Get the identifier
355 strncpy( id
, value
, length
);
359 // Determine the value
360 if ( isdigit( id
[ 0 ] ) )
361 current
= atof( id
);
363 current
= mlt_properties_get_int( this, id
);
365 // Apply the operation
383 op
= *value
!= '\0' ?
*value
++ : ' ';
386 error
= mlt_property_set_int( property
, total
);
387 mlt_properties_do_mirror( this, name
);
390 mlt_events_fire( this, "property-changed", name
, NULL
);
395 /** Set or default the property.
398 int mlt_properties_set_or_default( mlt_properties
this, char *name
, char *value
, char *def
)
400 return mlt_properties_set( this, name
, value
== NULL ? def
: value
);
403 /** Get a string value by name.
406 char *mlt_properties_get( mlt_properties
this, char *name
)
408 mlt_property value
= mlt_properties_find( this, name
);
409 return value
== NULL ? NULL
: mlt_property_get_string( value
);
412 /** Get a name by index.
415 char *mlt_properties_get_name( mlt_properties
this, int index
)
417 property_list
*list
= this->local
;
418 if ( index
>= 0 && index
< list
->count
)
419 return list
->name
[ index
];
423 /** Get a string value by index.
426 char *mlt_properties_get_value( mlt_properties
this, int index
)
428 property_list
*list
= this->local
;
429 if ( index
>= 0 && index
< list
->count
)
430 return mlt_property_get_string( list
->value
[ index
] );
434 /** Get a data value by index.
437 void *mlt_properties_get_data_at( mlt_properties
this, int index
, int *size
)
439 property_list
*list
= this->local
;
440 if ( index
>= 0 && index
< list
->count
)
441 return mlt_property_get_data( list
->value
[ index
], size
);
445 /** Return the number of items in the list.
448 int mlt_properties_count( mlt_properties
this )
450 property_list
*list
= this->local
;
454 /** Set a value by parsing a name=value string
457 int mlt_properties_parse( mlt_properties
this, char *namevalue
)
459 char *name
= strdup( namevalue
);
462 char *ptr
= strchr( name
, '=' );
470 value
= strdup( ptr
);
475 value
= strdup( ptr
);
476 if ( value
!= NULL
&& value
[ strlen( value
) - 1 ] == '\"' )
477 value
[ strlen( value
) - 1 ] = '\0';
482 value
= strdup( "" );
485 error
= mlt_properties_set( this, name
, value
);
493 /** Get a value associated to the name.
496 int mlt_properties_get_int( mlt_properties
this, char *name
)
498 mlt_property value
= mlt_properties_find( this, name
);
499 return value
== NULL ?
0 : mlt_property_get_int( value
);
502 /** Set a value associated to the name.
505 int mlt_properties_set_int( mlt_properties
this, char *name
, int value
)
509 // Fetch the property to work with
510 mlt_property property
= mlt_properties_fetch( this, name
);
512 // Set it if not NULL
513 if ( property
!= NULL
)
515 error
= mlt_property_set_int( property
, value
);
516 mlt_properties_do_mirror( this, name
);
519 mlt_events_fire( this, "property-changed", name
, NULL
);
524 /** Get a value associated to the name.
527 double mlt_properties_get_double( mlt_properties
this, char *name
)
529 mlt_property value
= mlt_properties_find( this, name
);
530 return value
== NULL ?
0 : mlt_property_get_double( value
);
533 /** Set a value associated to the name.
536 int mlt_properties_set_double( mlt_properties
this, char *name
, double value
)
540 // Fetch the property to work with
541 mlt_property property
= mlt_properties_fetch( this, name
);
543 // Set it if not NULL
544 if ( property
!= NULL
)
546 error
= mlt_property_set_double( property
, value
);
547 mlt_properties_do_mirror( this, name
);
550 mlt_events_fire( this, "property-changed", name
, NULL
);
555 /** Get a value associated to the name.
558 mlt_position
mlt_properties_get_position( mlt_properties
this, char *name
)
560 mlt_property value
= mlt_properties_find( this, name
);
561 return value
== NULL ?
0 : mlt_property_get_position( value
);
564 /** Set a value associated to the name.
567 int mlt_properties_set_position( mlt_properties
this, char *name
, mlt_position value
)
571 // Fetch the property to work with
572 mlt_property property
= mlt_properties_fetch( this, name
);
574 // Set it if not NULL
575 if ( property
!= NULL
)
577 error
= mlt_property_set_position( property
, value
);
578 mlt_properties_do_mirror( this, name
);
581 mlt_events_fire( this, "property-changed", name
, NULL
);
586 /** Get a value associated to the name.
589 void *mlt_properties_get_data( mlt_properties
this, char *name
, int *length
)
591 mlt_property value
= mlt_properties_find( this, name
);
592 return value
== NULL ? NULL
: mlt_property_get_data( value
, length
);
595 /** Set a value associated to the name.
598 int mlt_properties_set_data( mlt_properties
this, char *name
, void *value
, int length
, mlt_destructor destroy
, mlt_serialiser serialise
)
602 // Fetch the property to work with
603 mlt_property property
= mlt_properties_fetch( this, name
);
605 // Set it if not NULL
606 if ( property
!= NULL
)
607 error
= mlt_property_set_data( property
, value
, length
, destroy
, serialise
);
609 mlt_events_fire( this, "property-changed", name
, NULL
);
614 /** Rename a property.
617 int mlt_properties_rename( mlt_properties
this, char *source
, char *dest
)
619 mlt_property value
= mlt_properties_find( this, dest
);
623 property_list
*list
= this->local
;
627 for ( i
= 0; i
< list
->count
; i
++ )
629 if ( !strcmp( list
->name
[ i
], source
) )
631 free( list
->name
[ i
] );
632 list
->name
[ i
] = strdup( dest
);
633 list
->hash
[ generate_hash( dest
) ] = i
+ 1;
639 return value
!= NULL
;
642 /** Dump the properties.
645 void mlt_properties_dump( mlt_properties
this, FILE *output
)
647 property_list
*list
= this->local
;
649 for ( i
= 0; i
< list
->count
; i
++ )
650 if ( mlt_properties_get( this, list
->name
[ i
] ) != NULL
)
651 fprintf( output
, "%s=%s\n", list
->name
[ i
], mlt_properties_get( this, list
->name
[ i
] ) );
654 void mlt_properties_debug( mlt_properties
this, char *title
, FILE *output
)
656 fprintf( stderr
, "%s: ", title
);
659 property_list
*list
= this->local
;
661 fprintf( output
, "[ ref=%d", list
->ref_count
);
662 for ( i
= 0; i
< list
->count
; i
++ )
663 if ( mlt_properties_get( this, list
->name
[ i
] ) != NULL
)
664 fprintf( output
, ", %s=%s", list
->name
[ i
], mlt_properties_get( this, list
->name
[ i
] ) );
666 fprintf( output
, ", %s=%p", list
->name
[ i
], mlt_properties_get_data( this, list
->name
[ i
], NULL
) );
667 fprintf( output
, " ]" );
669 fprintf( stderr
, "\n" );
675 void mlt_properties_close( mlt_properties
this )
677 if ( this != NULL
&& mlt_properties_dec_ref( this ) <= 0 )
679 if ( this->close
!= NULL
)
681 this->close( this->close_object
);
685 property_list
*list
= this->local
;
688 #ifdef _MLT_PROPERTY_CHECKS_
690 mlt_properties_debug( this, "Closing", stderr
);
692 // Increment destroyed count
693 properties_destroyed
++;
695 // Show current stats - these should match when the app is closed
696 fprintf( stderr
, "Created %d, destroyed %d\n", properties_created
, properties_destroyed
);
699 // Clean up names and values
700 for ( index
= list
->count
- 1; index
>= 0; index
-- )
702 free( list
->name
[ index
] );
703 mlt_property_close( list
->value
[ index
] );
711 // Free this now if this has no child
712 if ( this->child
== NULL
)