]> git.sesse.net Git - mlt/blob - src/framework/mlt_properties.c
4f21d68cffdd94da5d0a8b4829d0e0f2f84e8049
[mlt] / src / framework / mlt_properties.c
1 /**
2  * \file mlt_properties.c
3  * \brief Properties class definition
4  * \see mlt_properties_s
5  *
6  * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7  * \author Charles Yates <charles.yates@pandora.be>
8  * \author Dan Dennedy <dan@dennedy.org>
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #include "mlt_properties.h"
26 #include "mlt_property.h"
27 #include "mlt_deque.h"
28 #include "mlt_log.h"
29 #include "mlt_factory.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <stdarg.h>
36 #include <pthread.h>
37 #include <sys/types.h>
38 #include <dirent.h>
39 #include <sys/stat.h>
40 #include <errno.h>
41 #include <locale.h>
42
43 #define PRESETS_DIR "/presets"
44
45 /** \brief private implementation of the property list */
46
47 typedef struct
48 {
49         int hash[ 199 ];
50         char **name;
51         mlt_property *value;
52         int count;
53         int size;
54         mlt_properties mirror;
55         int ref_count;
56         pthread_mutex_t mutex;
57         locale_t locale;
58 }
59 property_list;
60
61 /* Memory leak checks */
62
63 //#define _MLT_PROPERTY_CHECKS_ 2
64 #ifdef _MLT_PROPERTY_CHECKS_
65 static int properties_created = 0;
66 static int properties_destroyed = 0;
67 #endif
68
69 /** Initialize a properties object that was already allocated.
70  *
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
76  */
77
78 int mlt_properties_init( mlt_properties self, void *child )
79 {
80         if ( self != NULL )
81         {
82 #ifdef _MLT_PROPERTY_CHECKS_
83                 // Increment number of properties created
84                 properties_created ++;
85 #endif
86
87                 // NULL all methods
88                 memset( self, 0, sizeof( struct mlt_properties_s ) );
89
90                 // Assign the child of the object
91                 self->child = child;
92
93                 // Allocate the local structure
94                 self->local = calloc( 1, sizeof( property_list ) );
95
96                 // Increment the ref count
97                 ( ( property_list * )self->local )->ref_count = 1;
98                 pthread_mutex_init( &( ( property_list * )self->local )->mutex, NULL );;
99         }
100
101         // Check that initialisation was successful
102         return self != NULL && self->local == NULL;
103 }
104
105 /** Create a properties object.
106  *
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
111  */
112
113 mlt_properties mlt_properties_new( )
114 {
115         // Construct a standalone properties object
116         mlt_properties self = calloc( 1, sizeof( struct mlt_properties_s ) );
117
118         // Initialise self
119         mlt_properties_init( self, NULL );
120
121         // Return the pointer
122         return self;
123 }
124
125 /** Set the numeric locale used for string/double conversions.
126  *
127  * \public \memberof mlt_properties_s
128  * \param self a properties list
129  * \param locale the locale name
130  * \return true if error
131  */
132
133 int mlt_properties_set_lcnumeric( mlt_properties self, const char *locale )
134 {
135         int error = 0;
136
137         if ( self && locale )
138         {
139                 property_list *list = self->local;
140
141 #if defined(__linux__) || defined(__DARWIN__)
142                 if ( list->locale )
143                         freelocale( list->locale );
144                 list->locale = newlocale( LC_NUMERIC_MASK, locale, NULL );
145 #endif
146                 error = list->locale == NULL;
147         }
148         else
149                 error = 1;
150
151         return error;
152 }
153
154 /** Get the numeric locale for this properties object.
155  *
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
160  */
161
162 const char* mlt_properties_get_lcnumeric( mlt_properties self )
163 {
164         property_list *list = self->local;
165         const char *result = NULL;
166
167         if ( list->locale )
168         {
169 #if defined(__DARWIN__)
170                 result = querylocale( LC_NUMERIC, list->locale );
171 #elif defined(__linux__)
172                 result = list->locale->__names[ LC_NUMERIC ];
173 #else
174                 // TODO: not yet sure what to do on other platforms
175 #endif
176         }
177         return result;
178 }
179
180 static int load_properties( mlt_properties self, const char *filename )
181 {
182         // Open the file
183         FILE *file = fopen( filename, "r" );
184
185         // Load contents of file
186         if ( file != NULL )
187         {
188                 // Temp string
189                 char temp[ 1024 ];
190                 char last[ 1024 ] = "";
191
192                 // Read each string from the file
193                 while( fgets( temp, 1024, file ) )
194                 {
195                         // Chomp the new line character from the string
196                         int x = strlen( temp ) - 1;
197                         if ( temp[x] == '\n' || temp[x] == '\r' )
198                                 temp[x] = '\0';
199
200                         // Check if the line starts with a .
201                         if ( temp[ 0 ] == '.' )
202                         {
203                                 char temp2[ 1024 ];
204                                 sprintf( temp2, "%s%s", last, temp );
205                                 strcpy( temp, temp2 );
206                         }
207                         else if ( strchr( temp, '=' ) )
208                         {
209                                 strcpy( last, temp );
210                                 *( strchr( last, '=' ) ) = '\0';
211                         }
212
213                         // Parse and set the property
214                         if ( strcmp( temp, "" ) && temp[ 0 ] != '#' )
215                                 mlt_properties_parse( self, temp );
216                 }
217
218                 // Close the file
219                 fclose( file );
220         }
221         return file? 0 : errno;
222 }
223
224 /** Create a properties object by reading a .properties text file.
225  *
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
231  */
232
233 mlt_properties mlt_properties_load( const char *filename )
234 {
235         // Construct a standalone properties object
236         mlt_properties self = mlt_properties_new( );
237
238         if ( self != NULL )
239                 load_properties( self, filename );
240
241         // Return the pointer
242         return self;
243 }
244
245 /** Set properties from a preset.
246  *
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.
257  *
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
262  */
263
264 int mlt_properties_preset( mlt_properties self, const char *name )
265 {
266         struct stat stat_buff;
267
268         // validate input
269         if ( !( self && name && strlen( name ) ) )
270                 return 1;
271
272         // See if name is an explicit file
273         if ( ! stat( name, &stat_buff ) )
274         {
275                 return load_properties( self, name );
276         }
277         else
278         {
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" );
284                 int error = 0;
285
286                 if ( data )
287                 {
288                         data = strdup( data );
289                 }
290                 else
291                 {
292                         data = malloc( strlen( mlt_environment( "MLT_DATA" ) ) + strlen( PRESETS_DIR ) + 1 );
293                         strcpy( data, mlt_environment( "MLT_DATA" ) );
294                         strcat( data, PRESETS_DIR );
295                 }
296                 if ( data && type && service )
297                 {
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 ) )
301                         {
302                                 sprintf( path, "%s/%s/%s/%s", data, type, service, name );
303                                 error = load_properties( self, path );
304                         }
305                         free( path );
306                 }
307                 else
308                 {
309                         error = 1;
310                 }
311                 free( data );
312                 return error;
313         }
314 }
315
316 /** Generate a hash key.
317  *
318  * \private \memberof mlt_properties_s
319  * \param name a string
320  * \return an integer
321  */
322
323 static inline int generate_hash( const char *name )
324 {
325         int hash = 0;
326         int i = 1;
327         while ( *name )
328                 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
329         return hash;
330 }
331
332 /** Copy a serializable property to a properties list that is mirroring this one.
333  *
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
336  * real producer.
337  * \private \memberof mlt_properties_s
338  * \param self a properties list
339  * \param name the name of the property to copy
340  */
341
342 static inline void mlt_properties_do_mirror( mlt_properties self, const char *name )
343 {
344         property_list *list = self->local;
345         if ( list->mirror != NULL )
346         {
347                 char *value = mlt_properties_get( self, name );
348                 if ( value != NULL )
349                         mlt_properties_set( list->mirror, name, value );
350         }
351 }
352
353 /** Increment the reference count.
354  *
355  * \public \memberof mlt_properties_s
356  * \param self a properties list
357  * \return the new reference count
358  */
359
360 int mlt_properties_inc_ref( mlt_properties self )
361 {
362         int result = 0;
363         if ( self != NULL )
364         {
365                 property_list *list = self->local;
366                 pthread_mutex_lock( &list->mutex );
367                 result = ++ list->ref_count;
368                 pthread_mutex_unlock( &list->mutex );
369         }
370         return result;
371 }
372
373 /** Decrement the reference count.
374  *
375  * \public \memberof mlt_properties_s
376  * \param self a properties list
377  * \return the new reference count
378  */
379
380 int mlt_properties_dec_ref( mlt_properties self )
381 {
382         int result = 0;
383         if ( self != NULL )
384         {
385                 property_list *list = self->local;
386                 pthread_mutex_lock( &list->mutex );
387                 result = -- list->ref_count;
388                 pthread_mutex_unlock( &list->mutex );
389         }
390         return result;
391 }
392
393 /** Get the reference count.
394  *
395  * \public \memberof mlt_properties_s
396  * \param self a properties list
397  * \return the current reference count
398  */
399
400 int mlt_properties_ref_count( mlt_properties self )
401 {
402         if ( self != NULL )
403         {
404                 property_list *list = self->local;
405                 return list->ref_count;
406         }
407         return 0;
408 }
409
410 /** Set a properties list to be a mirror copy of another.
411  *
412  * Note that this does not copy all existing properties. Rather, you must
413  * call this before setting the properties that you wish to copy.
414  * \public \memberof mlt_properties_s
415  * \param that the properties which will receive copies of the properties as they are set.
416  * \param self the properties to mirror
417  */
418
419 void mlt_properties_mirror( mlt_properties self, mlt_properties that )
420 {
421         property_list *list = self->local;
422         list->mirror = that;
423 }
424
425 /** Copy all serializable properties to another properties list.
426  *
427  * \public \memberof mlt_properties_s
428  * \param self The properties to copy to
429  * \param that The properties to copy from
430  * \return false
431  */
432
433 int mlt_properties_inherit( mlt_properties self, mlt_properties that )
434 {
435         int count = mlt_properties_count( that );
436         int i = 0;
437         for ( i = 0; i < count; i ++ )
438         {
439                 char *value = mlt_properties_get_value( that, i );
440                 if ( value != NULL )
441                 {
442                         char *name = mlt_properties_get_name( that, i );
443                         mlt_properties_set( self, name, value );
444                 }
445         }
446         return 0;
447 }
448
449 /** Pass all serializable properties that match a prefix to another properties object
450  *
451  * \public \memberof mlt_properties_s
452  * \param self the properties to copy to
453  * \param that The properties to copy from
454  * \param prefix the property names to match (required)
455  * \return false
456  */
457
458 int mlt_properties_pass( mlt_properties self, mlt_properties that, const char *prefix )
459 {
460         int count = mlt_properties_count( that );
461         int length = strlen( prefix );
462         int i = 0;
463         for ( i = 0; i < count; i ++ )
464         {
465                 char *name = mlt_properties_get_name( that, i );
466                 if ( !strncmp( name, prefix, length ) )
467                 {
468                         char *value = mlt_properties_get_value( that, i );
469                         if ( value != NULL )
470                                 mlt_properties_set( self, name + length, value );
471                 }
472         }
473         return 0;
474 }
475
476 /** Locate a property by name.
477  *
478  * \private \memberof mlt_properties_s
479  * \param self a properties list
480  * \param name the property to lookup by name
481  * \return the property or NULL for failure
482  */
483
484 static inline mlt_property mlt_properties_find( mlt_properties self, const char *name )
485 {
486         property_list *list = self->local;
487         mlt_property value = NULL;
488         int key = generate_hash( name );
489
490         mlt_properties_lock( self );
491
492         int i = list->hash[ key ] - 1;
493         if ( i >= 0 )
494         {
495                 // Check if we're hashed
496                 if ( list->count > 0 &&
497                         name[ 0 ] == list->name[ i ][ 0 ] &&
498                         !strcmp( list->name[ i ], name ) )
499                         value = list->value[ i ];
500
501                 // Locate the item
502                 for ( i = list->count - 1; value == NULL && i >= 0; i -- )
503                         if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
504                                 value = list->value[ i ];
505         }
506         mlt_properties_unlock( self );
507
508         return value;
509 }
510
511 /** Add a new property.
512  *
513  * \private \memberof mlt_properties_s
514  * \param self a properties list
515  * \param name the name of the new property
516  * \return the new property
517  */
518
519 static mlt_property mlt_properties_add( mlt_properties self, const char *name )
520 {
521         property_list *list = self->local;
522         int key = generate_hash( name );
523         mlt_property result;
524
525         mlt_properties_lock( self );
526
527         // Check that we have space and resize if necessary
528         if ( list->count == list->size )
529         {
530                 list->size += 50;
531                 list->name = realloc( list->name, list->size * sizeof( const char * ) );
532                 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
533         }
534
535         // Assign name/value pair
536         list->name[ list->count ] = strdup( name );
537         list->value[ list->count ] = mlt_property_init( );
538
539         // Assign to hash table
540         if ( list->hash[ key ] == 0 )
541                 list->hash[ key ] = list->count + 1;
542
543         // Return and increment count accordingly
544         result = list->value[ list->count ++ ];
545
546         mlt_properties_unlock( self );
547
548         return result;
549 }
550
551 /** Fetch a property by name and add one if not found.
552  *
553  * \private \memberof mlt_properties_s
554  * \param self a properties list
555  * \param name the property to lookup or add
556  * \return the property
557  */
558
559 static mlt_property mlt_properties_fetch( mlt_properties self, const char *name )
560 {
561         // Try to find an existing property first
562         mlt_property property = mlt_properties_find( self, name );
563
564         // If it wasn't found, create one
565         if ( property == NULL )
566                 property = mlt_properties_add( self, name );
567
568         // Return the property
569         return property;
570 }
571
572 /** Copy a property to another properties list.
573  *
574  * \public \memberof mlt_properties_s
575  * \author Zach <zachary.drew@gmail.com>
576  * \param self the properties to copy to
577  * \param that the properties to copy from
578  * \param name the name of the property to copy
579  */
580
581 void mlt_properties_pass_property( mlt_properties self, mlt_properties that, const char *name )
582 {
583         // Make sure the source property isn't null.
584         mlt_property that_prop = mlt_properties_find( that, name );
585         if( that_prop == NULL )
586                 return;
587
588         mlt_property_pass( mlt_properties_fetch( self, name ), that_prop );
589 }
590
591 /** Copy all properties specified in a comma-separated list to another properties list.
592  *
593  * White space is also a delimiter.
594  * \public \memberof mlt_properties_s
595  * \author Zach <zachary.drew@gmail.com>
596  * \param self the properties to copy to
597  * \param that the properties to copy from
598  * \param list a delimited list of property names
599  * \return false
600  */
601
602
603 int mlt_properties_pass_list( mlt_properties self, mlt_properties that, const char *list )
604 {
605         char *props = strdup( list );
606         char *ptr = props;
607         const char *delim = " ,\t\n";   // Any combination of spaces, commas, tabs, and newlines
608         int count, done = 0;
609
610         while( !done )
611         {
612                 count = strcspn( ptr, delim );
613
614                 if( ptr[count] == '\0' )
615                         done = 1;
616                 else
617                         ptr[count] = '\0';      // Make it a real string
618
619                 mlt_properties_pass_property( self, that, ptr );
620
621                 ptr += count + 1;
622                 if ( !done )
623                         ptr += strspn( ptr, delim );
624         }
625
626         free( props );
627
628         return 0;
629 }
630
631
632 /** Set a property to a string.
633  *
634  * The property name "properties" is reserved to load the preset in \p value.
635  * When the value begins with '@' then it is interpreted as a very simple math
636  * expression containing only the +, -, *, and / operators.
637  * The event "property-changed" is fired after the property has been set.
638  *
639  * This makes a copy of the string value you supply.
640  * \public \memberof mlt_properties_s
641  * \param self a properties list
642  * \param name the property to set
643  * \param value the property's new value
644  * \return true if error
645  */
646
647 int mlt_properties_set( mlt_properties self, const char *name, const char *value )
648 {
649         int error = 1;
650
651         // Fetch the property to work with
652         mlt_property property = mlt_properties_fetch( self, name );
653
654         // Set it if not NULL
655         if ( property == NULL )
656         {
657                 mlt_log( NULL, MLT_LOG_FATAL, "Whoops - %s not found (should never occur)\n", name );
658         }
659         else if ( value == NULL )
660         {
661                 error = mlt_property_set_string( property, value );
662                 mlt_properties_do_mirror( self, name );
663         }
664         else if ( *value != '@' )
665         {
666                 error = mlt_property_set_string( property, value );
667                 mlt_properties_do_mirror( self, name );
668                 if ( !strcmp( name, "properties" ) )
669                         mlt_properties_preset( self, value );
670         }
671         else if ( value[ 0 ] == '@' )
672         {
673                 double total = 0;
674                 double current = 0;
675                 char id[ 255 ];
676                 char op = '+';
677
678                 value ++;
679
680                 while ( *value != '\0' )
681                 {
682                         int length = strcspn( value, "+-*/" );
683
684                         // Get the identifier
685                         strncpy( id, value, length );
686                         id[ length ] = '\0';
687                         value += length;
688
689                         // Determine the value
690                         if ( isdigit( id[ 0 ] ) )
691                                 current = atof( id );
692                         else
693                                 current = mlt_properties_get_double( self, id );
694
695                         // Apply the operation
696                         switch( op )
697                         {
698                                 case '+':
699                                         total += current;
700                                         break;
701                                 case '-':
702                                         total -= current;
703                                         break;
704                                 case '*':
705                                         total *= current;
706                                         break;
707                                 case '/':
708                                         total = total / current;
709                                         break;
710                         }
711
712                         // Get the next op
713                         op = *value != '\0' ? *value ++ : ' ';
714                 }
715
716                 error = mlt_property_set_double( property, total );
717                 mlt_properties_do_mirror( self, name );
718         }
719
720         mlt_events_fire( self, "property-changed", name, NULL );
721
722         return error;
723 }
724
725 /** Set or default a property to a string.
726  *
727  * This makes a copy of the string value you supply.
728  * \public \memberof mlt_properties_s
729  * \param self a properties list
730  * \param name the property to set
731  * \param value the string value to set or NULL to use the default
732  * \param def the default string if value is NULL
733  * \return true if error
734  */
735
736 int mlt_properties_set_or_default( mlt_properties self, const char *name, const char *value, const char *def )
737 {
738         return mlt_properties_set( self, name, value == NULL ? def : value );
739 }
740
741 /** Get a string value by name.
742  *
743  * Do not free the returned string. It's lifetime is controlled by the property
744  * and this properties object.
745  * \public \memberof mlt_properties_s
746  * \param self a properties list
747  * \param name the property to get
748  * \return the property's string value or NULL if it does not exist
749  */
750
751 char *mlt_properties_get( mlt_properties self, const char *name )
752 {
753         mlt_property value = mlt_properties_find( self, name );
754         property_list *list = self->local;
755         return value == NULL ? NULL : mlt_property_get_string_l( value, list->locale );
756 }
757
758 /** Get a property name by index.
759  *
760  * Do not free the returned string.
761  * \public \memberof mlt_properties_s
762  * \param self a properties list
763  * \param index the numeric index of the property
764  * \return the name of the property or NULL if index is out of range
765  */
766
767 char *mlt_properties_get_name( mlt_properties self, int index )
768 {
769         property_list *list = self->local;
770         if ( index >= 0 && index < list->count )
771                 return list->name[ index ];
772         return NULL;
773 }
774
775 /** Get a property's string value by index.
776  *
777  * Do not free the returned string.
778  * \public \memberof mlt_properties_s
779  * \param self a properties list
780  * \param index the numeric index of the property
781  * \return the property value as a string or NULL if the index is out of range
782  */
783
784 char *mlt_properties_get_value( mlt_properties self, int index )
785 {
786         property_list *list = self->local;
787         if ( index >= 0 && index < list->count )
788                 return mlt_property_get_string_l( list->value[ index ], list->locale );
789         return NULL;
790 }
791
792 /** Get a data value by index.
793  *
794  * Do not free the returned pointer if you supplied a destructor function when you
795  * set this property.
796  * \public \memberof mlt_properties_s
797  * \param self a properties list
798  * \param index the numeric index of the property
799  * \param[out] size the size of the binary data in bytes or NULL if the index is out of range
800  */
801
802 void *mlt_properties_get_data_at( mlt_properties self, int index, int *size )
803 {
804         property_list *list = self->local;
805         if ( index >= 0 && index < list->count )
806                 return mlt_property_get_data( list->value[ index ], size );
807         return NULL;
808 }
809
810 /** Return the number of items in the list.
811  *
812  * \public \memberof mlt_properties_s
813  * \param self a properties list
814  * \return the number of property objects
815  */
816
817 int mlt_properties_count( mlt_properties self )
818 {
819         property_list *list = self->local;
820         return list->count;
821 }
822
823 /** Set a value by parsing a name=value string.
824  *
825  * \public \memberof mlt_properties_s
826  * \param self a properties list
827  * \param namevalue a string containing name and value delimited by '='
828  * \return true if there was an error
829  */
830
831 int mlt_properties_parse( mlt_properties self, const char *namevalue )
832 {
833         char *name = strdup( namevalue );
834         char *value = NULL;
835         int error = 0;
836         char *ptr = strchr( name, '=' );
837
838         if ( ptr )
839         {
840                 *( ptr ++ ) = '\0';
841
842                 if ( *ptr != '\"' )
843                 {
844                         value = strdup( ptr );
845                 }
846                 else
847                 {
848                         ptr ++;
849                         value = strdup( ptr );
850                         if ( value != NULL && value[ strlen( value ) - 1 ] == '\"' )
851                                 value[ strlen( value ) - 1 ] = '\0';
852                 }
853         }
854         else
855         {
856                 value = strdup( "" );
857         }
858
859         error = mlt_properties_set( self, name, value );
860
861         free( name );
862         free( value );
863
864         return error;
865 }
866
867 /** Get an integer associated to the name.
868  *
869  * \public \memberof mlt_properties_s
870  * \param self a properties list
871  * \param name the property to get
872  * \return The integer value, 0 if not found (which may also be a legitimate value)
873  */
874
875 int mlt_properties_get_int( mlt_properties self, const char *name )
876 {
877         mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
878         double fps = mlt_profile_fps( profile );
879         property_list *list = self->local;
880         mlt_property value = mlt_properties_find( self, name );
881         return value == NULL ? 0 : mlt_property_get_int( value, fps, list->locale );
882 }
883
884 /** Set a property to an integer value.
885  *
886  * \public \memberof mlt_properties_s
887  * \param self a properties list
888  * \param name the property to set
889  * \param value the integer
890  * \return true if error
891  */
892
893 int mlt_properties_set_int( mlt_properties self, const char *name, int value )
894 {
895         int error = 1;
896
897         // Fetch the property to work with
898         mlt_property property = mlt_properties_fetch( self, name );
899
900         // Set it if not NULL
901         if ( property != NULL )
902         {
903                 error = mlt_property_set_int( property, value );
904                 mlt_properties_do_mirror( self, name );
905         }
906
907         mlt_events_fire( self, "property-changed", name, NULL );
908
909         return error;
910 }
911
912 /** Get a 64-bit integer associated to the name.
913  *
914  * \public \memberof mlt_properties_s
915  * \param self a properties list
916  * \param name the property to get
917  * \return the integer value, 0 if not found (which may also be a legitimate value)
918  */
919
920 int64_t mlt_properties_get_int64( mlt_properties self, const char *name )
921 {
922         mlt_property value = mlt_properties_find( self, name );
923         return value == NULL ? 0 : mlt_property_get_int64( value );
924 }
925
926 /** Set a property to a 64-bit integer value.
927  *
928  * \public \memberof mlt_properties_s
929  * \param self a properties list
930  * \param name the property to set
931  * \param value the integer
932  * \return true if error
933  */
934
935 int mlt_properties_set_int64( mlt_properties self, const char *name, int64_t value )
936 {
937         int error = 1;
938
939         // Fetch the property to work with
940         mlt_property property = mlt_properties_fetch( self, name );
941
942         // Set it if not NULL
943         if ( property != NULL )
944         {
945                 error = mlt_property_set_int64( property, value );
946                 mlt_properties_do_mirror( self, name );
947         }
948
949         mlt_events_fire( self, "property-changed", name, NULL );
950
951         return error;
952 }
953
954 /** Get a floating point value associated to the name.
955  *
956  * \public \memberof mlt_properties_s
957  * \param self a properties list
958  * \param name the property to get
959  * \return the floating point, 0 if not found (which may also be a legitimate value)
960  */
961
962 double mlt_properties_get_double( mlt_properties self, const char *name )
963 {
964         mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
965         double fps = mlt_profile_fps( profile );
966         mlt_property value = mlt_properties_find( self, name );
967         property_list *list = self->local;
968         return value == NULL ? 0 : mlt_property_get_double( value, fps, list->locale );
969 }
970
971 /** Set a property to a floating point value.
972  *
973  * \public \memberof mlt_properties_s
974  * \param self a properties list
975  * \param name the property to set
976  * \param value the floating point value
977  * \return true if error
978  */
979
980 int mlt_properties_set_double( mlt_properties self, const char *name, double value )
981 {
982         int error = 1;
983
984         // Fetch the property to work with
985         mlt_property property = mlt_properties_fetch( self, name );
986
987         // Set it if not NULL
988         if ( property != NULL )
989         {
990                 error = mlt_property_set_double( property, value );
991                 mlt_properties_do_mirror( self, name );
992         }
993
994         mlt_events_fire( self, "property-changed", name, NULL );
995
996         return error;
997 }
998
999 /** Get a position value associated to the name.
1000  *
1001  * \public \memberof mlt_properties_s
1002  * \param self a properties list
1003  * \param name the property to get
1004  * \return the position, 0 if not found (which may also be a legitimate value)
1005  */
1006
1007 mlt_position mlt_properties_get_position( mlt_properties self, const char *name )
1008 {
1009         mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
1010         double fps = mlt_profile_fps( profile );
1011         property_list *list = self->local;
1012         mlt_property value = mlt_properties_find( self, name );
1013         return value == NULL ? 0 : mlt_property_get_position( value, fps, list->locale );
1014 }
1015
1016 /** Set a property to a position value.
1017  *
1018  * \public \memberof mlt_properties_s
1019  * \param self a properties list
1020  * \param name the property to get
1021  * \param value the position
1022  * \return true if error
1023  */
1024
1025 int mlt_properties_set_position( mlt_properties self, const char *name, mlt_position value )
1026 {
1027         int error = 1;
1028
1029         // Fetch the property to work with
1030         mlt_property property = mlt_properties_fetch( self, name );
1031
1032         // Set it if not NULL
1033         if ( property != NULL )
1034         {
1035                 error = mlt_property_set_position( property, value );
1036                 mlt_properties_do_mirror( self, name );
1037         }
1038
1039         mlt_events_fire( self, "property-changed", name, NULL );
1040
1041         return error;
1042 }
1043
1044 /** Get a binary data value associated to the name.
1045  *
1046  * Do not free the returned pointer if you supplied a destructor function
1047  * when you set this property.
1048  * \public \memberof mlt_properties_s
1049  * \param self a properties list
1050  * \param name the property to get
1051  * \param[out] length The size of the binary data in bytes, if available (often it is not, you should know)
1052  */
1053
1054 void *mlt_properties_get_data( mlt_properties self, const char *name, int *length )
1055 {
1056         mlt_property value = mlt_properties_find( self, name );
1057         return value == NULL ? NULL : mlt_property_get_data( value, length );
1058 }
1059
1060 /** Store binary data as a property.
1061  *
1062  * \public \memberof mlt_properties_s
1063  * \param self a properties list
1064  * \param name the property to set
1065  * \param value an opaque pointer to binary data
1066  * \param length the size of the binary data in bytes (optional)
1067  * \param destroy a function to deallocate the binary data when the property is closed (optional)
1068  * \param serialise a function that can serialize the binary data as text (optional)
1069  * \return true if error
1070  */
1071
1072 int mlt_properties_set_data( mlt_properties self, const char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
1073 {
1074         int error = 1;
1075
1076         // Fetch the property to work with
1077         mlt_property property = mlt_properties_fetch( self, name );
1078
1079         // Set it if not NULL
1080         if ( property != NULL )
1081                 error = mlt_property_set_data( property, value, length, destroy, serialise );
1082
1083         mlt_events_fire( self, "property-changed", name, NULL );
1084
1085         return error;
1086 }
1087
1088 /** Rename a property.
1089  *
1090  * \public \memberof mlt_properties_s
1091  * \param self a properties list
1092  * \param source the property to rename
1093  * \param dest the new name
1094  * \return true if the name is already in use
1095  */
1096
1097 int mlt_properties_rename( mlt_properties self, const char *source, const char *dest )
1098 {
1099         mlt_property value = mlt_properties_find( self, dest );
1100
1101         if ( value == NULL )
1102         {
1103                 property_list *list = self->local;
1104                 int i = 0;
1105
1106                 // Locate the item
1107                 mlt_properties_lock( self );
1108                 for ( i = 0; i < list->count; i ++ )
1109                 {
1110                         if ( !strcmp( list->name[ i ], source ) )
1111                         {
1112                                 free( list->name[ i ] );
1113                                 list->name[ i ] = strdup( dest );
1114                                 list->hash[ generate_hash( dest ) ] = i + 1;
1115                                 break;
1116                         }
1117                 }
1118                 mlt_properties_unlock( self );
1119         }
1120
1121         return value != NULL;
1122 }
1123
1124 /** Dump the properties to a file handle.
1125  *
1126  * \public \memberof mlt_properties_s
1127  * \param self a properties list
1128  * \param output a file handle
1129  */
1130
1131 void mlt_properties_dump( mlt_properties self, FILE *output )
1132 {
1133         property_list *list = self->local;
1134         int i = 0;
1135         for ( i = 0; i < list->count; i ++ )
1136                 if ( mlt_properties_get( self, list->name[ i ] ) != NULL )
1137                         fprintf( output, "%s=%s\n", list->name[ i ], mlt_properties_get( self, list->name[ i ] ) );
1138 }
1139
1140 /** Output the properties to a file handle.
1141  *
1142  * This version includes reference counts and does not put each property on a new line.
1143  * \public \memberof mlt_properties_s
1144  * \param self a properties pointer
1145  * \param title a string to preface the output
1146  * \param output a file handle
1147  */
1148 void mlt_properties_debug( mlt_properties self, const char *title, FILE *output )
1149 {
1150         if ( output == NULL ) output = stderr;
1151         fprintf( output, "%s: ", title );
1152         if ( self != NULL )
1153         {
1154                 property_list *list = self->local;
1155                 int i = 0;
1156                 fprintf( output, "[ ref=%d", list->ref_count );
1157                 for ( i = 0; i < list->count; i ++ )
1158                         if ( mlt_properties_get( self, list->name[ i ] ) != NULL )
1159                                 fprintf( output, ", %s=%s", list->name[ i ], mlt_properties_get( self, list->name[ i ] ) );
1160                         else
1161                                 fprintf( output, ", %s=%p", list->name[ i ], mlt_properties_get_data( self, list->name[ i ], NULL ) );
1162                 fprintf( output, " ]" );
1163         }
1164         fprintf( output, "\n" );
1165 }
1166
1167 /** Save the properties to a file by name.
1168  *
1169  * This uses the dump format - one line per property.
1170  * \public \memberof mlt_properties_s
1171  * \param self a properties list
1172  * \param filename the name of a file to create or overwrite
1173  * \return true if there was an error
1174  */
1175
1176 int mlt_properties_save( mlt_properties self, const char *filename )
1177 {
1178         int error = 1;
1179         FILE *f = fopen( filename, "w" );
1180         if ( f != NULL )
1181         {
1182                 mlt_properties_dump( self, f );
1183                 fclose( f );
1184                 error = 0;
1185         }
1186         return error;
1187 }
1188
1189 /* This is a very basic cross platform fnmatch replacement - it will fail in
1190  * many cases, but for the basic *.XXX and YYY*.XXX, it will work ok.
1191  */
1192
1193 /** Test whether a filename or pathname matches a shell-style pattern.
1194  *
1195  * \private \memberof mlt_properties_s
1196  * \param wild a string containing a wildcard pattern
1197  * \param file the name of a file to test against
1198  * \return true if the file name matches the wildcard pattern
1199  */
1200
1201 static int mlt_fnmatch( const char *wild, const char *file )
1202 {
1203         int f = 0;
1204         int w = 0;
1205
1206         while( f < strlen( file ) && w < strlen( wild ) )
1207         {
1208                 if ( wild[ w ] == '*' )
1209                 {
1210                         w ++;
1211                         if ( w == strlen( wild ) )
1212                                 f = strlen( file );
1213                         while ( f != strlen( file ) && tolower( file[ f ] ) != tolower( wild[ w ] ) )
1214                                 f ++;
1215                 }
1216                 else if ( wild[ w ] == '?' || tolower( file[ f ] ) == tolower( wild[ w ] ) )
1217                 {
1218                         f ++;
1219                         w ++;
1220                 }
1221                 else if ( wild[ 0 ] == '*' )
1222                 {
1223                         w = 0;
1224                 }
1225                 else
1226                 {
1227                         return 0;
1228                 }
1229         }
1230
1231         return strlen( file ) == f &&  strlen( wild ) == w;
1232 }
1233
1234 /** Compare the string or serialized value of two properties.
1235  *
1236  * \private \memberof mlt_properties_s
1237  * \param self a property
1238  * \param that a property
1239  * \return < 0 if \p self less than \p that, 0 if equal, or > 0 if \p self is greater than \p that
1240  */
1241
1242 static int mlt_compare( const void *self, const void *that )
1243 {
1244     return strcmp( mlt_property_get_string( *( const mlt_property * )self ), mlt_property_get_string( *( const mlt_property * )that ) );
1245 }
1246
1247 /** Get the contents of a directory.
1248  *
1249  * Obtains an optionally sorted list of the files found in a directory with a specific wild card.
1250  * Entries in the list have a numeric name (running from 0 to count - 1). Only values change
1251  * position if sort is enabled. Designed to be posix compatible (linux, os/x, mingw etc).
1252  * \public \memberof mlt_properties_s
1253  * \param self a properties list
1254  * \param dirname the name of the directory
1255  * \param pattern a wildcard pattern to filter the directory listing
1256  * \param sort Do you want to sort the directory listing?
1257  * \return the number of items in the directory listing
1258  */
1259
1260 int mlt_properties_dir_list( mlt_properties self, const char *dirname, const char *pattern, int sort )
1261 {
1262         DIR *dir = opendir( dirname );
1263
1264         if ( dir )
1265         {
1266                 char key[ 20 ];
1267                 struct dirent *de = readdir( dir );
1268                 char fullname[ 1024 ];
1269                 while( de != NULL )
1270                 {
1271                         sprintf( key, "%d", mlt_properties_count( self ) );
1272                         snprintf( fullname, 1024, "%s/%s", dirname, de->d_name );
1273                         if ( pattern == NULL )
1274                                 mlt_properties_set( self, key, fullname );
1275                         else if ( de->d_name[ 0 ] != '.' && mlt_fnmatch( pattern, de->d_name ) )
1276                                 mlt_properties_set( self, key, fullname );
1277                         de = readdir( dir );
1278                 }
1279
1280                 closedir( dir );
1281         }
1282
1283         if ( sort && mlt_properties_count( self ) )
1284         {
1285                 property_list *list = self->local;
1286                 mlt_properties_lock( self );
1287                 qsort( list->value, mlt_properties_count( self ), sizeof( mlt_property ), mlt_compare );
1288                 mlt_properties_unlock( self );
1289         }
1290
1291         return mlt_properties_count( self );
1292 }
1293
1294 /** Close a properties object.
1295  *
1296  * Deallocates the properties object and everything it contains.
1297  * \public \memberof mlt_properties_s
1298  * \param self a properties object
1299  */
1300
1301 void mlt_properties_close( mlt_properties self )
1302 {
1303         if ( self != NULL && mlt_properties_dec_ref( self ) <= 0 )
1304         {
1305                 if ( self->close != NULL )
1306                 {
1307                         self->close( self->close_object );
1308                 }
1309                 else
1310                 {
1311                         property_list *list = self->local;
1312                         int index = 0;
1313
1314 #if _MLT_PROPERTY_CHECKS_ == 1
1315                         // Show debug info
1316                         mlt_properties_debug( self, "Closing", stderr );
1317 #endif
1318
1319 #ifdef _MLT_PROPERTY_CHECKS_
1320                         // Increment destroyed count
1321                         properties_destroyed ++;
1322
1323                         // Show current stats - these should match when the app is closed
1324                         mlt_log( NULL, MLT_LOG_DEBUG, "Created %d, destroyed %d\n", properties_created, properties_destroyed );
1325 #endif
1326
1327                         // Clean up names and values
1328                         for ( index = list->count - 1; index >= 0; index -- )
1329                         {
1330                                 mlt_property_close( list->value[ index ] );
1331                                 free( list->name[ index ] );
1332                         }
1333
1334 #if defined(__linux__) || defined(__DARWIN__)
1335                         // Cleanup locale
1336                         if ( list->locale )
1337                                 freelocale( list->locale );
1338 #endif
1339
1340                         // Clear up the list
1341                         pthread_mutex_destroy( &list->mutex );
1342                         free( list->name );
1343                         free( list->value );
1344                         free( list );
1345
1346                         // Free self now if self has no child
1347                         if ( self->child == NULL )
1348                                 free( self );
1349                 }
1350         }
1351 }
1352
1353 /** Determine if the properties list is really just a sequence or ordered list.
1354  *
1355  * \public \memberof mlt_properties_s
1356  * \param properties a properties list
1357  * \return true if all of the property names are numeric (a sequence)
1358  */
1359
1360 int mlt_properties_is_sequence( mlt_properties properties )
1361 {
1362         int i;
1363         int n = mlt_properties_count( properties );
1364         for ( i = 0; i < n; i++ )
1365                 if ( ! isdigit( mlt_properties_get_name( properties, i )[0] ) )
1366                         return 0;
1367         return 1;
1368 }
1369
1370 /** \brief YAML Tiny Parser context structure
1371  *
1372  * YAML is a nifty text format popular in the Ruby world as a cleaner,
1373  * less verbose alternative to XML. See this Wikipedia topic for an overview:
1374  * http://en.wikipedia.org/wiki/YAML
1375  * The YAML specification is at:
1376  * http://yaml.org/
1377  * YAML::Tiny is a Perl module that specifies a subset of YAML that we are
1378  * using here (for the same reasons):
1379  * http://search.cpan.org/~adamk/YAML-Tiny-1.25/lib/YAML/Tiny.pm
1380  * \private
1381  */
1382
1383 struct yaml_parser_context
1384 {
1385         mlt_deque stack;
1386         unsigned int level;
1387         unsigned int index;
1388         char block;
1389         char *block_name;
1390         unsigned int block_indent;
1391
1392 };
1393 typedef struct yaml_parser_context *yaml_parser;
1394
1395 /** Remove spaces from the left side of a string.
1396  *
1397  * \param s the string to trim
1398  * \return the number of characters removed
1399  */
1400
1401 static unsigned int ltrim( char **s )
1402 {
1403         unsigned int i = 0;
1404         char *c = *s;
1405         int n = strlen( c );
1406         for ( i = 0; i < n && *c == ' '; i++, c++ );
1407         *s = c;
1408         return i;
1409 }
1410
1411 /** Remove spaces from the right side of a string.
1412  *
1413  * \param s the string to trim
1414  * \return the number of characters removed
1415  */
1416
1417 static unsigned int rtrim( char *s )
1418 {
1419         int n = strlen( s );
1420         int i;
1421         for ( i = n; i > 0 && s[i - 1] == ' '; --i )
1422                 s[i - 1] = 0;
1423         return n - i;
1424 }
1425
1426 /** Parse a line of YAML Tiny.
1427  *
1428  * Adds a property if needed.
1429  * \private \memberof yaml_parser_context
1430  * \param context a YAML Tiny Parser context
1431  * \param namevalue a line of YAML Tiny
1432  * \return true if there was an error
1433  */
1434
1435 static int parse_yaml( yaml_parser context, const char *namevalue )
1436 {
1437         char *name_ = strdup( namevalue );
1438         char *name = name_;
1439         char *value = NULL;
1440         int error = 0;
1441         char *ptr = strchr( name, ':' );
1442         unsigned int indent = ltrim( &name );
1443         mlt_properties properties = mlt_deque_peek_front( context->stack );
1444
1445         // Ascending one more levels in the tree
1446         if ( indent < context->level )
1447         {
1448                 unsigned int i;
1449                 unsigned int n = ( context->level - indent ) / 2;
1450                 for ( i = 0; i < n; i++ )
1451                         mlt_deque_pop_front( context->stack );
1452                 properties = mlt_deque_peek_front( context->stack );
1453                 context->level = indent;
1454         }
1455
1456         // Descending a level in the tree
1457         else if ( indent > context->level && context->block == 0 )
1458         {
1459                 context->level = indent;
1460         }
1461
1462         // If there is a colon that is not part of a block
1463         if ( ptr && ( indent == context->level ) )
1464         {
1465                 // Reset block processing
1466                 if ( context->block_name )
1467                 {
1468                         free( context->block_name );
1469                         context->block_name = NULL;
1470                         context->block = 0;
1471                 }
1472
1473                 // Terminate the name and setup the value pointer
1474                 *( ptr ++ ) = 0;
1475
1476                 // Trim comment
1477                 char *comment = strchr( ptr, '#' );
1478                 if ( comment )
1479                 {
1480                         *comment = 0;
1481                 }
1482
1483                 // Trim leading and trailing spaces from bare value
1484                 ltrim( &ptr );
1485                 rtrim( ptr );
1486
1487                 // No value means a child
1488                 if ( strcmp( ptr, "" ) == 0 )
1489                 {
1490                         mlt_properties child = mlt_properties_new();
1491                         mlt_properties_set_lcnumeric( child, mlt_properties_get_lcnumeric( properties ) );
1492                         mlt_properties_set_data( properties, name, child, 0,
1493                                 ( mlt_destructor )mlt_properties_close, NULL );
1494                         mlt_deque_push_front( context->stack, child );
1495                         context->index = 0;
1496                         free( name_ );
1497                         return error;
1498                 }
1499
1500                 // A dash indicates a sequence item
1501                 if ( name[0] == '-' )
1502                 {
1503                         mlt_properties child = mlt_properties_new();
1504                         char key[20];
1505
1506                         mlt_properties_set_lcnumeric( child, mlt_properties_get_lcnumeric( properties ) );
1507                         snprintf( key, sizeof(key), "%d", context->index++ );
1508                         mlt_properties_set_data( properties, key, child, 0,
1509                                 ( mlt_destructor )mlt_properties_close, NULL );
1510                         mlt_deque_push_front( context->stack, child );
1511
1512                         name ++;
1513                         context->level += ltrim( &name ) + 1;
1514                         properties = child;
1515                 }
1516
1517                 // Value is quoted
1518                 if ( *ptr == '\"' )
1519                 {
1520                         ptr ++;
1521                         value = strdup( ptr );
1522                         if ( value && value[ strlen( value ) - 1 ] == '\"' )
1523                                 value[ strlen( value ) - 1 ] = 0;
1524                 }
1525
1526                 // Value is folded or unfolded block
1527                 else if ( *ptr == '|' || *ptr == '>' )
1528                 {
1529                         context->block = *ptr;
1530                         context->block_name = strdup( name );
1531                         context->block_indent = 0;
1532                         value = strdup( "" );
1533                 }
1534
1535                 // Bare value
1536                 else
1537                 {
1538                         value = strdup( ptr );
1539                 }
1540         }
1541
1542         // A list of scalars
1543         else if ( name[0] == '-' )
1544         {
1545                 // Reset block processing
1546                 if ( context->block_name )
1547                 {
1548                         free( context->block_name );
1549                         context->block_name = NULL;
1550                         context->block = 0;
1551                 }
1552
1553                 char key[20];
1554
1555                 snprintf( key, sizeof(key), "%d", context->index++ );
1556                 ptr = name + 1;
1557
1558                 // Trim comment
1559                 char *comment = strchr( ptr, '#' );
1560                 if ( comment )
1561                         *comment = 0;
1562
1563                 // Trim leading and trailing spaces from bare value
1564                 ltrim( &ptr );
1565                 rtrim( ptr );
1566
1567                 // Value is quoted
1568                 if ( *ptr == '\"' )
1569                 {
1570                         ptr ++;
1571                         value = strdup( ptr );
1572                         if ( value && value[ strlen( value ) - 1 ] == '\"' )
1573                                 value[ strlen( value ) - 1 ] = 0;
1574                 }
1575
1576                 // Value is folded or unfolded block
1577                 else if ( *ptr == '|' || *ptr == '>' )
1578                 {
1579                         context->block = *ptr;
1580                         context->block_name = strdup( key );
1581                         context->block_indent = 0;
1582                         value = strdup( "" );
1583                 }
1584
1585                 // Bare value
1586                 else
1587                 {
1588                         value = strdup( ptr );
1589                 }
1590
1591                 free( name_ );
1592                 name = name_ = strdup( key );
1593         }
1594
1595         // Non-folded block
1596         else if ( context->block == '|' )
1597         {
1598                 if ( context->block_indent == 0 )
1599                         context->block_indent = indent;
1600                 if ( indent > context->block_indent )
1601                         name = &name_[ context->block_indent ];
1602                 rtrim( name );
1603                 char *old_value = mlt_properties_get( properties, context->block_name );
1604                 value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
1605                 strcpy( value, old_value );
1606                 if ( strcmp( old_value, "" ) )
1607                         strcat( value, "\n" );
1608                 strcat( value, name );
1609                 name = context->block_name;
1610         }
1611
1612         // Folded block
1613         else if ( context->block == '>' )
1614         {
1615                 ltrim( &name );
1616                 rtrim( name );
1617                 char *old_value = mlt_properties_get( properties, context->block_name );
1618
1619                 // Blank line (prepended with spaces) is new line
1620                 if ( strcmp( name, "" ) == 0 )
1621                 {
1622                         value = calloc( 1, strlen( old_value ) + 2 );
1623                         strcat( value, old_value );
1624                         strcat( value, "\n" );
1625                 }
1626                 // Concatenate with space
1627                 else
1628                 {
1629                         value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
1630                         strcat( value, old_value );
1631                         if ( strcmp( old_value, "" ) && old_value[ strlen( old_value ) - 1 ] != '\n' )
1632                                 strcat( value, " " );
1633                         strcat( value, name );
1634                 }
1635                 name = context->block_name;
1636         }
1637
1638         else
1639         {
1640                 value = strdup( "" );
1641         }
1642
1643         error = mlt_properties_set( properties, name, value );
1644
1645         if ( !strcmp( name, "LC_NUMERIC" ) )
1646                 mlt_properties_set_lcnumeric( properties, value );
1647
1648         free( name_ );
1649         free( value );
1650
1651         return error;
1652 }
1653
1654 /** Parse a YAML Tiny file by name.
1655  *
1656  * \public \memberof mlt_properties_s
1657  * \param filename the name of a text file containing YAML Tiny
1658  * \return a new properties list
1659  */
1660
1661 mlt_properties mlt_properties_parse_yaml( const char *filename )
1662 {
1663         // Construct a standalone properties object
1664         mlt_properties self = mlt_properties_new( );
1665
1666         if ( self )
1667         {
1668                 // Open the file
1669                 FILE *file = fopen( filename, "r" );
1670
1671                 // Load contents of file
1672                 if ( file )
1673                 {
1674                         // Temp string
1675                         char temp[ 1024 ];
1676                         char *ptemp = &temp[ 0 ];
1677
1678                         // Default to LC_NUMERIC = C
1679                         mlt_properties_set_lcnumeric( self, "C" );
1680
1681                         // Parser context
1682                         yaml_parser context = calloc( 1, sizeof( struct yaml_parser_context ) );
1683                         context->stack = mlt_deque_init();
1684                         mlt_deque_push_front( context->stack, self );
1685
1686                         // Read each string from the file
1687                         while( fgets( temp, 1024, file ) )
1688                         {
1689                                 // Check for end-of-stream
1690                                 if ( strncmp( ptemp, "...", 3 ) == 0 )
1691                                         break;
1692
1693                                 // Chomp the string
1694                                 temp[ strlen( temp ) - 1 ] = '\0';
1695
1696                                 // Skip blank lines, comment lines, and document separator
1697                                 if ( strcmp( ptemp, "" ) && ptemp[ 0 ] != '#' && strncmp( ptemp, "---", 3 )
1698                                      && strncmp( ptemp, "%YAML", 5 ) && strncmp( ptemp, "% YAML", 6 ) )
1699                                         parse_yaml( context, temp );
1700                         }
1701
1702                         // Close the file
1703                         fclose( file );
1704                         mlt_deque_close( context->stack );
1705                         if ( context->block_name )
1706                                 free( context->block_name );
1707                         free( context );
1708                 }
1709         }
1710
1711         // Return the pointer
1712         return self;
1713 }
1714
1715 /*
1716  * YAML Tiny Serializer
1717  */
1718
1719 /** How many bytes to grow at a time */
1720 #define STRBUF_GROWTH (1024)
1721
1722 /** \brief Private to mlt_properties_s, a self-growing buffer for building strings
1723  * \private
1724  */
1725
1726 struct strbuf_s
1727 {
1728         size_t size;
1729         char *string;
1730 };
1731
1732 typedef struct strbuf_s *strbuf;
1733
1734 /** Create a new string buffer
1735  *
1736  * \private \memberof strbuf_s
1737  * \return a new string buffer
1738  */
1739
1740 static strbuf strbuf_new( )
1741 {
1742         strbuf buffer = calloc( 1, sizeof( struct strbuf_s ) );
1743         buffer->size = STRBUF_GROWTH;
1744         buffer->string = calloc( 1, buffer->size );
1745         return buffer;
1746 }
1747
1748 /** Destroy a string buffer
1749  *
1750  * \private \memberof strbuf_s
1751  * \param buffer the string buffer to close
1752  */
1753
1754 static void strbuf_close( strbuf buffer )
1755 {
1756         // We do not free buffer->string; strbuf user must save that pointer
1757         // and free it.
1758         if ( buffer )
1759                 free( buffer );
1760 }
1761
1762 /** Format a string into a string buffer
1763  *
1764  * A variable number of arguments follows the format string - one for each
1765  * format specifier.
1766  * \private \memberof strbuf_s
1767  * \param buffer the string buffer to write into
1768  * \param format a string that contains text and formatting instructions
1769  * \return the formatted string
1770  */
1771
1772 static char *strbuf_printf( strbuf buffer, const char *format, ... )
1773 {
1774         while ( buffer->string )
1775         {
1776                 va_list ap;
1777                 va_start( ap, format );
1778                 size_t len = strlen( buffer->string );
1779                 size_t remain = buffer->size - len - 1;
1780                 int need = vsnprintf( buffer->string + len, remain, format, ap );
1781                 va_end( ap );
1782                 if ( need > -1 && need < remain )
1783                         break;
1784                 buffer->string[ len ] = 0;
1785                 buffer->size += need + STRBUF_GROWTH;
1786                 buffer->string = realloc( buffer->string, buffer->size );
1787         }
1788         return buffer->string;
1789 }
1790
1791 /** Indent a line of YAML Tiny.
1792  *
1793  * \private \memberof strbuf_s
1794  * \param output a string buffer
1795  * \param indent the number of spaces to indent
1796  */
1797
1798 static inline void indent_yaml( strbuf output, int indent )
1799 {
1800         int j;
1801         for ( j = 0; j < indent; j++ )
1802                 strbuf_printf( output, " " );
1803 }
1804
1805 /** Convert a line string into a YAML block literal.
1806  *
1807  * \private \memberof strbuf_s
1808  * \param output a string buffer
1809  * \param value the string to format as a block literal
1810  * \param indent the number of spaces to indent
1811  */
1812
1813 static void output_yaml_block_literal( strbuf output, const char *value, int indent )
1814 {
1815         char *v = strdup( value );
1816         char *sol = v;
1817         char *eol = strchr( sol, '\n' );
1818
1819         while ( eol )
1820         {
1821                 indent_yaml( output, indent );
1822                 *eol = '\0';
1823                 strbuf_printf( output, "%s\n", sol );
1824                 sol = eol + 1;
1825                 eol = strchr( sol, '\n' );
1826         }
1827         indent_yaml( output, indent );
1828         strbuf_printf( output, "%s\n", sol );
1829 }
1830
1831 /** Recursively serialize a properties list into a string buffer as YAML Tiny.
1832  *
1833  * \private \memberof mlt_properties_s
1834  * \param self a properties list
1835  * \param output a string buffer to hold the serialized YAML Tiny
1836  * \param indent the number of spaces to indent (for recursion, initialize to 0)
1837  * \param is_parent_sequence Is this properties list really just a sequence (for recursion, initialize to 0)?
1838  */
1839
1840 static void serialise_yaml( mlt_properties self, strbuf output, int indent, int is_parent_sequence )
1841 {
1842         property_list *list = self->local;
1843         int i = 0;
1844
1845         for ( i = 0; i < list->count; i ++ )
1846         {
1847                 // This implementation assumes that all data elements are property lists.
1848                 // Unfortunately, we do not have run time type identification.
1849                 mlt_properties child = mlt_property_get_data( list->value[ i ], NULL );
1850
1851                 if ( mlt_properties_is_sequence( self ) )
1852                 {
1853                         // Ignore hidden/non-serialisable items
1854                         if ( list->name[ i ][ 0 ] != '_' )
1855                         {
1856                                 // Indicate a sequence item
1857                                 indent_yaml( output, indent );
1858                                 strbuf_printf( output, "- " );
1859
1860                                 // If the value can be represented as a string
1861                                 const char *value = mlt_properties_get( self, list->name[ i ] );
1862                                 if ( value && strcmp( value, "" ) )
1863                                 {
1864                                         // Determine if this is an unfolded block literal
1865                                         if ( strchr( value, '\n' ) )
1866                                         {
1867                                                 strbuf_printf( output, "|\n" );
1868                                                 output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( "|" ) );
1869                                         }
1870                                         else
1871                                         {
1872                                                 strbuf_printf( output, "%s\n", value );
1873                                         }
1874                                 }
1875                         }
1876                         // Recurse on child
1877                         if ( child )
1878                                 serialise_yaml( child, output, indent + 2, 1 );
1879                 }
1880                 else
1881                 {
1882                         // Assume this is a normal map-oriented properties list
1883                         const char *value = mlt_properties_get( self, list->name[ i ] );
1884
1885                         // Ignore hidden/non-serialisable items
1886                         // If the value can be represented as a string
1887                         if ( list->name[ i ][ 0 ] != '_' && value && strcmp( value, "" ) )
1888                         {
1889                                 if ( is_parent_sequence == 0 )
1890                                         indent_yaml( output, indent );
1891                                 else
1892                                         is_parent_sequence = 0;
1893
1894                                 // Determine if this is an unfolded block literal
1895                                 if ( strchr( value, '\n' ) )
1896                                 {
1897                                         strbuf_printf( output, "%s: |\n", list->name[ i ] );
1898                                         output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( ": " ) );
1899                                 }
1900                                 else
1901                                 {
1902                                         strbuf_printf( output, "%s: %s\n", list->name[ i ], value );
1903                                 }
1904                         }
1905
1906                         // Output a child as a map item
1907                         if ( child )
1908                         {
1909                                 indent_yaml( output, indent );
1910                                 strbuf_printf( output, "%s:\n", list->name[ i ] );
1911
1912                                 // Recurse on child
1913                                 serialise_yaml( child, output, indent + 2, 0 );
1914                         }
1915                 }
1916         }
1917 }
1918
1919 /** Serialize a properties list as a string of YAML Tiny.
1920  *
1921  * The caller MUST free the returned string!
1922  * This operates on properties containing properties as a hierarchical data
1923  * structure.
1924  * \public \memberof mlt_properties_s
1925  * \param self a properties list
1926  * \return a string containing YAML Tiny that represents the properties list
1927  */
1928
1929 char *mlt_properties_serialise_yaml( mlt_properties self )
1930 {
1931         const char *lc_numeric = mlt_properties_get_lcnumeric( self );
1932         strbuf b = strbuf_new();
1933         strbuf_printf( b, "---\n" );
1934         mlt_properties_set_lcnumeric( self, "C" );
1935         serialise_yaml( self, b, 0, 0 );
1936         mlt_properties_set_lcnumeric( self, lc_numeric );
1937         strbuf_printf( b, "...\n" );
1938         char *ret = b->string;
1939         strbuf_close( b );
1940         return ret;
1941 }
1942
1943 /** Protect a properties list against concurrent access.
1944  *
1945  * \public \memberof mlt_properties_s
1946  * \param self a properties list
1947  */
1948
1949 void mlt_properties_lock( mlt_properties self )
1950 {
1951         if ( self )
1952                 pthread_mutex_lock( &( ( property_list* )( self->local ) )->mutex );
1953 }
1954
1955 /** End protecting a properties list against concurrent access.
1956  *
1957  * \public \memberof mlt_properties_s
1958  * \param self a properties list
1959  */
1960
1961 void mlt_properties_unlock( mlt_properties self )
1962 {
1963         if ( self )
1964                 pthread_mutex_unlock( &( ( property_list* )( self->local ) )->mutex );
1965 }
1966
1967 /** Get a time string associated to the name.
1968  *
1969  * Do not free the returned string. It's lifetime is controlled by the property.
1970  * \public \memberof mlt_properties_s
1971  * \param self a properties list
1972  * \param name the property to get
1973  * \param format the time format that you want
1974  * \return the property's time value or NULL if \p name does not exist or there is no profile
1975  */
1976
1977 char *mlt_properties_get_time( mlt_properties self, const char* name, mlt_time_format format )
1978 {
1979         mlt_profile profile = mlt_properties_get_data( self, "_profile", NULL );
1980         if ( profile )
1981         {
1982                 double fps = mlt_profile_fps( profile );
1983                 mlt_property value = mlt_properties_find( self, name );
1984                 property_list *list = self->local;
1985                 return value == NULL ? NULL : mlt_property_get_time( value, format, fps, list->locale );
1986         }
1987         return NULL;
1988 }