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>
42 /** \brief private implementation of the property list */
51 mlt_properties mirror;
53 pthread_mutex_t mutex;
57 /* Memory leak checks */
59 //#define _MLT_PROPERTY_CHECKS_ 2
60 #ifdef _MLT_PROPERTY_CHECKS_
61 static int properties_created = 0;
62 static int properties_destroyed = 0;
65 /** Initialize a properties object that was already allocated.
67 * This does allocate its ::property_list, and it adds a reference count.
68 * \public \memberof mlt_properties_s
69 * \param self the properties structure to initialize
70 * \param child an opaque pointer to a subclass object
71 * \return true if failed
74 int mlt_properties_init( mlt_properties self, void *child )
78 #ifdef _MLT_PROPERTY_CHECKS_
79 // Increment number of properties created
80 properties_created ++;
84 memset( self, 0, sizeof( struct mlt_properties_s ) );
86 // Assign the child of the object
89 // Allocate the local structure
90 self->local = calloc( sizeof( property_list ), 1 );
92 // Increment the ref count
93 ( ( property_list * )self->local )->ref_count = 1;
94 pthread_mutex_init( &( ( property_list * )self->local )->mutex, NULL );;
97 // Check that initialisation was successful
98 return self != NULL && self->local == NULL;
101 /** Create a properties object.
103 * This allocates the properties structure and calls mlt_properties_init() on it.
104 * Free the properties object with mlt_properties_close().
105 * \public \memberof mlt_properties_s
106 * \return a new properties object
109 mlt_properties mlt_properties_new( )
111 // Construct a standalone properties object
112 mlt_properties self = calloc( sizeof( struct mlt_properties_s ), 1 );
115 mlt_properties_init( self, NULL );
117 // Return the pointer
121 static int load_properties( mlt_properties self, const char *filename )
124 FILE *file = fopen( filename, "r" );
126 // Load contents of file
131 char last[ 1024 ] = "";
133 // Read each string from the file
134 while( fgets( temp, 1024, file ) )
137 temp[ strlen( temp ) - 1 ] = '\0';
139 // Check if the line starts with a .
140 if ( temp[ 0 ] == '.' )
143 sprintf( temp2, "%s%s", last, temp );
144 strcpy( temp, temp2 );
146 else if ( strchr( temp, '=' ) )
148 strcpy( last, temp );
149 *( strchr( last, '=' ) ) = '\0';
152 // Parse and set the property
153 if ( strcmp( temp, "" ) && temp[ 0 ] != '#' )
154 mlt_properties_parse( self, temp );
160 return file? 0 : errno;
163 /** Create a properties object by reading a .properties text file.
165 * Free the properties object with mlt_properties_close().
166 * \deprecated Please start using mlt_properties_parse_yaml().
167 * \public \memberof mlt_properties_s
168 * \param filename the absolute file name
169 * \return a new properties object
172 mlt_properties mlt_properties_load( const char *filename )
174 // Construct a standalone properties object
175 mlt_properties self = mlt_properties_new( );
178 load_properties( self, filename );
180 // Return the pointer
184 /** Set properties from a preset.
186 * Presets are typically installed to $prefix/share/mlt/presets/{type}/{service}/[{profile}/]{name}.
187 * For example, "/usr/share/mlt/presets/consumer/avformat/dv_ntsc_wide/DVD"
188 * could be an encoding preset for a widescreen NTSC DVD Video.
189 * Do not specify the type and service in the preset name parameter; these are
190 * inferred automatically from the service to which you are applying the preset.
191 * Using the example above and assuming you are calling this function on the
192 * avformat consumer, the name passed to the function should simply be DVD.
193 * Note that the profile portion of the path is optional, but a profile-specific
194 * preset with the same name as a more generic one is given a higher priority.
195 * \todo Look in a user-specific location - somewhere in the home directory.
197 * \public \memberof mlt_properties_s
198 * \param self a properties list
199 * \param name the name of a preset in a well-known location or the explicit path
200 * \return true if error
203 int mlt_properties_preset( mlt_properties self, const char *name )
205 struct stat stat_buff;
208 if ( !( self && name && strlen( name ) ) )
211 // See if name is an explicit file
212 if ( ! stat( name, &stat_buff ) )
214 return load_properties( self, name );
218 // Look for profile-specific preset before a generic one.
219 char *data = getenv( "MLT_PRESETS_PATH" );
220 const char *type = mlt_properties_get( self, "mlt_type" );
221 const char *service = mlt_properties_get( self, "mlt_service" );
222 const char *profile = mlt_environment( "MLT_PROFILE" );
227 data = strdup( data );
231 data = malloc( strlen( mlt_environment( "MLT_DATA" ) ) + 9 );
232 strcpy( data, mlt_environment( "MLT_DATA" ) );
233 strcat( data, "/presets" );
235 if ( data && type && service )
237 char *path = malloc( 5 + strlen(name) + strlen(data) + strlen(type) + strlen(service) + ( profile? strlen(profile) : 0 ) );
238 sprintf( path, "%s/%s/%s/%s/%s", data, type, service, profile, name );
239 if ( load_properties( self, path ) )
241 sprintf( path, "%s/%s/%s/%s", data, type, service, name );
242 error = load_properties( self, path );
255 /** Generate a hash key.
257 * \private \memberof mlt_properties_s
258 * \param name a string
262 static inline int generate_hash( const char *name )
267 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
271 /** Copy a serializable property to a properties list that is mirroring this one.
273 * Special case - when a container (such as loader) is protecting another
274 * producer, we need to ensure that properties are passed through to the
276 * \private \memberof mlt_properties_s
277 * \param self a properties list
278 * \param name the name of the property to copy
281 static inline void mlt_properties_do_mirror( mlt_properties self, const char *name )
283 property_list *list = self->local;
284 if ( list->mirror != NULL )
286 char *value = mlt_properties_get( self, name );
288 mlt_properties_set( list->mirror, name, value );
292 /** Increment the reference count.
294 * \public \memberof mlt_properties_s
295 * \param self a properties list
296 * \return the new reference count
299 int mlt_properties_inc_ref( mlt_properties self )
304 property_list *list = self->local;
305 pthread_mutex_lock( &list->mutex );
306 result = ++ list->ref_count;
307 pthread_mutex_unlock( &list->mutex );
312 /** Decrement the reference count.
314 * \public \memberof mlt_properties_s
315 * \param self a properties list
316 * \return the new reference count
319 int mlt_properties_dec_ref( mlt_properties self )
324 property_list *list = self->local;
325 pthread_mutex_lock( &list->mutex );
326 result = -- list->ref_count;
327 pthread_mutex_unlock( &list->mutex );
332 /** Get the reference count.
334 * \public \memberof mlt_properties_s
335 * \param self a properties list
336 * \return the current reference count
339 int mlt_properties_ref_count( mlt_properties self )
343 property_list *list = self->local;
344 return list->ref_count;
349 /** Set a properties list to be a mirror copy of another.
351 * Note that this does not copy all existing properties. Rather, you must
352 * call this before setting the properties that you wish to copy.
353 * \public \memberof mlt_properties_s
354 * \param that the properties which will receive copies of the properties as they are set.
355 * \param self the properties to mirror
358 void mlt_properties_mirror( mlt_properties self, mlt_properties that )
360 property_list *list = self->local;
364 /** Copy all serializable properties to another properties list.
366 * \public \memberof mlt_properties_s
367 * \param self The properties to copy to
368 * \param that The properties to copy from
372 int mlt_properties_inherit( mlt_properties self, mlt_properties that )
374 int count = mlt_properties_count( that );
376 for ( i = 0; i < count; i ++ )
378 char *value = mlt_properties_get_value( that, i );
381 char *name = mlt_properties_get_name( that, i );
382 mlt_properties_set( self, name, value );
388 /** Pass all serializable properties that match a prefix to another properties object
390 * \public \memberof mlt_properties_s
391 * \param self the properties to copy to
392 * \param that The properties to copy from
393 * \param prefix the property names to match (required)
397 int mlt_properties_pass( mlt_properties self, mlt_properties that, const char *prefix )
399 int count = mlt_properties_count( that );
400 int length = strlen( prefix );
402 for ( i = 0; i < count; i ++ )
404 char *name = mlt_properties_get_name( that, i );
405 if ( !strncmp( name, prefix, length ) )
407 char *value = mlt_properties_get_value( that, i );
409 mlt_properties_set( self, name + length, value );
415 /** Locate a property by name.
417 * \private \memberof mlt_properties_s
418 * \param self a properties list
419 * \param name the property to lookup by name
420 * \return the property or NULL for failure
423 static inline mlt_property mlt_properties_find( mlt_properties self, const char *name )
425 property_list *list = self->local;
426 mlt_property value = NULL;
427 int key = generate_hash( name );
429 mlt_properties_lock( self );
431 int i = list->hash[ key ] - 1;
434 // Check if we're hashed
435 if ( list->count > 0 &&
436 name[ 0 ] == list->name[ i ][ 0 ] &&
437 !strcmp( list->name[ i ], name ) )
438 value = list->value[ i ];
441 for ( i = list->count - 1; value == NULL && i >= 0; i -- )
442 if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
443 value = list->value[ i ];
445 mlt_properties_unlock( self );
450 /** Add a new property.
452 * \private \memberof mlt_properties_s
453 * \param self a properties list
454 * \param name the name of the new property
455 * \return the new property
458 static mlt_property mlt_properties_add( mlt_properties self, const char *name )
460 property_list *list = self->local;
461 int key = generate_hash( name );
464 mlt_properties_lock( self );
466 // Check that we have space and resize if necessary
467 if ( list->count == list->size )
470 list->name = realloc( list->name, list->size * sizeof( const char * ) );
471 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
474 // Assign name/value pair
475 list->name[ list->count ] = strdup( name );
476 list->value[ list->count ] = mlt_property_init( );
478 // Assign to hash table
479 if ( list->hash[ key ] == 0 )
480 list->hash[ key ] = list->count + 1;
482 // Return and increment count accordingly
483 result = list->value[ list->count ++ ];
485 mlt_properties_unlock( self );
490 /** Fetch a property by name and add one if not found.
492 * \private \memberof mlt_properties_s
493 * \param self a properties list
494 * \param name the property to lookup or add
495 * \return the property
498 static mlt_property mlt_properties_fetch( mlt_properties self, const char *name )
500 // Try to find an existing property first
501 mlt_property property = mlt_properties_find( self, name );
503 // If it wasn't found, create one
504 if ( property == NULL )
505 property = mlt_properties_add( self, name );
507 // Return the property
511 /** Copy a property to another properties list.
513 * \public \memberof mlt_properties_s
514 * \author Zach <zachary.drew@gmail.com>
515 * \param self the properties to copy to
516 * \param that the properties to copy from
517 * \param name the name of the property to copy
520 void mlt_properties_pass_property( mlt_properties self, mlt_properties that, const char *name )
522 // Make sure the source property isn't null.
523 mlt_property that_prop = mlt_properties_find( that, name );
524 if( that_prop == NULL )
527 mlt_property_pass( mlt_properties_fetch( self, name ), that_prop );
530 /** Copy all properties specified in a comma-separated list to another properties list.
532 * White space is also a delimiter.
533 * \public \memberof mlt_properties_s
534 * \author Zach <zachary.drew@gmail.com>
535 * \param self the properties to copy to
536 * \param that the properties to copy from
537 * \param list a delimited list of property names
542 int mlt_properties_pass_list( mlt_properties self, mlt_properties that, const char *list )
544 char *props = strdup( list );
546 const char *delim = " ,\t\n"; // Any combination of spaces, commas, tabs, and newlines
551 count = strcspn( ptr, delim );
553 if( ptr[count] == '\0' )
556 ptr[count] = '\0'; // Make it a real string
558 mlt_properties_pass_property( self, that, ptr );
561 ptr += strspn( ptr, delim );
570 /** Set a property to a string.
572 * The property name "properties" is reserved to load the preset in \p value.
573 * When the value begins with '@' then it is interpreted as a very simple math
574 * expression containing only the +, -, *, and / operators.
575 * The event "property-changed" is fired after the property has been set.
577 * This makes a copy of the string value you supply.
578 * \public \memberof mlt_properties_s
579 * \param self a properties list
580 * \param name the property to set
581 * \param value the property's new value
582 * \return true if error
585 int mlt_properties_set( mlt_properties self, const char *name, const char *value )
589 // Fetch the property to work with
590 mlt_property property = mlt_properties_fetch( self, name );
592 // Set it if not NULL
593 if ( property == NULL )
595 mlt_log( NULL, MLT_LOG_FATAL, "Whoops - %s not found (should never occur)\n", name );
597 else if ( value == NULL )
599 error = mlt_property_set_string( property, value );
600 mlt_properties_do_mirror( self, name );
602 else if ( *value != '@' )
604 error = mlt_property_set_string( property, value );
605 mlt_properties_do_mirror( self, name );
606 if ( !strcmp( name, "properties" ) )
607 mlt_properties_preset( self, value );
609 else if ( value[ 0 ] == '@' )
618 while ( *value != '\0' )
620 int length = strcspn( value, "+-*/" );
622 // Get the identifier
623 strncpy( id, value, length );
627 // Determine the value
628 if ( isdigit( id[ 0 ] ) )
629 current = atof( id );
631 current = mlt_properties_get_double( self, id );
633 // Apply the operation
646 total = total / current;
651 op = *value != '\0' ? *value ++ : ' ';
654 error = mlt_property_set_double( property, total );
655 mlt_properties_do_mirror( self, name );
658 mlt_events_fire( self, "property-changed", name, NULL );
663 /** Set or default a property to a string.
665 * This makes a copy of the string value you supply.
666 * \public \memberof mlt_properties_s
667 * \param self a properties list
668 * \param name the property to set
669 * \param value the string value to set or NULL to use the default
670 * \param def the default string if value is NULL
671 * \return true if error
674 int mlt_properties_set_or_default( mlt_properties self, const char *name, const char *value, const char *def )
676 return mlt_properties_set( self, name, value == NULL ? def : value );
679 /** Get a string value by name.
681 * Do not free the returned string. It's lifetime is controlled by the property
682 * and this properties object.
683 * \public \memberof mlt_properties_s
684 * \param self a properties list
685 * \param name the property to get
686 * \return the property's string value or NULL if it does not exist
689 char *mlt_properties_get( mlt_properties self, const char *name )
691 mlt_property value = mlt_properties_find( self, name );
692 return value == NULL ? NULL : mlt_property_get_string( value );
695 /** Get a property name by index.
697 * Do not free the returned string.
698 * \public \memberof mlt_properties_s
699 * \param self a properties list
700 * \param index the numeric index of the property
701 * \return the name of the property or NULL if index is out of range
704 char *mlt_properties_get_name( mlt_properties self, int index )
706 property_list *list = self->local;
707 if ( index >= 0 && index < list->count )
708 return list->name[ index ];
712 /** Get a property's string value by index.
714 * Do not free the returned string.
715 * \public \memberof mlt_properties_s
716 * \param self a properties list
717 * \param index the numeric index of the property
718 * \return the property value as a string or NULL if the index is out of range
721 char *mlt_properties_get_value( mlt_properties self, int index )
723 property_list *list = self->local;
724 if ( index >= 0 && index < list->count )
725 return mlt_property_get_string( list->value[ index ] );
729 /** Get a data value by index.
731 * Do not free the returned pointer if you supplied a destructor function when you
733 * \public \memberof mlt_properties_s
734 * \param self a properties list
735 * \param index the numeric index of the property
736 * \param[out] size the size of the binary data in bytes or NULL if the index is out of range
739 void *mlt_properties_get_data_at( mlt_properties self, int index, int *size )
741 property_list *list = self->local;
742 if ( index >= 0 && index < list->count )
743 return mlt_property_get_data( list->value[ index ], size );
747 /** Return the number of items in the list.
749 * \public \memberof mlt_properties_s
750 * \param self a properties list
751 * \return the number of property objects
754 int mlt_properties_count( mlt_properties self )
756 property_list *list = self->local;
760 /** Set a value by parsing a name=value string.
762 * \public \memberof mlt_properties_s
763 * \param self a properties list
764 * \param namevalue a string containing name and value delimited by '='
765 * \return true if there was an error
768 int mlt_properties_parse( mlt_properties self, const char *namevalue )
770 char *name = strdup( namevalue );
773 char *ptr = strchr( name, '=' );
781 value = strdup( ptr );
786 value = strdup( ptr );
787 if ( value != NULL && value[ strlen( value ) - 1 ] == '\"' )
788 value[ strlen( value ) - 1 ] = '\0';
793 value = strdup( "" );
796 error = mlt_properties_set( self, name, value );
804 /** Get an integer associated to the name.
806 * \public \memberof mlt_properties_s
807 * \param self a properties list
808 * \param name the property to get
809 * \return The integer value, 0 if not found (which may also be a legitimate value)
812 int mlt_properties_get_int( mlt_properties self, const char *name )
814 mlt_property value = mlt_properties_find( self, name );
815 return value == NULL ? 0 : mlt_property_get_int( value );
818 /** Set a property to an integer value.
820 * \public \memberof mlt_properties_s
821 * \param self a properties list
822 * \param name the property to set
823 * \param value the integer
824 * \return true if error
827 int mlt_properties_set_int( mlt_properties self, const char *name, int value )
831 // Fetch the property to work with
832 mlt_property property = mlt_properties_fetch( self, name );
834 // Set it if not NULL
835 if ( property != NULL )
837 error = mlt_property_set_int( property, value );
838 mlt_properties_do_mirror( self, name );
841 mlt_events_fire( self, "property-changed", name, NULL );
846 /** Get a 64-bit integer associated to the name.
848 * \public \memberof mlt_properties_s
849 * \param self a properties list
850 * \param name the property to get
851 * \return the integer value, 0 if not found (which may also be a legitimate value)
854 int64_t mlt_properties_get_int64( mlt_properties self, const char *name )
856 mlt_property value = mlt_properties_find( self, name );
857 return value == NULL ? 0 : mlt_property_get_int64( value );
860 /** Set a property to a 64-bit integer value.
862 * \public \memberof mlt_properties_s
863 * \param self a properties list
864 * \param name the property to set
865 * \param value the integer
866 * \return true if error
869 int mlt_properties_set_int64( mlt_properties self, const char *name, int64_t value )
873 // Fetch the property to work with
874 mlt_property property = mlt_properties_fetch( self, name );
876 // Set it if not NULL
877 if ( property != NULL )
879 error = mlt_property_set_int64( property, value );
880 mlt_properties_do_mirror( self, name );
883 mlt_events_fire( self, "property-changed", name, NULL );
888 /** Get a floating point value associated to the name.
890 * \public \memberof mlt_properties_s
891 * \param self a properties list
892 * \param name the property to get
893 * \return the floating point, 0 if not found (which may also be a legitimate value)
896 double mlt_properties_get_double( mlt_properties self, const char *name )
898 mlt_property value = mlt_properties_find( self, name );
899 return value == NULL ? 0 : mlt_property_get_double( value );
902 /** Set a property to a floating point value.
904 * \public \memberof mlt_properties_s
905 * \param self a properties list
906 * \param name the property to set
907 * \param value the floating point value
908 * \return true if error
911 int mlt_properties_set_double( mlt_properties self, const char *name, double value )
915 // Fetch the property to work with
916 mlt_property property = mlt_properties_fetch( self, name );
918 // Set it if not NULL
919 if ( property != NULL )
921 error = mlt_property_set_double( property, value );
922 mlt_properties_do_mirror( self, name );
925 mlt_events_fire( self, "property-changed", name, NULL );
930 /** Get a position value associated to the name.
932 * \public \memberof mlt_properties_s
933 * \param self a properties list
934 * \param name the property to get
935 * \return the position, 0 if not found (which may also be a legitimate value)
938 mlt_position mlt_properties_get_position( mlt_properties self, const char *name )
940 mlt_property value = mlt_properties_find( self, name );
941 return value == NULL ? 0 : mlt_property_get_position( value );
944 /** Set a property to a position value.
946 * \public \memberof mlt_properties_s
947 * \param self a properties list
948 * \param name the property to get
949 * \param value the position
950 * \return true if error
953 int mlt_properties_set_position( mlt_properties self, const char *name, mlt_position value )
957 // Fetch the property to work with
958 mlt_property property = mlt_properties_fetch( self, name );
960 // Set it if not NULL
961 if ( property != NULL )
963 error = mlt_property_set_position( property, value );
964 mlt_properties_do_mirror( self, name );
967 mlt_events_fire( self, "property-changed", name, NULL );
972 /** Get a binary data value associated to the name.
974 * Do not free the returned pointer if you supplied a destructor function
975 * when you set this property.
976 * \public \memberof mlt_properties_s
977 * \param self a properties list
978 * \param name the property to get
979 * \param[out] length The size of the binary data in bytes, if available (often it is not, you should know)
982 void *mlt_properties_get_data( mlt_properties self, const char *name, int *length )
984 mlt_property value = mlt_properties_find( self, name );
985 return value == NULL ? NULL : mlt_property_get_data( value, length );
988 /** Store binary data as a property.
990 * \public \memberof mlt_properties_s
991 * \param self a properties list
992 * \param name the property to set
993 * \param value an opaque pointer to binary data
994 * \param length the size of the binary data in bytes (optional)
995 * \param destroy a function to deallocate the binary data when the property is closed (optional)
996 * \param serialise a function that can serialize the binary data as text (optional)
997 * \return true if error
1000 int mlt_properties_set_data( mlt_properties self, const char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
1004 // Fetch the property to work with
1005 mlt_property property = mlt_properties_fetch( self, name );
1007 // Set it if not NULL
1008 if ( property != NULL )
1009 error = mlt_property_set_data( property, value, length, destroy, serialise );
1011 mlt_events_fire( self, "property-changed", name, NULL );
1016 /** Rename a property.
1018 * \public \memberof mlt_properties_s
1019 * \param self a properties list
1020 * \param source the property to rename
1021 * \param dest the new name
1022 * \return true if the name is already in use
1025 int mlt_properties_rename( mlt_properties self, const char *source, const char *dest )
1027 mlt_property value = mlt_properties_find( self, dest );
1029 if ( value == NULL )
1031 property_list *list = self->local;
1035 mlt_properties_lock( self );
1036 for ( i = 0; i < list->count; i ++ )
1038 if ( !strcmp( list->name[ i ], source ) )
1040 free( list->name[ i ] );
1041 list->name[ i ] = strdup( dest );
1042 list->hash[ generate_hash( dest ) ] = i + 1;
1046 mlt_properties_unlock( self );
1049 return value != NULL;
1052 /** Dump the properties to a file handle.
1054 * \public \memberof mlt_properties_s
1055 * \param self a properties list
1056 * \param output a file handle
1059 void mlt_properties_dump( mlt_properties self, FILE *output )
1061 property_list *list = self->local;
1063 for ( i = 0; i < list->count; i ++ )
1064 if ( mlt_properties_get( self, list->name[ i ] ) != NULL )
1065 fprintf( output, "%s=%s\n", list->name[ i ], mlt_properties_get( self, list->name[ i ] ) );
1068 /** Output the properties to a file handle.
1070 * This version includes reference counts and does not put each property on a new line.
1071 * \public \memberof mlt_properties_s
1072 * \param self a properties pointer
1073 * \param title a string to preface the output
1074 * \param output a file handle
1076 void mlt_properties_debug( mlt_properties self, const char *title, FILE *output )
1078 if ( output == NULL ) output = stderr;
1079 fprintf( output, "%s: ", title );
1082 property_list *list = self->local;
1084 fprintf( output, "[ ref=%d", list->ref_count );
1085 for ( i = 0; i < list->count; i ++ )
1086 if ( mlt_properties_get( self, list->name[ i ] ) != NULL )
1087 fprintf( output, ", %s=%s", list->name[ i ], mlt_properties_get( self, list->name[ i ] ) );
1089 fprintf( output, ", %s=%p", list->name[ i ], mlt_properties_get_data( self, list->name[ i ], NULL ) );
1090 fprintf( output, " ]" );
1092 fprintf( output, "\n" );
1095 /** Save the properties to a file by name.
1097 * This uses the dump format - one line per property.
1098 * \public \memberof mlt_properties_s
1099 * \param self a properties list
1100 * \param filename the name of a file to create or overwrite
1101 * \return true if there was an error
1104 int mlt_properties_save( mlt_properties self, const char *filename )
1107 FILE *f = fopen( filename, "w" );
1110 mlt_properties_dump( self, f );
1117 /* This is a very basic cross platform fnmatch replacement - it will fail in
1118 * many cases, but for the basic *.XXX and YYY*.XXX, it will work ok.
1121 /** Test whether a filename or pathname matches a shell-style pattern.
1123 * \private \memberof mlt_properties_s
1124 * \param wild a string containing a wildcard pattern
1125 * \param file the name of a file to test against
1126 * \return true if the file name matches the wildcard pattern
1129 static int mlt_fnmatch( const char *wild, const char *file )
1134 while( f < strlen( file ) && w < strlen( wild ) )
1136 if ( wild[ w ] == '*' )
1139 if ( w == strlen( wild ) )
1141 while ( f != strlen( file ) && tolower( file[ f ] ) != tolower( wild[ w ] ) )
1144 else if ( wild[ w ] == '?' || tolower( file[ f ] ) == tolower( wild[ w ] ) )
1149 else if ( wild[ 0 ] == '*' )
1159 return strlen( file ) == f && strlen( wild ) == w;
1162 /** Compare the string or serialized value of two properties.
1164 * \private \memberof mlt_properties_s
1165 * \param self a property
1166 * \param that a property
1167 * \return < 0 if \p self less than \p that, 0 if equal, or > 0 if \p self is greater than \p that
1170 static int mlt_compare( const void *self, const void *that )
1172 return strcmp( mlt_property_get_string( *( const mlt_property * )self ), mlt_property_get_string( *( const mlt_property * )that ) );
1175 /** Get the contents of a directory.
1177 * Obtains an optionally sorted list of the files found in a directory with a specific wild card.
1178 * Entries in the list have a numeric name (running from 0 to count - 1). Only values change
1179 * position if sort is enabled. Designed to be posix compatible (linux, os/x, mingw etc).
1180 * \public \memberof mlt_properties_s
1181 * \param self a properties list
1182 * \param dirname the name of the directory
1183 * \param pattern a wildcard pattern to filter the directory listing
1184 * \param sort Do you want to sort the directory listing?
1185 * \return the number of items in the directory listing
1188 int mlt_properties_dir_list( mlt_properties self, const char *dirname, const char *pattern, int sort )
1190 DIR *dir = opendir( dirname );
1195 struct dirent *de = readdir( dir );
1196 char fullname[ 1024 ];
1199 sprintf( key, "%d", mlt_properties_count( self ) );
1200 snprintf( fullname, 1024, "%s/%s", dirname, de->d_name );
1201 if ( pattern == NULL )
1202 mlt_properties_set( self, key, fullname );
1203 else if ( de->d_name[ 0 ] != '.' && mlt_fnmatch( pattern, de->d_name ) )
1204 mlt_properties_set( self, key, fullname );
1205 de = readdir( dir );
1211 if ( sort && mlt_properties_count( self ) )
1213 property_list *list = self->local;
1214 mlt_properties_lock( self );
1215 qsort( list->value, mlt_properties_count( self ), sizeof( mlt_property ), mlt_compare );
1216 mlt_properties_unlock( self );
1219 return mlt_properties_count( self );
1222 /** Close a properties object.
1224 * Deallocates the properties object and everything it contains.
1225 * \public \memberof mlt_properties_s
1226 * \param self a properties object
1229 void mlt_properties_close( mlt_properties self )
1231 if ( self != NULL && mlt_properties_dec_ref( self ) <= 0 )
1233 if ( self->close != NULL )
1235 self->close( self->close_object );
1239 property_list *list = self->local;
1242 #if _MLT_PROPERTY_CHECKS_ == 1
1244 mlt_properties_debug( self, "Closing", stderr );
1247 #ifdef _MLT_PROPERTY_CHECKS_
1248 // Increment destroyed count
1249 properties_destroyed ++;
1251 // Show current stats - these should match when the app is closed
1252 mlt_log( NULL, MLT_LOG_DEBUG, "Created %d, destroyed %d\n", properties_created, properties_destroyed );
1255 // Clean up names and values
1256 for ( index = list->count - 1; index >= 0; index -- )
1258 mlt_property_close( list->value[ index ] );
1259 free( list->name[ index ] );
1262 // Clear up the list
1263 pthread_mutex_destroy( &list->mutex );
1265 free( list->value );
1268 // Free self now if self has no child
1269 if ( self->child == NULL )
1275 /** Determine if the properties list is really just a sequence or ordered list.
1277 * \public \memberof mlt_properties_s
1278 * \param properties a properties list
1279 * \return true if all of the property names are numeric (a sequence)
1282 int mlt_properties_is_sequence( mlt_properties properties )
1285 int n = mlt_properties_count( properties );
1286 for ( i = 0; i < n; i++ )
1287 if ( ! isdigit( mlt_properties_get_name( properties, i )[0] ) )
1292 /** \brief YAML Tiny Parser context structure
1294 * YAML is a nifty text format popular in the Ruby world as a cleaner,
1295 * less verbose alternative to XML. See this Wikipedia topic for an overview:
1296 * http://en.wikipedia.org/wiki/YAML
1297 * The YAML specification is at:
1299 * YAML::Tiny is a Perl module that specifies a subset of YAML that we are
1300 * using here (for the same reasons):
1301 * http://search.cpan.org/~adamk/YAML-Tiny-1.25/lib/YAML/Tiny.pm
1305 struct yaml_parser_context
1312 unsigned int block_indent;
1315 typedef struct yaml_parser_context *yaml_parser;
1317 /** Remove spaces from the left side of a string.
1319 * \param s the string to trim
1320 * \return the number of characters removed
1323 static unsigned int ltrim( char **s )
1327 int n = strlen( c );
1328 for ( i = 0; i < n && *c == ' '; i++, c++ );
1333 /** Remove spaces from the right side of a string.
1335 * \param s the string to trim
1336 * \return the number of characters removed
1339 static unsigned int rtrim( char *s )
1341 int n = strlen( s );
1343 for ( i = n; i > 0 && s[i - 1] == ' '; --i )
1348 /** Parse a line of YAML Tiny.
1350 * Adds a property if needed.
1351 * \private \memberof yaml_parser_context
1352 * \param context a YAML Tiny Parser context
1353 * \param namevalue a line of YAML Tiny
1354 * \return true if there was an error
1357 static int parse_yaml( yaml_parser context, const char *namevalue )
1359 char *name_ = strdup( namevalue );
1363 char *ptr = strchr( name, ':' );
1364 unsigned int indent = ltrim( &name );
1365 mlt_properties properties = mlt_deque_peek_front( context->stack );
1367 // Ascending one more levels in the tree
1368 if ( indent < context->level )
1371 unsigned int n = ( context->level - indent ) / 2;
1372 for ( i = 0; i < n; i++ )
1373 mlt_deque_pop_front( context->stack );
1374 properties = mlt_deque_peek_front( context->stack );
1375 context->level = indent;
1378 // Descending a level in the tree
1379 else if ( indent > context->level && context->block == 0 )
1381 context->level = indent;
1384 // If there is a colon that is not part of a block
1385 if ( ptr && ( indent == context->level ) )
1387 // Reset block processing
1388 if ( context->block_name )
1390 free( context->block_name );
1391 context->block_name = NULL;
1395 // Terminate the name and setup the value pointer
1399 char *comment = strchr( ptr, '#' );
1405 // Trim leading and trailing spaces from bare value
1409 // No value means a child
1410 if ( strcmp( ptr, "" ) == 0 )
1412 mlt_properties child = mlt_properties_new();
1413 mlt_properties_set_data( properties, name, child, 0,
1414 ( mlt_destructor )mlt_properties_close, NULL );
1415 mlt_deque_push_front( context->stack, child );
1421 // A dash indicates a sequence item
1422 if ( name[0] == '-' )
1424 mlt_properties child = mlt_properties_new();
1427 snprintf( key, sizeof(key), "%d", context->index++ );
1428 mlt_properties_set_data( properties, key, child, 0,
1429 ( mlt_destructor )mlt_properties_close, NULL );
1430 mlt_deque_push_front( context->stack, child );
1433 context->level += ltrim( &name ) + 1;
1441 value = strdup( ptr );
1442 if ( value && value[ strlen( value ) - 1 ] == '\"' )
1443 value[ strlen( value ) - 1 ] = 0;
1446 // Value is folded or unfolded block
1447 else if ( *ptr == '|' || *ptr == '>' )
1449 context->block = *ptr;
1450 context->block_name = strdup( name );
1451 context->block_indent = 0;
1452 value = strdup( "" );
1458 value = strdup( ptr );
1462 // A list of scalars
1463 else if ( name[0] == '-' )
1465 // Reset block processing
1466 if ( context->block_name )
1468 free( context->block_name );
1469 context->block_name = NULL;
1475 snprintf( key, sizeof(key), "%d", context->index++ );
1479 char *comment = strchr( ptr, '#' );
1483 // Trim leading and trailing spaces from bare value
1491 value = strdup( ptr );
1492 if ( value && value[ strlen( value ) - 1 ] == '\"' )
1493 value[ strlen( value ) - 1 ] = 0;
1496 // Value is folded or unfolded block
1497 else if ( *ptr == '|' || *ptr == '>' )
1499 context->block = *ptr;
1500 context->block_name = strdup( key );
1501 context->block_indent = 0;
1502 value = strdup( "" );
1508 value = strdup( ptr );
1512 name = name_ = strdup( key );
1516 else if ( context->block == '|' )
1518 if ( context->block_indent == 0 )
1519 context->block_indent = indent;
1520 if ( indent > context->block_indent )
1521 name = &name_[ context->block_indent ];
1523 char *old_value = mlt_properties_get( properties, context->block_name );
1524 value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
1525 strcpy( value, old_value );
1526 if ( strcmp( old_value, "" ) )
1527 strcat( value, "\n" );
1528 strcat( value, name );
1529 name = context->block_name;
1533 else if ( context->block == '>' )
1537 char *old_value = mlt_properties_get( properties, context->block_name );
1539 // Blank line (prepended with spaces) is new line
1540 if ( strcmp( name, "" ) == 0 )
1542 value = calloc( 1, strlen( old_value ) + 2 );
1543 strcat( value, old_value );
1544 strcat( value, "\n" );
1546 // Concatenate with space
1549 value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
1550 strcat( value, old_value );
1551 if ( strcmp( old_value, "" ) && old_value[ strlen( old_value ) - 1 ] != '\n' )
1552 strcat( value, " " );
1553 strcat( value, name );
1555 name = context->block_name;
1560 value = strdup( "" );
1563 error = mlt_properties_set( properties, name, value );
1571 /** Parse a YAML Tiny file by name.
1573 * \public \memberof mlt_properties_s
1574 * \param filename the name of a text file containing YAML Tiny
1575 * \return a new properties list
1578 mlt_properties mlt_properties_parse_yaml( const char *filename )
1580 // Construct a standalone properties object
1581 mlt_properties self = mlt_properties_new( );
1586 FILE *file = fopen( filename, "r" );
1588 // Load contents of file
1593 char *ptemp = &temp[ 0 ];
1596 yaml_parser context = calloc( 1, sizeof( struct yaml_parser_context ) );
1597 context->stack = mlt_deque_init();
1598 mlt_deque_push_front( context->stack, self );
1600 // Read each string from the file
1601 while( fgets( temp, 1024, file ) )
1603 // Check for end-of-stream
1604 if ( strncmp( ptemp, "...", 3 ) == 0 )
1608 temp[ strlen( temp ) - 1 ] = '\0';
1610 // Skip blank lines, comment lines, and document separator
1611 if ( strcmp( ptemp, "" ) && ptemp[ 0 ] != '#' && strncmp( ptemp, "---", 3 )
1612 && strncmp( ptemp, "%YAML", 5 ) && strncmp( ptemp, "% YAML", 6 ) )
1613 parse_yaml( context, temp );
1618 mlt_deque_close( context->stack );
1619 if ( context->block_name )
1620 free( context->block_name );
1625 // Return the pointer
1630 * YAML Tiny Serializer
1633 /** How many bytes to grow at a time */
1634 #define STRBUF_GROWTH (1024)
1636 /** \brief Private to mlt_properties_s, a self-growing buffer for building strings
1646 typedef struct strbuf_s *strbuf;
1648 /** Create a new string buffer
1650 * \private \memberof strbuf_s
1651 * \return a new string buffer
1654 static strbuf strbuf_new( )
1656 strbuf buffer = calloc( 1, sizeof( struct strbuf_s ) );
1657 buffer->size = STRBUF_GROWTH;
1658 buffer->string = calloc( 1, buffer->size );
1662 /** Destroy a string buffer
1664 * \private \memberof strbuf_s
1665 * \param buffer the string buffer to close
1668 static void strbuf_close( strbuf buffer )
1670 // We do not free buffer->string; strbuf user must save that pointer
1676 /** Format a string into a string buffer
1678 * A variable number of arguments follows the format string - one for each
1680 * \private \memberof strbuf_s
1681 * \param buffer the string buffer to write into
1682 * \param format a string that contains text and formatting instructions
1683 * \return the formatted string
1686 static char *strbuf_printf( strbuf buffer, const char *format, ... )
1688 while ( buffer->string )
1691 va_start( ap, format );
1692 size_t len = strlen( buffer->string );
1693 size_t remain = buffer->size - len - 1;
1694 int need = vsnprintf( buffer->string + len, remain, format, ap );
1696 if ( need > -1 && need < remain )
1698 buffer->string[ len ] = 0;
1699 buffer->size += need + STRBUF_GROWTH;
1700 buffer->string = realloc( buffer->string, buffer->size );
1702 return buffer->string;
1705 /** Indent a line of YAML Tiny.
1707 * \private \memberof strbuf_s
1708 * \param output a string buffer
1709 * \param indent the number of spaces to indent
1712 static inline void indent_yaml( strbuf output, int indent )
1715 for ( j = 0; j < indent; j++ )
1716 strbuf_printf( output, " " );
1719 /** Convert a line string into a YAML block literal.
1721 * \private \memberof strbuf_s
1722 * \param output a string buffer
1723 * \param value the string to format as a block literal
1724 * \param indent the number of spaces to indent
1727 static void output_yaml_block_literal( strbuf output, const char *value, int indent )
1729 char *v = strdup( value );
1731 char *eol = strchr( sol, '\n' );
1735 indent_yaml( output, indent );
1737 strbuf_printf( output, "%s\n", sol );
1739 eol = strchr( sol, '\n' );
1741 indent_yaml( output, indent );
1742 strbuf_printf( output, "%s\n", sol );
1745 /** Recursively serialize a properties list into a string buffer as YAML Tiny.
1747 * \private \memberof mlt_properties_s
1748 * \param self a properties list
1749 * \param output a string buffer to hold the serialized YAML Tiny
1750 * \param indent the number of spaces to indent (for recursion, initialize to 0)
1751 * \param is_parent_sequence Is this properties list really just a sequence (for recursion, initialize to 0)?
1754 static void serialise_yaml( mlt_properties self, strbuf output, int indent, int is_parent_sequence )
1756 property_list *list = self->local;
1759 for ( i = 0; i < list->count; i ++ )
1761 // This implementation assumes that all data elements are property lists.
1762 // Unfortunately, we do not have run time type identification.
1763 mlt_properties child = mlt_property_get_data( list->value[ i ], NULL );
1765 if ( mlt_properties_is_sequence( self ) )
1767 // Ignore hidden/non-serialisable items
1768 if ( list->name[ i ][ 0 ] != '_' )
1770 // Indicate a sequence item
1771 indent_yaml( output, indent );
1772 strbuf_printf( output, "- " );
1774 // If the value can be represented as a string
1775 const char *value = mlt_properties_get( self, list->name[ i ] );
1776 if ( value && strcmp( value, "" ) )
1778 // Determine if this is an unfolded block literal
1779 if ( strchr( value, '\n' ) )
1781 strbuf_printf( output, "|\n" );
1782 output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( "|" ) );
1786 strbuf_printf( output, "%s\n", value );
1792 serialise_yaml( child, output, indent + 2, 1 );
1796 // Assume this is a normal map-oriented properties list
1797 const char *value = mlt_properties_get( self, list->name[ i ] );
1799 // Ignore hidden/non-serialisable items
1800 // If the value can be represented as a string
1801 if ( list->name[ i ][ 0 ] != '_' && value && strcmp( value, "" ) )
1803 if ( is_parent_sequence == 0 )
1804 indent_yaml( output, indent );
1806 is_parent_sequence = 0;
1808 // Determine if this is an unfolded block literal
1809 if ( strchr( value, '\n' ) )
1811 strbuf_printf( output, "%s: |\n", list->name[ i ] );
1812 output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( ": " ) );
1816 strbuf_printf( output, "%s: %s\n", list->name[ i ], value );
1820 // Output a child as a map item
1823 indent_yaml( output, indent );
1824 strbuf_printf( output, "%s:\n", list->name[ i ] );
1827 serialise_yaml( child, output, indent + 2, 0 );
1833 /** Serialize a properties list as a string of YAML Tiny.
1835 * The caller MUST free the returned string!
1836 * This operates on properties containing properties as a hierarchical data
1838 * \public \memberof mlt_properties_s
1839 * \param self a properties list
1840 * \return a string containing YAML Tiny that represents the properties list
1843 char *mlt_properties_serialise_yaml( mlt_properties self )
1845 strbuf b = strbuf_new();
1846 strbuf_printf( b, "---\n" );
1847 serialise_yaml( self, b, 0, 0 );
1848 strbuf_printf( b, "...\n" );
1849 char *ret = b->string;
1854 /** Protect a properties list against concurrent access.
1856 * \public \memberof mlt_properties_s
1857 * \param self a properties list
1860 void mlt_properties_lock( mlt_properties self )
1863 pthread_mutex_lock( &( ( property_list* )( self->local ) )->mutex );
1866 /** End protecting a properties list against concurrent access.
1868 * \public \memberof mlt_properties_s
1869 * \param self a properties list
1872 void mlt_properties_unlock( mlt_properties self )
1875 pthread_mutex_unlock( &( ( property_list* )( self->local ) )->mutex );