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 /** \brief private implementation of the property list */
52 mlt_properties mirror;
54 pthread_mutex_t mutex;
59 /* Memory leak checks */
61 //#define _MLT_PROPERTY_CHECKS_ 2
62 #ifdef _MLT_PROPERTY_CHECKS_
63 static int properties_created = 0;
64 static int properties_destroyed = 0;
67 /** Initialize a properties object that was already allocated.
69 * This does allocate its ::property_list, and it adds a reference count.
70 * \public \memberof mlt_properties_s
71 * \param self the properties structure to initialize
72 * \param child an opaque pointer to a subclass object
73 * \return true if failed
76 int mlt_properties_init( mlt_properties self, void *child )
80 #ifdef _MLT_PROPERTY_CHECKS_
81 // Increment number of properties created
82 properties_created ++;
86 memset( self, 0, sizeof( struct mlt_properties_s ) );
88 // Assign the child of the object
91 // Allocate the local structure
92 self->local = calloc( sizeof( property_list ), 1 );
94 // Increment the ref count
95 ( ( property_list * )self->local )->ref_count = 1;
96 pthread_mutex_init( &( ( property_list * )self->local )->mutex, NULL );;
99 // Check that initialisation was successful
100 return self != NULL && self->local == NULL;
103 /** Create a properties object.
105 * This allocates the properties structure and calls mlt_properties_init() on it.
106 * Free the properties object with mlt_properties_close().
107 * \public \memberof mlt_properties_s
108 * \return a new properties object
111 mlt_properties mlt_properties_new( )
113 // Construct a standalone properties object
114 mlt_properties self = calloc( sizeof( struct mlt_properties_s ), 1 );
117 mlt_properties_init( self, NULL );
119 // Return the pointer
123 /** Set the numeric locale used for string/double conversions.
125 * \public \memberof mlt_properties_s
126 * \param self a properties list
127 * \param locale the locale name
128 * \return true if error
131 int mlt_properties_set_lcnumeric( mlt_properties self, const char *locale )
135 if ( self && locale )
137 property_list *list = self->local;
140 freelocale( list->locale );
141 list->locale = newlocale( LC_NUMERIC, locale, NULL );
142 error = list->locale == NULL;
150 /** Get the numeric locale for this properties object.
152 * \public \memberof mlt_properties_s
153 * \param self a properties list
154 * \return the locale name if this properties has a specific locale it is using, NULL otherwise
157 const char* mlt_properties_get_lcnumeric( mlt_properties self )
159 property_list *list = self->local;
160 const char *result = NULL;
165 result = querylocale( LC_NUMERIC, list->locale );
167 result = list->locale->__names[ LC_NUMERIC ];
173 static int load_properties( mlt_properties self, const char *filename )
176 FILE *file = fopen( filename, "r" );
178 // Load contents of file
183 char last[ 1024 ] = "";
185 // Read each string from the file
186 while( fgets( temp, 1024, file ) )
189 temp[ strlen( temp ) - 1 ] = '\0';
191 // Check if the line starts with a .
192 if ( temp[ 0 ] == '.' )
195 sprintf( temp2, "%s%s", last, temp );
196 strcpy( temp, temp2 );
198 else if ( strchr( temp, '=' ) )
200 strcpy( last, temp );
201 *( strchr( last, '=' ) ) = '\0';
204 // Parse and set the property
205 if ( strcmp( temp, "" ) && temp[ 0 ] != '#' )
206 mlt_properties_parse( self, temp );
212 return file? 0 : errno;
215 /** Create a properties object by reading a .properties text file.
217 * Free the properties object with mlt_properties_close().
218 * \deprecated Please start using mlt_properties_parse_yaml().
219 * \public \memberof mlt_properties_s
220 * \param filename the absolute file name
221 * \return a new properties object
224 mlt_properties mlt_properties_load( const char *filename )
226 // Construct a standalone properties object
227 mlt_properties self = mlt_properties_new( );
230 load_properties( self, filename );
232 // Return the pointer
236 /** Set properties from a preset.
238 * Presets are typically installed to $prefix/share/mlt/presets/{type}/{service}/[{profile}/]{name}.
239 * For example, "/usr/share/mlt/presets/consumer/avformat/dv_ntsc_wide/DVD"
240 * could be an encoding preset for a widescreen NTSC DVD Video.
241 * Do not specify the type and service in the preset name parameter; these are
242 * inferred automatically from the service to which you are applying the preset.
243 * Using the example above and assuming you are calling this function on the
244 * avformat consumer, the name passed to the function should simply be DVD.
245 * Note that the profile portion of the path is optional, but a profile-specific
246 * preset with the same name as a more generic one is given a higher priority.
247 * \todo Look in a user-specific location - somewhere in the home directory.
249 * \public \memberof mlt_properties_s
250 * \param self a properties list
251 * \param name the name of a preset in a well-known location or the explicit path
252 * \return true if error
255 int mlt_properties_preset( mlt_properties self, const char *name )
257 struct stat stat_buff;
260 if ( !( self && name && strlen( name ) ) )
263 // See if name is an explicit file
264 if ( ! stat( name, &stat_buff ) )
266 return load_properties( self, name );
270 // Look for profile-specific preset before a generic one.
271 char *data = getenv( "MLT_PRESETS_PATH" );
272 const char *type = mlt_properties_get( self, "mlt_type" );
273 const char *service = mlt_properties_get( self, "mlt_service" );
274 const char *profile = mlt_environment( "MLT_PROFILE" );
279 data = strdup( data );
283 data = malloc( strlen( mlt_environment( "MLT_DATA" ) ) + 9 );
284 strcpy( data, mlt_environment( "MLT_DATA" ) );
285 strcat( data, "/presets" );
287 if ( data && type && service )
289 char *path = malloc( 5 + strlen(name) + strlen(data) + strlen(type) + strlen(service) + ( profile? strlen(profile) : 0 ) );
290 sprintf( path, "%s/%s/%s/%s/%s", data, type, service, profile, name );
291 if ( load_properties( self, path ) )
293 sprintf( path, "%s/%s/%s/%s", data, type, service, name );
294 error = load_properties( self, path );
307 /** Generate a hash key.
309 * \private \memberof mlt_properties_s
310 * \param name a string
314 static inline int generate_hash( const char *name )
319 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
323 /** Copy a serializable property to a properties list that is mirroring this one.
325 * Special case - when a container (such as loader) is protecting another
326 * producer, we need to ensure that properties are passed through to the
328 * \private \memberof mlt_properties_s
329 * \param self a properties list
330 * \param name the name of the property to copy
333 static inline void mlt_properties_do_mirror( mlt_properties self, const char *name )
335 property_list *list = self->local;
336 if ( list->mirror != NULL )
338 char *value = mlt_properties_get( self, name );
340 mlt_properties_set( list->mirror, name, value );
344 /** Increment the reference count.
346 * \public \memberof mlt_properties_s
347 * \param self a properties list
348 * \return the new reference count
351 int mlt_properties_inc_ref( mlt_properties self )
356 property_list *list = self->local;
357 pthread_mutex_lock( &list->mutex );
358 result = ++ list->ref_count;
359 pthread_mutex_unlock( &list->mutex );
364 /** Decrement the reference count.
366 * \public \memberof mlt_properties_s
367 * \param self a properties list
368 * \return the new reference count
371 int mlt_properties_dec_ref( mlt_properties self )
376 property_list *list = self->local;
377 pthread_mutex_lock( &list->mutex );
378 result = -- list->ref_count;
379 pthread_mutex_unlock( &list->mutex );
384 /** Get the reference count.
386 * \public \memberof mlt_properties_s
387 * \param self a properties list
388 * \return the current reference count
391 int mlt_properties_ref_count( mlt_properties self )
395 property_list *list = self->local;
396 return list->ref_count;
401 /** Set a properties list to be a mirror copy of another.
403 * Note that this does not copy all existing properties. Rather, you must
404 * call this before setting the properties that you wish to copy.
405 * \public \memberof mlt_properties_s
406 * \param that the properties which will receive copies of the properties as they are set.
407 * \param self the properties to mirror
410 void mlt_properties_mirror( mlt_properties self, mlt_properties that )
412 property_list *list = self->local;
416 /** Copy all serializable properties to another properties list.
418 * \public \memberof mlt_properties_s
419 * \param self The properties to copy to
420 * \param that The properties to copy from
424 int mlt_properties_inherit( mlt_properties self, mlt_properties that )
426 int count = mlt_properties_count( that );
428 for ( i = 0; i < count; i ++ )
430 char *value = mlt_properties_get_value( that, i );
433 char *name = mlt_properties_get_name( that, i );
434 mlt_properties_set( self, name, value );
440 /** Pass all serializable properties that match a prefix to another properties object
442 * \public \memberof mlt_properties_s
443 * \param self the properties to copy to
444 * \param that The properties to copy from
445 * \param prefix the property names to match (required)
449 int mlt_properties_pass( mlt_properties self, mlt_properties that, const char *prefix )
451 int count = mlt_properties_count( that );
452 int length = strlen( prefix );
454 for ( i = 0; i < count; i ++ )
456 char *name = mlt_properties_get_name( that, i );
457 if ( !strncmp( name, prefix, length ) )
459 char *value = mlt_properties_get_value( that, i );
461 mlt_properties_set( self, name + length, value );
467 /** Locate a property by name.
469 * \private \memberof mlt_properties_s
470 * \param self a properties list
471 * \param name the property to lookup by name
472 * \return the property or NULL for failure
475 static inline mlt_property mlt_properties_find( mlt_properties self, const char *name )
477 property_list *list = self->local;
478 mlt_property value = NULL;
479 int key = generate_hash( name );
481 mlt_properties_lock( self );
483 int i = list->hash[ key ] - 1;
486 // Check if we're hashed
487 if ( list->count > 0 &&
488 name[ 0 ] == list->name[ i ][ 0 ] &&
489 !strcmp( list->name[ i ], name ) )
490 value = list->value[ i ];
493 for ( i = list->count - 1; value == NULL && i >= 0; i -- )
494 if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
495 value = list->value[ i ];
497 mlt_properties_unlock( self );
502 /** Add a new property.
504 * \private \memberof mlt_properties_s
505 * \param self a properties list
506 * \param name the name of the new property
507 * \return the new property
510 static mlt_property mlt_properties_add( mlt_properties self, const char *name )
512 property_list *list = self->local;
513 int key = generate_hash( name );
516 mlt_properties_lock( self );
518 // Check that we have space and resize if necessary
519 if ( list->count == list->size )
522 list->name = realloc( list->name, list->size * sizeof( const char * ) );
523 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
526 // Assign name/value pair
527 list->name[ list->count ] = strdup( name );
528 list->value[ list->count ] = mlt_property_init( );
530 // Assign to hash table
531 if ( list->hash[ key ] == 0 )
532 list->hash[ key ] = list->count + 1;
534 // Return and increment count accordingly
535 result = list->value[ list->count ++ ];
537 mlt_properties_unlock( self );
542 /** Fetch a property by name and add one if not found.
544 * \private \memberof mlt_properties_s
545 * \param self a properties list
546 * \param name the property to lookup or add
547 * \return the property
550 static mlt_property mlt_properties_fetch( mlt_properties self, const char *name )
552 // Try to find an existing property first
553 mlt_property property = mlt_properties_find( self, name );
555 // If it wasn't found, create one
556 if ( property == NULL )
557 property = mlt_properties_add( self, name );
559 // Return the property
563 /** Copy a property to another properties list.
565 * \public \memberof mlt_properties_s
566 * \author Zach <zachary.drew@gmail.com>
567 * \param self the properties to copy to
568 * \param that the properties to copy from
569 * \param name the name of the property to copy
572 void mlt_properties_pass_property( mlt_properties self, mlt_properties that, const char *name )
574 // Make sure the source property isn't null.
575 mlt_property that_prop = mlt_properties_find( that, name );
576 if( that_prop == NULL )
579 mlt_property_pass( mlt_properties_fetch( self, name ), that_prop );
582 /** Copy all properties specified in a comma-separated list to another properties list.
584 * White space is also a delimiter.
585 * \public \memberof mlt_properties_s
586 * \author Zach <zachary.drew@gmail.com>
587 * \param self the properties to copy to
588 * \param that the properties to copy from
589 * \param list a delimited list of property names
594 int mlt_properties_pass_list( mlt_properties self, mlt_properties that, const char *list )
596 char *props = strdup( list );
598 const char *delim = " ,\t\n"; // Any combination of spaces, commas, tabs, and newlines
603 count = strcspn( ptr, delim );
605 if( ptr[count] == '\0' )
608 ptr[count] = '\0'; // Make it a real string
610 mlt_properties_pass_property( self, that, ptr );
613 ptr += strspn( ptr, delim );
622 /** Set a property to a string.
624 * The property name "properties" is reserved to load the preset in \p value.
625 * When the value begins with '@' then it is interpreted as a very simple math
626 * expression containing only the +, -, *, and / operators.
627 * The event "property-changed" is fired after the property has been set.
629 * This makes a copy of the string value you supply.
630 * \public \memberof mlt_properties_s
631 * \param self a properties list
632 * \param name the property to set
633 * \param value the property's new value
634 * \return true if error
637 int mlt_properties_set( mlt_properties self, const char *name, const char *value )
641 // Fetch the property to work with
642 mlt_property property = mlt_properties_fetch( self, name );
644 // Set it if not NULL
645 if ( property == NULL )
647 mlt_log( NULL, MLT_LOG_FATAL, "Whoops - %s not found (should never occur)\n", name );
649 else if ( value == NULL )
651 error = mlt_property_set_string( property, value );
652 mlt_properties_do_mirror( self, name );
654 else if ( *value != '@' )
656 error = mlt_property_set_string( property, value );
657 mlt_properties_do_mirror( self, name );
658 if ( !strcmp( name, "properties" ) )
659 mlt_properties_preset( self, value );
661 else if ( value[ 0 ] == '@' )
670 while ( *value != '\0' )
672 int length = strcspn( value, "+-*/" );
674 // Get the identifier
675 strncpy( id, value, length );
679 // Determine the value
680 if ( isdigit( id[ 0 ] ) )
681 current = atof( id );
683 current = mlt_properties_get_double( self, id );
685 // Apply the operation
698 total = total / current;
703 op = *value != '\0' ? *value ++ : ' ';
706 error = mlt_property_set_double( property, total );
707 mlt_properties_do_mirror( self, name );
710 mlt_events_fire( self, "property-changed", name, NULL );
715 /** Set or default a property to a string.
717 * This makes a copy of the string value you supply.
718 * \public \memberof mlt_properties_s
719 * \param self a properties list
720 * \param name the property to set
721 * \param value the string value to set or NULL to use the default
722 * \param def the default string if value is NULL
723 * \return true if error
726 int mlt_properties_set_or_default( mlt_properties self, const char *name, const char *value, const char *def )
728 return mlt_properties_set( self, name, value == NULL ? def : value );
731 /** Get a string value by name.
733 * Do not free the returned string. It's lifetime is controlled by the property
734 * and this properties object.
735 * \public \memberof mlt_properties_s
736 * \param self a properties list
737 * \param name the property to get
738 * \return the property's string value or NULL if it does not exist
741 char *mlt_properties_get( mlt_properties self, const char *name )
743 mlt_property value = mlt_properties_find( self, name );
744 property_list *list = self->local;
745 return value == NULL ? NULL : mlt_property_get_string_l( value, list->locale );
748 /** Get a property name by index.
750 * Do not free the returned string.
751 * \public \memberof mlt_properties_s
752 * \param self a properties list
753 * \param index the numeric index of the property
754 * \return the name of the property or NULL if index is out of range
757 char *mlt_properties_get_name( mlt_properties self, int index )
759 property_list *list = self->local;
760 if ( index >= 0 && index < list->count )
761 return list->name[ index ];
765 /** Get a property's string value by index.
767 * Do not free the returned string.
768 * \public \memberof mlt_properties_s
769 * \param self a properties list
770 * \param index the numeric index of the property
771 * \return the property value as a string or NULL if the index is out of range
774 char *mlt_properties_get_value( mlt_properties self, int index )
776 property_list *list = self->local;
777 if ( index >= 0 && index < list->count )
778 return mlt_property_get_string_l( list->value[ index ], list->locale );
782 /** Get a data value by index.
784 * Do not free the returned pointer if you supplied a destructor function when you
786 * \public \memberof mlt_properties_s
787 * \param self a properties list
788 * \param index the numeric index of the property
789 * \param[out] size the size of the binary data in bytes or NULL if the index is out of range
792 void *mlt_properties_get_data_at( mlt_properties self, int index, int *size )
794 property_list *list = self->local;
795 if ( index >= 0 && index < list->count )
796 return mlt_property_get_data( list->value[ index ], size );
800 /** Return the number of items in the list.
802 * \public \memberof mlt_properties_s
803 * \param self a properties list
804 * \return the number of property objects
807 int mlt_properties_count( mlt_properties self )
809 property_list *list = self->local;
813 /** Set a value by parsing a name=value string.
815 * \public \memberof mlt_properties_s
816 * \param self a properties list
817 * \param namevalue a string containing name and value delimited by '='
818 * \return true if there was an error
821 int mlt_properties_parse( mlt_properties self, const char *namevalue )
823 char *name = strdup( namevalue );
826 char *ptr = strchr( name, '=' );
834 value = strdup( ptr );
839 value = strdup( ptr );
840 if ( value != NULL && value[ strlen( value ) - 1 ] == '\"' )
841 value[ strlen( value ) - 1 ] = '\0';
846 value = strdup( "" );
849 error = mlt_properties_set( self, name, value );
857 /** Get an integer associated to the name.
859 * \public \memberof mlt_properties_s
860 * \param self a properties list
861 * \param name the property to get
862 * \return The integer value, 0 if not found (which may also be a legitimate value)
865 int mlt_properties_get_int( mlt_properties self, const char *name )
867 mlt_property value = mlt_properties_find( self, name );
868 return value == NULL ? 0 : mlt_property_get_int( value );
871 /** Set a property to an integer value.
873 * \public \memberof mlt_properties_s
874 * \param self a properties list
875 * \param name the property to set
876 * \param value the integer
877 * \return true if error
880 int mlt_properties_set_int( mlt_properties self, const char *name, int value )
884 // Fetch the property to work with
885 mlt_property property = mlt_properties_fetch( self, name );
887 // Set it if not NULL
888 if ( property != NULL )
890 error = mlt_property_set_int( property, value );
891 mlt_properties_do_mirror( self, name );
894 mlt_events_fire( self, "property-changed", name, NULL );
899 /** Get a 64-bit integer associated to the name.
901 * \public \memberof mlt_properties_s
902 * \param self a properties list
903 * \param name the property to get
904 * \return the integer value, 0 if not found (which may also be a legitimate value)
907 int64_t mlt_properties_get_int64( mlt_properties self, const char *name )
909 mlt_property value = mlt_properties_find( self, name );
910 return value == NULL ? 0 : mlt_property_get_int64( value );
913 /** Set a property to a 64-bit integer value.
915 * \public \memberof mlt_properties_s
916 * \param self a properties list
917 * \param name the property to set
918 * \param value the integer
919 * \return true if error
922 int mlt_properties_set_int64( mlt_properties self, const char *name, int64_t value )
926 // Fetch the property to work with
927 mlt_property property = mlt_properties_fetch( self, name );
929 // Set it if not NULL
930 if ( property != NULL )
932 error = mlt_property_set_int64( property, value );
933 mlt_properties_do_mirror( self, name );
936 mlt_events_fire( self, "property-changed", name, NULL );
941 /** Get a floating point value associated to the name.
943 * \public \memberof mlt_properties_s
944 * \param self a properties list
945 * \param name the property to get
946 * \return the floating point, 0 if not found (which may also be a legitimate value)
949 double mlt_properties_get_double( mlt_properties self, const char *name )
951 mlt_property value = mlt_properties_find( self, name );
952 property_list *list = self->local;
953 return value == NULL ? 0 : mlt_property_get_double_l( value, list->locale );
956 /** Set a property to a floating point value.
958 * \public \memberof mlt_properties_s
959 * \param self a properties list
960 * \param name the property to set
961 * \param value the floating point value
962 * \return true if error
965 int mlt_properties_set_double( mlt_properties self, const char *name, double value )
969 // Fetch the property to work with
970 mlt_property property = mlt_properties_fetch( self, name );
972 // Set it if not NULL
973 if ( property != NULL )
975 error = mlt_property_set_double( property, value );
976 mlt_properties_do_mirror( self, name );
979 mlt_events_fire( self, "property-changed", name, NULL );
984 /** Get a position value associated to the name.
986 * \public \memberof mlt_properties_s
987 * \param self a properties list
988 * \param name the property to get
989 * \return the position, 0 if not found (which may also be a legitimate value)
992 mlt_position mlt_properties_get_position( mlt_properties self, const char *name )
994 mlt_property value = mlt_properties_find( self, name );
995 return value == NULL ? 0 : mlt_property_get_position( value );
998 /** Set a property to a position value.
1000 * \public \memberof mlt_properties_s
1001 * \param self a properties list
1002 * \param name the property to get
1003 * \param value the position
1004 * \return true if error
1007 int mlt_properties_set_position( mlt_properties self, const char *name, mlt_position value )
1011 // Fetch the property to work with
1012 mlt_property property = mlt_properties_fetch( self, name );
1014 // Set it if not NULL
1015 if ( property != NULL )
1017 error = mlt_property_set_position( property, value );
1018 mlt_properties_do_mirror( self, name );
1021 mlt_events_fire( self, "property-changed", name, NULL );
1026 /** Get a binary data value associated to the name.
1028 * Do not free the returned pointer if you supplied a destructor function
1029 * when you set this property.
1030 * \public \memberof mlt_properties_s
1031 * \param self a properties list
1032 * \param name the property to get
1033 * \param[out] length The size of the binary data in bytes, if available (often it is not, you should know)
1036 void *mlt_properties_get_data( mlt_properties self, const char *name, int *length )
1038 mlt_property value = mlt_properties_find( self, name );
1039 return value == NULL ? NULL : mlt_property_get_data( value, length );
1042 /** Store binary data as a property.
1044 * \public \memberof mlt_properties_s
1045 * \param self a properties list
1046 * \param name the property to set
1047 * \param value an opaque pointer to binary data
1048 * \param length the size of the binary data in bytes (optional)
1049 * \param destroy a function to deallocate the binary data when the property is closed (optional)
1050 * \param serialise a function that can serialize the binary data as text (optional)
1051 * \return true if error
1054 int mlt_properties_set_data( mlt_properties self, const char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
1058 // Fetch the property to work with
1059 mlt_property property = mlt_properties_fetch( self, name );
1061 // Set it if not NULL
1062 if ( property != NULL )
1063 error = mlt_property_set_data( property, value, length, destroy, serialise );
1065 mlt_events_fire( self, "property-changed", name, NULL );
1070 /** Rename a property.
1072 * \public \memberof mlt_properties_s
1073 * \param self a properties list
1074 * \param source the property to rename
1075 * \param dest the new name
1076 * \return true if the name is already in use
1079 int mlt_properties_rename( mlt_properties self, const char *source, const char *dest )
1081 mlt_property value = mlt_properties_find( self, dest );
1083 if ( value == NULL )
1085 property_list *list = self->local;
1089 mlt_properties_lock( self );
1090 for ( i = 0; i < list->count; i ++ )
1092 if ( !strcmp( list->name[ i ], source ) )
1094 free( list->name[ i ] );
1095 list->name[ i ] = strdup( dest );
1096 list->hash[ generate_hash( dest ) ] = i + 1;
1100 mlt_properties_unlock( self );
1103 return value != NULL;
1106 /** Dump the properties to a file handle.
1108 * \public \memberof mlt_properties_s
1109 * \param self a properties list
1110 * \param output a file handle
1113 void mlt_properties_dump( mlt_properties self, FILE *output )
1115 property_list *list = self->local;
1117 for ( i = 0; i < list->count; i ++ )
1118 if ( mlt_properties_get( self, list->name[ i ] ) != NULL )
1119 fprintf( output, "%s=%s\n", list->name[ i ], mlt_properties_get( self, list->name[ i ] ) );
1122 /** Output the properties to a file handle.
1124 * This version includes reference counts and does not put each property on a new line.
1125 * \public \memberof mlt_properties_s
1126 * \param self a properties pointer
1127 * \param title a string to preface the output
1128 * \param output a file handle
1130 void mlt_properties_debug( mlt_properties self, const char *title, FILE *output )
1132 if ( output == NULL ) output = stderr;
1133 fprintf( output, "%s: ", title );
1136 property_list *list = self->local;
1138 fprintf( output, "[ ref=%d", list->ref_count );
1139 for ( i = 0; i < list->count; i ++ )
1140 if ( mlt_properties_get( self, list->name[ i ] ) != NULL )
1141 fprintf( output, ", %s=%s", list->name[ i ], mlt_properties_get( self, list->name[ i ] ) );
1143 fprintf( output, ", %s=%p", list->name[ i ], mlt_properties_get_data( self, list->name[ i ], NULL ) );
1144 fprintf( output, " ]" );
1146 fprintf( output, "\n" );
1149 /** Save the properties to a file by name.
1151 * This uses the dump format - one line per property.
1152 * \public \memberof mlt_properties_s
1153 * \param self a properties list
1154 * \param filename the name of a file to create or overwrite
1155 * \return true if there was an error
1158 int mlt_properties_save( mlt_properties self, const char *filename )
1161 FILE *f = fopen( filename, "w" );
1164 mlt_properties_dump( self, f );
1171 /* This is a very basic cross platform fnmatch replacement - it will fail in
1172 * many cases, but for the basic *.XXX and YYY*.XXX, it will work ok.
1175 /** Test whether a filename or pathname matches a shell-style pattern.
1177 * \private \memberof mlt_properties_s
1178 * \param wild a string containing a wildcard pattern
1179 * \param file the name of a file to test against
1180 * \return true if the file name matches the wildcard pattern
1183 static int mlt_fnmatch( const char *wild, const char *file )
1188 while( f < strlen( file ) && w < strlen( wild ) )
1190 if ( wild[ w ] == '*' )
1193 if ( w == strlen( wild ) )
1195 while ( f != strlen( file ) && tolower( file[ f ] ) != tolower( wild[ w ] ) )
1198 else if ( wild[ w ] == '?' || tolower( file[ f ] ) == tolower( wild[ w ] ) )
1203 else if ( wild[ 0 ] == '*' )
1213 return strlen( file ) == f && strlen( wild ) == w;
1216 /** Compare the string or serialized value of two properties.
1218 * \private \memberof mlt_properties_s
1219 * \param self a property
1220 * \param that a property
1221 * \return < 0 if \p self less than \p that, 0 if equal, or > 0 if \p self is greater than \p that
1224 static int mlt_compare( const void *self, const void *that )
1226 return strcmp( mlt_property_get_string( *( const mlt_property * )self ), mlt_property_get_string( *( const mlt_property * )that ) );
1229 /** Get the contents of a directory.
1231 * Obtains an optionally sorted list of the files found in a directory with a specific wild card.
1232 * Entries in the list have a numeric name (running from 0 to count - 1). Only values change
1233 * position if sort is enabled. Designed to be posix compatible (linux, os/x, mingw etc).
1234 * \public \memberof mlt_properties_s
1235 * \param self a properties list
1236 * \param dirname the name of the directory
1237 * \param pattern a wildcard pattern to filter the directory listing
1238 * \param sort Do you want to sort the directory listing?
1239 * \return the number of items in the directory listing
1242 int mlt_properties_dir_list( mlt_properties self, const char *dirname, const char *pattern, int sort )
1244 DIR *dir = opendir( dirname );
1249 struct dirent *de = readdir( dir );
1250 char fullname[ 1024 ];
1253 sprintf( key, "%d", mlt_properties_count( self ) );
1254 snprintf( fullname, 1024, "%s/%s", dirname, de->d_name );
1255 if ( pattern == NULL )
1256 mlt_properties_set( self, key, fullname );
1257 else if ( de->d_name[ 0 ] != '.' && mlt_fnmatch( pattern, de->d_name ) )
1258 mlt_properties_set( self, key, fullname );
1259 de = readdir( dir );
1265 if ( sort && mlt_properties_count( self ) )
1267 property_list *list = self->local;
1268 mlt_properties_lock( self );
1269 qsort( list->value, mlt_properties_count( self ), sizeof( mlt_property ), mlt_compare );
1270 mlt_properties_unlock( self );
1273 return mlt_properties_count( self );
1276 /** Close a properties object.
1278 * Deallocates the properties object and everything it contains.
1279 * \public \memberof mlt_properties_s
1280 * \param self a properties object
1283 void mlt_properties_close( mlt_properties self )
1285 if ( self != NULL && mlt_properties_dec_ref( self ) <= 0 )
1287 if ( self->close != NULL )
1289 self->close( self->close_object );
1293 property_list *list = self->local;
1296 #if _MLT_PROPERTY_CHECKS_ == 1
1298 mlt_properties_debug( self, "Closing", stderr );
1301 #ifdef _MLT_PROPERTY_CHECKS_
1302 // Increment destroyed count
1303 properties_destroyed ++;
1305 // Show current stats - these should match when the app is closed
1306 mlt_log( NULL, MLT_LOG_DEBUG, "Created %d, destroyed %d\n", properties_created, properties_destroyed );
1309 // Clean up names and values
1310 for ( index = list->count - 1; index >= 0; index -- )
1312 mlt_property_close( list->value[ index ] );
1313 free( list->name[ index ] );
1318 freelocale( list->locale );
1320 // Clear up the list
1321 pthread_mutex_destroy( &list->mutex );
1323 free( list->value );
1326 // Free self now if self has no child
1327 if ( self->child == NULL )
1333 /** Determine if the properties list is really just a sequence or ordered list.
1335 * \public \memberof mlt_properties_s
1336 * \param properties a properties list
1337 * \return true if all of the property names are numeric (a sequence)
1340 int mlt_properties_is_sequence( mlt_properties properties )
1343 int n = mlt_properties_count( properties );
1344 for ( i = 0; i < n; i++ )
1345 if ( ! isdigit( mlt_properties_get_name( properties, i )[0] ) )
1350 /** \brief YAML Tiny Parser context structure
1352 * YAML is a nifty text format popular in the Ruby world as a cleaner,
1353 * less verbose alternative to XML. See this Wikipedia topic for an overview:
1354 * http://en.wikipedia.org/wiki/YAML
1355 * The YAML specification is at:
1357 * YAML::Tiny is a Perl module that specifies a subset of YAML that we are
1358 * using here (for the same reasons):
1359 * http://search.cpan.org/~adamk/YAML-Tiny-1.25/lib/YAML/Tiny.pm
1363 struct yaml_parser_context
1370 unsigned int block_indent;
1373 typedef struct yaml_parser_context *yaml_parser;
1375 /** Remove spaces from the left side of a string.
1377 * \param s the string to trim
1378 * \return the number of characters removed
1381 static unsigned int ltrim( char **s )
1385 int n = strlen( c );
1386 for ( i = 0; i < n && *c == ' '; i++, c++ );
1391 /** Remove spaces from the right side of a string.
1393 * \param s the string to trim
1394 * \return the number of characters removed
1397 static unsigned int rtrim( char *s )
1399 int n = strlen( s );
1401 for ( i = n; i > 0 && s[i - 1] == ' '; --i )
1406 /** Parse a line of YAML Tiny.
1408 * Adds a property if needed.
1409 * \private \memberof yaml_parser_context
1410 * \param context a YAML Tiny Parser context
1411 * \param namevalue a line of YAML Tiny
1412 * \return true if there was an error
1415 static int parse_yaml( yaml_parser context, const char *namevalue )
1417 char *name_ = strdup( namevalue );
1421 char *ptr = strchr( name, ':' );
1422 unsigned int indent = ltrim( &name );
1423 mlt_properties properties = mlt_deque_peek_front( context->stack );
1425 // Ascending one more levels in the tree
1426 if ( indent < context->level )
1429 unsigned int n = ( context->level - indent ) / 2;
1430 for ( i = 0; i < n; i++ )
1431 mlt_deque_pop_front( context->stack );
1432 properties = mlt_deque_peek_front( context->stack );
1433 context->level = indent;
1436 // Descending a level in the tree
1437 else if ( indent > context->level && context->block == 0 )
1439 context->level = indent;
1442 // If there is a colon that is not part of a block
1443 if ( ptr && ( indent == context->level ) )
1445 // Reset block processing
1446 if ( context->block_name )
1448 free( context->block_name );
1449 context->block_name = NULL;
1453 // Terminate the name and setup the value pointer
1457 char *comment = strchr( ptr, '#' );
1463 // Trim leading and trailing spaces from bare value
1467 // No value means a child
1468 if ( strcmp( ptr, "" ) == 0 )
1470 mlt_properties child = mlt_properties_new();
1471 mlt_properties_set_data( properties, name, child, 0,
1472 ( mlt_destructor )mlt_properties_close, NULL );
1473 mlt_deque_push_front( context->stack, child );
1479 // A dash indicates a sequence item
1480 if ( name[0] == '-' )
1482 mlt_properties child = mlt_properties_new();
1485 snprintf( key, sizeof(key), "%d", context->index++ );
1486 mlt_properties_set_data( properties, key, child, 0,
1487 ( mlt_destructor )mlt_properties_close, NULL );
1488 mlt_deque_push_front( context->stack, child );
1491 context->level += ltrim( &name ) + 1;
1499 value = strdup( ptr );
1500 if ( value && value[ strlen( value ) - 1 ] == '\"' )
1501 value[ strlen( value ) - 1 ] = 0;
1504 // Value is folded or unfolded block
1505 else if ( *ptr == '|' || *ptr == '>' )
1507 context->block = *ptr;
1508 context->block_name = strdup( name );
1509 context->block_indent = 0;
1510 value = strdup( "" );
1516 value = strdup( ptr );
1520 // A list of scalars
1521 else if ( name[0] == '-' )
1523 // Reset block processing
1524 if ( context->block_name )
1526 free( context->block_name );
1527 context->block_name = NULL;
1533 snprintf( key, sizeof(key), "%d", context->index++ );
1537 char *comment = strchr( ptr, '#' );
1541 // Trim leading and trailing spaces from bare value
1549 value = strdup( ptr );
1550 if ( value && value[ strlen( value ) - 1 ] == '\"' )
1551 value[ strlen( value ) - 1 ] = 0;
1554 // Value is folded or unfolded block
1555 else if ( *ptr == '|' || *ptr == '>' )
1557 context->block = *ptr;
1558 context->block_name = strdup( key );
1559 context->block_indent = 0;
1560 value = strdup( "" );
1566 value = strdup( ptr );
1570 name = name_ = strdup( key );
1574 else if ( context->block == '|' )
1576 if ( context->block_indent == 0 )
1577 context->block_indent = indent;
1578 if ( indent > context->block_indent )
1579 name = &name_[ context->block_indent ];
1581 char *old_value = mlt_properties_get( properties, context->block_name );
1582 value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
1583 strcpy( value, old_value );
1584 if ( strcmp( old_value, "" ) )
1585 strcat( value, "\n" );
1586 strcat( value, name );
1587 name = context->block_name;
1591 else if ( context->block == '>' )
1595 char *old_value = mlt_properties_get( properties, context->block_name );
1597 // Blank line (prepended with spaces) is new line
1598 if ( strcmp( name, "" ) == 0 )
1600 value = calloc( 1, strlen( old_value ) + 2 );
1601 strcat( value, old_value );
1602 strcat( value, "\n" );
1604 // Concatenate with space
1607 value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
1608 strcat( value, old_value );
1609 if ( strcmp( old_value, "" ) && old_value[ strlen( old_value ) - 1 ] != '\n' )
1610 strcat( value, " " );
1611 strcat( value, name );
1613 name = context->block_name;
1618 value = strdup( "" );
1621 error = mlt_properties_set( properties, name, value );
1629 /** Parse a YAML Tiny file by name.
1631 * \public \memberof mlt_properties_s
1632 * \param filename the name of a text file containing YAML Tiny
1633 * \return a new properties list
1636 mlt_properties mlt_properties_parse_yaml( const char *filename )
1638 // Construct a standalone properties object
1639 mlt_properties self = mlt_properties_new( );
1644 FILE *file = fopen( filename, "r" );
1646 // Load contents of file
1651 char *ptemp = &temp[ 0 ];
1654 yaml_parser context = calloc( 1, sizeof( struct yaml_parser_context ) );
1655 context->stack = mlt_deque_init();
1656 mlt_deque_push_front( context->stack, self );
1658 // Read each string from the file
1659 while( fgets( temp, 1024, file ) )
1661 // Check for end-of-stream
1662 if ( strncmp( ptemp, "...", 3 ) == 0 )
1666 temp[ strlen( temp ) - 1 ] = '\0';
1668 // Skip blank lines, comment lines, and document separator
1669 if ( strcmp( ptemp, "" ) && ptemp[ 0 ] != '#' && strncmp( ptemp, "---", 3 )
1670 && strncmp( ptemp, "%YAML", 5 ) && strncmp( ptemp, "% YAML", 6 ) )
1671 parse_yaml( context, temp );
1676 mlt_deque_close( context->stack );
1677 if ( context->block_name )
1678 free( context->block_name );
1683 // Return the pointer
1688 * YAML Tiny Serializer
1691 /** How many bytes to grow at a time */
1692 #define STRBUF_GROWTH (1024)
1694 /** \brief Private to mlt_properties_s, a self-growing buffer for building strings
1704 typedef struct strbuf_s *strbuf;
1706 /** Create a new string buffer
1708 * \private \memberof strbuf_s
1709 * \return a new string buffer
1712 static strbuf strbuf_new( )
1714 strbuf buffer = calloc( 1, sizeof( struct strbuf_s ) );
1715 buffer->size = STRBUF_GROWTH;
1716 buffer->string = calloc( 1, buffer->size );
1720 /** Destroy a string buffer
1722 * \private \memberof strbuf_s
1723 * \param buffer the string buffer to close
1726 static void strbuf_close( strbuf buffer )
1728 // We do not free buffer->string; strbuf user must save that pointer
1734 /** Format a string into a string buffer
1736 * A variable number of arguments follows the format string - one for each
1738 * \private \memberof strbuf_s
1739 * \param buffer the string buffer to write into
1740 * \param format a string that contains text and formatting instructions
1741 * \return the formatted string
1744 static char *strbuf_printf( strbuf buffer, const char *format, ... )
1746 while ( buffer->string )
1749 va_start( ap, format );
1750 size_t len = strlen( buffer->string );
1751 size_t remain = buffer->size - len - 1;
1752 int need = vsnprintf( buffer->string + len, remain, format, ap );
1754 if ( need > -1 && need < remain )
1756 buffer->string[ len ] = 0;
1757 buffer->size += need + STRBUF_GROWTH;
1758 buffer->string = realloc( buffer->string, buffer->size );
1760 return buffer->string;
1763 /** Indent a line of YAML Tiny.
1765 * \private \memberof strbuf_s
1766 * \param output a string buffer
1767 * \param indent the number of spaces to indent
1770 static inline void indent_yaml( strbuf output, int indent )
1773 for ( j = 0; j < indent; j++ )
1774 strbuf_printf( output, " " );
1777 /** Convert a line string into a YAML block literal.
1779 * \private \memberof strbuf_s
1780 * \param output a string buffer
1781 * \param value the string to format as a block literal
1782 * \param indent the number of spaces to indent
1785 static void output_yaml_block_literal( strbuf output, const char *value, int indent )
1787 char *v = strdup( value );
1789 char *eol = strchr( sol, '\n' );
1793 indent_yaml( output, indent );
1795 strbuf_printf( output, "%s\n", sol );
1797 eol = strchr( sol, '\n' );
1799 indent_yaml( output, indent );
1800 strbuf_printf( output, "%s\n", sol );
1803 /** Recursively serialize a properties list into a string buffer as YAML Tiny.
1805 * \private \memberof mlt_properties_s
1806 * \param self a properties list
1807 * \param output a string buffer to hold the serialized YAML Tiny
1808 * \param indent the number of spaces to indent (for recursion, initialize to 0)
1809 * \param is_parent_sequence Is this properties list really just a sequence (for recursion, initialize to 0)?
1812 static void serialise_yaml( mlt_properties self, strbuf output, int indent, int is_parent_sequence )
1814 property_list *list = self->local;
1817 for ( i = 0; i < list->count; i ++ )
1819 // This implementation assumes that all data elements are property lists.
1820 // Unfortunately, we do not have run time type identification.
1821 mlt_properties child = mlt_property_get_data( list->value[ i ], NULL );
1823 if ( mlt_properties_is_sequence( self ) )
1825 // Ignore hidden/non-serialisable items
1826 if ( list->name[ i ][ 0 ] != '_' )
1828 // Indicate a sequence item
1829 indent_yaml( output, indent );
1830 strbuf_printf( output, "- " );
1832 // If the value can be represented as a string
1833 const char *value = mlt_properties_get( self, list->name[ i ] );
1834 if ( value && strcmp( value, "" ) )
1836 // Determine if this is an unfolded block literal
1837 if ( strchr( value, '\n' ) )
1839 strbuf_printf( output, "|\n" );
1840 output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( "|" ) );
1844 strbuf_printf( output, "%s\n", value );
1850 serialise_yaml( child, output, indent + 2, 1 );
1854 // Assume this is a normal map-oriented properties list
1855 const char *value = mlt_properties_get( self, list->name[ i ] );
1857 // Ignore hidden/non-serialisable items
1858 // If the value can be represented as a string
1859 if ( list->name[ i ][ 0 ] != '_' && value && strcmp( value, "" ) )
1861 if ( is_parent_sequence == 0 )
1862 indent_yaml( output, indent );
1864 is_parent_sequence = 0;
1866 // Determine if this is an unfolded block literal
1867 if ( strchr( value, '\n' ) )
1869 strbuf_printf( output, "%s: |\n", list->name[ i ] );
1870 output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( ": " ) );
1874 strbuf_printf( output, "%s: %s\n", list->name[ i ], value );
1878 // Output a child as a map item
1881 indent_yaml( output, indent );
1882 strbuf_printf( output, "%s:\n", list->name[ i ] );
1885 serialise_yaml( child, output, indent + 2, 0 );
1891 /** Serialize a properties list as a string of YAML Tiny.
1893 * The caller MUST free the returned string!
1894 * This operates on properties containing properties as a hierarchical data
1896 * \public \memberof mlt_properties_s
1897 * \param self a properties list
1898 * \return a string containing YAML Tiny that represents the properties list
1901 char *mlt_properties_serialise_yaml( mlt_properties self )
1903 strbuf b = strbuf_new();
1904 strbuf_printf( b, "---\n" );
1905 serialise_yaml( self, b, 0, 0 );
1906 strbuf_printf( b, "...\n" );
1907 char *ret = b->string;
1912 /** Protect a properties list against concurrent access.
1914 * \public \memberof mlt_properties_s
1915 * \param self a properties list
1918 void mlt_properties_lock( mlt_properties self )
1921 pthread_mutex_lock( &( ( property_list* )( self->local ) )->mutex );
1924 /** End protecting a properties list against concurrent access.
1926 * \public \memberof mlt_properties_s
1927 * \param self a properties list
1930 void mlt_properties_unlock( mlt_properties self )
1933 pthread_mutex_unlock( &( ( property_list* )( self->local ) )->mutex );