2 * \file mlt_properties.c
3 * \brief Properties class definition
5 * Copyright (C) 2003-2008 Ushodaya Enterprises Limited
6 * \author Charles Yates <charles.yates@pandora.be>
7 * \author Dan Dennedy <dan@dennedy.org>
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.
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.
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
24 #include "mlt_properties.h"
25 #include "mlt_property.h"
26 #include "mlt_deque.h"
35 #include <sys/types.h>
39 /** \brief private implementation of the property list */
48 mlt_properties mirror;
50 pthread_mutex_t mutex;
54 /* Memory leak checks */
56 //#define _MLT_PROPERTY_CHECKS_ 2
57 #ifdef _MLT_PROPERTY_CHECKS_
58 static int properties_created = 0;
59 static int properties_destroyed = 0;
62 /** Initialize a properties object that was already allocated.
64 * This does allocate its ::property_list, and it adds a reference count.
65 * \public \memberof mlt_properties_s
66 * \param this the properties structure to initialize
67 * \param child an opaque pointer to a subclass object
68 * \return true if failed
71 int mlt_properties_init( mlt_properties this, void *child )
75 #ifdef _MLT_PROPERTY_CHECKS_
76 // Increment number of properties created
77 properties_created ++;
81 memset( this, 0, sizeof( struct mlt_properties_s ) );
83 // Assign the child of the object
86 // Allocate the local structure
87 this->local = calloc( sizeof( property_list ), 1 );
89 // Increment the ref count
90 ( ( property_list * )this->local )->ref_count = 1;
91 pthread_mutex_init( &( ( property_list * )this->local )->mutex, NULL );;
94 // Check that initialisation was successful
95 return this != NULL && this->local == NULL;
98 /** Create a properties object.
100 * This allocates the properties structure and calls mlt_properties_init() on it.
101 * Free the properties object with mlt_properties_close().
102 * \public \memberof mlt_properties_s
103 * \return a new properties object
106 mlt_properties mlt_properties_new( )
108 // Construct a standalone properties object
109 mlt_properties this = calloc( sizeof( struct mlt_properties_s ), 1 );
112 mlt_properties_init( this, NULL );
114 // Return the pointer
118 /** Create a properties object by reading a .properties text file.
120 * Free the properties object with mlt_properties_close().
121 * \deprecated Please start using mlt_properties_parse_yaml().
122 * \public \memberof mlt_properties_s
123 * \param filename a string contain the absolute file name
124 * \return a new properties object
127 mlt_properties mlt_properties_load( const char *filename )
129 // Construct a standalone properties object
130 mlt_properties this = mlt_properties_new( );
135 FILE *file = fopen( filename, "r" );
137 // Load contents of file
142 char last[ 1024 ] = "";
144 // Read each string from the file
145 while( fgets( temp, 1024, file ) )
148 temp[ strlen( temp ) - 1 ] = '\0';
150 // Check if the line starts with a .
151 if ( temp[ 0 ] == '.' )
154 sprintf( temp2, "%s%s", last, temp );
155 strcpy( temp, temp2 );
157 else if ( strchr( temp, '=' ) )
159 strcpy( last, temp );
160 *( strchr( last, '=' ) ) = '\0';
163 // Parse and set the property
164 if ( strcmp( temp, "" ) && temp[ 0 ] != '#' )
165 mlt_properties_parse( this, temp );
173 // Return the pointer
177 /** Generate a hash key.
179 * \private \memberof mlt_properties_s
180 * \param name a string
184 static inline int generate_hash( const char *name )
189 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
193 /** Copy a serializable property to properties list that is mirroring this one.
195 * Special case - when a container (such as fezzik) is protecting another
196 * producer, we need to ensure that properties are passed through to the
198 * \private \memberof mlt_properties_s
199 * \param this a properties list
200 * \param name the name of the property to copy
203 static inline void mlt_properties_do_mirror( mlt_properties this, const char *name )
205 property_list *list = this->local;
206 if ( list->mirror != NULL )
208 char *value = mlt_properties_get( this, name );
210 mlt_properties_set( list->mirror, name, value );
214 /** Increment the reference count.
216 * \public \memberof mlt_properties_s
217 * \param this a properties list
218 * \return the new reference count
221 int mlt_properties_inc_ref( mlt_properties this )
226 property_list *list = this->local;
227 pthread_mutex_lock( &list->mutex );
228 result = ++ list->ref_count;
229 pthread_mutex_unlock( &list->mutex );
234 /** Decrement the reference count.
236 * \public \memberof mlt_properties_s
237 * \param this a properties list
238 * \return the new reference count
241 int mlt_properties_dec_ref( mlt_properties this )
246 property_list *list = this->local;
247 pthread_mutex_lock( &list->mutex );
248 result = -- list->ref_count;
249 pthread_mutex_unlock( &list->mutex );
254 /** Get the reference count.
256 * \public \memberof mlt_properties_s
257 * \param this a properties list
258 * \return the current reference count
261 int mlt_properties_ref_count( mlt_properties this )
265 property_list *list = this->local;
266 return list->ref_count;
271 /** Set a properties list to be a mirror copy of another.
273 * Note that this does not copy all existing properties. Rather, you must
274 * call this before setting the properties that you wish to copy.
275 * \public \memberof mlt_properties_s
276 * \param that the properties which will receive copies of the properties as they are set.
277 * \param this the properties to mirror
280 void mlt_properties_mirror( mlt_properties this, mlt_properties that )
282 property_list *list = this->local;
286 /** Copy all serializable properties to another properties list.
288 * \public \memberof mlt_properties_s
289 * \param this The properties to copy to
290 * \param that The properties to copy from
294 int mlt_properties_inherit( mlt_properties this, mlt_properties that )
296 int count = mlt_properties_count( that );
298 for ( i = 0; i < count; i ++ )
300 char *value = mlt_properties_get_value( that, i );
303 char *name = mlt_properties_get_name( that, i );
304 mlt_properties_set( this, name, value );
310 /** Pass all serializable properties that match a prefix to another properties object
312 * \public \memberof mlt_properties_s
313 * \param this the properties to copy to
314 * \param that The properties to copy from
315 * \param prefix the property names to match (required)
319 int mlt_properties_pass( mlt_properties this, mlt_properties that, const char *prefix )
321 int count = mlt_properties_count( that );
322 int length = strlen( prefix );
324 for ( i = 0; i < count; i ++ )
326 char *name = mlt_properties_get_name( that, i );
327 if ( !strncmp( name, prefix, length ) )
329 char *value = mlt_properties_get_value( that, i );
331 mlt_properties_set( this, name + length, value );
337 /** Locate a property by name.
339 * \private \memberof mlt_properties_s
340 * \param this a properties list
341 * \param name the property to lookup by name
342 * \return the property or NULL for failure
345 static inline mlt_property mlt_properties_find( mlt_properties this, const char *name )
347 property_list *list = this->local;
348 mlt_property value = NULL;
349 int key = generate_hash( name );
350 int i = list->hash[ key ] - 1;
354 // Check if we're hashed
355 if ( list->count > 0 &&
356 name[ 0 ] == list->name[ i ][ 0 ] &&
357 !strcmp( list->name[ i ], name ) )
358 value = list->value[ i ];
361 for ( i = list->count - 1; value == NULL && i >= 0; i -- )
362 if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
363 value = list->value[ i ];
369 /** Add a new property.
371 * \private \memberof mlt_properties_s
372 * \param this a properties list
373 * \param name the name of the new property
374 * \return the new property
377 static mlt_property mlt_properties_add( mlt_properties this, const char *name )
379 property_list *list = this->local;
380 int key = generate_hash( name );
382 // Check that we have space and resize if necessary
383 if ( list->count == list->size )
386 list->name = realloc( list->name, list->size * sizeof( const char * ) );
387 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
390 // Assign name/value pair
391 list->name[ list->count ] = strdup( name );
392 list->value[ list->count ] = mlt_property_init( );
394 // Assign to hash table
395 if ( list->hash[ key ] == 0 )
396 list->hash[ key ] = list->count + 1;
398 // Return and increment count accordingly
399 return list->value[ list->count ++ ];
402 /** Fetch a property by name and add one if not found.
404 * \private \memberof mlt_properties_s
405 * \param this a properties list
406 * \param name the property to lookup or add
407 * \return the property
410 static mlt_property mlt_properties_fetch( mlt_properties this, const char *name )
412 // Try to find an existing property first
413 mlt_property property = mlt_properties_find( this, name );
415 // If it wasn't found, create one
416 if ( property == NULL )
417 property = mlt_properties_add( this, name );
419 // Return the property
423 /** Copy a property to another properties list.
425 * \public \memberof mlt_properties_s
426 * \author Zach <zachary.drew@gmail.com>
427 * \param this the properties to copy to
428 * \param that the properties to copy from
429 * \param name the name of the property to copy
432 void mlt_properties_pass_property( mlt_properties this, mlt_properties that, const char *name )
434 // Make sure the source property isn't null.
435 mlt_property that_prop = mlt_properties_find( that, name );
436 if( that_prop == NULL )
439 mlt_property_pass( mlt_properties_fetch( this, name ), that_prop );
442 /** Copy all properties specified in a comma-separated list to another properties list.
444 * White space is also a delimiter.
445 * \public \memberof mlt_properties_s
446 * \author Zach <zachary.drew@gmail.com>
447 * \param this the properties to copy to
448 * \param that the properties to copy from
449 * \param list a delimited list of property names
454 int mlt_properties_pass_list( mlt_properties this, mlt_properties that, const char *list )
456 char *props = strdup( list );
458 char *delim = " ,\t\n"; // Any combination of spaces, commas, tabs, and newlines
463 count = strcspn( ptr, delim );
465 if( ptr[count] == '\0' )
468 ptr[count] = '\0'; // Make it a real string
470 mlt_properties_pass_property( this, that, ptr );
473 ptr += strspn( ptr, delim );
482 /** Set a property to a string.
484 * This makes a copy of the string value you supply.
485 * \public \memberof mlt_properties_s
486 * \param this a properties list
487 * \param name the property to set
488 * \param value the property's new value
489 * \return true if error
492 int mlt_properties_set( mlt_properties this, const char *name, const char *value )
496 // Fetch the property to work with
497 mlt_property property = mlt_properties_fetch( this, name );
499 // Set it if not NULL
500 if ( property == NULL )
502 mlt_log( NULL, MLT_LOG_FATAL, "Whoops - %s not found (should never occur)\n", name );
504 else if ( value == NULL )
506 error = mlt_property_set_string( property, value );
507 mlt_properties_do_mirror( this, name );
509 else if ( *value != '@' )
511 error = mlt_property_set_string( property, value );
512 mlt_properties_do_mirror( this, name );
514 else if ( value[ 0 ] == '@' )
523 while ( *value != '\0' )
525 int length = strcspn( value, "+-*/" );
527 // Get the identifier
528 strncpy( id, value, length );
532 // Determine the value
533 if ( isdigit( id[ 0 ] ) )
534 current = atof( id );
536 current = mlt_properties_get_double( this, id );
538 // Apply the operation
551 total = total / current;
556 op = *value != '\0' ? *value ++ : ' ';
559 error = mlt_property_set_double( property, total );
560 mlt_properties_do_mirror( this, name );
563 mlt_events_fire( this, "property-changed", name, NULL );
568 /** Set or default a property to a string.
570 * This makes a copy of the string value you supply.
571 * \public \memberof mlt_properties_s
572 * \param this a properties list
573 * \param name the property to set
574 * \param value the string value to set or NULL to use the default
575 * \param def the default string if value is NULL
576 * \return true if error
579 int mlt_properties_set_or_default( mlt_properties this, const char *name, const char *value, const char *def )
581 return mlt_properties_set( this, name, value == NULL ? def : value );
584 /** Get a string value by name.
586 * Do not free the returned string. It's lifetime is controlled by the property
587 * and this properties object.
588 * \public \memberof mlt_properties_s
589 * \param this a properties list
590 * \param name the property to get
591 * \return the property's string value or NULL if it does not exist
594 char *mlt_properties_get( mlt_properties this, const char *name )
596 mlt_property value = mlt_properties_find( this, name );
597 return value == NULL ? NULL : mlt_property_get_string( value );
600 /** Get a property name by index.
602 * Do not free the returned string.
603 * \public \memberof mlt_properties_s
604 * \param this a properties list
605 * \param index the numeric index of the property
606 * \return the name of the property or NULL if index is out of range
609 char *mlt_properties_get_name( mlt_properties this, int index )
611 property_list *list = this->local;
612 if ( index >= 0 && index < list->count )
613 return list->name[ index ];
617 /** Get a property's string value by index.
619 * Do not free the returned string.
620 * \public \memberof mlt_properties_s
621 * \param this a properties list
622 * \param index the numeric index of the property
623 * \return the property value as a string or NULL if the index is out of range
626 char *mlt_properties_get_value( mlt_properties this, int index )
628 property_list *list = this->local;
629 if ( index >= 0 && index < list->count )
630 return mlt_property_get_string( list->value[ index ] );
634 /** Get a data value by index.
636 * Do not free the returned pointer if you supplied a destructor function when you
638 * \public \memberof mlt_properties_s
639 * \param this a properties list
640 * \param index the numeric index of the property
641 * \param size the size of the binary data in bytes or NULL if the index is out of range
644 void *mlt_properties_get_data_at( mlt_properties this, int index, int *size )
646 property_list *list = this->local;
647 if ( index >= 0 && index < list->count )
648 return mlt_property_get_data( list->value[ index ], size );
652 /** Return the number of items in the list.
654 * \public \memberof mlt_properties_s
655 * \param this a properties list
656 * \return the number of property objects
659 int mlt_properties_count( mlt_properties this )
661 property_list *list = this->local;
665 /** Set a value by parsing a name=value string.
667 * \public \memberof mlt_properties_s
668 * \param this a properties list
669 * \param namevalue a string containing name and value delimited by '='
670 * \return true if there was an error
673 int mlt_properties_parse( mlt_properties this, const char *namevalue )
675 char *name = strdup( namevalue );
678 char *ptr = strchr( name, '=' );
686 value = strdup( ptr );
691 value = strdup( ptr );
692 if ( value != NULL && value[ strlen( value ) - 1 ] == '\"' )
693 value[ strlen( value ) - 1 ] = '\0';
698 value = strdup( "" );
701 error = mlt_properties_set( this, name, value );
709 /** Get an integer associated to the name.
711 * \public \memberof mlt_properties_s
712 * \param this a properties list
713 * \param name the property to get
714 * \return The integer value, 0 if not found (which may also be a legitimate value)
717 int mlt_properties_get_int( mlt_properties this, const char *name )
719 mlt_property value = mlt_properties_find( this, name );
720 return value == NULL ? 0 : mlt_property_get_int( value );
723 /** Set a property to an integer value.
725 * \public \memberof mlt_properties_s
726 * \param this a properties list
727 * \param name the property to set
728 * \param value the integer
729 * \return true if error
732 int mlt_properties_set_int( mlt_properties this, const char *name, int value )
736 // Fetch the property to work with
737 mlt_property property = mlt_properties_fetch( this, name );
739 // Set it if not NULL
740 if ( property != NULL )
742 error = mlt_property_set_int( property, value );
743 mlt_properties_do_mirror( this, name );
746 mlt_events_fire( this, "property-changed", name, NULL );
751 /** Get a 64-bit integer associated to the name.
753 * \public \memberof mlt_properties_s
754 * \param this a properties list
755 * \param name the property to get
756 * \return the integer value, 0 if not found (which may also be a legitimate value)
759 int64_t mlt_properties_get_int64( mlt_properties this, const char *name )
761 mlt_property value = mlt_properties_find( this, name );
762 return value == NULL ? 0 : mlt_property_get_int64( value );
765 /** Set a property to a 64-bit integer value.
767 * \public \memberof mlt_properties_s
768 * \param this a properties list
769 * \param name the property to set
770 * \param value the integer
771 * \return true if error
774 int mlt_properties_set_int64( mlt_properties this, const char *name, int64_t value )
778 // Fetch the property to work with
779 mlt_property property = mlt_properties_fetch( this, name );
781 // Set it if not NULL
782 if ( property != NULL )
784 error = mlt_property_set_int64( property, value );
785 mlt_properties_do_mirror( this, name );
788 mlt_events_fire( this, "property-changed", name, NULL );
793 /** Get a floating point value associated to the name.
795 * \public \memberof mlt_properties_s
796 * \param this a properties list
797 * \param name the property to get
798 * \return the floating point, 0 if not found (which may also be a legitimate value)
801 double mlt_properties_get_double( mlt_properties this, const char *name )
803 mlt_property value = mlt_properties_find( this, name );
804 return value == NULL ? 0 : mlt_property_get_double( value );
807 /** Set a property to a floating point value.
809 * \public \memberof mlt_properties_s
810 * \param this a properties list
811 * \param name the property to set
812 * \param value the floating point value
813 * \return true if error
816 int mlt_properties_set_double( mlt_properties this, const char *name, double value )
820 // Fetch the property to work with
821 mlt_property property = mlt_properties_fetch( this, name );
823 // Set it if not NULL
824 if ( property != NULL )
826 error = mlt_property_set_double( property, value );
827 mlt_properties_do_mirror( this, name );
830 mlt_events_fire( this, "property-changed", name, NULL );
835 /** Get a position value associated to the name.
837 * \public \memberof mlt_properties_s
838 * \param this a properties list
839 * \param name the property to get
840 * \return the position, 0 if not found (which may also be a legitimate value)
843 mlt_position mlt_properties_get_position( mlt_properties this, const char *name )
845 mlt_property value = mlt_properties_find( this, name );
846 return value == NULL ? 0 : mlt_property_get_position( value );
849 /** Set a property to a position value.
851 * \public \memberof mlt_properties_s
852 * \param this a properties list
853 * \param name the property to get
854 * \param value the position
855 * \return true if error
858 int mlt_properties_set_position( mlt_properties this, const char *name, mlt_position value )
862 // Fetch the property to work with
863 mlt_property property = mlt_properties_fetch( this, name );
865 // Set it if not NULL
866 if ( property != NULL )
868 error = mlt_property_set_position( property, value );
869 mlt_properties_do_mirror( this, name );
872 mlt_events_fire( this, "property-changed", name, NULL );
877 /** Get a binary data value associated to the name.
879 * Do not free the returned pointer if you supplied a destructor function
880 * when you set this property.
881 * \public \memberof mlt_properties_s
882 * \param this a properties list
883 * \param name the property to get
884 * \param length The size of the binary data in bytes, if available (often it is not, you should know)
887 void *mlt_properties_get_data( mlt_properties this, const char *name, int *length )
889 mlt_property value = mlt_properties_find( this, name );
890 return value == NULL ? NULL : mlt_property_get_data( value, length );
893 /** Store binary data as a property.
895 * \public \memberof mlt_properties_s
896 * \param this a properties list
897 * \param name the property to set
898 * \param value an opaque pointer to binary data
899 * \param length the size of the binary data in bytes (optional)
900 * \param destroy a function to dellacate the binary data when the property is closed (optional)
901 * \param serialise a function that can serialize the binary data as text (optional)
902 * \return true if error
905 int mlt_properties_set_data( mlt_properties this, const char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
909 // Fetch the property to work with
910 mlt_property property = mlt_properties_fetch( this, name );
912 // Set it if not NULL
913 if ( property != NULL )
914 error = mlt_property_set_data( property, value, length, destroy, serialise );
916 mlt_events_fire( this, "property-changed", name, NULL );
921 /** Rename a property.
923 * \public \memberof mlt_properties_s
924 * \param this a properties list
925 * \param source the property to rename
926 * \param dest the new name
927 * \return true if the name is already in use
930 int mlt_properties_rename( mlt_properties this, const char *source, const char *dest )
932 mlt_property value = mlt_properties_find( this, dest );
936 property_list *list = this->local;
940 for ( i = 0; i < list->count; i ++ )
942 if ( !strcmp( list->name[ i ], source ) )
944 free( list->name[ i ] );
945 list->name[ i ] = strdup( dest );
946 list->hash[ generate_hash( dest ) ] = i + 1;
952 return value != NULL;
955 /** Dump the properties to a file handle.
957 * \public \memberof mlt_properties_s
958 * \param this a properties list
959 * \param output a file handle
962 void mlt_properties_dump( mlt_properties this, FILE *output )
964 property_list *list = this->local;
966 for ( i = 0; i < list->count; i ++ )
967 if ( mlt_properties_get( this, list->name[ i ] ) != NULL )
968 fprintf( output, "%s=%s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
971 /** Output the properties to a file handle.
973 * This version includes reference counts and does not put each property on a new line.
974 * \public \memberof mlt_properties_s
975 * \param this a properties pointer
976 * \param title a string to preface the output
977 * \param output a file handle
979 void mlt_properties_debug( mlt_properties this, const char *title, FILE *output )
981 if ( output == NULL ) output = stderr;
982 fprintf( output, "%s: ", title );
985 property_list *list = this->local;
987 fprintf( output, "[ ref=%d", list->ref_count );
988 for ( i = 0; i < list->count; i ++ )
989 if ( mlt_properties_get( this, list->name[ i ] ) != NULL )
990 fprintf( output, ", %s=%s", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
992 fprintf( output, ", %s=%p", list->name[ i ], mlt_properties_get_data( this, list->name[ i ], NULL ) );
993 fprintf( output, " ]" );
995 fprintf( output, "\n" );
998 /** Save the properties to a file by name.
1000 * This uses the dump format - one line per property.
1001 * \public \memberof mlt_properties_s
1002 * \param this a properties list
1003 * \param filename the name of a file to create or overwrite
1004 * \return true if there was an error
1007 int mlt_properties_save( mlt_properties this, const char *filename )
1010 FILE *f = fopen( filename, "w" );
1013 mlt_properties_dump( this, f );
1020 /* This is a very basic cross platform fnmatch replacement - it will fail in
1021 * many cases, but for the basic *.XXX and YYY*.XXX, it will work ok.
1024 /** Test whether a filename or pathname matches a shell-style pattern.
1026 * \private \memberof mlt_properties_s
1027 * \param wild a string containing a wildcard pattern
1028 * \param file the name of a file to test against
1029 * \return true if the file name matches the wildcard pattern
1032 static int mlt_fnmatch( const char *wild, const char *file )
1037 while( f < strlen( file ) && w < strlen( wild ) )
1039 if ( wild[ w ] == '*' )
1042 if ( w == strlen( wild ) )
1044 while ( f != strlen( file ) && tolower( file[ f ] ) != tolower( wild[ w ] ) )
1047 else if ( wild[ w ] == '?' || tolower( file[ f ] ) == tolower( wild[ w ] ) )
1052 else if ( wild[ 0 ] == '*' )
1062 return strlen( file ) == f && strlen( wild ) == w;
1065 /** Compare the string or serialized value of two properties.
1067 * \private \memberof mlt_properties_s
1068 * \param this a property
1069 * \param that a property
1070 * \return < 0 if 'this' less than 'that', 0 if equal, or > 0 if 'this' is greater than 'that'
1073 static int mlt_compare( const void *this, const void *that )
1075 return strcmp( mlt_property_get_string( *( mlt_property * )this ), mlt_property_get_string( *( mlt_property * )that ) );
1078 /** Get the contents of a directory.
1080 * Obtains an optionally sorted list of the files found in a directory with a specific wild card.
1081 * Entries in the list have a numeric name (running from 0 to count - 1). Only values change
1082 * position if sort is enabled. Designed to be posix compatible (linux, os/x, mingw etc).
1083 * \public \memberof mlt_properties_s
1084 * \param this a properties list
1085 * \param dirname the name of the directory
1086 * \param pattern a wildcard pattern to filter the directory listing
1087 * \param sort Do you want to sort the directory listing?
1088 * \return the number of items in the directory listing
1091 int mlt_properties_dir_list( mlt_properties this, const char *dirname, const char *pattern, int sort )
1093 DIR *dir = opendir( dirname );
1098 struct dirent *de = readdir( dir );
1099 char fullname[ 1024 ];
1102 sprintf( key, "%d", mlt_properties_count( this ) );
1103 snprintf( fullname, 1024, "%s/%s", dirname, de->d_name );
1104 if ( pattern == NULL )
1105 mlt_properties_set( this, key, fullname );
1106 else if ( de->d_name[ 0 ] != '.' && mlt_fnmatch( pattern, de->d_name ) )
1107 mlt_properties_set( this, key, fullname );
1108 de = readdir( dir );
1114 if ( sort && mlt_properties_count( this ) )
1116 property_list *list = this->local;
1117 qsort( list->value, mlt_properties_count( this ), sizeof( mlt_property ), mlt_compare );
1120 return mlt_properties_count( this );
1123 /** Close a properties object.
1125 * Deallocates the properties object and everything it contains.
1126 * \public \memberof mlt_properties_s
1127 * \param this a properties object
1130 void mlt_properties_close( mlt_properties this )
1132 if ( this != NULL && mlt_properties_dec_ref( this ) <= 0 )
1134 if ( this->close != NULL )
1136 this->close( this->close_object );
1140 property_list *list = this->local;
1143 #if _MLT_PROPERTY_CHECKS_ == 1
1145 mlt_properties_debug( this, "Closing", stderr );
1148 #ifdef _MLT_PROPERTY_CHECKS_
1149 // Increment destroyed count
1150 properties_destroyed ++;
1152 // Show current stats - these should match when the app is closed
1153 mlt_log( NULL, MLT_LOG_DEBUG, "Created %d, destroyed %d\n", properties_created, properties_destroyed );
1156 // Clean up names and values
1157 for ( index = list->count - 1; index >= 0; index -- )
1159 free( list->name[ index ] );
1160 mlt_property_close( list->value[ index ] );
1163 // Clear up the list
1164 pthread_mutex_destroy( &list->mutex );
1166 free( list->value );
1169 // Free this now if this has no child
1170 if ( this->child == NULL )
1176 /** Determine if the properties list is really just a sequence or ordered list.
1178 * \public \memberof mlt_properties_s
1179 * \param properties a properties list
1180 * \return true if all of the property names are numeric (a sequence)
1183 int mlt_properties_is_sequence( mlt_properties properties )
1186 int n = mlt_properties_count( properties );
1187 for ( i = 0; i < n; i++ )
1188 if ( ! isdigit( mlt_properties_get_name( properties, i )[0] ) )
1193 /** \brief YAML Tiny Parser context structure
1195 * YAML is a nifty text format popular in the Ruby world as a cleaner,
1196 * less verbose alternative to XML. See this Wikipedia topic for an overview:
1197 * http://en.wikipedia.org/wiki/YAML
1198 * The YAML specification is at:
1200 * YAML::Tiny is a Perl module that specifies a subset of YAML that we are
1201 * using here (for the same reasons):
1202 * http://search.cpan.org/~adamk/YAML-Tiny-1.25/lib/YAML/Tiny.pm
1206 struct yaml_parser_context
1213 unsigned int block_indent;
1216 typedef struct yaml_parser_context *yaml_parser;
1218 /** Remove spaces from the left side of a string.
1220 * \param s the string to trim
1221 * \return the number of characters removed
1224 static unsigned int ltrim( char **s )
1228 int n = strlen( c );
1229 for ( i = 0; i < n && *c == ' '; i++, c++ );
1234 /** Remove spaces from the right side of a string.
1236 * \param s the string to trim
1237 * \return the number of characters removed
1240 static unsigned int rtrim( char *s )
1242 int n = strlen( s );
1244 for ( i = n; i > 0 && s[i - 1] == ' '; --i )
1249 /** Parse a line of YAML Tiny.
1251 * Adds a property if needed.
1252 * \private \memberof yaml_parser_context
1253 * \param context a YAML Tiny Parser context
1254 * \param namevalue a line of YAML Tiny
1255 * \return true if there was an error
1258 static int parse_yaml( yaml_parser context, const char *namevalue )
1260 char *name_ = strdup( namevalue );
1264 char *ptr = strchr( name, ':' );
1265 unsigned int indent = ltrim( &name );
1266 mlt_properties properties = mlt_deque_peek_front( context->stack );
1268 // Ascending one more levels in the tree
1269 if ( indent < context->level )
1272 unsigned int n = ( context->level - indent ) / 2;
1273 for ( i = 0; i < n; i++ )
1274 mlt_deque_pop_front( context->stack );
1275 properties = mlt_deque_peek_front( context->stack );
1276 context->level = indent;
1279 // Descending a level in the tree
1280 else if ( indent > context->level && context->block == 0 )
1282 context->level = indent;
1285 // If there is a colon that is not part of a block
1286 if ( ptr && ( indent == context->level ) )
1288 // Reset block processing
1289 if ( context->block_name )
1291 free( context->block_name );
1292 context->block_name = NULL;
1296 // Terminate the name and setup the value pointer
1300 char *comment = strchr( ptr, '#' );
1306 // Trim leading and trailing spaces from bare value
1310 // No value means a child
1311 if ( strcmp( ptr, "" ) == 0 )
1313 mlt_properties child = mlt_properties_new();
1314 mlt_properties_set_data( properties, name, child, 0,
1315 ( mlt_destructor )mlt_properties_close, NULL );
1316 mlt_deque_push_front( context->stack, child );
1322 // A dash indicates a sequence item
1323 if ( name[0] == '-' )
1325 mlt_properties child = mlt_properties_new();
1328 snprintf( key, sizeof(key), "%d", context->index++ );
1329 mlt_properties_set_data( properties, key, child, 0,
1330 ( mlt_destructor )mlt_properties_close, NULL );
1331 mlt_deque_push_front( context->stack, child );
1334 context->level += ltrim( &name ) + 1;
1342 value = strdup( ptr );
1343 if ( value && value[ strlen( value ) - 1 ] == '\"' )
1344 value[ strlen( value ) - 1 ] = 0;
1347 // Value is folded or unfolded block
1348 else if ( *ptr == '|' || *ptr == '>' )
1350 context->block = *ptr;
1351 context->block_name = strdup( name );
1352 context->block_indent = 0;
1353 value = strdup( "" );
1359 value = strdup( ptr );
1363 // A list of scalars
1364 else if ( name[0] == '-' )
1366 // Reset block processing
1367 if ( context->block_name )
1369 free( context->block_name );
1370 context->block_name = NULL;
1376 snprintf( key, sizeof(key), "%d", context->index++ );
1380 char *comment = strchr( ptr, '#' );
1384 // Trim leading and trailing spaces from bare value
1392 value = strdup( ptr );
1393 if ( value && value[ strlen( value ) - 1 ] == '\"' )
1394 value[ strlen( value ) - 1 ] = 0;
1397 // Value is folded or unfolded block
1398 else if ( *ptr == '|' || *ptr == '>' )
1400 context->block = *ptr;
1401 context->block_name = strdup( key );
1402 context->block_indent = 0;
1403 value = strdup( "" );
1409 value = strdup( ptr );
1413 name = name_ = strdup( key );
1417 else if ( context->block == '|' )
1419 if ( context->block_indent == 0 )
1420 context->block_indent = indent;
1421 if ( indent > context->block_indent )
1422 name = &name_[ context->block_indent ];
1424 char *old_value = mlt_properties_get( properties, context->block_name );
1425 value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
1426 strcpy( value, old_value );
1427 if ( strcmp( old_value, "" ) )
1428 strcat( value, "\n" );
1429 strcat( value, name );
1430 name = context->block_name;
1434 else if ( context->block == '>' )
1438 char *old_value = mlt_properties_get( properties, context->block_name );
1440 // Blank line (prepended with spaces) is new line
1441 if ( strcmp( name, "" ) == 0 )
1443 value = calloc( 1, strlen( old_value ) + 2 );
1444 strcat( value, old_value );
1445 strcat( value, "\n" );
1447 // Concatenate with space
1450 value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
1451 strcat( value, old_value );
1452 if ( strcmp( old_value, "" ) && old_value[ strlen( old_value ) - 1 ] != '\n' )
1453 strcat( value, " " );
1454 strcat( value, name );
1456 name = context->block_name;
1461 value = strdup( "" );
1464 error = mlt_properties_set( properties, name, value );
1472 /** Parse a YAML Tiny file by name.
1474 * \public \memberof mlt_properties_s
1475 * \param filename the name of a text file containing YAML Tiny
1476 * \return a new properties list
1479 mlt_properties mlt_properties_parse_yaml( const char *filename )
1481 // Construct a standalone properties object
1482 mlt_properties this = mlt_properties_new( );
1487 FILE *file = fopen( filename, "r" );
1489 // Load contents of file
1494 char *ptemp = &temp[ 0 ];
1497 yaml_parser context = calloc( 1, sizeof( struct yaml_parser_context ) );
1498 context->stack = mlt_deque_init();
1499 mlt_deque_push_front( context->stack, this );
1501 // Read each string from the file
1502 while( fgets( temp, 1024, file ) )
1504 // Check for end-of-stream
1505 if ( strncmp( ptemp, "...", 3 ) == 0 )
1509 temp[ strlen( temp ) - 1 ] = '\0';
1511 // Skip blank lines, comment lines, and document separator
1512 if ( strcmp( ptemp, "" ) && ptemp[ 0 ] != '#' && strncmp( ptemp, "---", 3 )
1513 && strncmp( ptemp, "%YAML", 5 ) && strncmp( ptemp, "% YAML", 6 ) )
1514 parse_yaml( context, temp );
1519 mlt_deque_close( context->stack );
1520 if ( context->block_name )
1521 free( context->block_name );
1526 // Return the pointer
1531 * YAML Tiny Serializer
1534 /** How many bytes to grow at a time */
1535 #define STRBUF_GROWTH (1024)
1537 /** \brief Self-growing buffer for building strings
1547 typedef struct strbuf_s *strbuf;
1549 /** Create a new string buffer
1551 * \private \memberof strbuf_s
1552 * \return a new string buffer
1555 static strbuf strbuf_new( )
1557 strbuf buffer = calloc( 1, sizeof( struct strbuf_s ) );
1558 buffer->size = STRBUF_GROWTH;
1559 buffer->string = calloc( 1, buffer->size );
1563 /** Destroy a string buffer
1565 * \private \memberof strbuf_s
1566 * \param buffer the string buffer to close
1569 static void strbuf_close( strbuf buffer )
1571 // We do not free buffer->string; strbuf user must save that pointer
1577 /** Format a string into a string buffer
1579 * A variable number of arguments follows the format string - one for each
1581 * \private \memberof strbuf_s
1582 * \param buffer the string buffer to write into
1583 * \param format a string that contains text and formatting instructions
1584 * \return the formatted string
1587 static char *strbuf_printf( strbuf buffer, const char *format, ... )
1589 while ( buffer->string )
1592 va_start( ap, format );
1593 size_t len = strlen( buffer->string );
1594 size_t remain = buffer->size - len - 1;
1595 int need = vsnprintf( buffer->string + len, remain, format, ap );
1597 if ( need > -1 && need < remain )
1599 buffer->string[ len ] = 0;
1600 buffer->size += need + STRBUF_GROWTH;
1601 buffer->string = realloc( buffer->string, buffer->size );
1603 return buffer->string;
1606 /** Indent a line of YAML Tiny.
1608 * \private \memberof strbuf_s
1609 * \param output a string buffer
1610 * \param indent the number of spaces to indent
1613 static inline void indent_yaml( strbuf output, int indent )
1616 for ( j = 0; j < indent; j++ )
1617 strbuf_printf( output, " " );
1620 /** Convert a line string into a YAML block literal.
1622 * \private \memberof strbuf_s
1623 * \param output a string buffer
1624 * \param value the string to format as a block literal
1625 * \param indent the number of spaces to indent
1628 static void output_yaml_block_literal( strbuf output, const char *value, int indent )
1630 char *v = strdup( value );
1632 char *eol = strchr( sol, '\n' );
1636 indent_yaml( output, indent );
1638 strbuf_printf( output, "%s\n", sol );
1640 eol = strchr( sol, '\n' );
1642 indent_yaml( output, indent );
1643 strbuf_printf( output, "%s\n", sol );
1646 /** Recursively serialize a properties list into a string buffer as YAML Tiny.
1648 * \private \memberof mlt_properties_s
1649 * \param this a properties list
1650 * \param output a string buffer to hold the serialized YAML Tiny
1651 * \param indent the number of spaces to indent (for recursion, initialize to 0)
1652 * \param is_parent_sequence Is 'this' properties list really just a sequence (for recursion, initialize to 0)?
1655 static void serialise_yaml( mlt_properties this, strbuf output, int indent, int is_parent_sequence )
1657 property_list *list = this->local;
1660 for ( i = 0; i < list->count; i ++ )
1662 // This implementation assumes that all data elements are property lists.
1663 // Unfortunately, we do not have run time type identification.
1664 mlt_properties child = mlt_property_get_data( list->value[ i ], NULL );
1666 if ( mlt_properties_is_sequence( this ) )
1668 // Ignore hidden/non-serialisable items
1669 if ( list->name[ i ][ 0 ] != '_' )
1671 // Indicate a sequence item
1672 indent_yaml( output, indent );
1673 strbuf_printf( output, "- " );
1675 // If the value can be represented as a string
1676 const char *value = mlt_properties_get( this, list->name[ i ] );
1677 if ( value && strcmp( value, "" ) )
1679 // Determine if this is an unfolded block literal
1680 if ( strchr( value, '\n' ) )
1682 strbuf_printf( output, "|\n" );
1683 output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( "|" ) );
1687 strbuf_printf( output, "%s\n", value );
1693 serialise_yaml( child, output, indent + 2, 1 );
1697 // Assume this is a normal map-oriented properties list
1698 const char *value = mlt_properties_get( this, list->name[ i ] );
1700 // Ignore hidden/non-serialisable items
1701 // If the value can be represented as a string
1702 if ( list->name[ i ][ 0 ] != '_' && value && strcmp( value, "" ) )
1704 if ( is_parent_sequence == 0 )
1705 indent_yaml( output, indent );
1707 is_parent_sequence = 0;
1709 // Determine if this is an unfolded block literal
1710 if ( strchr( value, '\n' ) )
1712 strbuf_printf( output, "%s: |\n", list->name[ i ] );
1713 output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( ": " ) );
1717 strbuf_printf( output, "%s: %s\n", list->name[ i ], value );
1721 // Output a child as a map item
1724 indent_yaml( output, indent );
1725 strbuf_printf( output, "%s:\n", list->name[ i ] );
1728 serialise_yaml( child, output, indent + 2, 0 );
1734 /** Serialize a properties list as a string of YAML Tiny.
1736 * The caller MUST free the returned string!
1737 * This operates on properties containing properties as a hierarchical data
1739 * \public \memberof mlt_properties_s
1740 * \param this a properties list
1741 * \return a string containing YAML Tiny that represents the properties list
1744 char *mlt_properties_serialise_yaml( mlt_properties this )
1746 strbuf b = strbuf_new();
1747 strbuf_printf( b, "---\n" );
1748 serialise_yaml( this, b, 0, 0 );
1749 strbuf_printf( b, "...\n" );
1750 char *ret = b->string;