2 * \file mlt_properties.c
3 * \brief Properties class definition
4 * \see mlt_properties_s
6 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
8 * \author Dan Dennedy <dan@dennedy.org>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "mlt_properties.h"
26 #include "mlt_property.h"
27 #include "mlt_deque.h"
29 #include "mlt_factory.h"
37 #include <sys/types.h>
43 #define PRESETS_DIR "/presets"
45 /** \brief private implementation of the property list */
54 mlt_properties mirror;
56 pthread_mutex_t mutex;
61 /* Memory leak checks */
63 //#define _MLT_PROPERTY_CHECKS_ 2
64 #ifdef _MLT_PROPERTY_CHECKS_
65 static int properties_created = 0;
66 static int properties_destroyed = 0;
69 /** Initialize a properties object that was already allocated.
71 * This does allocate its ::property_list, and it adds a reference count.
72 * \public \memberof mlt_properties_s
73 * \param self the properties structure to initialize
74 * \param child an opaque pointer to a subclass object
75 * \return true if failed
78 int mlt_properties_init( mlt_properties self, void *child )
82 #ifdef _MLT_PROPERTY_CHECKS_
83 // Increment number of properties created
84 properties_created ++;
88 memset( self, 0, sizeof( struct mlt_properties_s ) );
90 // Assign the child of the object
93 // Allocate the local structure
94 self->local = calloc( sizeof( property_list ), 1 );
96 // Increment the ref count
97 ( ( property_list * )self->local )->ref_count = 1;
98 pthread_mutex_init( &( ( property_list * )self->local )->mutex, NULL );;
101 // Check that initialisation was successful
102 return self != NULL && self->local == NULL;
105 /** Create a properties object.
107 * This allocates the properties structure and calls mlt_properties_init() on it.
108 * Free the properties object with mlt_properties_close().
109 * \public \memberof mlt_properties_s
110 * \return a new properties object
113 mlt_properties mlt_properties_new( )
115 // Construct a standalone properties object
116 mlt_properties self = calloc( sizeof( struct mlt_properties_s ), 1 );
119 mlt_properties_init( self, NULL );
121 // Return the pointer
125 /** Set the numeric locale used for string/double conversions.
127 * \public \memberof mlt_properties_s
128 * \param self a properties list
129 * \param locale the locale name
130 * \return true if error
133 int mlt_properties_set_lcnumeric( mlt_properties self, const char *locale )
137 if ( self && locale )
139 property_list *list = self->local;
141 #if defined(__linux__) || defined(__DARWIN__)
143 freelocale( list->locale );
144 list->locale = newlocale( LC_NUMERIC_MASK, locale, NULL );
146 error = list->locale == NULL;
154 /** Get the numeric locale for this properties object.
156 * Do not free the result.
157 * \public \memberof mlt_properties_s
158 * \param self a properties list
159 * \return the locale name if this properties has a specific locale it is using, NULL otherwise
162 const char* mlt_properties_get_lcnumeric( mlt_properties self )
164 property_list *list = self->local;
165 const char *result = NULL;
169 #if defined(__DARWIN__)
170 result = querylocale( LC_NUMERIC, list->locale );
171 #elif defined(__linux__)
172 result = list->locale->__names[ LC_NUMERIC ];
174 // TODO: not yet sure what to do on other platforms
180 static int load_properties( mlt_properties self, const char *filename )
183 FILE *file = fopen( filename, "r" );
185 // Load contents of file
190 char last[ 1024 ] = "";
192 // Read each string from the file
193 while( fgets( temp, 1024, file ) )
196 temp[ strlen( temp ) - 1 ] = '\0';
198 // Check if the line starts with a .
199 if ( temp[ 0 ] == '.' )
202 sprintf( temp2, "%s%s", last, temp );
203 strcpy( temp, temp2 );
205 else if ( strchr( temp, '=' ) )
207 strcpy( last, temp );
208 *( strchr( last, '=' ) ) = '\0';
211 // Parse and set the property
212 if ( strcmp( temp, "" ) && temp[ 0 ] != '#' )
213 mlt_properties_parse( self, temp );
219 return file? 0 : errno;
222 /** Create a properties object by reading a .properties text file.
224 * Free the properties object with mlt_properties_close().
225 * \deprecated Please start using mlt_properties_parse_yaml().
226 * \public \memberof mlt_properties_s
227 * \param filename the absolute file name
228 * \return a new properties object
231 mlt_properties mlt_properties_load( const char *filename )
233 // Construct a standalone properties object
234 mlt_properties self = mlt_properties_new( );
237 load_properties( self, filename );
239 // Return the pointer
243 /** Set properties from a preset.
245 * Presets are typically installed to $prefix/share/mlt/presets/{type}/{service}/[{profile}/]{name}.
246 * For example, "/usr/share/mlt/presets/consumer/avformat/dv_ntsc_wide/DVD"
247 * could be an encoding preset for a widescreen NTSC DVD Video.
248 * Do not specify the type and service in the preset name parameter; these are
249 * inferred automatically from the service to which you are applying the preset.
250 * Using the example above and assuming you are calling this function on the
251 * avformat consumer, the name passed to the function should simply be DVD.
252 * Note that the profile portion of the path is optional, but a profile-specific
253 * preset with the same name as a more generic one is given a higher priority.
254 * \todo Look in a user-specific location - somewhere in the home directory.
256 * \public \memberof mlt_properties_s
257 * \param self a properties list
258 * \param name the name of a preset in a well-known location or the explicit path
259 * \return true if error
262 int mlt_properties_preset( mlt_properties self, const char *name )
264 struct stat stat_buff;
267 if ( !( self && name && strlen( name ) ) )
270 // See if name is an explicit file
271 if ( ! stat( name, &stat_buff ) )
273 return load_properties( self, name );
277 // Look for profile-specific preset before a generic one.
278 char *data = getenv( "MLT_PRESETS_PATH" );
279 const char *type = mlt_properties_get( self, "mlt_type" );
280 const char *service = mlt_properties_get( self, "mlt_service" );
281 const char *profile = mlt_environment( "MLT_PROFILE" );
286 data = strdup( data );
290 data = malloc( strlen( mlt_environment( "MLT_DATA" ) ) + strlen( PRESETS_DIR ) + 1 );
291 strcpy( data, mlt_environment( "MLT_DATA" ) );
292 strcat( data, PRESETS_DIR );
294 if ( data && type && service )
296 char *path = malloc( 5 + strlen(name) + strlen(data) + strlen(type) + strlen(service) + ( profile? strlen(profile) : 0 ) );
297 sprintf( path, "%s/%s/%s/%s/%s", data, type, service, profile, name );
298 if ( load_properties( self, path ) )
300 sprintf( path, "%s/%s/%s/%s", data, type, service, name );
301 error = load_properties( self, path );
314 /** Generate a hash key.
316 * \private \memberof mlt_properties_s
317 * \param name a string
321 static inline int generate_hash( const char *name )
326 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
330 /** Copy a serializable property to a properties list that is mirroring this one.
332 * Special case - when a container (such as loader) is protecting another
333 * producer, we need to ensure that properties are passed through to the
335 * \private \memberof mlt_properties_s
336 * \param self a properties list
337 * \param name the name of the property to copy
340 static inline void mlt_properties_do_mirror( mlt_properties self, const char *name )
342 property_list *list = self->local;
343 if ( list->mirror != NULL )
345 char *value = mlt_properties_get( self, name );
347 mlt_properties_set( list->mirror, name, value );
351 /** Increment the reference count.
353 * \public \memberof mlt_properties_s
354 * \param self a properties list
355 * \return the new reference count
358 int mlt_properties_inc_ref( mlt_properties self )
363 property_list *list = self->local;
364 pthread_mutex_lock( &list->mutex );
365 result = ++ list->ref_count;
366 pthread_mutex_unlock( &list->mutex );
371 /** Decrement the reference count.
373 * \public \memberof mlt_properties_s
374 * \param self a properties list
375 * \return the new reference count
378 int mlt_properties_dec_ref( mlt_properties self )
383 property_list *list = self->local;
384 pthread_mutex_lock( &list->mutex );
385 result = -- list->ref_count;
386 pthread_mutex_unlock( &list->mutex );
391 /** Get the reference count.
393 * \public \memberof mlt_properties_s
394 * \param self a properties list
395 * \return the current reference count
398 int mlt_properties_ref_count( mlt_properties self )
402 property_list *list = self->local;
403 return list->ref_count;
408 /** Set a properties list to be a mirror copy of another.
410 * Note that this does not copy all existing properties. Rather, you must
411 * call this before setting the properties that you wish to copy.
412 * \public \memberof mlt_properties_s
413 * \param that the properties which will receive copies of the properties as they are set.
414 * \param self the properties to mirror
417 void mlt_properties_mirror( mlt_properties self, mlt_properties that )
419 property_list *list = self->local;
423 /** Copy all serializable properties to another properties list.
425 * \public \memberof mlt_properties_s
426 * \param self The properties to copy to
427 * \param that The properties to copy from
431 int mlt_properties_inherit( mlt_properties self, mlt_properties that )
433 int count = mlt_properties_count( that );
435 for ( i = 0; i < count; i ++ )
437 char *value = mlt_properties_get_value( that, i );
440 char *name = mlt_properties_get_name( that, i );
441 mlt_properties_set( self, name, value );
447 /** Pass all serializable properties that match a prefix to another properties object
449 * \public \memberof mlt_properties_s
450 * \param self the properties to copy to
451 * \param that The properties to copy from
452 * \param prefix the property names to match (required)
456 int mlt_properties_pass( mlt_properties self, mlt_properties that, const char *prefix )
458 int count = mlt_properties_count( that );
459 int length = strlen( prefix );
461 for ( i = 0; i < count; i ++ )
463 char *name = mlt_properties_get_name( that, i );
464 if ( !strncmp( name, prefix, length ) )
466 char *value = mlt_properties_get_value( that, i );
468 mlt_properties_set( self, name + length, value );
474 /** Locate a property by name.
476 * \private \memberof mlt_properties_s
477 * \param self a properties list
478 * \param name the property to lookup by name
479 * \return the property or NULL for failure
482 static inline mlt_property mlt_properties_find( mlt_properties self, const char *name )
484 property_list *list = self->local;
485 mlt_property value = NULL;
486 int key = generate_hash( name );
488 mlt_properties_lock( self );
490 int i = list->hash[ key ] - 1;
493 // Check if we're hashed
494 if ( list->count > 0 &&
495 name[ 0 ] == list->name[ i ][ 0 ] &&
496 !strcmp( list->name[ i ], name ) )
497 value = list->value[ i ];
500 for ( i = list->count - 1; value == NULL && i >= 0; i -- )
501 if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
502 value = list->value[ i ];
504 mlt_properties_unlock( self );
509 /** Add a new property.
511 * \private \memberof mlt_properties_s
512 * \param self a properties list
513 * \param name the name of the new property
514 * \return the new property
517 static mlt_property mlt_properties_add( mlt_properties self, const char *name )
519 property_list *list = self->local;
520 int key = generate_hash( name );
523 mlt_properties_lock( self );
525 // Check that we have space and resize if necessary
526 if ( list->count == list->size )
529 list->name = realloc( list->name, list->size * sizeof( const char * ) );
530 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
533 // Assign name/value pair
534 list->name[ list->count ] = strdup( name );
535 list->value[ list->count ] = mlt_property_init( );
537 // Assign to hash table
538 if ( list->hash[ key ] == 0 )
539 list->hash[ key ] = list->count + 1;
541 // Return and increment count accordingly
542 result = list->value[ list->count ++ ];
544 mlt_properties_unlock( self );
549 /** Fetch a property by name and add one if not found.
551 * \private \memberof mlt_properties_s
552 * \param self a properties list
553 * \param name the property to lookup or add
554 * \return the property
557 static mlt_property mlt_properties_fetch( mlt_properties self, const char *name )
559 // Try to find an existing property first
560 mlt_property property = mlt_properties_find( self, name );
562 // If it wasn't found, create one
563 if ( property == NULL )
564 property = mlt_properties_add( self, name );
566 // Return the property
570 /** Copy a property to another properties list.
572 * \public \memberof mlt_properties_s
573 * \author Zach <zachary.drew@gmail.com>
574 * \param self the properties to copy to
575 * \param that the properties to copy from
576 * \param name the name of the property to copy
579 void mlt_properties_pass_property( mlt_properties self, mlt_properties that, const char *name )
581 // Make sure the source property isn't null.
582 mlt_property that_prop = mlt_properties_find( that, name );
583 if( that_prop == NULL )
586 mlt_property_pass( mlt_properties_fetch( self, name ), that_prop );
589 /** Copy all properties specified in a comma-separated list to another properties list.
591 * White space is also a delimiter.
592 * \public \memberof mlt_properties_s
593 * \author Zach <zachary.drew@gmail.com>
594 * \param self the properties to copy to
595 * \param that the properties to copy from
596 * \param list a delimited list of property names
601 int mlt_properties_pass_list( mlt_properties self, mlt_properties that, const char *list )
603 char *props = strdup( list );
605 const char *delim = " ,\t\n"; // Any combination of spaces, commas, tabs, and newlines
610 count = strcspn( ptr, delim );
612 if( ptr[count] == '\0' )
615 ptr[count] = '\0'; // Make it a real string
617 mlt_properties_pass_property( self, that, ptr );
620 ptr += strspn( ptr, delim );
629 /** Set a property to a string.
631 * The property name "properties" is reserved to load the preset in \p value.
632 * When the value begins with '@' then it is interpreted as a very simple math
633 * expression containing only the +, -, *, and / operators.
634 * The event "property-changed" is fired after the property has been set.
636 * This makes a copy of the string value you supply.
637 * \public \memberof mlt_properties_s
638 * \param self a properties list
639 * \param name the property to set
640 * \param value the property's new value
641 * \return true if error
644 int mlt_properties_set( mlt_properties self, const char *name, const char *value )
648 // Fetch the property to work with
649 mlt_property property = mlt_properties_fetch( self, name );
651 // Set it if not NULL
652 if ( property == NULL )
654 mlt_log( NULL, MLT_LOG_FATAL, "Whoops - %s not found (should never occur)\n", name );
656 else if ( value == NULL )
658 error = mlt_property_set_string( property, value );
659 mlt_properties_do_mirror( self, name );
661 else if ( *value != '@' )
663 error = mlt_property_set_string( property, value );
664 mlt_properties_do_mirror( self, name );
665 if ( !strcmp( name, "properties" ) )
666 mlt_properties_preset( self, value );
668 else if ( value[ 0 ] == '@' )
677 while ( *value != '\0' )
679 int length = strcspn( value, "+-*/" );
681 // Get the identifier
682 strncpy( id, value, length );
686 // Determine the value
687 if ( isdigit( id[ 0 ] ) )
688 current = atof( id );
690 current = mlt_properties_get_double( self, id );
692 // Apply the operation
705 total = total / current;
710 op = *value != '\0' ? *value ++ : ' ';
713 error = mlt_property_set_double( property, total );
714 mlt_properties_do_mirror( self, name );
717 mlt_events_fire( self, "property-changed", name, NULL );
722 /** Set or default a property to a string.
724 * This makes a copy of the string value you supply.
725 * \public \memberof mlt_properties_s
726 * \param self a properties list
727 * \param name the property to set
728 * \param value the string value to set or NULL to use the default
729 * \param def the default string if value is NULL
730 * \return true if error
733 int mlt_properties_set_or_default( mlt_properties self, const char *name, const char *value, const char *def )
735 return mlt_properties_set( self, name, value == NULL ? def : value );
738 /** Get a string value by name.
740 * Do not free the returned string. It's lifetime is controlled by the property
741 * and this properties object.
742 * \public \memberof mlt_properties_s
743 * \param self a properties list
744 * \param name the property to get
745 * \return the property's string value or NULL if it does not exist
748 char *mlt_properties_get( mlt_properties self, const char *name )
750 mlt_property value = mlt_properties_find( self, name );
751 property_list *list = self->local;
752 return value == NULL ? NULL : mlt_property_get_string_l( value, list->locale );
755 /** Get a property name by index.
757 * Do not free the returned string.
758 * \public \memberof mlt_properties_s
759 * \param self a properties list
760 * \param index the numeric index of the property
761 * \return the name of the property or NULL if index is out of range
764 char *mlt_properties_get_name( mlt_properties self, int index )
766 property_list *list = self->local;
767 if ( index >= 0 && index < list->count )
768 return list->name[ index ];
772 /** Get a property's string value by index.
774 * Do not free the returned string.
775 * \public \memberof mlt_properties_s
776 * \param self a properties list
777 * \param index the numeric index of the property
778 * \return the property value as a string or NULL if the index is out of range
781 char *mlt_properties_get_value( mlt_properties self, int index )
783 property_list *list = self->local;
784 if ( index >= 0 && index < list->count )
785 return mlt_property_get_string_l( list->value[ index ], list->locale );
789 /** Get a data value by index.
791 * Do not free the returned pointer if you supplied a destructor function when you
793 * \public \memberof mlt_properties_s
794 * \param self a properties list
795 * \param index the numeric index of the property
796 * \param[out] size the size of the binary data in bytes or NULL if the index is out of range
799 void *mlt_properties_get_data_at( mlt_properties self, int index, int *size )
801 property_list *list = self->local;
802 if ( index >= 0 && index < list->count )
803 return mlt_property_get_data( list->value[ index ], size );
807 /** Return the number of items in the list.
809 * \public \memberof mlt_properties_s
810 * \param self a properties list
811 * \return the number of property objects
814 int mlt_properties_count( mlt_properties self )
816 property_list *list = self->local;
820 /** Set a value by parsing a name=value string.
822 * \public \memberof mlt_properties_s
823 * \param self a properties list
824 * \param namevalue a string containing name and value delimited by '='
825 * \return true if there was an error
828 int mlt_properties_parse( mlt_properties self, const char *namevalue )
830 char *name = strdup( namevalue );
833 char *ptr = strchr( name, '=' );
841 value = strdup( ptr );
846 value = strdup( ptr );
847 if ( value != NULL && value[ strlen( value ) - 1 ] == '\"' )
848 value[ strlen( value ) - 1 ] = '\0';
853 value = strdup( "" );
856 error = mlt_properties_set( self, name, value );
864 /** Get an integer associated to the name.
866 * \public \memberof mlt_properties_s
867 * \param self a properties list
868 * \param name the property to get
869 * \return The integer value, 0 if not found (which may also be a legitimate value)
872 int mlt_properties_get_int( mlt_properties self, const char *name )
874 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
875 double fps = mlt_profile_fps( profile );
876 property_list *list = self->local;
877 mlt_property value = mlt_properties_find( self, name );
878 return value == NULL ? 0 : mlt_property_get_int( value, fps, list->locale );
881 /** Set a property to an integer value.
883 * \public \memberof mlt_properties_s
884 * \param self a properties list
885 * \param name the property to set
886 * \param value the integer
887 * \return true if error
890 int mlt_properties_set_int( mlt_properties self, const char *name, int value )
894 // Fetch the property to work with
895 mlt_property property = mlt_properties_fetch( self, name );
897 // Set it if not NULL
898 if ( property != NULL )
900 error = mlt_property_set_int( property, value );
901 mlt_properties_do_mirror( self, name );
904 mlt_events_fire( self, "property-changed", name, NULL );
909 /** Get a 64-bit integer associated to the name.
911 * \public \memberof mlt_properties_s
912 * \param self a properties list
913 * \param name the property to get
914 * \return the integer value, 0 if not found (which may also be a legitimate value)
917 int64_t mlt_properties_get_int64( mlt_properties self, const char *name )
919 mlt_property value = mlt_properties_find( self, name );
920 return value == NULL ? 0 : mlt_property_get_int64( value );
923 /** Set a property to a 64-bit integer value.
925 * \public \memberof mlt_properties_s
926 * \param self a properties list
927 * \param name the property to set
928 * \param value the integer
929 * \return true if error
932 int mlt_properties_set_int64( mlt_properties self, const char *name, int64_t value )
936 // Fetch the property to work with
937 mlt_property property = mlt_properties_fetch( self, name );
939 // Set it if not NULL
940 if ( property != NULL )
942 error = mlt_property_set_int64( property, value );
943 mlt_properties_do_mirror( self, name );
946 mlt_events_fire( self, "property-changed", name, NULL );
951 /** Get a floating point value associated to the name.
953 * \public \memberof mlt_properties_s
954 * \param self a properties list
955 * \param name the property to get
956 * \return the floating point, 0 if not found (which may also be a legitimate value)
959 double mlt_properties_get_double( mlt_properties self, const char *name )
961 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
962 double fps = mlt_profile_fps( profile );
963 mlt_property value = mlt_properties_find( self, name );
964 property_list *list = self->local;
965 return value == NULL ? 0 : mlt_property_get_double( value, fps, list->locale );
968 /** Set a property to a floating point value.
970 * \public \memberof mlt_properties_s
971 * \param self a properties list
972 * \param name the property to set
973 * \param value the floating point value
974 * \return true if error
977 int mlt_properties_set_double( mlt_properties self, const char *name, double value )
981 // Fetch the property to work with
982 mlt_property property = mlt_properties_fetch( self, name );
984 // Set it if not NULL
985 if ( property != NULL )
987 error = mlt_property_set_double( property, value );
988 mlt_properties_do_mirror( self, name );
991 mlt_events_fire( self, "property-changed", name, NULL );
996 /** Get a position value associated to the name.
998 * \public \memberof mlt_properties_s
999 * \param self a properties list
1000 * \param name the property to get
1001 * \return the position, 0 if not found (which may also be a legitimate value)
1004 mlt_position mlt_properties_get_position( mlt_properties self, const char *name )
1006 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
1007 double fps = mlt_profile_fps( profile );
1008 property_list *list = self->local;
1009 mlt_property value = mlt_properties_find( self, name );
1010 return value == NULL ? 0 : mlt_property_get_position( value, fps, list->locale );
1013 /** Set a property to a position value.
1015 * \public \memberof mlt_properties_s
1016 * \param self a properties list
1017 * \param name the property to get
1018 * \param value the position
1019 * \return true if error
1022 int mlt_properties_set_position( mlt_properties self, const char *name, mlt_position value )
1026 // Fetch the property to work with
1027 mlt_property property = mlt_properties_fetch( self, name );
1029 // Set it if not NULL
1030 if ( property != NULL )
1032 error = mlt_property_set_position( property, value );
1033 mlt_properties_do_mirror( self, name );
1036 mlt_events_fire( self, "property-changed", name, NULL );
1041 /** Get a binary data value associated to the name.
1043 * Do not free the returned pointer if you supplied a destructor function
1044 * when you set this property.
1045 * \public \memberof mlt_properties_s
1046 * \param self a properties list
1047 * \param name the property to get
1048 * \param[out] length The size of the binary data in bytes, if available (often it is not, you should know)
1051 void *mlt_properties_get_data( mlt_properties self, const char *name, int *length )
1053 mlt_property value = mlt_properties_find( self, name );
1054 return value == NULL ? NULL : mlt_property_get_data( value, length );
1057 /** Store binary data as a property.
1059 * \public \memberof mlt_properties_s
1060 * \param self a properties list
1061 * \param name the property to set
1062 * \param value an opaque pointer to binary data
1063 * \param length the size of the binary data in bytes (optional)
1064 * \param destroy a function to deallocate the binary data when the property is closed (optional)
1065 * \param serialise a function that can serialize the binary data as text (optional)
1066 * \return true if error
1069 int mlt_properties_set_data( mlt_properties self, const char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
1073 // Fetch the property to work with
1074 mlt_property property = mlt_properties_fetch( self, name );
1076 // Set it if not NULL
1077 if ( property != NULL )
1078 error = mlt_property_set_data( property, value, length, destroy, serialise );
1080 mlt_events_fire( self, "property-changed", name, NULL );
1085 /** Rename a property.
1087 * \public \memberof mlt_properties_s
1088 * \param self a properties list
1089 * \param source the property to rename
1090 * \param dest the new name
1091 * \return true if the name is already in use
1094 int mlt_properties_rename( mlt_properties self, const char *source, const char *dest )
1096 mlt_property value = mlt_properties_find( self, dest );
1098 if ( value == NULL )
1100 property_list *list = self->local;
1104 mlt_properties_lock( self );
1105 for ( i = 0; i < list->count; i ++ )
1107 if ( !strcmp( list->name[ i ], source ) )
1109 free( list->name[ i ] );
1110 list->name[ i ] = strdup( dest );
1111 list->hash[ generate_hash( dest ) ] = i + 1;
1115 mlt_properties_unlock( self );
1118 return value != NULL;
1121 /** Dump the properties to a file handle.
1123 * \public \memberof mlt_properties_s
1124 * \param self a properties list
1125 * \param output a file handle
1128 void mlt_properties_dump( mlt_properties self, FILE *output )
1130 property_list *list = self->local;
1132 for ( i = 0; i < list->count; i ++ )
1133 if ( mlt_properties_get( self, list->name[ i ] ) != NULL )
1134 fprintf( output, "%s=%s\n", list->name[ i ], mlt_properties_get( self, list->name[ i ] ) );
1137 /** Output the properties to a file handle.
1139 * This version includes reference counts and does not put each property on a new line.
1140 * \public \memberof mlt_properties_s
1141 * \param self a properties pointer
1142 * \param title a string to preface the output
1143 * \param output a file handle
1145 void mlt_properties_debug( mlt_properties self, const char *title, FILE *output )
1147 if ( output == NULL ) output = stderr;
1148 fprintf( output, "%s: ", title );
1151 property_list *list = self->local;
1153 fprintf( output, "[ ref=%d", list->ref_count );
1154 for ( i = 0; i < list->count; i ++ )
1155 if ( mlt_properties_get( self, list->name[ i ] ) != NULL )
1156 fprintf( output, ", %s=%s", list->name[ i ], mlt_properties_get( self, list->name[ i ] ) );
1158 fprintf( output, ", %s=%p", list->name[ i ], mlt_properties_get_data( self, list->name[ i ], NULL ) );
1159 fprintf( output, " ]" );
1161 fprintf( output, "\n" );
1164 /** Save the properties to a file by name.
1166 * This uses the dump format - one line per property.
1167 * \public \memberof mlt_properties_s
1168 * \param self a properties list
1169 * \param filename the name of a file to create or overwrite
1170 * \return true if there was an error
1173 int mlt_properties_save( mlt_properties self, const char *filename )
1176 FILE *f = fopen( filename, "w" );
1179 mlt_properties_dump( self, f );
1186 /* This is a very basic cross platform fnmatch replacement - it will fail in
1187 * many cases, but for the basic *.XXX and YYY*.XXX, it will work ok.
1190 /** Test whether a filename or pathname matches a shell-style pattern.
1192 * \private \memberof mlt_properties_s
1193 * \param wild a string containing a wildcard pattern
1194 * \param file the name of a file to test against
1195 * \return true if the file name matches the wildcard pattern
1198 static int mlt_fnmatch( const char *wild, const char *file )
1203 while( f < strlen( file ) && w < strlen( wild ) )
1205 if ( wild[ w ] == '*' )
1208 if ( w == strlen( wild ) )
1210 while ( f != strlen( file ) && tolower( file[ f ] ) != tolower( wild[ w ] ) )
1213 else if ( wild[ w ] == '?' || tolower( file[ f ] ) == tolower( wild[ w ] ) )
1218 else if ( wild[ 0 ] == '*' )
1228 return strlen( file ) == f && strlen( wild ) == w;
1231 /** Compare the string or serialized value of two properties.
1233 * \private \memberof mlt_properties_s
1234 * \param self a property
1235 * \param that a property
1236 * \return < 0 if \p self less than \p that, 0 if equal, or > 0 if \p self is greater than \p that
1239 static int mlt_compare( const void *self, const void *that )
1241 return strcmp( mlt_property_get_string( *( const mlt_property * )self ), mlt_property_get_string( *( const mlt_property * )that ) );
1244 /** Get the contents of a directory.
1246 * Obtains an optionally sorted list of the files found in a directory with a specific wild card.
1247 * Entries in the list have a numeric name (running from 0 to count - 1). Only values change
1248 * position if sort is enabled. Designed to be posix compatible (linux, os/x, mingw etc).
1249 * \public \memberof mlt_properties_s
1250 * \param self a properties list
1251 * \param dirname the name of the directory
1252 * \param pattern a wildcard pattern to filter the directory listing
1253 * \param sort Do you want to sort the directory listing?
1254 * \return the number of items in the directory listing
1257 int mlt_properties_dir_list( mlt_properties self, const char *dirname, const char *pattern, int sort )
1259 DIR *dir = opendir( dirname );
1264 struct dirent *de = readdir( dir );
1265 char fullname[ 1024 ];
1268 sprintf( key, "%d", mlt_properties_count( self ) );
1269 snprintf( fullname, 1024, "%s/%s", dirname, de->d_name );
1270 if ( pattern == NULL )
1271 mlt_properties_set( self, key, fullname );
1272 else if ( de->d_name[ 0 ] != '.' && mlt_fnmatch( pattern, de->d_name ) )
1273 mlt_properties_set( self, key, fullname );
1274 de = readdir( dir );
1280 if ( sort && mlt_properties_count( self ) )
1282 property_list *list = self->local;
1283 mlt_properties_lock( self );
1284 qsort( list->value, mlt_properties_count( self ), sizeof( mlt_property ), mlt_compare );
1285 mlt_properties_unlock( self );
1288 return mlt_properties_count( self );
1291 /** Close a properties object.
1293 * Deallocates the properties object and everything it contains.
1294 * \public \memberof mlt_properties_s
1295 * \param self a properties object
1298 void mlt_properties_close( mlt_properties self )
1300 if ( self != NULL && mlt_properties_dec_ref( self ) <= 0 )
1302 if ( self->close != NULL )
1304 self->close( self->close_object );
1308 property_list *list = self->local;
1311 #if _MLT_PROPERTY_CHECKS_ == 1
1313 mlt_properties_debug( self, "Closing", stderr );
1316 #ifdef _MLT_PROPERTY_CHECKS_
1317 // Increment destroyed count
1318 properties_destroyed ++;
1320 // Show current stats - these should match when the app is closed
1321 mlt_log( NULL, MLT_LOG_DEBUG, "Created %d, destroyed %d\n", properties_created, properties_destroyed );
1324 // Clean up names and values
1325 for ( index = list->count - 1; index >= 0; index -- )
1327 mlt_property_close( list->value[ index ] );
1328 free( list->name[ index ] );
1331 #if defined(__linux__) || defined(__DARWIN__)
1334 freelocale( list->locale );
1337 // Clear up the list
1338 pthread_mutex_destroy( &list->mutex );
1340 free( list->value );
1343 // Free self now if self has no child
1344 if ( self->child == NULL )
1350 /** Determine if the properties list is really just a sequence or ordered list.
1352 * \public \memberof mlt_properties_s
1353 * \param properties a properties list
1354 * \return true if all of the property names are numeric (a sequence)
1357 int mlt_properties_is_sequence( mlt_properties properties )
1360 int n = mlt_properties_count( properties );
1361 for ( i = 0; i < n; i++ )
1362 if ( ! isdigit( mlt_properties_get_name( properties, i )[0] ) )
1367 /** \brief YAML Tiny Parser context structure
1369 * YAML is a nifty text format popular in the Ruby world as a cleaner,
1370 * less verbose alternative to XML. See this Wikipedia topic for an overview:
1371 * http://en.wikipedia.org/wiki/YAML
1372 * The YAML specification is at:
1374 * YAML::Tiny is a Perl module that specifies a subset of YAML that we are
1375 * using here (for the same reasons):
1376 * http://search.cpan.org/~adamk/YAML-Tiny-1.25/lib/YAML/Tiny.pm
1380 struct yaml_parser_context
1387 unsigned int block_indent;
1390 typedef struct yaml_parser_context *yaml_parser;
1392 /** Remove spaces from the left side of a string.
1394 * \param s the string to trim
1395 * \return the number of characters removed
1398 static unsigned int ltrim( char **s )
1402 int n = strlen( c );
1403 for ( i = 0; i < n && *c == ' '; i++, c++ );
1408 /** Remove spaces from the right side of a string.
1410 * \param s the string to trim
1411 * \return the number of characters removed
1414 static unsigned int rtrim( char *s )
1416 int n = strlen( s );
1418 for ( i = n; i > 0 && s[i - 1] == ' '; --i )
1423 /** Parse a line of YAML Tiny.
1425 * Adds a property if needed.
1426 * \private \memberof yaml_parser_context
1427 * \param context a YAML Tiny Parser context
1428 * \param namevalue a line of YAML Tiny
1429 * \return true if there was an error
1432 static int parse_yaml( yaml_parser context, const char *namevalue )
1434 char *name_ = strdup( namevalue );
1438 char *ptr = strchr( name, ':' );
1439 unsigned int indent = ltrim( &name );
1440 mlt_properties properties = mlt_deque_peek_front( context->stack );
1442 // Ascending one more levels in the tree
1443 if ( indent < context->level )
1446 unsigned int n = ( context->level - indent ) / 2;
1447 for ( i = 0; i < n; i++ )
1448 mlt_deque_pop_front( context->stack );
1449 properties = mlt_deque_peek_front( context->stack );
1450 context->level = indent;
1453 // Descending a level in the tree
1454 else if ( indent > context->level && context->block == 0 )
1456 context->level = indent;
1459 // If there is a colon that is not part of a block
1460 if ( ptr && ( indent == context->level ) )
1462 // Reset block processing
1463 if ( context->block_name )
1465 free( context->block_name );
1466 context->block_name = NULL;
1470 // Terminate the name and setup the value pointer
1474 char *comment = strchr( ptr, '#' );
1480 // Trim leading and trailing spaces from bare value
1484 // No value means a child
1485 if ( strcmp( ptr, "" ) == 0 )
1487 mlt_properties child = mlt_properties_new();
1488 mlt_properties_set_lcnumeric( child, mlt_properties_get_lcnumeric( properties ) );
1489 mlt_properties_set_data( properties, name, child, 0,
1490 ( mlt_destructor )mlt_properties_close, NULL );
1491 mlt_deque_push_front( context->stack, child );
1497 // A dash indicates a sequence item
1498 if ( name[0] == '-' )
1500 mlt_properties child = mlt_properties_new();
1503 mlt_properties_set_lcnumeric( child, mlt_properties_get_lcnumeric( properties ) );
1504 snprintf( key, sizeof(key), "%d", context->index++ );
1505 mlt_properties_set_data( properties, key, child, 0,
1506 ( mlt_destructor )mlt_properties_close, NULL );
1507 mlt_deque_push_front( context->stack, child );
1510 context->level += ltrim( &name ) + 1;
1518 value = strdup( ptr );
1519 if ( value && value[ strlen( value ) - 1 ] == '\"' )
1520 value[ strlen( value ) - 1 ] = 0;
1523 // Value is folded or unfolded block
1524 else if ( *ptr == '|' || *ptr == '>' )
1526 context->block = *ptr;
1527 context->block_name = strdup( name );
1528 context->block_indent = 0;
1529 value = strdup( "" );
1535 value = strdup( ptr );
1539 // A list of scalars
1540 else if ( name[0] == '-' )
1542 // Reset block processing
1543 if ( context->block_name )
1545 free( context->block_name );
1546 context->block_name = NULL;
1552 snprintf( key, sizeof(key), "%d", context->index++ );
1556 char *comment = strchr( ptr, '#' );
1560 // Trim leading and trailing spaces from bare value
1568 value = strdup( ptr );
1569 if ( value && value[ strlen( value ) - 1 ] == '\"' )
1570 value[ strlen( value ) - 1 ] = 0;
1573 // Value is folded or unfolded block
1574 else if ( *ptr == '|' || *ptr == '>' )
1576 context->block = *ptr;
1577 context->block_name = strdup( key );
1578 context->block_indent = 0;
1579 value = strdup( "" );
1585 value = strdup( ptr );
1589 name = name_ = strdup( key );
1593 else if ( context->block == '|' )
1595 if ( context->block_indent == 0 )
1596 context->block_indent = indent;
1597 if ( indent > context->block_indent )
1598 name = &name_[ context->block_indent ];
1600 char *old_value = mlt_properties_get( properties, context->block_name );
1601 value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
1602 strcpy( value, old_value );
1603 if ( strcmp( old_value, "" ) )
1604 strcat( value, "\n" );
1605 strcat( value, name );
1606 name = context->block_name;
1610 else if ( context->block == '>' )
1614 char *old_value = mlt_properties_get( properties, context->block_name );
1616 // Blank line (prepended with spaces) is new line
1617 if ( strcmp( name, "" ) == 0 )
1619 value = calloc( 1, strlen( old_value ) + 2 );
1620 strcat( value, old_value );
1621 strcat( value, "\n" );
1623 // Concatenate with space
1626 value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
1627 strcat( value, old_value );
1628 if ( strcmp( old_value, "" ) && old_value[ strlen( old_value ) - 1 ] != '\n' )
1629 strcat( value, " " );
1630 strcat( value, name );
1632 name = context->block_name;
1637 value = strdup( "" );
1640 error = mlt_properties_set( properties, name, value );
1642 if ( !strcmp( name, "LC_NUMERIC" ) )
1643 mlt_properties_set_lcnumeric( properties, value );
1651 /** Parse a YAML Tiny file by name.
1653 * \public \memberof mlt_properties_s
1654 * \param filename the name of a text file containing YAML Tiny
1655 * \return a new properties list
1658 mlt_properties mlt_properties_parse_yaml( const char *filename )
1660 // Construct a standalone properties object
1661 mlt_properties self = mlt_properties_new( );
1666 FILE *file = fopen( filename, "r" );
1668 // Load contents of file
1673 char *ptemp = &temp[ 0 ];
1675 // Default to LC_NUMERIC = C
1676 mlt_properties_set_lcnumeric( self, "C" );
1679 yaml_parser context = calloc( 1, sizeof( struct yaml_parser_context ) );
1680 context->stack = mlt_deque_init();
1681 mlt_deque_push_front( context->stack, self );
1683 // Read each string from the file
1684 while( fgets( temp, 1024, file ) )
1686 // Check for end-of-stream
1687 if ( strncmp( ptemp, "...", 3 ) == 0 )
1691 temp[ strlen( temp ) - 1 ] = '\0';
1693 // Skip blank lines, comment lines, and document separator
1694 if ( strcmp( ptemp, "" ) && ptemp[ 0 ] != '#' && strncmp( ptemp, "---", 3 )
1695 && strncmp( ptemp, "%YAML", 5 ) && strncmp( ptemp, "% YAML", 6 ) )
1696 parse_yaml( context, temp );
1701 mlt_deque_close( context->stack );
1702 if ( context->block_name )
1703 free( context->block_name );
1708 // Return the pointer
1713 * YAML Tiny Serializer
1716 /** How many bytes to grow at a time */
1717 #define STRBUF_GROWTH (1024)
1719 /** \brief Private to mlt_properties_s, a self-growing buffer for building strings
1729 typedef struct strbuf_s *strbuf;
1731 /** Create a new string buffer
1733 * \private \memberof strbuf_s
1734 * \return a new string buffer
1737 static strbuf strbuf_new( )
1739 strbuf buffer = calloc( 1, sizeof( struct strbuf_s ) );
1740 buffer->size = STRBUF_GROWTH;
1741 buffer->string = calloc( 1, buffer->size );
1745 /** Destroy a string buffer
1747 * \private \memberof strbuf_s
1748 * \param buffer the string buffer to close
1751 static void strbuf_close( strbuf buffer )
1753 // We do not free buffer->string; strbuf user must save that pointer
1759 /** Format a string into a string buffer
1761 * A variable number of arguments follows the format string - one for each
1763 * \private \memberof strbuf_s
1764 * \param buffer the string buffer to write into
1765 * \param format a string that contains text and formatting instructions
1766 * \return the formatted string
1769 static char *strbuf_printf( strbuf buffer, const char *format, ... )
1771 while ( buffer->string )
1774 va_start( ap, format );
1775 size_t len = strlen( buffer->string );
1776 size_t remain = buffer->size - len - 1;
1777 int need = vsnprintf( buffer->string + len, remain, format, ap );
1779 if ( need > -1 && need < remain )
1781 buffer->string[ len ] = 0;
1782 buffer->size += need + STRBUF_GROWTH;
1783 buffer->string = realloc( buffer->string, buffer->size );
1785 return buffer->string;
1788 /** Indent a line of YAML Tiny.
1790 * \private \memberof strbuf_s
1791 * \param output a string buffer
1792 * \param indent the number of spaces to indent
1795 static inline void indent_yaml( strbuf output, int indent )
1798 for ( j = 0; j < indent; j++ )
1799 strbuf_printf( output, " " );
1802 /** Convert a line string into a YAML block literal.
1804 * \private \memberof strbuf_s
1805 * \param output a string buffer
1806 * \param value the string to format as a block literal
1807 * \param indent the number of spaces to indent
1810 static void output_yaml_block_literal( strbuf output, const char *value, int indent )
1812 char *v = strdup( value );
1814 char *eol = strchr( sol, '\n' );
1818 indent_yaml( output, indent );
1820 strbuf_printf( output, "%s\n", sol );
1822 eol = strchr( sol, '\n' );
1824 indent_yaml( output, indent );
1825 strbuf_printf( output, "%s\n", sol );
1828 /** Recursively serialize a properties list into a string buffer as YAML Tiny.
1830 * \private \memberof mlt_properties_s
1831 * \param self a properties list
1832 * \param output a string buffer to hold the serialized YAML Tiny
1833 * \param indent the number of spaces to indent (for recursion, initialize to 0)
1834 * \param is_parent_sequence Is this properties list really just a sequence (for recursion, initialize to 0)?
1837 static void serialise_yaml( mlt_properties self, strbuf output, int indent, int is_parent_sequence )
1839 property_list *list = self->local;
1842 for ( i = 0; i < list->count; i ++ )
1844 // This implementation assumes that all data elements are property lists.
1845 // Unfortunately, we do not have run time type identification.
1846 mlt_properties child = mlt_property_get_data( list->value[ i ], NULL );
1848 if ( mlt_properties_is_sequence( self ) )
1850 // Ignore hidden/non-serialisable items
1851 if ( list->name[ i ][ 0 ] != '_' )
1853 // Indicate a sequence item
1854 indent_yaml( output, indent );
1855 strbuf_printf( output, "- " );
1857 // If the value can be represented as a string
1858 const char *value = mlt_properties_get( self, list->name[ i ] );
1859 if ( value && strcmp( value, "" ) )
1861 // Determine if this is an unfolded block literal
1862 if ( strchr( value, '\n' ) )
1864 strbuf_printf( output, "|\n" );
1865 output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( "|" ) );
1869 strbuf_printf( output, "%s\n", value );
1875 serialise_yaml( child, output, indent + 2, 1 );
1879 // Assume this is a normal map-oriented properties list
1880 const char *value = mlt_properties_get( self, list->name[ i ] );
1882 // Ignore hidden/non-serialisable items
1883 // If the value can be represented as a string
1884 if ( list->name[ i ][ 0 ] != '_' && value && strcmp( value, "" ) )
1886 if ( is_parent_sequence == 0 )
1887 indent_yaml( output, indent );
1889 is_parent_sequence = 0;
1891 // Determine if this is an unfolded block literal
1892 if ( strchr( value, '\n' ) )
1894 strbuf_printf( output, "%s: |\n", list->name[ i ] );
1895 output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( ": " ) );
1899 strbuf_printf( output, "%s: %s\n", list->name[ i ], value );
1903 // Output a child as a map item
1906 indent_yaml( output, indent );
1907 strbuf_printf( output, "%s:\n", list->name[ i ] );
1910 serialise_yaml( child, output, indent + 2, 0 );
1916 /** Serialize a properties list as a string of YAML Tiny.
1918 * The caller MUST free the returned string!
1919 * This operates on properties containing properties as a hierarchical data
1921 * \public \memberof mlt_properties_s
1922 * \param self a properties list
1923 * \return a string containing YAML Tiny that represents the properties list
1926 char *mlt_properties_serialise_yaml( mlt_properties self )
1928 const char *lc_numeric = mlt_properties_get_lcnumeric( self );
1929 strbuf b = strbuf_new();
1930 strbuf_printf( b, "---\n" );
1931 mlt_properties_set_lcnumeric( self, "C" );
1932 serialise_yaml( self, b, 0, 0 );
1933 mlt_properties_set_lcnumeric( self, lc_numeric );
1934 strbuf_printf( b, "...\n" );
1935 char *ret = b->string;
1940 /** Protect a properties list against concurrent access.
1942 * \public \memberof mlt_properties_s
1943 * \param self a properties list
1946 void mlt_properties_lock( mlt_properties self )
1949 pthread_mutex_lock( &( ( property_list* )( self->local ) )->mutex );
1952 /** End protecting a properties list against concurrent access.
1954 * \public \memberof mlt_properties_s
1955 * \param self a properties list
1958 void mlt_properties_unlock( mlt_properties self )
1961 pthread_mutex_unlock( &( ( property_list* )( self->local ) )->mutex );
1964 /** Get a time string associated to the name.
1966 * Do not free the returned string. It's lifetime is controlled by the property.
1967 * \public \memberof mlt_properties_s
1968 * \param self a properties list
1969 * \param name the property to get
1970 * \param format the time format that you want
1971 * \return the property's time value or NULL if \p name does not exist or there is no profile
1974 char *mlt_properties_get_time( mlt_properties self, const char* name, mlt_time_format format )
1976 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
1979 double fps = mlt_profile_fps( profile );
1980 mlt_property value = mlt_properties_find( self, name );
1981 property_list *list = self->local;
1982 return value == NULL ? NULL : mlt_property_get_time( value, format, fps, list->locale );