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