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( 1, sizeof( property_list ) );
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( 1, sizeof( struct mlt_properties_s ) );
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 ) )
195 // Chomp the new line character from the string
196 int x = strlen( temp ) - 1;
197 if ( temp[x] == '\n' || temp[x] == '\r' )
200 // Check if the line starts with a .
201 if ( temp[ 0 ] == '.' )
204 sprintf( temp2, "%s%s", last, temp );
205 strcpy( temp, temp2 );
207 else if ( strchr( temp, '=' ) )
209 strcpy( last, temp );
210 *( strchr( last, '=' ) ) = '\0';
213 // Parse and set the property
214 if ( strcmp( temp, "" ) && temp[ 0 ] != '#' )
215 mlt_properties_parse( self, temp );
221 return file? 0 : errno;
224 /** Create a properties object by reading a .properties text file.
226 * Free the properties object with mlt_properties_close().
227 * \deprecated Please start using mlt_properties_parse_yaml().
228 * \public \memberof mlt_properties_s
229 * \param filename the absolute file name
230 * \return a new properties object
233 mlt_properties mlt_properties_load( const char *filename )
235 // Construct a standalone properties object
236 mlt_properties self = mlt_properties_new( );
239 load_properties( self, filename );
241 // Return the pointer
245 /** Set properties from a preset.
247 * Presets are typically installed to $prefix/share/mlt/presets/{type}/{service}/[{profile}/]{name}.
248 * For example, "/usr/share/mlt/presets/consumer/avformat/dv_ntsc_wide/DVD"
249 * could be an encoding preset for a widescreen NTSC DVD Video.
250 * Do not specify the type and service in the preset name parameter; these are
251 * inferred automatically from the service to which you are applying the preset.
252 * Using the example above and assuming you are calling this function on the
253 * avformat consumer, the name passed to the function should simply be DVD.
254 * Note that the profile portion of the path is optional, but a profile-specific
255 * preset with the same name as a more generic one is given a higher priority.
256 * \todo Look in a user-specific location - somewhere in the home directory.
258 * \public \memberof mlt_properties_s
259 * \param self a properties list
260 * \param name the name of a preset in a well-known location or the explicit path
261 * \return true if error
264 int mlt_properties_preset( mlt_properties self, const char *name )
266 struct stat stat_buff;
269 if ( !( self && name && strlen( name ) ) )
272 // See if name is an explicit file
273 if ( ! stat( name, &stat_buff ) )
275 return load_properties( self, name );
279 // Look for profile-specific preset before a generic one.
280 char *data = getenv( "MLT_PRESETS_PATH" );
281 const char *type = mlt_properties_get( self, "mlt_type" );
282 const char *service = mlt_properties_get( self, "mlt_service" );
283 const char *profile = mlt_environment( "MLT_PROFILE" );
288 data = strdup( data );
292 data = malloc( strlen( mlt_environment( "MLT_DATA" ) ) + strlen( PRESETS_DIR ) + 1 );
293 strcpy( data, mlt_environment( "MLT_DATA" ) );
294 strcat( data, PRESETS_DIR );
296 if ( data && type && service )
298 char *path = malloc( 5 + strlen(name) + strlen(data) + strlen(type) + strlen(service) + ( profile? strlen(profile) : 0 ) );
299 sprintf( path, "%s/%s/%s/%s/%s", data, type, service, profile, name );
300 if ( load_properties( self, path ) )
302 sprintf( path, "%s/%s/%s/%s", data, type, service, name );
303 error = load_properties( self, path );
316 /** Generate a hash key.
318 * \private \memberof mlt_properties_s
319 * \param name a string
323 static inline int generate_hash( const char *name )
328 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
332 /** Copy a serializable property to a properties list that is mirroring this one.
334 * Special case - when a container (such as loader) is protecting another
335 * producer, we need to ensure that properties are passed through to the
337 * \private \memberof mlt_properties_s
338 * \param self a properties list
339 * \param name the name of the property to copy
342 static inline void mlt_properties_do_mirror( mlt_properties self, const char *name )
345 property_list *list = self->local;
346 if ( list->mirror != NULL )
348 char *value = mlt_properties_get( self, name );
350 mlt_properties_set( list->mirror, name, value );
354 /** Increment the reference count.
356 * \public \memberof mlt_properties_s
357 * \param self a properties list
358 * \return the new reference count
361 int mlt_properties_inc_ref( mlt_properties self )
366 property_list *list = self->local;
367 pthread_mutex_lock( &list->mutex );
368 result = ++ list->ref_count;
369 pthread_mutex_unlock( &list->mutex );
374 /** Decrement the reference count.
376 * \public \memberof mlt_properties_s
377 * \param self a properties list
378 * \return the new reference count
381 int mlt_properties_dec_ref( mlt_properties self )
386 property_list *list = self->local;
387 pthread_mutex_lock( &list->mutex );
388 result = -- list->ref_count;
389 pthread_mutex_unlock( &list->mutex );
394 /** Get the reference count.
396 * \public \memberof mlt_properties_s
397 * \param self a properties list
398 * \return the current reference count
401 int mlt_properties_ref_count( mlt_properties self )
405 property_list *list = self->local;
406 return list->ref_count;
411 /** Set a properties list to be a mirror copy of another.
413 * Note that this does not copy all existing properties. Rather, you must
414 * call this before setting the properties that you wish to copy.
415 * \public \memberof mlt_properties_s
416 * \param that the properties which will receive copies of the properties as they are set.
417 * \param self the properties to mirror
420 void mlt_properties_mirror( mlt_properties self, mlt_properties that )
423 property_list *list = self->local;
427 /** Copy all serializable properties to another properties list.
429 * \public \memberof mlt_properties_s
430 * \param self The properties to copy to
431 * \param that The properties to copy from
432 * \return true if error
435 int mlt_properties_inherit( mlt_properties self, mlt_properties that )
437 if ( !self || !that ) return 1;
438 int count = mlt_properties_count( that );
440 for ( i = 0; i < count; i ++ )
442 char *value = mlt_properties_get_value( that, i );
445 char *name = mlt_properties_get_name( that, i );
446 mlt_properties_set( self, name, value );
452 /** Pass all serializable properties that match a prefix to another properties object
454 * \public \memberof mlt_properties_s
455 * \param self the properties to copy to
456 * \param that The properties to copy from
457 * \param prefix the property names to match (required)
458 * \return true if error
461 int mlt_properties_pass( mlt_properties self, mlt_properties that, const char *prefix )
463 if ( !self || !that ) return 1;
464 int count = mlt_properties_count( that );
465 int length = strlen( prefix );
467 for ( i = 0; i < count; i ++ )
469 char *name = mlt_properties_get_name( that, i );
470 if ( !strncmp( name, prefix, length ) )
472 char *value = mlt_properties_get_value( that, i );
474 mlt_properties_set( self, name + length, value );
480 /** Locate a property by name.
482 * \private \memberof mlt_properties_s
483 * \param self a properties list
484 * \param name the property to lookup by name
485 * \return the property or NULL for failure
488 static inline mlt_property mlt_properties_find( mlt_properties self, const char *name )
490 if ( !self || !name ) return NULL;
491 property_list *list = self->local;
492 mlt_property value = NULL;
493 int key = generate_hash( name );
495 mlt_properties_lock( self );
497 int i = list->hash[ key ] - 1;
500 // Check if we're hashed
501 if ( list->count > 0 &&
502 name[ 0 ] == list->name[ i ][ 0 ] &&
503 !strcmp( list->name[ i ], name ) )
504 value = list->value[ i ];
507 for ( i = list->count - 1; value == NULL && i >= 0; i -- )
508 if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
509 value = list->value[ i ];
511 mlt_properties_unlock( self );
516 /** Add a new property.
518 * \private \memberof mlt_properties_s
519 * \param self a properties list
520 * \param name the name of the new property
521 * \return the new property
524 static mlt_property mlt_properties_add( mlt_properties self, const char *name )
526 property_list *list = self->local;
527 int key = generate_hash( name );
530 mlt_properties_lock( self );
532 // Check that we have space and resize if necessary
533 if ( list->count == list->size )
536 list->name = realloc( list->name, list->size * sizeof( const char * ) );
537 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
540 // Assign name/value pair
541 list->name[ list->count ] = strdup( name );
542 list->value[ list->count ] = mlt_property_init( );
544 // Assign to hash table
545 if ( list->hash[ key ] == 0 )
546 list->hash[ key ] = list->count + 1;
548 // Return and increment count accordingly
549 result = list->value[ list->count ++ ];
551 mlt_properties_unlock( self );
556 /** Fetch a property by name and add one if not found.
558 * \private \memberof mlt_properties_s
559 * \param self a properties list
560 * \param name the property to lookup or add
561 * \return the property
564 static mlt_property mlt_properties_fetch( mlt_properties self, const char *name )
566 // Try to find an existing property first
567 mlt_property property = mlt_properties_find( self, name );
569 // If it wasn't found, create one
570 if ( property == NULL )
571 property = mlt_properties_add( self, name );
573 // Return the property
577 /** Copy a property to another properties list.
579 * \public \memberof mlt_properties_s
580 * \author Zach <zachary.drew@gmail.com>
581 * \param self the properties to copy to
582 * \param that the properties to copy from
583 * \param name the name of the property to copy
586 void mlt_properties_pass_property( mlt_properties self, mlt_properties that, const char *name )
588 // Make sure the source property isn't null.
589 mlt_property that_prop = mlt_properties_find( that, name );
590 if( that_prop == NULL )
593 mlt_property_pass( mlt_properties_fetch( self, name ), that_prop );
596 /** Copy all properties specified in a comma-separated list to another properties list.
598 * White space is also a delimiter.
599 * \public \memberof mlt_properties_s
600 * \author Zach <zachary.drew@gmail.com>
601 * \param self the properties to copy to
602 * \param that the properties to copy from
603 * \param list a delimited list of property names
604 * \return true if error
608 int mlt_properties_pass_list( mlt_properties self, mlt_properties that, const char *list )
610 if ( !self || !that || !list ) return 1;
611 char *props = strdup( list );
613 const char *delim = " ,\t\n"; // Any combination of spaces, commas, tabs, and newlines
618 count = strcspn( ptr, delim );
620 if( ptr[count] == '\0' )
623 ptr[count] = '\0'; // Make it a real string
625 mlt_properties_pass_property( self, that, ptr );
629 ptr += strspn( ptr, delim );
638 /** Set a property to a string.
640 * The property name "properties" is reserved to load the preset in \p value.
641 * When the value begins with '@' then it is interpreted as a very simple math
642 * expression containing only the +, -, *, and / operators.
643 * The event "property-changed" is fired after the property has been set.
645 * This makes a copy of the string value you supply.
646 * \public \memberof mlt_properties_s
647 * \param self a properties list
648 * \param name the property to set
649 * \param value the property's new value
650 * \return true if error
653 int mlt_properties_set( mlt_properties self, const char *name, const char *value )
657 if ( !self || !name ) return error;
659 // Fetch the property to work with
660 mlt_property property = mlt_properties_fetch( self, name );
662 // Set it if not NULL
663 if ( property == NULL )
665 mlt_log( NULL, MLT_LOG_FATAL, "Whoops - %s not found (should never occur)\n", name );
667 else if ( value == NULL )
669 error = mlt_property_set_string( property, value );
670 mlt_properties_do_mirror( self, name );
672 else if ( *value != '@' )
674 error = mlt_property_set_string( property, value );
675 mlt_properties_do_mirror( self, name );
676 if ( !strcmp( name, "properties" ) )
677 mlt_properties_preset( self, value );
679 else if ( value[ 0 ] == '@' )
688 while ( *value != '\0' )
690 int length = strcspn( value, "+-*/" );
692 // Get the identifier
693 strncpy( id, value, length );
697 // Determine the value
698 if ( isdigit( id[ 0 ] ) )
699 current = atof( id );
701 current = mlt_properties_get_double( self, id );
703 // Apply the operation
716 total = total / current;
721 op = *value != '\0' ? *value ++ : ' ';
724 error = mlt_property_set_double( property, total );
725 mlt_properties_do_mirror( self, name );
728 mlt_events_fire( self, "property-changed", name, NULL );
733 /** Set or default a property to a string.
735 * This makes a copy of the string value you supply.
736 * \public \memberof mlt_properties_s
737 * \param self a properties list
738 * \param name the property to set
739 * \param value the string value to set or NULL to use the default
740 * \param def the default string if value is NULL
741 * \return true if error
744 int mlt_properties_set_or_default( mlt_properties self, const char *name, const char *value, const char *def )
746 return mlt_properties_set( self, name, value == NULL ? def : value );
749 /** Get a string value by name.
751 * Do not free the returned string. It's lifetime is controlled by the property
752 * and this properties object.
753 * \public \memberof mlt_properties_s
754 * \param self a properties list
755 * \param name the property to get
756 * \return the property's string value or NULL if it does not exist
759 char *mlt_properties_get( mlt_properties self, const char *name )
761 mlt_property value = mlt_properties_find( self, name );
762 property_list *list = self->local;
763 return value == NULL ? NULL : mlt_property_get_string_l( value, list->locale );
766 /** Get a property name by index.
768 * Do not free the returned string.
769 * \public \memberof mlt_properties_s
770 * \param self a properties list
771 * \param index the numeric index of the property
772 * \return the name of the property or NULL if index is out of range
775 char *mlt_properties_get_name( mlt_properties self, int index )
777 if ( !self ) return NULL;
778 property_list *list = self->local;
779 if ( index >= 0 && index < list->count )
780 return list->name[ index ];
784 /** Get a property's string value by index.
786 * Do not free the returned string.
787 * \public \memberof mlt_properties_s
788 * \param self a properties list
789 * \param index the numeric index of the property
790 * \return the property value as a string or NULL if the index is out of range
793 char *mlt_properties_get_value( mlt_properties self, int index )
795 if ( !self ) return NULL;
796 property_list *list = self->local;
797 if ( index >= 0 && index < list->count )
798 return mlt_property_get_string_l( list->value[ index ], list->locale );
802 /** Get a data value by index.
804 * Do not free the returned pointer if you supplied a destructor function when you
806 * \public \memberof mlt_properties_s
807 * \param self a properties list
808 * \param index the numeric index of the property
809 * \param[out] size the size of the binary data in bytes or NULL if the index is out of range
812 void *mlt_properties_get_data_at( mlt_properties self, int index, int *size )
814 if ( !self ) return NULL;
815 property_list *list = self->local;
816 if ( index >= 0 && index < list->count )
817 return mlt_property_get_data( list->value[ index ], size );
821 /** Return the number of items in the list.
823 * \public \memberof mlt_properties_s
824 * \param self a properties list
825 * \return the number of property objects or -1 if error
828 int mlt_properties_count( mlt_properties self )
830 if ( !self ) return -1;
831 property_list *list = self->local;
835 /** Set a value by parsing a name=value string.
837 * \public \memberof mlt_properties_s
838 * \param self a properties list
839 * \param namevalue a string containing name and value delimited by '='
840 * \return true if there was an error
843 int mlt_properties_parse( mlt_properties self, const char *namevalue )
845 if ( !self ) return 1;
846 char *name = strdup( namevalue );
849 char *ptr = strchr( name, '=' );
857 value = strdup( ptr );
862 value = strdup( ptr );
863 if ( value != NULL && value[ strlen( value ) - 1 ] == '\"' )
864 value[ strlen( value ) - 1 ] = '\0';
869 value = strdup( "" );
872 error = mlt_properties_set( self, name, value );
880 /** Get an integer associated to the name.
882 * \public \memberof mlt_properties_s
883 * \param self a properties list
884 * \param name the property to get
885 * \return The integer value, 0 if not found (which may also be a legitimate value)
888 int mlt_properties_get_int( mlt_properties self, const char *name )
890 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
891 double fps = mlt_profile_fps( profile );
892 property_list *list = self->local;
893 mlt_property value = mlt_properties_find( self, name );
894 return value == NULL ? 0 : mlt_property_get_int( value, fps, list->locale );
897 /** Set a property to an integer value.
899 * \public \memberof mlt_properties_s
900 * \param self a properties list
901 * \param name the property to set
902 * \param value the integer
903 * \return true if error
906 int mlt_properties_set_int( mlt_properties self, const char *name, int value )
910 if ( !self || !name ) return error;
912 // Fetch the property to work with
913 mlt_property property = mlt_properties_fetch( self, name );
915 // Set it if not NULL
916 if ( property != NULL )
918 error = mlt_property_set_int( property, value );
919 mlt_properties_do_mirror( self, name );
922 mlt_events_fire( self, "property-changed", name, NULL );
927 /** Get a 64-bit integer associated to the name.
929 * \public \memberof mlt_properties_s
930 * \param self a properties list
931 * \param name the property to get
932 * \return the integer value, 0 if not found (which may also be a legitimate value)
935 int64_t mlt_properties_get_int64( mlt_properties self, const char *name )
937 mlt_property value = mlt_properties_find( self, name );
938 return value == NULL ? 0 : mlt_property_get_int64( value );
941 /** Set a property to a 64-bit integer value.
943 * \public \memberof mlt_properties_s
944 * \param self a properties list
945 * \param name the property to set
946 * \param value the integer
947 * \return true if error
950 int mlt_properties_set_int64( mlt_properties self, const char *name, int64_t value )
954 if ( !self || !name ) return error;
956 // Fetch the property to work with
957 mlt_property property = mlt_properties_fetch( self, name );
959 // Set it if not NULL
960 if ( property != NULL )
962 error = mlt_property_set_int64( property, value );
963 mlt_properties_do_mirror( self, name );
966 mlt_events_fire( self, "property-changed", name, NULL );
971 /** Get a floating point value associated to the name.
973 * \public \memberof mlt_properties_s
974 * \param self a properties list
975 * \param name the property to get
976 * \return the floating point, 0 if not found (which may also be a legitimate value)
979 double mlt_properties_get_double( mlt_properties self, const char *name )
981 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
982 double fps = mlt_profile_fps( profile );
983 mlt_property value = mlt_properties_find( self, name );
984 property_list *list = self->local;
985 return value == NULL ? 0 : mlt_property_get_double( value, fps, list->locale );
988 /** Set a property to a floating point value.
990 * \public \memberof mlt_properties_s
991 * \param self a properties list
992 * \param name the property to set
993 * \param value the floating point value
994 * \return true if error
997 int mlt_properties_set_double( mlt_properties self, const char *name, double value )
1001 if ( !self || !name ) return error;
1003 // Fetch the property to work with
1004 mlt_property property = mlt_properties_fetch( self, name );
1006 // Set it if not NULL
1007 if ( property != NULL )
1009 error = mlt_property_set_double( property, value );
1010 mlt_properties_do_mirror( self, name );
1013 mlt_events_fire( self, "property-changed", name, NULL );
1018 /** Get a position value associated to the name.
1020 * \public \memberof mlt_properties_s
1021 * \param self a properties list
1022 * \param name the property to get
1023 * \return the position, 0 if not found (which may also be a legitimate value)
1026 mlt_position mlt_properties_get_position( mlt_properties self, const char *name )
1028 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
1029 double fps = mlt_profile_fps( profile );
1030 property_list *list = self->local;
1031 mlt_property value = mlt_properties_find( self, name );
1032 return value == NULL ? 0 : mlt_property_get_position( value, fps, list->locale );
1035 /** Set a property to a position value.
1037 * \public \memberof mlt_properties_s
1038 * \param self a properties list
1039 * \param name the property to get
1040 * \param value the position
1041 * \return true if error
1044 int mlt_properties_set_position( mlt_properties self, const char *name, mlt_position value )
1048 if ( !self || !name ) return error;
1050 // Fetch the property to work with
1051 mlt_property property = mlt_properties_fetch( self, name );
1053 // Set it if not NULL
1054 if ( property != NULL )
1056 error = mlt_property_set_position( property, value );
1057 mlt_properties_do_mirror( self, name );
1060 mlt_events_fire( self, "property-changed", name, NULL );
1065 /** Get a binary data value associated to the name.
1067 * Do not free the returned pointer if you supplied a destructor function
1068 * when you set this property.
1069 * \public \memberof mlt_properties_s
1070 * \param self a properties list
1071 * \param name the property to get
1072 * \param[out] length The size of the binary data in bytes, if available (often it is not, you should know)
1075 void *mlt_properties_get_data( mlt_properties self, const char *name, int *length )
1077 mlt_property value = mlt_properties_find( self, name );
1078 return value == NULL ? NULL : mlt_property_get_data( value, length );
1081 /** Store binary data as a property.
1083 * \public \memberof mlt_properties_s
1084 * \param self a properties list
1085 * \param name the property to set
1086 * \param value an opaque pointer to binary data
1087 * \param length the size of the binary data in bytes (optional)
1088 * \param destroy a function to deallocate the binary data when the property is closed (optional)
1089 * \param serialise a function that can serialize the binary data as text (optional)
1090 * \return true if error
1093 int mlt_properties_set_data( mlt_properties self, const char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
1097 if ( !self || !name ) return error;
1099 // Fetch the property to work with
1100 mlt_property property = mlt_properties_fetch( self, name );
1102 // Set it if not NULL
1103 if ( property != NULL )
1104 error = mlt_property_set_data( property, value, length, destroy, serialise );
1106 mlt_events_fire( self, "property-changed", name, NULL );
1111 /** Rename a property.
1113 * \public \memberof mlt_properties_s
1114 * \param self a properties list
1115 * \param source the property to rename
1116 * \param dest the new name
1117 * \return true if the name is already in use
1120 int mlt_properties_rename( mlt_properties self, const char *source, const char *dest )
1122 mlt_property value = mlt_properties_find( self, dest );
1124 if ( value == NULL )
1126 property_list *list = self->local;
1130 mlt_properties_lock( self );
1131 for ( i = 0; i < list->count; i ++ )
1133 if ( !strcmp( list->name[ i ], source ) )
1135 free( list->name[ i ] );
1136 list->name[ i ] = strdup( dest );
1137 list->hash[ generate_hash( dest ) ] = i + 1;
1141 mlt_properties_unlock( self );
1144 return value != NULL;
1147 /** Dump the properties to a file handle.
1149 * \public \memberof mlt_properties_s
1150 * \param self a properties list
1151 * \param output a file handle
1154 void mlt_properties_dump( mlt_properties self, FILE *output )
1156 if ( !self || !output ) return;
1157 property_list *list = self->local;
1159 for ( i = 0; i < list->count; i ++ )
1160 if ( mlt_properties_get( self, list->name[ i ] ) != NULL )
1161 fprintf( output, "%s=%s\n", list->name[ i ], mlt_properties_get( self, list->name[ i ] ) );
1164 /** Output the properties to a file handle.
1166 * This version includes reference counts and does not put each property on a new line.
1167 * \public \memberof mlt_properties_s
1168 * \param self a properties pointer
1169 * \param title a string to preface the output
1170 * \param output a file handle
1172 void mlt_properties_debug( mlt_properties self, const char *title, FILE *output )
1174 if ( !self || !output ) return;
1175 if ( output == NULL ) output = stderr;
1176 fprintf( output, "%s: ", title );
1179 property_list *list = self->local;
1181 fprintf( output, "[ ref=%d", list->ref_count );
1182 for ( i = 0; i < list->count; i ++ )
1183 if ( mlt_properties_get( self, list->name[ i ] ) != NULL )
1184 fprintf( output, ", %s=%s", list->name[ i ], mlt_properties_get( self, list->name[ i ] ) );
1186 fprintf( output, ", %s=%p", list->name[ i ], mlt_properties_get_data( self, list->name[ i ], NULL ) );
1187 fprintf( output, " ]" );
1189 fprintf( output, "\n" );
1192 /** Save the properties to a file by name.
1194 * This uses the dump format - one line per property.
1195 * \public \memberof mlt_properties_s
1196 * \param self a properties list
1197 * \param filename the name of a file to create or overwrite
1198 * \return true if there was an error
1201 int mlt_properties_save( mlt_properties self, const char *filename )
1204 if ( !self || !filename ) return error;
1205 FILE *f = fopen( filename, "w" );
1208 mlt_properties_dump( self, f );
1215 /* This is a very basic cross platform fnmatch replacement - it will fail in
1216 * many cases, but for the basic *.XXX and YYY*.XXX, it will work ok.
1219 /** Test whether a filename or pathname matches a shell-style pattern.
1221 * \private \memberof mlt_properties_s
1222 * \param wild a string containing a wildcard pattern
1223 * \param file the name of a file to test against
1224 * \return true if the file name matches the wildcard pattern
1227 static int mlt_fnmatch( const char *wild, const char *file )
1232 while( f < strlen( file ) && w < strlen( wild ) )
1234 if ( wild[ w ] == '*' )
1237 if ( w == strlen( wild ) )
1239 while ( f != strlen( file ) && tolower( file[ f ] ) != tolower( wild[ w ] ) )
1242 else if ( wild[ w ] == '?' || tolower( file[ f ] ) == tolower( wild[ w ] ) )
1247 else if ( wild[ 0 ] == '*' )
1257 return strlen( file ) == f && strlen( wild ) == w;
1260 /** Compare the string or serialized value of two properties.
1262 * \private \memberof mlt_properties_s
1263 * \param self a property
1264 * \param that a property
1265 * \return < 0 if \p self less than \p that, 0 if equal, or > 0 if \p self is greater than \p that
1268 static int mlt_compare( const void *self, const void *that )
1270 return strcmp( mlt_property_get_string( *( const mlt_property * )self ), mlt_property_get_string( *( const mlt_property * )that ) );
1273 /** Get the contents of a directory.
1275 * Obtains an optionally sorted list of the files found in a directory with a specific wild card.
1276 * Entries in the list have a numeric name (running from 0 to count - 1). Only values change
1277 * position if sort is enabled. Designed to be posix compatible (linux, os/x, mingw etc).
1278 * \public \memberof mlt_properties_s
1279 * \param self a properties list
1280 * \param dirname the name of the directory
1281 * \param pattern a wildcard pattern to filter the directory listing
1282 * \param sort Do you want to sort the directory listing?
1283 * \return the number of items in the directory listing
1286 int mlt_properties_dir_list( mlt_properties self, const char *dirname, const char *pattern, int sort )
1288 DIR *dir = opendir( dirname );
1293 struct dirent *de = readdir( dir );
1294 char fullname[ 1024 ];
1297 sprintf( key, "%d", mlt_properties_count( self ) );
1298 snprintf( fullname, 1024, "%s/%s", dirname, de->d_name );
1299 if ( pattern == NULL )
1300 mlt_properties_set( self, key, fullname );
1301 else if ( de->d_name[ 0 ] != '.' && mlt_fnmatch( pattern, de->d_name ) )
1302 mlt_properties_set( self, key, fullname );
1303 de = readdir( dir );
1309 if ( sort && mlt_properties_count( self ) )
1311 property_list *list = self->local;
1312 mlt_properties_lock( self );
1313 qsort( list->value, mlt_properties_count( self ), sizeof( mlt_property ), mlt_compare );
1314 mlt_properties_unlock( self );
1317 return mlt_properties_count( self );
1320 /** Close a properties object.
1322 * Deallocates the properties object and everything it contains.
1323 * \public \memberof mlt_properties_s
1324 * \param self a properties object
1327 void mlt_properties_close( mlt_properties self )
1329 if ( self != NULL && mlt_properties_dec_ref( self ) <= 0 )
1331 if ( self->close != NULL )
1333 self->close( self->close_object );
1337 property_list *list = self->local;
1340 #if _MLT_PROPERTY_CHECKS_ == 1
1342 mlt_properties_debug( self, "Closing", stderr );
1345 #ifdef _MLT_PROPERTY_CHECKS_
1346 // Increment destroyed count
1347 properties_destroyed ++;
1349 // Show current stats - these should match when the app is closed
1350 mlt_log( NULL, MLT_LOG_DEBUG, "Created %d, destroyed %d\n", properties_created, properties_destroyed );
1353 // Clean up names and values
1354 for ( index = list->count - 1; index >= 0; index -- )
1356 mlt_property_close( list->value[ index ] );
1357 free( list->name[ index ] );
1360 #if defined(__linux__) || defined(__DARWIN__)
1363 freelocale( list->locale );
1366 // Clear up the list
1367 pthread_mutex_destroy( &list->mutex );
1369 free( list->value );
1372 // Free self now if self has no child
1373 if ( self->child == NULL )
1379 /** Determine if the properties list is really just a sequence or ordered list.
1381 * \public \memberof mlt_properties_s
1382 * \param properties a properties list
1383 * \return true if all of the property names are numeric (a sequence)
1386 int mlt_properties_is_sequence( mlt_properties properties )
1389 int n = mlt_properties_count( properties );
1390 for ( i = 0; i < n; i++ )
1391 if ( ! isdigit( mlt_properties_get_name( properties, i )[0] ) )
1396 /** \brief YAML Tiny Parser context structure
1398 * YAML is a nifty text format popular in the Ruby world as a cleaner,
1399 * less verbose alternative to XML. See this Wikipedia topic for an overview:
1400 * http://en.wikipedia.org/wiki/YAML
1401 * The YAML specification is at:
1403 * YAML::Tiny is a Perl module that specifies a subset of YAML that we are
1404 * using here (for the same reasons):
1405 * http://search.cpan.org/~adamk/YAML-Tiny-1.25/lib/YAML/Tiny.pm
1409 struct yaml_parser_context
1414 mlt_deque index_stack;
1417 unsigned int block_indent;
1420 typedef struct yaml_parser_context *yaml_parser;
1422 /** Remove spaces from the left side of a string.
1424 * \param s the string to trim
1425 * \return the number of characters removed
1428 static unsigned int ltrim( char **s )
1432 int n = strlen( c );
1433 for ( i = 0; i < n && *c == ' '; i++, c++ );
1438 /** Remove spaces from the right side of a string.
1440 * \param s the string to trim
1441 * \return the number of characters removed
1444 static unsigned int rtrim( char *s )
1446 int n = strlen( s );
1448 for ( i = n; i > 0 && s[i - 1] == ' '; --i )
1453 /** Parse a line of YAML Tiny.
1455 * Adds a property if needed.
1456 * \private \memberof yaml_parser_context
1457 * \param context a YAML Tiny Parser context
1458 * \param namevalue a line of YAML Tiny
1459 * \return true if there was an error
1462 static int parse_yaml( yaml_parser context, const char *namevalue )
1464 char *name_ = strdup( namevalue );
1468 char *ptr = strchr( name, ':' );
1469 unsigned int indent = ltrim( &name );
1470 mlt_properties properties = mlt_deque_peek_back( context->stack );
1472 // Ascending one more levels in the tree
1473 if ( indent < context->level )
1476 unsigned int n = ( context->level - indent ) / 2;
1477 for ( i = 0; i < n; i++ )
1479 mlt_deque_pop_back( context->stack );
1480 context->index = mlt_deque_pop_back_int( context->index_stack );
1482 properties = mlt_deque_peek_back( context->stack );
1483 context->level = indent;
1486 // Descending a level in the tree
1487 else if ( indent > context->level && context->block == 0 )
1489 context->level = indent;
1492 // If there is a colon that is not part of a block
1493 if ( ptr && ( indent == context->level ) )
1495 // Reset block processing
1496 if ( context->block_name )
1498 free( context->block_name );
1499 context->block_name = NULL;
1503 // Terminate the name and setup the value pointer
1507 char *comment = strchr( ptr, '#' );
1513 // Trim leading and trailing spaces from bare value
1517 // No value means a child
1518 if ( strcmp( ptr, "" ) == 0 )
1520 mlt_properties child = mlt_properties_new();
1521 mlt_properties_set_lcnumeric( child, mlt_properties_get_lcnumeric( properties ) );
1522 mlt_properties_set_data( properties, name, child, 0,
1523 ( mlt_destructor )mlt_properties_close, NULL );
1524 mlt_deque_push_back( context->stack, child );
1525 mlt_deque_push_back_int( context->index_stack, context->index );
1531 // A dash indicates a sequence item
1532 if ( name[0] == '-' )
1534 mlt_properties child = mlt_properties_new();
1537 mlt_properties_set_lcnumeric( child, mlt_properties_get_lcnumeric( properties ) );
1538 snprintf( key, sizeof(key), "%d", context->index++ );
1539 mlt_properties_set_data( properties, key, child, 0,
1540 ( mlt_destructor )mlt_properties_close, NULL );
1541 mlt_deque_push_back( context->stack, child );
1542 mlt_deque_push_back_int( context->index_stack, context->index );
1545 context->level += ltrim( &name ) + 1;
1553 value = strdup( ptr );
1554 if ( value && value[ strlen( value ) - 1 ] == '\"' )
1555 value[ strlen( value ) - 1 ] = 0;
1558 // Value is folded or unfolded block
1559 else if ( *ptr == '|' || *ptr == '>' )
1561 context->block = *ptr;
1562 context->block_name = strdup( name );
1563 context->block_indent = 0;
1564 value = strdup( "" );
1570 value = strdup( ptr );
1574 // A list of scalars
1575 else if ( name[0] == '-' )
1577 // Reset block processing
1578 if ( context->block_name )
1580 free( context->block_name );
1581 context->block_name = NULL;
1587 snprintf( key, sizeof(key), "%d", context->index++ );
1591 char *comment = strchr( ptr, '#' );
1595 // Trim leading and trailing spaces from bare value
1603 value = strdup( ptr );
1604 if ( value && value[ strlen( value ) - 1 ] == '\"' )
1605 value[ strlen( value ) - 1 ] = 0;
1608 // Value is folded or unfolded block
1609 else if ( *ptr == '|' || *ptr == '>' )
1611 context->block = *ptr;
1612 context->block_name = strdup( key );
1613 context->block_indent = 0;
1614 value = strdup( "" );
1620 value = strdup( ptr );
1624 name = name_ = strdup( key );
1628 else if ( context->block == '|' )
1630 if ( context->block_indent == 0 )
1631 context->block_indent = indent;
1632 if ( indent > context->block_indent )
1633 name = &name_[ context->block_indent ];
1635 char *old_value = mlt_properties_get( properties, context->block_name );
1636 value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
1637 strcpy( value, old_value );
1638 if ( strcmp( old_value, "" ) )
1639 strcat( value, "\n" );
1640 strcat( value, name );
1641 name = context->block_name;
1645 else if ( context->block == '>' )
1649 char *old_value = mlt_properties_get( properties, context->block_name );
1651 // Blank line (prepended with spaces) is new line
1652 if ( strcmp( name, "" ) == 0 )
1654 value = calloc( 1, strlen( old_value ) + 2 );
1655 strcat( value, old_value );
1656 strcat( value, "\n" );
1658 // Concatenate with space
1661 value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
1662 strcat( value, old_value );
1663 if ( strcmp( old_value, "" ) && old_value[ strlen( old_value ) - 1 ] != '\n' )
1664 strcat( value, " " );
1665 strcat( value, name );
1667 name = context->block_name;
1672 value = strdup( "" );
1675 error = mlt_properties_set( properties, name, value );
1677 if ( !strcmp( name, "LC_NUMERIC" ) )
1678 mlt_properties_set_lcnumeric( properties, value );
1686 /** Parse a YAML Tiny file by name.
1688 * \public \memberof mlt_properties_s
1689 * \param filename the name of a text file containing YAML Tiny
1690 * \return a new properties list
1693 mlt_properties mlt_properties_parse_yaml( const char *filename )
1695 // Construct a standalone properties object
1696 mlt_properties self = mlt_properties_new( );
1701 FILE *file = fopen( filename, "r" );
1703 // Load contents of file
1708 char *ptemp = &temp[ 0 ];
1710 // Default to LC_NUMERIC = C
1711 mlt_properties_set_lcnumeric( self, "C" );
1714 yaml_parser context = calloc( 1, sizeof( struct yaml_parser_context ) );
1715 context->stack = mlt_deque_init();
1716 context->index_stack = mlt_deque_init();
1717 mlt_deque_push_back( context->stack, self );
1718 mlt_deque_push_back_int( context->index_stack, 0 );
1720 // Read each string from the file
1721 while( fgets( temp, 1024, file ) )
1723 // Check for end-of-stream
1724 if ( strncmp( ptemp, "...", 3 ) == 0 )
1728 temp[ strlen( temp ) - 1 ] = '\0';
1730 // Skip blank lines, comment lines, and document separator
1731 if ( strcmp( ptemp, "" ) && ptemp[ 0 ] != '#' && strncmp( ptemp, "---", 3 )
1732 && strncmp( ptemp, "%YAML", 5 ) && strncmp( ptemp, "% YAML", 6 ) )
1733 parse_yaml( context, temp );
1738 mlt_deque_close( context->stack );
1739 mlt_deque_close( context->index_stack );
1740 if ( context->block_name )
1741 free( context->block_name );
1746 // Return the pointer
1751 * YAML Tiny Serializer
1754 /** How many bytes to grow at a time */
1755 #define STRBUF_GROWTH (1024)
1757 /** \brief Private to mlt_properties_s, a self-growing buffer for building strings
1767 typedef struct strbuf_s *strbuf;
1769 /** Create a new string buffer
1771 * \private \memberof strbuf_s
1772 * \return a new string buffer
1775 static strbuf strbuf_new( )
1777 strbuf buffer = calloc( 1, sizeof( struct strbuf_s ) );
1778 buffer->size = STRBUF_GROWTH;
1779 buffer->string = calloc( 1, buffer->size );
1783 /** Destroy a string buffer
1785 * \private \memberof strbuf_s
1786 * \param buffer the string buffer to close
1789 static void strbuf_close( strbuf buffer )
1791 // We do not free buffer->string; strbuf user must save that pointer
1797 /** Format a string into a string buffer
1799 * A variable number of arguments follows the format string - one for each
1801 * \private \memberof strbuf_s
1802 * \param buffer the string buffer to write into
1803 * \param format a string that contains text and formatting instructions
1804 * \return the formatted string
1807 static char *strbuf_printf( strbuf buffer, const char *format, ... )
1809 while ( buffer->string )
1812 va_start( ap, format );
1813 size_t len = strlen( buffer->string );
1814 size_t remain = buffer->size - len - 1;
1815 int need = vsnprintf( buffer->string + len, remain, format, ap );
1817 if ( need > -1 && need < remain )
1819 buffer->string[ len ] = 0;
1820 buffer->size += need + STRBUF_GROWTH;
1821 buffer->string = realloc( buffer->string, buffer->size );
1823 return buffer->string;
1826 /** Indent a line of YAML Tiny.
1828 * \private \memberof strbuf_s
1829 * \param output a string buffer
1830 * \param indent the number of spaces to indent
1833 static inline void indent_yaml( strbuf output, int indent )
1836 for ( j = 0; j < indent; j++ )
1837 strbuf_printf( output, " " );
1840 static void strbuf_escape( strbuf output, const char *value, char c )
1842 char *v = strdup( value );
1844 char *found = strchr( s, c );
1849 strbuf_printf( output, "%s\\%c", s, c );
1851 found = strchr( s, c );
1853 strbuf_printf( output, "%s", s );
1857 /** Convert a line string into a YAML block literal.
1859 * \private \memberof strbuf_s
1860 * \param output a string buffer
1861 * \param value the string to format as a block literal
1862 * \param indent the number of spaces to indent
1865 static void output_yaml_block_literal( strbuf output, const char *value, int indent )
1867 char *v = strdup( value );
1869 char *eol = strchr( sol, '\n' );
1873 indent_yaml( output, indent );
1875 strbuf_printf( output, "%s\n", sol );
1877 eol = strchr( sol, '\n' );
1879 indent_yaml( output, indent );
1880 strbuf_printf( output, "%s\n", sol );
1884 /** Recursively serialize a properties list into a string buffer as YAML Tiny.
1886 * \private \memberof mlt_properties_s
1887 * \param self a properties list
1888 * \param output a string buffer to hold the serialized YAML Tiny
1889 * \param indent the number of spaces to indent (for recursion, initialize to 0)
1890 * \param is_parent_sequence Is this properties list really just a sequence (for recursion, initialize to 0)?
1893 static void serialise_yaml( mlt_properties self, strbuf output, int indent, int is_parent_sequence )
1895 property_list *list = self->local;
1898 for ( i = 0; i < list->count; i ++ )
1900 // This implementation assumes that all data elements are property lists.
1901 // Unfortunately, we do not have run time type identification.
1902 mlt_properties child = mlt_property_get_data( list->value[ i ], NULL );
1904 if ( mlt_properties_is_sequence( self ) )
1906 // Ignore hidden/non-serialisable items
1907 if ( list->name[ i ][ 0 ] != '_' )
1909 // Indicate a sequence item
1910 indent_yaml( output, indent );
1911 strbuf_printf( output, "- " );
1913 // If the value can be represented as a string
1914 const char *value = mlt_properties_get( self, list->name[ i ] );
1915 if ( value && strcmp( value, "" ) )
1917 // Determine if this is an unfolded block literal
1918 if ( strchr( value, '\n' ) )
1920 strbuf_printf( output, "|\n" );
1921 output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( "|" ) );
1923 else if ( strchr( value, ':' ) || strchr( value, '[' ) )
1925 strbuf_printf( output, "\"" );
1926 strbuf_escape( output, value, '"' );
1927 strbuf_printf( output, "\"\n", value );
1931 strbuf_printf( output, "%s\n", value );
1937 serialise_yaml( child, output, indent + 2, 1 );
1941 // Assume this is a normal map-oriented properties list
1942 const char *value = mlt_properties_get( self, list->name[ i ] );
1944 // Ignore hidden/non-serialisable items
1945 // If the value can be represented as a string
1946 if ( list->name[ i ][ 0 ] != '_' && value && strcmp( value, "" ) )
1948 if ( is_parent_sequence == 0 )
1949 indent_yaml( output, indent );
1951 is_parent_sequence = 0;
1953 // Determine if this is an unfolded block literal
1954 if ( strchr( value, '\n' ) )
1956 strbuf_printf( output, "%s: |\n", list->name[ i ] );
1957 output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( ": " ) );
1959 else if ( strchr( value, ':' ) || strchr( value, '[' ) )
1961 strbuf_printf( output, "%s: \"", list->name[ i ] );
1962 strbuf_escape( output, value, '"' );
1963 strbuf_printf( output, "\"\n" );
1967 strbuf_printf( output, "%s: %s\n", list->name[ i ], value );
1971 // Output a child as a map item
1974 indent_yaml( output, indent );
1975 strbuf_printf( output, "%s:\n", list->name[ i ] );
1978 serialise_yaml( child, output, indent + 2, 0 );
1984 /** Serialize a properties list as a string of YAML Tiny.
1986 * The caller MUST free the returned string!
1987 * This operates on properties containing properties as a hierarchical data
1989 * \public \memberof mlt_properties_s
1990 * \param self a properties list
1991 * \return a string containing YAML Tiny that represents the properties list
1994 char *mlt_properties_serialise_yaml( mlt_properties self )
1996 if ( !self ) return NULL;
1997 const char *lc_numeric = mlt_properties_get_lcnumeric( self );
1998 strbuf b = strbuf_new();
1999 strbuf_printf( b, "---\n" );
2000 mlt_properties_set_lcnumeric( self, "C" );
2001 serialise_yaml( self, b, 0, 0 );
2002 mlt_properties_set_lcnumeric( self, lc_numeric );
2003 strbuf_printf( b, "...\n" );
2004 char *ret = b->string;
2009 /** Protect a properties list against concurrent access.
2011 * \public \memberof mlt_properties_s
2012 * \param self a properties list
2015 void mlt_properties_lock( mlt_properties self )
2018 pthread_mutex_lock( &( ( property_list* )( self->local ) )->mutex );
2021 /** End protecting a properties list against concurrent access.
2023 * \public \memberof mlt_properties_s
2024 * \param self a properties list
2027 void mlt_properties_unlock( mlt_properties self )
2030 pthread_mutex_unlock( &( ( property_list* )( self->local ) )->mutex );
2033 /** Get a time string associated to the name.
2035 * Do not free the returned string. It's lifetime is controlled by the property.
2036 * \public \memberof mlt_properties_s
2037 * \param self a properties list
2038 * \param name the property to get
2039 * \param format the time format that you want
2040 * \return the property's time value or NULL if \p name does not exist or there is no profile
2043 char *mlt_properties_get_time( mlt_properties self, const char* name, mlt_time_format format )
2045 mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
2048 double fps = mlt_profile_fps( profile );
2049 mlt_property value = mlt_properties_find( self, name );
2050 property_list *list = self->local;
2051 return value == NULL ? NULL : mlt_property_get_time( value, format, fps, list->locale );