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"
34 #include <sys/types.h>
38 /** \brief private implementation of the property list */
47 mlt_properties mirror
;
49 pthread_mutex_t mutex
;
53 /* Memory leak checks */
55 //#define _MLT_PROPERTY_CHECKS_ 2
56 #ifdef _MLT_PROPERTY_CHECKS_
57 static int properties_created
= 0;
58 static int properties_destroyed
= 0;
61 /** Initialize a properties object that was already allocated.
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
70 int mlt_properties_init( mlt_properties
this, void *child
)
74 #ifdef _MLT_PROPERTY_CHECKS_
75 // Increment number of properties created
76 properties_created
++;
80 memset( this, 0, sizeof( struct mlt_properties_s
) );
82 // Assign the child of the object
85 // Allocate the local structure
86 this->local
= calloc( sizeof( property_list
), 1 );
88 // Increment the ref count
89 ( ( property_list
* )this->local
)->ref_count
= 1;
90 pthread_mutex_init( &( ( property_list
* )this->local
)->mutex
, NULL
);;
93 // Check that initialisation was successful
94 return this != NULL
&& this->local
== NULL
;
97 /** Create a properties object.
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
105 mlt_properties
mlt_properties_new( )
107 // Construct a standalone properties object
108 mlt_properties
this = calloc( sizeof( struct mlt_properties_s
), 1 );
111 mlt_properties_init( this, NULL
);
113 // Return the pointer
117 /** Create a properties object by reading a .properties text file.
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
126 mlt_properties
mlt_properties_load( const char *filename
)
128 // Construct a standalone properties object
129 mlt_properties
this = mlt_properties_new( );
134 FILE *file
= fopen( filename
, "r" );
136 // Load contents of file
141 char last
[ 1024 ] = "";
143 // Read each string from the file
144 while( fgets( temp
, 1024, file
) )
147 temp
[ strlen( temp
) - 1 ] = '\0';
149 // Check if the line starts with a .
150 if ( temp
[ 0 ] == '.' )
153 sprintf( temp2
, "%s%s", last
, temp
);
154 strcpy( temp
, temp2
);
156 else if ( strchr( temp
, '=' ) )
158 strcpy( last
, temp
);
159 *( strchr( last
, '=' ) ) = '\0';
162 // Parse and set the property
163 if ( strcmp( temp
, "" ) && temp
[ 0 ] != '#' )
164 mlt_properties_parse( this, temp
);
172 // Return the pointer
176 /** Generate a hash key.
178 * \private \memberof mlt_properties_s
179 * \param name a string
183 static inline int generate_hash( const char *name
)
188 hash
= ( hash
+ ( i
++ * ( *name
++ & 31 ) ) ) % 199;
192 /** Copy a serializable property to properties list that is mirroring this one.
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
197 * \private \memberof mlt_properties_s
198 * \param this a properties list
199 * \param name the name of the property to copy
202 static inline void mlt_properties_do_mirror( mlt_properties
this, const char *name
)
204 property_list
*list
= this->local
;
205 if ( list
->mirror
!= NULL
)
207 char *value
= mlt_properties_get( this, name
);
209 mlt_properties_set( list
->mirror
, name
, value
);
213 /** Increment the reference count.
215 * \public \memberof mlt_properties_s
216 * \param this a properties list
217 * \return the new reference count
220 int mlt_properties_inc_ref( mlt_properties
this )
225 property_list
*list
= this->local
;
226 pthread_mutex_lock( &list
->mutex
);
227 result
= ++ list
->ref_count
;
228 pthread_mutex_unlock( &list
->mutex
);
233 /** Decrement the reference count.
235 * \public \memberof mlt_properties_s
236 * \param this a properties list
237 * \return the new reference count
240 int mlt_properties_dec_ref( mlt_properties
this )
245 property_list
*list
= this->local
;
246 pthread_mutex_lock( &list
->mutex
);
247 result
= -- list
->ref_count
;
248 pthread_mutex_unlock( &list
->mutex
);
253 /** Get the reference count.
255 * \public \memberof mlt_properties_s
256 * \param this a properties list
257 * \return the current reference count
260 int mlt_properties_ref_count( mlt_properties
this )
264 property_list
*list
= this->local
;
265 return list
->ref_count
;
270 /** Set a properties list to be a mirror copy of another.
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
279 void mlt_properties_mirror( mlt_properties
this, mlt_properties that
)
281 property_list
*list
= this->local
;
285 /** Copy all serializable properties to another properties list.
287 * \public \memberof mlt_properties_s
288 * \param this The properties to copy from
289 * \param that The properties to copy to
293 int mlt_properties_inherit( mlt_properties
this, mlt_properties that
)
295 int count
= mlt_properties_count( that
);
297 for ( i
= 0; i
< count
; i
++ )
299 char *value
= mlt_properties_get_value( that
, i
);
302 char *name
= mlt_properties_get_name( that
, i
);
303 mlt_properties_set( this, name
, value
);
309 /** Pass all serializable properties that match a prefix to another properties object
311 * \public \memberof mlt_properties_s
312 * \param this the properties to copy from
313 * \param that The properties to copy to
314 * \param prefix the property names to match (required)
318 int mlt_properties_pass( mlt_properties
this, mlt_properties that
, const char *prefix
)
320 int count
= mlt_properties_count( that
);
321 int length
= strlen( prefix
);
323 for ( i
= 0; i
< count
; i
++ )
325 char *name
= mlt_properties_get_name( that
, i
);
326 if ( !strncmp( name
, prefix
, length
) )
328 char *value
= mlt_properties_get_value( that
, i
);
330 mlt_properties_set( this, name
+ length
, value
);
336 /** Locate a property by name.
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
344 static inline mlt_property
mlt_properties_find( mlt_properties
this, const char *name
)
346 property_list
*list
= this->local
;
347 mlt_property value
= NULL
;
348 int key
= generate_hash( name
);
349 int i
= list
->hash
[ key
] - 1;
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
];
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
];
368 /** Add a new property.
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
376 static mlt_property
mlt_properties_add( mlt_properties
this, const char *name
)
378 property_list
*list
= this->local
;
379 int key
= generate_hash( name
);
381 // Check that we have space and resize if necessary
382 if ( list
->count
== list
->size
)
385 list
->name
= realloc( list
->name
, list
->size
* sizeof( const char * ) );
386 list
->value
= realloc( list
->value
, list
->size
* sizeof( mlt_property
) );
389 // Assign name/value pair
390 list
->name
[ list
->count
] = strdup( name
);
391 list
->value
[ list
->count
] = mlt_property_init( );
393 // Assign to hash table
394 if ( list
->hash
[ key
] == 0 )
395 list
->hash
[ key
] = list
->count
+ 1;
397 // Return and increment count accordingly
398 return list
->value
[ list
->count
++ ];
401 /** Fetch a property by name and add one if not found.
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
409 static mlt_property
mlt_properties_fetch( mlt_properties
this, const char *name
)
411 // Try to find an existing property first
412 mlt_property property
= mlt_properties_find( this, name
);
414 // If it wasn't found, create one
415 if ( property
== NULL
)
416 property
= mlt_properties_add( this, name
);
418 // Return the property
422 /** Copy a property to another properties list.
424 * \public \memberof mlt_properties_s
425 * \author Zach <zachary.drew@gmail.com>
426 * \param this the properties to copy from
427 * \param that the properties to copy to
428 * \param name the name of the property to copy
431 void mlt_properties_pass_property( mlt_properties
this, mlt_properties that
, const char *name
)
433 // Make sure the source property isn't null.
434 mlt_property that_prop
= mlt_properties_find( that
, name
);
435 if( that_prop
== NULL
)
438 mlt_property_pass( mlt_properties_fetch( this, name
), that_prop
);
441 /** Copy all properties specified in a comma-separated list to another properties list.
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 from
447 * \param that the properties to copy to
448 * \param list a delimited list of property names
453 int mlt_properties_pass_list( mlt_properties
this, mlt_properties that
, const char *list
)
455 char *props
= strdup( list
);
457 char *delim
= " ,\t\n"; // Any combination of spaces, commas, tabs, and newlines
462 count
= strcspn( ptr
, delim
);
464 if( ptr
[count
] == '\0' )
467 ptr
[count
] = '\0'; // Make it a real string
469 mlt_properties_pass_property( this, that
, ptr
);
472 ptr
+= strspn( ptr
, delim
);
481 /** Set a property to a string.
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
491 int mlt_properties_set( mlt_properties
this, const char *name
, const char *value
)
495 // Fetch the property to work with
496 mlt_property property
= mlt_properties_fetch( this, name
);
498 // Set it if not NULL
499 if ( property
== NULL
)
501 fprintf( stderr
, "Whoops - %s not found (should never occur)\n", name
);
503 else if ( value
== NULL
)
505 error
= mlt_property_set_string( property
, value
);
506 mlt_properties_do_mirror( this, name
);
508 else if ( *value
!= '@' )
510 error
= mlt_property_set_string( property
, value
);
511 mlt_properties_do_mirror( this, name
);
513 else if ( value
[ 0 ] == '@' )
522 while ( *value
!= '\0' )
524 int length
= strcspn( value
, "+-*/" );
526 // Get the identifier
527 strncpy( id
, value
, length
);
531 // Determine the value
532 if ( isdigit( id
[ 0 ] ) )
533 current
= atof( id
);
535 current
= mlt_properties_get_double( this, id
);
537 // Apply the operation
550 total
= total
/ current
;
555 op
= *value
!= '\0' ?
*value
++ : ' ';
558 error
= mlt_property_set_double( property
, total
);
559 mlt_properties_do_mirror( this, name
);
562 mlt_events_fire( this, "property-changed", name
, NULL
);
567 /** Set or default a property to a string.
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
578 int mlt_properties_set_or_default( mlt_properties
this, const char *name
, const char *value
, const char *def
)
580 return mlt_properties_set( this, name
, value
== NULL ? def
: value
);
583 /** Get a string value by name.
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
593 char *mlt_properties_get( mlt_properties
this, const char *name
)
595 mlt_property value
= mlt_properties_find( this, name
);
596 return value
== NULL ? NULL
: mlt_property_get_string( value
);
599 /** Get a property name by index.
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
608 char *mlt_properties_get_name( mlt_properties
this, int index
)
610 property_list
*list
= this->local
;
611 if ( index
>= 0 && index
< list
->count
)
612 return list
->name
[ index
];
616 /** Get a property's string value by index.
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
625 char *mlt_properties_get_value( mlt_properties
this, int index
)
627 property_list
*list
= this->local
;
628 if ( index
>= 0 && index
< list
->count
)
629 return mlt_property_get_string( list
->value
[ index
] );
633 /** Get a data value by index.
635 * Do not free the returned pointer if you supplied a destructor function when you
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
643 void *mlt_properties_get_data_at( mlt_properties
this, int index
, int *size
)
645 property_list
*list
= this->local
;
646 if ( index
>= 0 && index
< list
->count
)
647 return mlt_property_get_data( list
->value
[ index
], size
);
651 /** Return the number of items in the list.
653 * \public \memberof mlt_properties_s
654 * \param this a properties list
655 * \return the number of property objects
658 int mlt_properties_count( mlt_properties
this )
660 property_list
*list
= this->local
;
664 /** Set a value by parsing a name=value string.
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
672 int mlt_properties_parse( mlt_properties
this, const char *namevalue
)
674 char *name
= strdup( namevalue
);
677 char *ptr
= strchr( name
, '=' );
685 value
= strdup( ptr
);
690 value
= strdup( ptr
);
691 if ( value
!= NULL
&& value
[ strlen( value
) - 1 ] == '\"' )
692 value
[ strlen( value
) - 1 ] = '\0';
697 value
= strdup( "" );
700 error
= mlt_properties_set( this, name
, value
);
708 /** Get an integer associated to the name.
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)
716 int mlt_properties_get_int( mlt_properties
this, const char *name
)
718 mlt_property value
= mlt_properties_find( this, name
);
719 return value
== NULL ?
0 : mlt_property_get_int( value
);
722 /** Set a property to an integer value.
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
731 int mlt_properties_set_int( mlt_properties
this, const char *name
, int value
)
735 // Fetch the property to work with
736 mlt_property property
= mlt_properties_fetch( this, name
);
738 // Set it if not NULL
739 if ( property
!= NULL
)
741 error
= mlt_property_set_int( property
, value
);
742 mlt_properties_do_mirror( this, name
);
745 mlt_events_fire( this, "property-changed", name
, NULL
);
750 /** Get a 64-bit integer associated to the name.
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)
758 int64_t mlt_properties_get_int64( mlt_properties
this, const char *name
)
760 mlt_property value
= mlt_properties_find( this, name
);
761 return value
== NULL ?
0 : mlt_property_get_int64( value
);
764 /** Set a property to a 64-bit integer value.
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
773 int mlt_properties_set_int64( mlt_properties
this, const char *name
, int64_t value
)
777 // Fetch the property to work with
778 mlt_property property
= mlt_properties_fetch( this, name
);
780 // Set it if not NULL
781 if ( property
!= NULL
)
783 error
= mlt_property_set_int64( property
, value
);
784 mlt_properties_do_mirror( this, name
);
787 mlt_events_fire( this, "property-changed", name
, NULL
);
792 /** Get a floating point value associated to the name.
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)
800 double mlt_properties_get_double( mlt_properties
this, const char *name
)
802 mlt_property value
= mlt_properties_find( this, name
);
803 return value
== NULL ?
0 : mlt_property_get_double( value
);
806 /** Set a property to a floating point value.
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
815 int mlt_properties_set_double( mlt_properties
this, const char *name
, double value
)
819 // Fetch the property to work with
820 mlt_property property
= mlt_properties_fetch( this, name
);
822 // Set it if not NULL
823 if ( property
!= NULL
)
825 error
= mlt_property_set_double( property
, value
);
826 mlt_properties_do_mirror( this, name
);
829 mlt_events_fire( this, "property-changed", name
, NULL
);
834 /** Get a position value associated to the name.
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)
842 mlt_position
mlt_properties_get_position( mlt_properties
this, const char *name
)
844 mlt_property value
= mlt_properties_find( this, name
);
845 return value
== NULL ?
0 : mlt_property_get_position( value
);
848 /** Set a property to a position value.
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
857 int mlt_properties_set_position( mlt_properties
this, const char *name
, mlt_position value
)
861 // Fetch the property to work with
862 mlt_property property
= mlt_properties_fetch( this, name
);
864 // Set it if not NULL
865 if ( property
!= NULL
)
867 error
= mlt_property_set_position( property
, value
);
868 mlt_properties_do_mirror( this, name
);
871 mlt_events_fire( this, "property-changed", name
, NULL
);
876 /** Get a binary data value associated to the name.
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)
886 void *mlt_properties_get_data( mlt_properties
this, const char *name
, int *length
)
888 mlt_property value
= mlt_properties_find( this, name
);
889 return value
== NULL ? NULL
: mlt_property_get_data( value
, length
);
892 /** Store binary data as a property.
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
904 int mlt_properties_set_data( mlt_properties
this, const char *name
, void *value
, int length
, mlt_destructor destroy
, mlt_serialiser serialise
)
908 // Fetch the property to work with
909 mlt_property property
= mlt_properties_fetch( this, name
);
911 // Set it if not NULL
912 if ( property
!= NULL
)
913 error
= mlt_property_set_data( property
, value
, length
, destroy
, serialise
);
915 mlt_events_fire( this, "property-changed", name
, NULL
);
920 /** Rename a property.
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
929 int mlt_properties_rename( mlt_properties
this, const char *source
, const char *dest
)
931 mlt_property value
= mlt_properties_find( this, dest
);
935 property_list
*list
= this->local
;
939 for ( i
= 0; i
< list
->count
; i
++ )
941 if ( !strcmp( list
->name
[ i
], source
) )
943 free( list
->name
[ i
] );
944 list
->name
[ i
] = strdup( dest
);
945 list
->hash
[ generate_hash( dest
) ] = i
+ 1;
951 return value
!= NULL
;
954 /** Dump the properties to a file handle.
956 * \public \memberof mlt_properties_s
957 * \param this a properties list
958 * \param output a file handle
961 void mlt_properties_dump( mlt_properties
this, FILE *output
)
963 property_list
*list
= this->local
;
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
] ) );
970 /** Output the properties to a file handle.
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
978 void mlt_properties_debug( mlt_properties
this, const char *title
, FILE *output
)
980 if ( output
== NULL
) output
= stderr
;
981 fprintf( output
, "%s: ", title
);
984 property_list
*list
= this->local
;
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
] ) );
991 fprintf( output
, ", %s=%p", list
->name
[ i
], mlt_properties_get_data( this, list
->name
[ i
], NULL
) );
992 fprintf( output
, " ]" );
994 fprintf( output
, "\n" );
997 /** Save the properties to a file by name.
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
1006 int mlt_properties_save( mlt_properties
this, const char *filename
)
1009 FILE *f
= fopen( filename
, "w" );
1012 mlt_properties_dump( this, f
);
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.
1023 /** Test whether a filename or pathname matches a shell-style pattern.
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
1031 static int mlt_fnmatch( const char *wild
, const char *file
)
1036 while( f
< strlen( file
) && w
< strlen( wild
) )
1038 if ( wild
[ w
] == '*' )
1041 if ( w
== strlen( wild
) )
1043 while ( f
!= strlen( file
) && tolower( file
[ f
] ) != tolower( wild
[ w
] ) )
1046 else if ( wild
[ w
] == '?' || tolower( file
[ f
] ) == tolower( wild
[ w
] ) )
1051 else if ( wild
[ 0 ] == '*' )
1061 return strlen( file
) == f
&& strlen( wild
) == w
;
1064 /** Compare the string or serialized value of two properties.
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'
1072 static int mlt_compare( const void *this, const void *that
)
1074 return strcmp( mlt_property_get_string( *( mlt_property
* )this ), mlt_property_get_string( *( mlt_property
* )that
) );
1077 /** Get the contents of a directory.
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
1090 int mlt_properties_dir_list( mlt_properties
this, const char *dirname
, const char *pattern
, int sort
)
1092 DIR *dir
= opendir( dirname
);
1097 struct dirent
*de
= readdir( dir
);
1098 char fullname
[ 1024 ];
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
);
1113 if ( sort
&& mlt_properties_count( this ) )
1115 property_list
*list
= this->local
;
1116 qsort( list
->value
, mlt_properties_count( this ), sizeof( mlt_property
), mlt_compare
);
1119 return mlt_properties_count( this );
1122 /** Close a properties object.
1124 * Deallocates the properties object and everything it contains.
1125 * \public \memberof mlt_properties_s
1126 * \param this a properties object
1129 void mlt_properties_close( mlt_properties
this )
1131 if ( this != NULL
&& mlt_properties_dec_ref( this ) <= 0 )
1133 if ( this->close
!= NULL
)
1135 this->close( this->close_object
);
1139 property_list
*list
= this->local
;
1142 #if _MLT_PROPERTY_CHECKS_ == 1
1144 mlt_properties_debug( this, "Closing", stderr
);
1147 #ifdef _MLT_PROPERTY_CHECKS_
1148 // Increment destroyed count
1149 properties_destroyed
++;
1151 // Show current stats - these should match when the app is closed
1152 fprintf( stderr
, "Created %d, destroyed %d\n", properties_created
, properties_destroyed
);
1155 // Clean up names and values
1156 for ( index
= list
->count
- 1; index
>= 0; index
-- )
1158 free( list
->name
[ index
] );
1159 mlt_property_close( list
->value
[ index
] );
1162 // Clear up the list
1163 pthread_mutex_destroy( &list
->mutex
);
1165 free( list
->value
);
1168 // Free this now if this has no child
1169 if ( this->child
== NULL
)
1175 /** Determine if the properties list is really just a sequence or ordered list.
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)
1182 int mlt_properties_is_sequence( mlt_properties properties
)
1185 int n
= mlt_properties_count( properties
);
1186 for ( i
= 0; i
< n
; i
++ )
1187 if ( ! isdigit( mlt_properties_get_name( properties
, i
)[0] ) )
1192 /** \brief YAML Tiny Parser context structure
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:
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
1205 struct yaml_parser_context
1212 unsigned int block_indent
;
1215 typedef struct yaml_parser_context
*yaml_parser
;
1217 /** Remove spaces from the left side of a string.
1219 * \param s the string to trim
1220 * \return the number of characters removed
1223 static unsigned int ltrim( char **s
)
1227 int n
= strlen( c
);
1228 for ( i
= 0; i
< n
&& *c
== ' '; i
++, c
++ );
1233 /** Remove spaces from the right side of a string.
1235 * \param s the string to trim
1236 * \return the number of characters removed
1239 static unsigned int rtrim( char *s
)
1241 int n
= strlen( s
);
1243 for ( i
= n
; i
> 0 && s
[i
- 1] == ' '; --i
)
1248 /** Parse a line of YAML Tiny.
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
1257 static int parse_yaml( yaml_parser context
, const char *namevalue
)
1259 char *name_
= strdup( namevalue
);
1263 char *ptr
= strchr( name
, ':' );
1264 unsigned int indent
= ltrim( &name
);
1265 mlt_properties properties
= mlt_deque_peek_front( context
->stack
);
1267 // Ascending one more levels in the tree
1268 if ( indent
< context
->level
)
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
;
1278 // Descending a level in the tree
1279 else if ( indent
> context
->level
&& context
->block
== 0 )
1281 context
->level
= indent
;
1284 // If there is a colon that is not part of a block
1285 if ( ptr
&& ( indent
== context
->level
) )
1287 // Reset block processing
1288 if ( context
->block_name
)
1290 free( context
->block_name
);
1291 context
->block_name
= NULL
;
1295 // Terminate the name and setup the value pointer
1299 char *comment
= strchr( ptr
, '#' );
1305 // Trim leading and trailing spaces from bare value
1309 // No value means a child
1310 if ( strcmp( ptr
, "" ) == 0 )
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
);
1321 // A dash indicates a sequence item
1322 if ( name
[0] == '-' )
1324 mlt_properties child
= mlt_properties_new();
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
);
1333 context
->level
+= ltrim( &name
) + 1;
1341 value
= strdup( ptr
);
1342 if ( value
&& value
[ strlen( value
) - 1 ] == '\"' )
1343 value
[ strlen( value
) - 1 ] = 0;
1346 // Value is folded or unfolded block
1347 else if ( *ptr
== '|' || *ptr
== '>' )
1349 context
->block
= *ptr
;
1350 context
->block_name
= strdup( name
);
1351 context
->block_indent
= 0;
1352 value
= strdup( "" );
1358 value
= strdup( ptr
);
1362 // A list of scalars
1363 else if ( name
[0] == '-' )
1365 // Reset block processing
1366 if ( context
->block_name
)
1368 free( context
->block_name
);
1369 context
->block_name
= NULL
;
1375 snprintf( key
, sizeof(key
), "%d", context
->index
++ );
1379 char *comment
= strchr( ptr
, '#' );
1383 // Trim leading and trailing spaces from bare value
1391 value
= strdup( ptr
);
1392 if ( value
&& value
[ strlen( value
) - 1 ] == '\"' )
1393 value
[ strlen( value
) - 1 ] = 0;
1396 // Value is folded or unfolded block
1397 else if ( *ptr
== '|' || *ptr
== '>' )
1399 context
->block
= *ptr
;
1400 context
->block_name
= strdup( key
);
1401 context
->block_indent
= 0;
1402 value
= strdup( "" );
1408 value
= strdup( ptr
);
1412 name
= name_
= strdup( key
);
1416 else if ( context
->block
== '|' )
1418 if ( context
->block_indent
== 0 )
1419 context
->block_indent
= indent
;
1420 if ( indent
> context
->block_indent
)
1421 name
= &name_
[ context
->block_indent
];
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
;
1433 else if ( context
->block
== '>' )
1437 char *old_value
= mlt_properties_get( properties
, context
->block_name
);
1439 // Blank line (prepended with spaces) is new line
1440 if ( strcmp( name
, "" ) == 0 )
1442 value
= calloc( 1, strlen( old_value
) + 2 );
1443 strcat( value
, old_value
);
1444 strcat( value
, "\n" );
1446 // Concatenate with space
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
);
1455 name
= context
->block_name
;
1460 value
= strdup( "" );
1463 error
= mlt_properties_set( properties
, name
, value
);
1471 /** Parse a YAML Tiny file by name.
1473 * \public \memberof mlt_properties_s
1474 * \param filename the name of a text file containing YAML Tiny
1475 * \return a new properties list
1478 mlt_properties
mlt_properties_parse_yaml( const char *filename
)
1480 // Construct a standalone properties object
1481 mlt_properties
this = mlt_properties_new( );
1486 FILE *file
= fopen( filename
, "r" );
1488 // Load contents of file
1493 char *ptemp
= &temp
[ 0 ];
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 );
1500 // Read each string from the file
1501 while( fgets( temp
, 1024, file
) )
1503 // Check for end-of-stream
1504 if ( strncmp( ptemp
, "...", 3 ) == 0 )
1508 temp
[ strlen( temp
) - 1 ] = '\0';
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
);
1518 mlt_deque_close( context
->stack
);
1519 if ( context
->block_name
)
1520 free( context
->block_name
);
1525 // Return the pointer
1530 * YAML Tiny Serializer
1533 /** How many bytes to grow at a time */
1534 #define STRBUF_GROWTH (1024)
1536 /** \brief Self-growing buffer for building strings
1546 typedef struct strbuf_s
*strbuf
;
1548 /** Create a new string buffer
1550 * \private \memberof strbuf_s
1551 * \return a new string buffer
1554 static strbuf
strbuf_new( )
1556 strbuf buffer
= calloc( 1, sizeof( struct strbuf_s
) );
1557 buffer
->size
= STRBUF_GROWTH
;
1558 buffer
->string
= calloc( 1, buffer
->size
);
1562 /** Destroy a string buffer
1564 * \private \memberof strbuf_s
1565 * \param buffer the string buffer to close
1568 static void strbuf_close( strbuf buffer
)
1570 // We do not free buffer->string; strbuf user must save that pointer
1576 /** Format a string into a string buffer
1578 * A variable number of arguments follows the format string - one for each
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
1586 static char *strbuf_printf( strbuf buffer
, const char *format
, ... )
1588 while ( buffer
->string
)
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
);
1596 if ( need
> -1 && need
< remain
)
1598 buffer
->string
[ len
] = 0;
1599 buffer
->size
+= need
+ STRBUF_GROWTH
;
1600 buffer
->string
= realloc( buffer
->string
, buffer
->size
);
1602 return buffer
->string
;
1605 /** Indent a line of YAML Tiny.
1607 * \private \memberof strbuf_s
1608 * \param output a string buffer
1609 * \param indent the number of spaces to indent
1612 static inline void indent_yaml( strbuf output
, int indent
)
1615 for ( j
= 0; j
< indent
; j
++ )
1616 strbuf_printf( output
, " " );
1619 /** Convert a line string into a YAML block literal.
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
1627 static void output_yaml_block_literal( strbuf output
, const char *value
, int indent
)
1629 char *v
= strdup( value
);
1631 char *eol
= strchr( sol
, '\n' );
1635 indent_yaml( output
, indent
);
1637 strbuf_printf( output
, "%s\n", sol
);
1639 eol
= strchr( sol
, '\n' );
1641 indent_yaml( output
, indent
);
1642 strbuf_printf( output
, "%s\n", sol
);
1645 /** Recursively serialize a properties list into a string buffer as YAML Tiny.
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)?
1654 static void serialise_yaml( mlt_properties
this, strbuf output
, int indent
, int is_parent_sequence
)
1656 property_list
*list
= this->local
;
1659 for ( i
= 0; i
< list
->count
; i
++ )
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
);
1665 if ( mlt_properties_is_sequence( this ) )
1667 // Ignore hidden/non-serialisable items
1668 if ( list
->name
[ i
][ 0 ] != '_' )
1670 // Indicate a sequence item
1671 indent_yaml( output
, indent
);
1672 strbuf_printf( output
, "- " );
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
, "" ) )
1678 // Determine if this is an unfolded block literal
1679 if ( strchr( value
, '\n' ) )
1681 strbuf_printf( output
, "|\n" );
1682 output_yaml_block_literal( output
, value
, indent
+ strlen( list
->name
[ i
] ) + strlen( "|" ) );
1686 strbuf_printf( output
, "%s\n", value
);
1692 serialise_yaml( child
, output
, indent
+ 2, 1 );
1696 // Assume this is a normal map-oriented properties list
1697 const char *value
= mlt_properties_get( this, list
->name
[ i
] );
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
, "" ) )
1703 if ( is_parent_sequence
== 0 )
1704 indent_yaml( output
, indent
);
1706 is_parent_sequence
= 0;
1708 // Determine if this is an unfolded block literal
1709 if ( strchr( value
, '\n' ) )
1711 strbuf_printf( output
, "%s: |\n", list
->name
[ i
] );
1712 output_yaml_block_literal( output
, value
, indent
+ strlen( list
->name
[ i
] ) + strlen( ": " ) );
1716 strbuf_printf( output
, "%s: %s\n", list
->name
[ i
], value
);
1720 // Output a child as a map item
1723 indent_yaml( output
, indent
);
1724 strbuf_printf( output
, "%s:\n", list
->name
[ i
] );
1727 serialise_yaml( child
, output
, indent
+ 2, 0 );
1733 /** Serialize a properties list as a string of YAML Tiny.
1735 * The caller MUST free the returned string!
1736 * This operates on properties containing properties as a hierarchical data
1738 * \public \memberof mlt_properties_s
1739 * \param this a properties list
1740 * \return a string containing YAML Tiny that represents the properties list
1743 char *mlt_properties_serialise_yaml( mlt_properties
this )
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
;