2 * \file mlt_properties.c
3 * \brief Properties class definition
4 * \see mlt_properties_s
6 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
8 * \author Dan Dennedy <dan@dennedy.org>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "mlt_properties.h"
26 #include "mlt_property.h"
27 #include "mlt_deque.h"
36 #include <sys/types.h>
40 /** \brief private implementation of the property list */
49 mlt_properties mirror;
51 pthread_mutex_t mutex;
55 /* Memory leak checks */
57 //#define _MLT_PROPERTY_CHECKS_ 2
58 #ifdef _MLT_PROPERTY_CHECKS_
59 static int properties_created = 0;
60 static int properties_destroyed = 0;
63 /** Initialize a properties object that was already allocated.
65 * This does allocate its ::property_list, and it adds a reference count.
66 * \public \memberof mlt_properties_s
67 * \param self the properties structure to initialize
68 * \param child an opaque pointer to a subclass object
69 * \return true if failed
72 int mlt_properties_init( mlt_properties self, void *child )
76 #ifdef _MLT_PROPERTY_CHECKS_
77 // Increment number of properties created
78 properties_created ++;
82 memset( self, 0, sizeof( struct mlt_properties_s ) );
84 // Assign the child of the object
87 // Allocate the local structure
88 self->local = calloc( sizeof( property_list ), 1 );
90 // Increment the ref count
91 ( ( property_list * )self->local )->ref_count = 1;
92 pthread_mutex_init( &( ( property_list * )self->local )->mutex, NULL );;
95 // Check that initialisation was successful
96 return self != NULL && self->local == NULL;
99 /** Create a properties object.
101 * This allocates the properties structure and calls mlt_properties_init() on it.
102 * Free the properties object with mlt_properties_close().
103 * \public \memberof mlt_properties_s
104 * \return a new properties object
107 mlt_properties mlt_properties_new( )
109 // Construct a standalone properties object
110 mlt_properties self = calloc( sizeof( struct mlt_properties_s ), 1 );
113 mlt_properties_init( self, NULL );
115 // Return the pointer
119 /** Create a properties object by reading a .properties text file.
121 * Free the properties object with mlt_properties_close().
122 * \deprecated Please start using mlt_properties_parse_yaml().
123 * \public \memberof mlt_properties_s
124 * \param filename a string contain the absolute file name
125 * \return a new properties object
128 mlt_properties mlt_properties_load( const char *filename )
130 // Construct a standalone properties object
131 mlt_properties self = mlt_properties_new( );
136 FILE *file = fopen( filename, "r" );
138 // Load contents of file
143 char last[ 1024 ] = "";
145 // Read each string from the file
146 while( fgets( temp, 1024, file ) )
149 temp[ strlen( temp ) - 1 ] = '\0';
151 // Check if the line starts with a .
152 if ( temp[ 0 ] == '.' )
155 sprintf( temp2, "%s%s", last, temp );
156 strcpy( temp, temp2 );
158 else if ( strchr( temp, '=' ) )
160 strcpy( last, temp );
161 *( strchr( last, '=' ) ) = '\0';
164 // Parse and set the property
165 if ( strcmp( temp, "" ) && temp[ 0 ] != '#' )
166 mlt_properties_parse( self, temp );
174 // Return the pointer
178 /** Generate a hash key.
180 * \private \memberof mlt_properties_s
181 * \param name a string
185 static inline int generate_hash( const char *name )
190 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
194 /** Copy a serializable property to a properties list that is mirroring this one.
196 * Special case - when a container (such as loader) is protecting another
197 * producer, we need to ensure that properties are passed through to the
199 * \private \memberof mlt_properties_s
200 * \param self a properties list
201 * \param name the name of the property to copy
204 static inline void mlt_properties_do_mirror( mlt_properties self, const char *name )
206 property_list *list = self->local;
207 if ( list->mirror != NULL )
209 char *value = mlt_properties_get( self, name );
211 mlt_properties_set( list->mirror, name, value );
215 /** Increment the reference count.
217 * \public \memberof mlt_properties_s
218 * \param self a properties list
219 * \return the new reference count
222 int mlt_properties_inc_ref( mlt_properties self )
227 property_list *list = self->local;
228 pthread_mutex_lock( &list->mutex );
229 result = ++ list->ref_count;
230 pthread_mutex_unlock( &list->mutex );
235 /** Decrement the reference count.
237 * \public \memberof mlt_properties_s
238 * \param self a properties list
239 * \return the new reference count
242 int mlt_properties_dec_ref( mlt_properties self )
247 property_list *list = self->local;
248 pthread_mutex_lock( &list->mutex );
249 result = -- list->ref_count;
250 pthread_mutex_unlock( &list->mutex );
255 /** Get the reference count.
257 * \public \memberof mlt_properties_s
258 * \param self a properties list
259 * \return the current reference count
262 int mlt_properties_ref_count( mlt_properties self )
266 property_list *list = self->local;
267 return list->ref_count;
272 /** Set a properties list to be a mirror copy of another.
274 * Note that this does not copy all existing properties. Rather, you must
275 * call this before setting the properties that you wish to copy.
276 * \public \memberof mlt_properties_s
277 * \param that the properties which will receive copies of the properties as they are set.
278 * \param self the properties to mirror
281 void mlt_properties_mirror( mlt_properties self, mlt_properties that )
283 property_list *list = self->local;
287 /** Copy all serializable properties to another properties list.
289 * \public \memberof mlt_properties_s
290 * \param self The properties to copy to
291 * \param that The properties to copy from
295 int mlt_properties_inherit( mlt_properties self, mlt_properties that )
297 int count = mlt_properties_count( that );
299 for ( i = 0; i < count; i ++ )
301 char *value = mlt_properties_get_value( that, i );
304 char *name = mlt_properties_get_name( that, i );
305 mlt_properties_set( self, name, value );
311 /** Pass all serializable properties that match a prefix to another properties object
313 * \public \memberof mlt_properties_s
314 * \param self the properties to copy to
315 * \param that The properties to copy from
316 * \param prefix the property names to match (required)
320 int mlt_properties_pass( mlt_properties self, mlt_properties that, const char *prefix )
322 int count = mlt_properties_count( that );
323 int length = strlen( prefix );
325 for ( i = 0; i < count; i ++ )
327 char *name = mlt_properties_get_name( that, i );
328 if ( !strncmp( name, prefix, length ) )
330 char *value = mlt_properties_get_value( that, i );
332 mlt_properties_set( self, name + length, value );
338 /** Locate a property by name.
340 * \private \memberof mlt_properties_s
341 * \param self a properties list
342 * \param name the property to lookup by name
343 * \return the property or NULL for failure
346 static inline mlt_property mlt_properties_find( mlt_properties self, const char *name )
348 property_list *list = self->local;
349 mlt_property value = NULL;
350 int key = generate_hash( name );
352 mlt_properties_lock( self );
354 int i = list->hash[ key ] - 1;
357 // Check if we're hashed
358 if ( list->count > 0 &&
359 name[ 0 ] == list->name[ i ][ 0 ] &&
360 !strcmp( list->name[ i ], name ) )
361 value = list->value[ i ];
364 for ( i = list->count - 1; value == NULL && i >= 0; i -- )
365 if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
366 value = list->value[ i ];
368 mlt_properties_unlock( self );
373 /** Add a new property.
375 * \private \memberof mlt_properties_s
376 * \param self a properties list
377 * \param name the name of the new property
378 * \return the new property
381 static mlt_property mlt_properties_add( mlt_properties self, const char *name )
383 property_list *list = self->local;
384 int key = generate_hash( name );
387 mlt_properties_lock( self );
389 // Check that we have space and resize if necessary
390 if ( list->count == list->size )
393 list->name = realloc( list->name, list->size * sizeof( const char * ) );
394 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
397 // Assign name/value pair
398 list->name[ list->count ] = strdup( name );
399 list->value[ list->count ] = mlt_property_init( );
401 // Assign to hash table
402 if ( list->hash[ key ] == 0 )
403 list->hash[ key ] = list->count + 1;
405 // Return and increment count accordingly
406 result = list->value[ list->count ++ ];
408 mlt_properties_unlock( self );
413 /** Fetch a property by name and add one if not found.
415 * \private \memberof mlt_properties_s
416 * \param self a properties list
417 * \param name the property to lookup or add
418 * \return the property
421 static mlt_property mlt_properties_fetch( mlt_properties self, const char *name )
423 // Try to find an existing property first
424 mlt_property property = mlt_properties_find( self, name );
426 // If it wasn't found, create one
427 if ( property == NULL )
428 property = mlt_properties_add( self, name );
430 // Return the property
434 /** Copy a property to another properties list.
436 * \public \memberof mlt_properties_s
437 * \author Zach <zachary.drew@gmail.com>
438 * \param self the properties to copy to
439 * \param that the properties to copy from
440 * \param name the name of the property to copy
443 void mlt_properties_pass_property( mlt_properties self, mlt_properties that, const char *name )
445 // Make sure the source property isn't null.
446 mlt_property that_prop = mlt_properties_find( that, name );
447 if( that_prop == NULL )
450 mlt_property_pass( mlt_properties_fetch( self, name ), that_prop );
453 /** Copy all properties specified in a comma-separated list to another properties list.
455 * White space is also a delimiter.
456 * \public \memberof mlt_properties_s
457 * \author Zach <zachary.drew@gmail.com>
458 * \param self the properties to copy to
459 * \param that the properties to copy from
460 * \param list a delimited list of property names
465 int mlt_properties_pass_list( mlt_properties self, mlt_properties that, const char *list )
467 char *props = strdup( list );
469 const char *delim = " ,\t\n"; // Any combination of spaces, commas, tabs, and newlines
474 count = strcspn( ptr, delim );
476 if( ptr[count] == '\0' )
479 ptr[count] = '\0'; // Make it a real string
481 mlt_properties_pass_property( self, that, ptr );
484 ptr += strspn( ptr, delim );
493 /** Set a property to a string.
495 * This makes a copy of the string value you supply.
496 * \public \memberof mlt_properties_s
497 * \param self a properties list
498 * \param name the property to set
499 * \param value the property's new value
500 * \return true if error
503 int mlt_properties_set( mlt_properties self, const char *name, const char *value )
507 // Fetch the property to work with
508 mlt_property property = mlt_properties_fetch( self, name );
510 // Set it if not NULL
511 if ( property == NULL )
513 mlt_log( NULL, MLT_LOG_FATAL, "Whoops - %s not found (should never occur)\n", name );
515 else if ( value == NULL )
517 error = mlt_property_set_string( property, value );
518 mlt_properties_do_mirror( self, name );
520 else if ( *value != '@' )
522 error = mlt_property_set_string( property, value );
523 mlt_properties_do_mirror( self, name );
525 else if ( value[ 0 ] == '@' )
534 while ( *value != '\0' )
536 int length = strcspn( value, "+-*/" );
538 // Get the identifier
539 strncpy( id, value, length );
543 // Determine the value
544 if ( isdigit( id[ 0 ] ) )
545 current = atof( id );
547 current = mlt_properties_get_double( self, id );
549 // Apply the operation
562 total = total / current;
567 op = *value != '\0' ? *value ++ : ' ';
570 error = mlt_property_set_double( property, total );
571 mlt_properties_do_mirror( self, name );
574 mlt_events_fire( self, "property-changed", name, NULL );
579 /** Set or default a property to a string.
581 * This makes a copy of the string value you supply.
582 * \public \memberof mlt_properties_s
583 * \param self a properties list
584 * \param name the property to set
585 * \param value the string value to set or NULL to use the default
586 * \param def the default string if value is NULL
587 * \return true if error
590 int mlt_properties_set_or_default( mlt_properties self, const char *name, const char *value, const char *def )
592 return mlt_properties_set( self, name, value == NULL ? def : value );
595 /** Get a string value by name.
597 * Do not free the returned string. It's lifetime is controlled by the property
598 * and this properties object.
599 * \public \memberof mlt_properties_s
600 * \param self a properties list
601 * \param name the property to get
602 * \return the property's string value or NULL if it does not exist
605 char *mlt_properties_get( mlt_properties self, const char *name )
607 mlt_property value = mlt_properties_find( self, name );
608 return value == NULL ? NULL : mlt_property_get_string( value );
611 /** Get a property name by index.
613 * Do not free the returned string.
614 * \public \memberof mlt_properties_s
615 * \param self a properties list
616 * \param index the numeric index of the property
617 * \return the name of the property or NULL if index is out of range
620 char *mlt_properties_get_name( mlt_properties self, int index )
622 property_list *list = self->local;
623 if ( index >= 0 && index < list->count )
624 return list->name[ index ];
628 /** Get a property's string value by index.
630 * Do not free the returned string.
631 * \public \memberof mlt_properties_s
632 * \param self a properties list
633 * \param index the numeric index of the property
634 * \return the property value as a string or NULL if the index is out of range
637 char *mlt_properties_get_value( mlt_properties self, int index )
639 property_list *list = self->local;
640 if ( index >= 0 && index < list->count )
641 return mlt_property_get_string( list->value[ index ] );
645 /** Get a data value by index.
647 * Do not free the returned pointer if you supplied a destructor function when you
649 * \public \memberof mlt_properties_s
650 * \param self a properties list
651 * \param index the numeric index of the property
652 * \param[out] size the size of the binary data in bytes or NULL if the index is out of range
655 void *mlt_properties_get_data_at( mlt_properties self, int index, int *size )
657 property_list *list = self->local;
658 if ( index >= 0 && index < list->count )
659 return mlt_property_get_data( list->value[ index ], size );
663 /** Return the number of items in the list.
665 * \public \memberof mlt_properties_s
666 * \param self a properties list
667 * \return the number of property objects
670 int mlt_properties_count( mlt_properties self )
672 property_list *list = self->local;
676 /** Set a value by parsing a name=value string.
678 * \public \memberof mlt_properties_s
679 * \param self a properties list
680 * \param namevalue a string containing name and value delimited by '='
681 * \return true if there was an error
684 int mlt_properties_parse( mlt_properties self, const char *namevalue )
686 char *name = strdup( namevalue );
689 char *ptr = strchr( name, '=' );
697 value = strdup( ptr );
702 value = strdup( ptr );
703 if ( value != NULL && value[ strlen( value ) - 1 ] == '\"' )
704 value[ strlen( value ) - 1 ] = '\0';
709 value = strdup( "" );
712 error = mlt_properties_set( self, name, value );
720 /** Get an integer associated to the name.
722 * \public \memberof mlt_properties_s
723 * \param self a properties list
724 * \param name the property to get
725 * \return The integer value, 0 if not found (which may also be a legitimate value)
728 int mlt_properties_get_int( mlt_properties self, const char *name )
730 mlt_property value = mlt_properties_find( self, name );
731 return value == NULL ? 0 : mlt_property_get_int( value );
734 /** Set a property to an integer value.
736 * \public \memberof mlt_properties_s
737 * \param self a properties list
738 * \param name the property to set
739 * \param value the integer
740 * \return true if error
743 int mlt_properties_set_int( mlt_properties self, const char *name, int value )
747 // Fetch the property to work with
748 mlt_property property = mlt_properties_fetch( self, name );
750 // Set it if not NULL
751 if ( property != NULL )
753 error = mlt_property_set_int( property, value );
754 mlt_properties_do_mirror( self, name );
757 mlt_events_fire( self, "property-changed", name, NULL );
762 /** Get a 64-bit integer associated to the name.
764 * \public \memberof mlt_properties_s
765 * \param self a properties list
766 * \param name the property to get
767 * \return the integer value, 0 if not found (which may also be a legitimate value)
770 int64_t mlt_properties_get_int64( mlt_properties self, const char *name )
772 mlt_property value = mlt_properties_find( self, name );
773 return value == NULL ? 0 : mlt_property_get_int64( value );
776 /** Set a property to a 64-bit integer value.
778 * \public \memberof mlt_properties_s
779 * \param self a properties list
780 * \param name the property to set
781 * \param value the integer
782 * \return true if error
785 int mlt_properties_set_int64( mlt_properties self, const char *name, int64_t value )
789 // Fetch the property to work with
790 mlt_property property = mlt_properties_fetch( self, name );
792 // Set it if not NULL
793 if ( property != NULL )
795 error = mlt_property_set_int64( property, value );
796 mlt_properties_do_mirror( self, name );
799 mlt_events_fire( self, "property-changed", name, NULL );
804 /** Get a floating point value associated to the name.
806 * \public \memberof mlt_properties_s
807 * \param self a properties list
808 * \param name the property to get
809 * \return the floating point, 0 if not found (which may also be a legitimate value)
812 double mlt_properties_get_double( mlt_properties self, const char *name )
814 mlt_property value = mlt_properties_find( self, name );
815 return value == NULL ? 0 : mlt_property_get_double( value );
818 /** Set a property to a floating point value.
820 * \public \memberof mlt_properties_s
821 * \param self a properties list
822 * \param name the property to set
823 * \param value the floating point value
824 * \return true if error
827 int mlt_properties_set_double( mlt_properties self, const char *name, double value )
831 // Fetch the property to work with
832 mlt_property property = mlt_properties_fetch( self, name );
834 // Set it if not NULL
835 if ( property != NULL )
837 error = mlt_property_set_double( property, value );
838 mlt_properties_do_mirror( self, name );
841 mlt_events_fire( self, "property-changed", name, NULL );
846 /** Get a position value associated to the name.
848 * \public \memberof mlt_properties_s
849 * \param self a properties list
850 * \param name the property to get
851 * \return the position, 0 if not found (which may also be a legitimate value)
854 mlt_position mlt_properties_get_position( mlt_properties self, const char *name )
856 mlt_property value = mlt_properties_find( self, name );
857 return value == NULL ? 0 : mlt_property_get_position( value );
860 /** Set a property to a position value.
862 * \public \memberof mlt_properties_s
863 * \param self a properties list
864 * \param name the property to get
865 * \param value the position
866 * \return true if error
869 int mlt_properties_set_position( mlt_properties self, const char *name, mlt_position value )
873 // Fetch the property to work with
874 mlt_property property = mlt_properties_fetch( self, name );
876 // Set it if not NULL
877 if ( property != NULL )
879 error = mlt_property_set_position( property, value );
880 mlt_properties_do_mirror( self, name );
883 mlt_events_fire( self, "property-changed", name, NULL );
888 /** Get a binary data value associated to the name.
890 * Do not free the returned pointer if you supplied a destructor function
891 * when you set this property.
892 * \public \memberof mlt_properties_s
893 * \param self a properties list
894 * \param name the property to get
895 * \param[out] length The size of the binary data in bytes, if available (often it is not, you should know)
898 void *mlt_properties_get_data( mlt_properties self, const char *name, int *length )
900 mlt_property value = mlt_properties_find( self, name );
901 return value == NULL ? NULL : mlt_property_get_data( value, length );
904 /** Store binary data as a property.
906 * \public \memberof mlt_properties_s
907 * \param self a properties list
908 * \param name the property to set
909 * \param value an opaque pointer to binary data
910 * \param length the size of the binary data in bytes (optional)
911 * \param destroy a function to deallocate the binary data when the property is closed (optional)
912 * \param serialise a function that can serialize the binary data as text (optional)
913 * \return true if error
916 int mlt_properties_set_data( mlt_properties self, const char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
920 // Fetch the property to work with
921 mlt_property property = mlt_properties_fetch( self, name );
923 // Set it if not NULL
924 if ( property != NULL )
925 error = mlt_property_set_data( property, value, length, destroy, serialise );
927 mlt_events_fire( self, "property-changed", name, NULL );
932 /** Rename a property.
934 * \public \memberof mlt_properties_s
935 * \param self a properties list
936 * \param source the property to rename
937 * \param dest the new name
938 * \return true if the name is already in use
941 int mlt_properties_rename( mlt_properties self, const char *source, const char *dest )
943 mlt_property value = mlt_properties_find( self, dest );
947 property_list *list = self->local;
951 mlt_properties_lock( self );
952 for ( i = 0; i < list->count; i ++ )
954 if ( !strcmp( list->name[ i ], source ) )
956 free( list->name[ i ] );
957 list->name[ i ] = strdup( dest );
958 list->hash[ generate_hash( dest ) ] = i + 1;
962 mlt_properties_unlock( self );
965 return value != NULL;
968 /** Dump the properties to a file handle.
970 * \public \memberof mlt_properties_s
971 * \param self a properties list
972 * \param output a file handle
975 void mlt_properties_dump( mlt_properties self, FILE *output )
977 property_list *list = self->local;
979 for ( i = 0; i < list->count; i ++ )
980 if ( mlt_properties_get( self, list->name[ i ] ) != NULL )
981 fprintf( output, "%s=%s\n", list->name[ i ], mlt_properties_get( self, list->name[ i ] ) );
984 /** Output the properties to a file handle.
986 * This version includes reference counts and does not put each property on a new line.
987 * \public \memberof mlt_properties_s
988 * \param self a properties pointer
989 * \param title a string to preface the output
990 * \param output a file handle
992 void mlt_properties_debug( mlt_properties self, const char *title, FILE *output )
994 if ( output == NULL ) output = stderr;
995 fprintf( output, "%s: ", title );
998 property_list *list = self->local;
1000 fprintf( output, "[ ref=%d", list->ref_count );
1001 for ( i = 0; i < list->count; i ++ )
1002 if ( mlt_properties_get( self, list->name[ i ] ) != NULL )
1003 fprintf( output, ", %s=%s", list->name[ i ], mlt_properties_get( self, list->name[ i ] ) );
1005 fprintf( output, ", %s=%p", list->name[ i ], mlt_properties_get_data( self, list->name[ i ], NULL ) );
1006 fprintf( output, " ]" );
1008 fprintf( output, "\n" );
1011 /** Save the properties to a file by name.
1013 * This uses the dump format - one line per property.
1014 * \public \memberof mlt_properties_s
1015 * \param self a properties list
1016 * \param filename the name of a file to create or overwrite
1017 * \return true if there was an error
1020 int mlt_properties_save( mlt_properties self, const char *filename )
1023 FILE *f = fopen( filename, "w" );
1026 mlt_properties_dump( self, f );
1033 /* This is a very basic cross platform fnmatch replacement - it will fail in
1034 * many cases, but for the basic *.XXX and YYY*.XXX, it will work ok.
1037 /** Test whether a filename or pathname matches a shell-style pattern.
1039 * \private \memberof mlt_properties_s
1040 * \param wild a string containing a wildcard pattern
1041 * \param file the name of a file to test against
1042 * \return true if the file name matches the wildcard pattern
1045 static int mlt_fnmatch( const char *wild, const char *file )
1050 while( f < strlen( file ) && w < strlen( wild ) )
1052 if ( wild[ w ] == '*' )
1055 if ( w == strlen( wild ) )
1057 while ( f != strlen( file ) && tolower( file[ f ] ) != tolower( wild[ w ] ) )
1060 else if ( wild[ w ] == '?' || tolower( file[ f ] ) == tolower( wild[ w ] ) )
1065 else if ( wild[ 0 ] == '*' )
1075 return strlen( file ) == f && strlen( wild ) == w;
1078 /** Compare the string or serialized value of two properties.
1080 * \private \memberof mlt_properties_s
1081 * \param self a property
1082 * \param that a property
1083 * \return < 0 if \p self less than \p that, 0 if equal, or > 0 if \p self is greater than \p that
1086 static int mlt_compare( const void *self, const void *that )
1088 return strcmp( mlt_property_get_string( *( const mlt_property * )self ), mlt_property_get_string( *( const mlt_property * )that ) );
1091 /** Get the contents of a directory.
1093 * Obtains an optionally sorted list of the files found in a directory with a specific wild card.
1094 * Entries in the list have a numeric name (running from 0 to count - 1). Only values change
1095 * position if sort is enabled. Designed to be posix compatible (linux, os/x, mingw etc).
1096 * \public \memberof mlt_properties_s
1097 * \param self a properties list
1098 * \param dirname the name of the directory
1099 * \param pattern a wildcard pattern to filter the directory listing
1100 * \param sort Do you want to sort the directory listing?
1101 * \return the number of items in the directory listing
1104 int mlt_properties_dir_list( mlt_properties self, const char *dirname, const char *pattern, int sort )
1106 DIR *dir = opendir( dirname );
1111 struct dirent *de = readdir( dir );
1112 char fullname[ 1024 ];
1115 sprintf( key, "%d", mlt_properties_count( self ) );
1116 snprintf( fullname, 1024, "%s/%s", dirname, de->d_name );
1117 if ( pattern == NULL )
1118 mlt_properties_set( self, key, fullname );
1119 else if ( de->d_name[ 0 ] != '.' && mlt_fnmatch( pattern, de->d_name ) )
1120 mlt_properties_set( self, key, fullname );
1121 de = readdir( dir );
1127 if ( sort && mlt_properties_count( self ) )
1129 property_list *list = self->local;
1130 mlt_properties_lock( self );
1131 qsort( list->value, mlt_properties_count( self ), sizeof( mlt_property ), mlt_compare );
1132 mlt_properties_unlock( self );
1135 return mlt_properties_count( self );
1138 /** Close a properties object.
1140 * Deallocates the properties object and everything it contains.
1141 * \public \memberof mlt_properties_s
1142 * \param self a properties object
1145 void mlt_properties_close( mlt_properties self )
1147 if ( self != NULL && mlt_properties_dec_ref( self ) <= 0 )
1149 if ( self->close != NULL )
1151 self->close( self->close_object );
1155 property_list *list = self->local;
1158 #if _MLT_PROPERTY_CHECKS_ == 1
1160 mlt_properties_debug( self, "Closing", stderr );
1163 #ifdef _MLT_PROPERTY_CHECKS_
1164 // Increment destroyed count
1165 properties_destroyed ++;
1167 // Show current stats - these should match when the app is closed
1168 mlt_log( NULL, MLT_LOG_DEBUG, "Created %d, destroyed %d\n", properties_created, properties_destroyed );
1171 // Clean up names and values
1172 for ( index = list->count - 1; index >= 0; index -- )
1174 mlt_property_close( list->value[ index ] );
1175 free( list->name[ index ] );
1178 // Clear up the list
1179 pthread_mutex_destroy( &list->mutex );
1181 free( list->value );
1184 // Free self now if self has no child
1185 if ( self->child == NULL )
1191 /** Determine if the properties list is really just a sequence or ordered list.
1193 * \public \memberof mlt_properties_s
1194 * \param properties a properties list
1195 * \return true if all of the property names are numeric (a sequence)
1198 int mlt_properties_is_sequence( mlt_properties properties )
1201 int n = mlt_properties_count( properties );
1202 for ( i = 0; i < n; i++ )
1203 if ( ! isdigit( mlt_properties_get_name( properties, i )[0] ) )
1208 /** \brief YAML Tiny Parser context structure
1210 * YAML is a nifty text format popular in the Ruby world as a cleaner,
1211 * less verbose alternative to XML. See this Wikipedia topic for an overview:
1212 * http://en.wikipedia.org/wiki/YAML
1213 * The YAML specification is at:
1215 * YAML::Tiny is a Perl module that specifies a subset of YAML that we are
1216 * using here (for the same reasons):
1217 * http://search.cpan.org/~adamk/YAML-Tiny-1.25/lib/YAML/Tiny.pm
1221 struct yaml_parser_context
1228 unsigned int block_indent;
1231 typedef struct yaml_parser_context *yaml_parser;
1233 /** Remove spaces from the left side of a string.
1235 * \param s the string to trim
1236 * \return the number of characters removed
1239 static unsigned int ltrim( char **s )
1243 int n = strlen( c );
1244 for ( i = 0; i < n && *c == ' '; i++, c++ );
1249 /** Remove spaces from the right side of a string.
1251 * \param s the string to trim
1252 * \return the number of characters removed
1255 static unsigned int rtrim( char *s )
1257 int n = strlen( s );
1259 for ( i = n; i > 0 && s[i - 1] == ' '; --i )
1264 /** Parse a line of YAML Tiny.
1266 * Adds a property if needed.
1267 * \private \memberof yaml_parser_context
1268 * \param context a YAML Tiny Parser context
1269 * \param namevalue a line of YAML Tiny
1270 * \return true if there was an error
1273 static int parse_yaml( yaml_parser context, const char *namevalue )
1275 char *name_ = strdup( namevalue );
1279 char *ptr = strchr( name, ':' );
1280 unsigned int indent = ltrim( &name );
1281 mlt_properties properties = mlt_deque_peek_front( context->stack );
1283 // Ascending one more levels in the tree
1284 if ( indent < context->level )
1287 unsigned int n = ( context->level - indent ) / 2;
1288 for ( i = 0; i < n; i++ )
1289 mlt_deque_pop_front( context->stack );
1290 properties = mlt_deque_peek_front( context->stack );
1291 context->level = indent;
1294 // Descending a level in the tree
1295 else if ( indent > context->level && context->block == 0 )
1297 context->level = indent;
1300 // If there is a colon that is not part of a block
1301 if ( ptr && ( indent == context->level ) )
1303 // Reset block processing
1304 if ( context->block_name )
1306 free( context->block_name );
1307 context->block_name = NULL;
1311 // Terminate the name and setup the value pointer
1315 char *comment = strchr( ptr, '#' );
1321 // Trim leading and trailing spaces from bare value
1325 // No value means a child
1326 if ( strcmp( ptr, "" ) == 0 )
1328 mlt_properties child = mlt_properties_new();
1329 mlt_properties_set_data( properties, name, child, 0,
1330 ( mlt_destructor )mlt_properties_close, NULL );
1331 mlt_deque_push_front( context->stack, child );
1337 // A dash indicates a sequence item
1338 if ( name[0] == '-' )
1340 mlt_properties child = mlt_properties_new();
1343 snprintf( key, sizeof(key), "%d", context->index++ );
1344 mlt_properties_set_data( properties, key, child, 0,
1345 ( mlt_destructor )mlt_properties_close, NULL );
1346 mlt_deque_push_front( context->stack, child );
1349 context->level += ltrim( &name ) + 1;
1357 value = strdup( ptr );
1358 if ( value && value[ strlen( value ) - 1 ] == '\"' )
1359 value[ strlen( value ) - 1 ] = 0;
1362 // Value is folded or unfolded block
1363 else if ( *ptr == '|' || *ptr == '>' )
1365 context->block = *ptr;
1366 context->block_name = strdup( name );
1367 context->block_indent = 0;
1368 value = strdup( "" );
1374 value = strdup( ptr );
1378 // A list of scalars
1379 else if ( name[0] == '-' )
1381 // Reset block processing
1382 if ( context->block_name )
1384 free( context->block_name );
1385 context->block_name = NULL;
1391 snprintf( key, sizeof(key), "%d", context->index++ );
1395 char *comment = strchr( ptr, '#' );
1399 // Trim leading and trailing spaces from bare value
1407 value = strdup( ptr );
1408 if ( value && value[ strlen( value ) - 1 ] == '\"' )
1409 value[ strlen( value ) - 1 ] = 0;
1412 // Value is folded or unfolded block
1413 else if ( *ptr == '|' || *ptr == '>' )
1415 context->block = *ptr;
1416 context->block_name = strdup( key );
1417 context->block_indent = 0;
1418 value = strdup( "" );
1424 value = strdup( ptr );
1428 name = name_ = strdup( key );
1432 else if ( context->block == '|' )
1434 if ( context->block_indent == 0 )
1435 context->block_indent = indent;
1436 if ( indent > context->block_indent )
1437 name = &name_[ context->block_indent ];
1439 char *old_value = mlt_properties_get( properties, context->block_name );
1440 value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
1441 strcpy( value, old_value );
1442 if ( strcmp( old_value, "" ) )
1443 strcat( value, "\n" );
1444 strcat( value, name );
1445 name = context->block_name;
1449 else if ( context->block == '>' )
1453 char *old_value = mlt_properties_get( properties, context->block_name );
1455 // Blank line (prepended with spaces) is new line
1456 if ( strcmp( name, "" ) == 0 )
1458 value = calloc( 1, strlen( old_value ) + 2 );
1459 strcat( value, old_value );
1460 strcat( value, "\n" );
1462 // Concatenate with space
1465 value = calloc( 1, strlen( old_value ) + strlen( name ) + 2 );
1466 strcat( value, old_value );
1467 if ( strcmp( old_value, "" ) && old_value[ strlen( old_value ) - 1 ] != '\n' )
1468 strcat( value, " " );
1469 strcat( value, name );
1471 name = context->block_name;
1476 value = strdup( "" );
1479 error = mlt_properties_set( properties, name, value );
1487 /** Parse a YAML Tiny file by name.
1489 * \public \memberof mlt_properties_s
1490 * \param filename the name of a text file containing YAML Tiny
1491 * \return a new properties list
1494 mlt_properties mlt_properties_parse_yaml( const char *filename )
1496 // Construct a standalone properties object
1497 mlt_properties self = mlt_properties_new( );
1502 FILE *file = fopen( filename, "r" );
1504 // Load contents of file
1509 char *ptemp = &temp[ 0 ];
1512 yaml_parser context = calloc( 1, sizeof( struct yaml_parser_context ) );
1513 context->stack = mlt_deque_init();
1514 mlt_deque_push_front( context->stack, self );
1516 // Read each string from the file
1517 while( fgets( temp, 1024, file ) )
1519 // Check for end-of-stream
1520 if ( strncmp( ptemp, "...", 3 ) == 0 )
1524 temp[ strlen( temp ) - 1 ] = '\0';
1526 // Skip blank lines, comment lines, and document separator
1527 if ( strcmp( ptemp, "" ) && ptemp[ 0 ] != '#' && strncmp( ptemp, "---", 3 )
1528 && strncmp( ptemp, "%YAML", 5 ) && strncmp( ptemp, "% YAML", 6 ) )
1529 parse_yaml( context, temp );
1534 mlt_deque_close( context->stack );
1535 if ( context->block_name )
1536 free( context->block_name );
1541 // Return the pointer
1546 * YAML Tiny Serializer
1549 /** How many bytes to grow at a time */
1550 #define STRBUF_GROWTH (1024)
1552 /** \brief Private to mlt_properties_s, a self-growing buffer for building strings
1562 typedef struct strbuf_s *strbuf;
1564 /** Create a new string buffer
1566 * \private \memberof strbuf_s
1567 * \return a new string buffer
1570 static strbuf strbuf_new( )
1572 strbuf buffer = calloc( 1, sizeof( struct strbuf_s ) );
1573 buffer->size = STRBUF_GROWTH;
1574 buffer->string = calloc( 1, buffer->size );
1578 /** Destroy a string buffer
1580 * \private \memberof strbuf_s
1581 * \param buffer the string buffer to close
1584 static void strbuf_close( strbuf buffer )
1586 // We do not free buffer->string; strbuf user must save that pointer
1592 /** Format a string into a string buffer
1594 * A variable number of arguments follows the format string - one for each
1596 * \private \memberof strbuf_s
1597 * \param buffer the string buffer to write into
1598 * \param format a string that contains text and formatting instructions
1599 * \return the formatted string
1602 static char *strbuf_printf( strbuf buffer, const char *format, ... )
1604 while ( buffer->string )
1607 va_start( ap, format );
1608 size_t len = strlen( buffer->string );
1609 size_t remain = buffer->size - len - 1;
1610 int need = vsnprintf( buffer->string + len, remain, format, ap );
1612 if ( need > -1 && need < remain )
1614 buffer->string[ len ] = 0;
1615 buffer->size += need + STRBUF_GROWTH;
1616 buffer->string = realloc( buffer->string, buffer->size );
1618 return buffer->string;
1621 /** Indent a line of YAML Tiny.
1623 * \private \memberof strbuf_s
1624 * \param output a string buffer
1625 * \param indent the number of spaces to indent
1628 static inline void indent_yaml( strbuf output, int indent )
1631 for ( j = 0; j < indent; j++ )
1632 strbuf_printf( output, " " );
1635 /** Convert a line string into a YAML block literal.
1637 * \private \memberof strbuf_s
1638 * \param output a string buffer
1639 * \param value the string to format as a block literal
1640 * \param indent the number of spaces to indent
1643 static void output_yaml_block_literal( strbuf output, const char *value, int indent )
1645 char *v = strdup( value );
1647 char *eol = strchr( sol, '\n' );
1651 indent_yaml( output, indent );
1653 strbuf_printf( output, "%s\n", sol );
1655 eol = strchr( sol, '\n' );
1657 indent_yaml( output, indent );
1658 strbuf_printf( output, "%s\n", sol );
1661 /** Recursively serialize a properties list into a string buffer as YAML Tiny.
1663 * \private \memberof mlt_properties_s
1664 * \param self a properties list
1665 * \param output a string buffer to hold the serialized YAML Tiny
1666 * \param indent the number of spaces to indent (for recursion, initialize to 0)
1667 * \param is_parent_sequence Is this properties list really just a sequence (for recursion, initialize to 0)?
1670 static void serialise_yaml( mlt_properties self, strbuf output, int indent, int is_parent_sequence )
1672 property_list *list = self->local;
1675 for ( i = 0; i < list->count; i ++ )
1677 // This implementation assumes that all data elements are property lists.
1678 // Unfortunately, we do not have run time type identification.
1679 mlt_properties child = mlt_property_get_data( list->value[ i ], NULL );
1681 if ( mlt_properties_is_sequence( self ) )
1683 // Ignore hidden/non-serialisable items
1684 if ( list->name[ i ][ 0 ] != '_' )
1686 // Indicate a sequence item
1687 indent_yaml( output, indent );
1688 strbuf_printf( output, "- " );
1690 // If the value can be represented as a string
1691 const char *value = mlt_properties_get( self, list->name[ i ] );
1692 if ( value && strcmp( value, "" ) )
1694 // Determine if this is an unfolded block literal
1695 if ( strchr( value, '\n' ) )
1697 strbuf_printf( output, "|\n" );
1698 output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( "|" ) );
1702 strbuf_printf( output, "%s\n", value );
1708 serialise_yaml( child, output, indent + 2, 1 );
1712 // Assume this is a normal map-oriented properties list
1713 const char *value = mlt_properties_get( self, list->name[ i ] );
1715 // Ignore hidden/non-serialisable items
1716 // If the value can be represented as a string
1717 if ( list->name[ i ][ 0 ] != '_' && value && strcmp( value, "" ) )
1719 if ( is_parent_sequence == 0 )
1720 indent_yaml( output, indent );
1722 is_parent_sequence = 0;
1724 // Determine if this is an unfolded block literal
1725 if ( strchr( value, '\n' ) )
1727 strbuf_printf( output, "%s: |\n", list->name[ i ] );
1728 output_yaml_block_literal( output, value, indent + strlen( list->name[ i ] ) + strlen( ": " ) );
1732 strbuf_printf( output, "%s: %s\n", list->name[ i ], value );
1736 // Output a child as a map item
1739 indent_yaml( output, indent );
1740 strbuf_printf( output, "%s:\n", list->name[ i ] );
1743 serialise_yaml( child, output, indent + 2, 0 );
1749 /** Serialize a properties list as a string of YAML Tiny.
1751 * The caller MUST free the returned string!
1752 * This operates on properties containing properties as a hierarchical data
1754 * \public \memberof mlt_properties_s
1755 * \param self a properties list
1756 * \return a string containing YAML Tiny that represents the properties list
1759 char *mlt_properties_serialise_yaml( mlt_properties self )
1761 strbuf b = strbuf_new();
1762 strbuf_printf( b, "---\n" );
1763 serialise_yaml( self, b, 0, 0 );
1764 strbuf_printf( b, "...\n" );
1765 char *ret = b->string;
1770 /** Protect a properties list against concurrent access.
1772 * \public \memberof mlt_properties_s
1773 * \param self a properties list
1776 void mlt_properties_lock( mlt_properties self )
1779 pthread_mutex_lock( &( ( property_list* )( self->local ) )->mutex );
1782 /** End protecting a properties list against concurrent access.
1784 * \public \memberof mlt_properties_s
1785 * \param self a properties list
1788 void mlt_properties_unlock( mlt_properties self )
1791 pthread_mutex_unlock( &( ( property_list* )( self->local ) )->mutex );