2 * mlt_properties.c -- base properties class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 * Contributor: Dan Dennedy <dan@dennedy.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "mlt_properties.h"
23 #include "mlt_property.h"
24 #include "mlt_deque.h"
32 #include <sys/types.h>
35 /* ---------------- // Private Implementation // ---------------- */
37 /** Private implementation of the property list.
47 mlt_properties mirror
;
49 pthread_mutex_t mutex
;
53 /** Memory leak checks.
56 //#define _MLT_PROPERTY_CHECKS_ 2
58 #ifdef _MLT_PROPERTY_CHECKS_
59 static int properties_created
= 0;
60 static int properties_destroyed
= 0;
63 /** Basic implementation.
66 int mlt_properties_init( mlt_properties
this, void *child
)
70 #ifdef _MLT_PROPERTY_CHECKS_
71 // Increment number of properties created
72 properties_created
++;
76 memset( this, 0, sizeof( struct mlt_properties_s
) );
78 // Assign the child of the object
81 // Allocate the local structure
82 this->local
= calloc( sizeof( property_list
), 1 );
84 // Increment the ref count
85 ( ( property_list
* )this->local
)->ref_count
= 1;
86 pthread_mutex_init( &( ( property_list
* )this->local
)->mutex
, NULL
);;
89 // Check that initialisation was successful
90 return this != NULL
&& this->local
== NULL
;
93 /** Constructor for stand alone object.
96 mlt_properties
mlt_properties_new( )
98 // Construct a standalone properties object
99 mlt_properties
this = calloc( sizeof( struct mlt_properties_s
), 1 );
102 mlt_properties_init( this, NULL
);
104 // Return the pointer
108 /** Load properties from a .properties file (DEPRECATED).
111 mlt_properties
mlt_properties_load( const char *filename
)
113 // Construct a standalone properties object
114 mlt_properties
this = mlt_properties_new( );
119 FILE *file
= fopen( filename
, "r" );
121 // Load contents of file
126 char last
[ 1024 ] = "";
128 // Read each string from the file
129 while( fgets( temp
, 1024, file
) )
132 temp
[ strlen( temp
) - 1 ] = '\0';
134 // Check if the line starts with a .
135 if ( temp
[ 0 ] == '.' )
138 sprintf( temp2
, "%s%s", last
, temp
);
139 strcpy( temp
, temp2
);
141 else if ( strchr( temp
, '=' ) )
143 strcpy( last
, temp
);
144 *( strchr( last
, '=' ) ) = '\0';
147 // Parse and set the property
148 if ( strcmp( temp
, "" ) && temp
[ 0 ] != '#' )
149 mlt_properties_parse( this, temp
);
157 // Return the pointer
161 static inline int generate_hash( const char *name
)
166 hash
= ( hash
+ ( i
++ * ( *name
++ & 31 ) ) ) % 199;
170 /** Special case - when a container (such as fezzik) is protecting another
171 producer, we need to ensure that properties are passed through to the
175 static inline void mlt_properties_do_mirror( mlt_properties
this, const char *name
)
177 property_list
*list
= this->local
;
178 if ( list
->mirror
!= NULL
)
180 char *value
= mlt_properties_get( this, name
);
182 mlt_properties_set( list
->mirror
, name
, value
);
186 /** Maintain ref count to allow multiple uses of an mlt object.
189 int mlt_properties_inc_ref( mlt_properties
this )
194 property_list
*list
= this->local
;
195 pthread_mutex_lock( &list
->mutex
);
196 result
= ++ list
->ref_count
;
197 pthread_mutex_unlock( &list
->mutex
);
202 /** Maintain ref count to allow multiple uses of an mlt object.
205 int mlt_properties_dec_ref( mlt_properties
this )
210 property_list
*list
= this->local
;
211 pthread_mutex_lock( &list
->mutex
);
212 result
= -- list
->ref_count
;
213 pthread_mutex_unlock( &list
->mutex
);
218 /** Return the ref count of this object.
221 int mlt_properties_ref_count( mlt_properties
this )
225 property_list
*list
= this->local
;
226 return list
->ref_count
;
231 /** Mirror properties set on 'this' to 'that'.
234 void mlt_properties_mirror( mlt_properties
this, mlt_properties that
)
236 property_list
*list
= this->local
;
240 /** Inherit all serialisable properties from that into this.
243 int mlt_properties_inherit( mlt_properties
this, mlt_properties that
)
245 int count
= mlt_properties_count( that
);
247 for ( i
= 0; i
< count
; i
++ )
249 char *value
= mlt_properties_get_value( that
, i
);
252 char *name
= mlt_properties_get_name( that
, i
);
253 mlt_properties_set( this, name
, value
);
259 /** Pass all properties from 'that' that match the prefix to 'this' (excluding the prefix).
262 int mlt_properties_pass( mlt_properties
this, mlt_properties that
, const char *prefix
)
264 int count
= mlt_properties_count( that
);
265 int length
= strlen( prefix
);
267 for ( i
= 0; i
< count
; i
++ )
269 char *name
= mlt_properties_get_name( that
, i
);
270 if ( !strncmp( name
, prefix
, length
) )
272 char *value
= mlt_properties_get_value( that
, i
);
274 mlt_properties_set( this, name
+ length
, value
);
280 /** Locate a property by name
283 static inline mlt_property
mlt_properties_find( mlt_properties
this, const char *name
)
285 property_list
*list
= this->local
;
286 mlt_property value
= NULL
;
287 int key
= generate_hash( name
);
288 int i
= list
->hash
[ key
] - 1;
292 // Check if we're hashed
293 if ( list
->count
> 0 &&
294 name
[ 0 ] == list
->name
[ i
][ 0 ] &&
295 !strcmp( list
->name
[ i
], name
) )
296 value
= list
->value
[ i
];
299 for ( i
= list
->count
- 1; value
== NULL
&& i
>= 0; i
-- )
300 if ( name
[ 0 ] == list
->name
[ i
][ 0 ] && !strcmp( list
->name
[ i
], name
) )
301 value
= list
->value
[ i
];
307 /** Add a new property.
310 static mlt_property
mlt_properties_add( mlt_properties
this, const char *name
)
312 property_list
*list
= this->local
;
313 int key
= generate_hash( name
);
315 // Check that we have space and resize if necessary
316 if ( list
->count
== list
->size
)
319 list
->name
= realloc( list
->name
, list
->size
* sizeof( const char * ) );
320 list
->value
= realloc( list
->value
, list
->size
* sizeof( mlt_property
) );
323 // Assign name/value pair
324 list
->name
[ list
->count
] = strdup( name
);
325 list
->value
[ list
->count
] = mlt_property_init( );
327 // Assign to hash table
328 if ( list
->hash
[ key
] == 0 )
329 list
->hash
[ key
] = list
->count
+ 1;
331 // Return and increment count accordingly
332 return list
->value
[ list
->count
++ ];
335 /** Fetch a property by name - this includes add if not found.
338 static mlt_property
mlt_properties_fetch( mlt_properties
this, const char *name
)
340 // Try to find an existing property first
341 mlt_property property
= mlt_properties_find( this, name
);
343 // If it wasn't found, create one
344 if ( property
== NULL
)
345 property
= mlt_properties_add( this, name
);
347 // Return the property
351 /** Pass property 'name' from 'that' to 'this'
352 * Who to blame: Zach <zachary.drew@gmail.com>
355 void mlt_properties_pass_property( mlt_properties
this, mlt_properties that
, const char *name
)
357 // Make sure the source property isn't null.
358 mlt_property that_prop
= mlt_properties_find( that
, name
);
359 if( that_prop
== NULL
)
362 mlt_property_pass( mlt_properties_fetch( this, name
), that_prop
);
365 /** Pass all properties from 'that' to 'this' as found in comma seperated 'list'.
366 * Who to blame: Zach <zachary.drew@gmail.com>
369 int mlt_properties_pass_list( mlt_properties
this, mlt_properties that
, const char *list
)
371 char *props
= strdup( list
);
373 char *delim
= " ,\t\n"; // Any combination of spaces, commas, tabs, and newlines
378 count
= strcspn( ptr
, delim
);
380 if( ptr
[count
] == '\0' )
383 ptr
[count
] = '\0'; // Make it a real string
385 mlt_properties_pass_property( this, that
, ptr
);
388 ptr
+= strspn( ptr
, delim
);
397 /** Set the property.
400 int mlt_properties_set( mlt_properties
this, const char *name
, const char *value
)
404 // Fetch the property to work with
405 mlt_property property
= mlt_properties_fetch( this, name
);
407 // Set it if not NULL
408 if ( property
== NULL
)
410 fprintf( stderr
, "Whoops - %s not found (should never occur)\n", name
);
412 else if ( value
== NULL
)
414 error
= mlt_property_set_string( property
, value
);
415 mlt_properties_do_mirror( this, name
);
417 else if ( *value
!= '@' )
419 error
= mlt_property_set_string( property
, value
);
420 mlt_properties_do_mirror( this, name
);
422 else if ( value
[ 0 ] == '@' )
431 while ( *value
!= '\0' )
433 int length
= strcspn( value
, "+-*/" );
435 // Get the identifier
436 strncpy( id
, value
, length
);
440 // Determine the value
441 if ( isdigit( id
[ 0 ] ) )
442 current
= atof( id
);
444 current
= mlt_properties_get_double( this, id
);
446 // Apply the operation
459 total
= total
/ current
;
464 op
= *value
!= '\0' ?
*value
++ : ' ';
467 error
= mlt_property_set_double( property
, total
);
468 mlt_properties_do_mirror( this, name
);
471 mlt_events_fire( this, "property-changed", name
, NULL
);
476 /** Set or default the property.
479 int mlt_properties_set_or_default( mlt_properties
this, const char *name
, const char *value
, const char *def
)
481 return mlt_properties_set( this, name
, value
== NULL ? def
: value
);
484 /** Get a string value by name.
487 char *mlt_properties_get( mlt_properties
this, const char *name
)
489 mlt_property value
= mlt_properties_find( this, name
);
490 return value
== NULL ? NULL
: mlt_property_get_string( value
);
493 /** Get a name by index.
496 char *mlt_properties_get_name( mlt_properties
this, int index
)
498 property_list
*list
= this->local
;
499 if ( index
>= 0 && index
< list
->count
)
500 return list
->name
[ index
];
504 /** Get a string value by index.
507 char *mlt_properties_get_value( mlt_properties
this, int index
)
509 property_list
*list
= this->local
;
510 if ( index
>= 0 && index
< list
->count
)
511 return mlt_property_get_string( list
->value
[ index
] );
515 /** Get a data value by index.
518 void *mlt_properties_get_data_at( mlt_properties
this, int index
, int *size
)
520 property_list
*list
= this->local
;
521 if ( index
>= 0 && index
< list
->count
)
522 return mlt_property_get_data( list
->value
[ index
], size
);
526 /** Return the number of items in the list.
529 int mlt_properties_count( mlt_properties
this )
531 property_list
*list
= this->local
;
535 /** Set a value by parsing a name=value string
538 int mlt_properties_parse( mlt_properties
this, const char *namevalue
)
540 char *name
= strdup( namevalue
);
543 char *ptr
= strchr( name
, '=' );
551 value
= strdup( ptr
);
556 value
= strdup( ptr
);
557 if ( value
!= NULL
&& value
[ strlen( value
) - 1 ] == '\"' )
558 value
[ strlen( value
) - 1 ] = '\0';
563 value
= strdup( "" );
566 error
= mlt_properties_set( this, name
, value
);
574 /** Get a value associated to the name.
577 int mlt_properties_get_int( mlt_properties
this, const char *name
)
579 mlt_property value
= mlt_properties_find( this, name
);
580 return value
== NULL ?
0 : mlt_property_get_int( value
);
583 /** Set a value associated to the name.
586 int mlt_properties_set_int( mlt_properties
this, const char *name
, int value
)
590 // Fetch the property to work with
591 mlt_property property
= mlt_properties_fetch( this, name
);
593 // Set it if not NULL
594 if ( property
!= NULL
)
596 error
= mlt_property_set_int( property
, value
);
597 mlt_properties_do_mirror( this, name
);
600 mlt_events_fire( this, "property-changed", name
, NULL
);
605 /** Get a value associated to the name.
608 int64_t mlt_properties_get_int64( mlt_properties
this, const char *name
)
610 mlt_property value
= mlt_properties_find( this, name
);
611 return value
== NULL ?
0 : mlt_property_get_int64( value
);
614 /** Set a value associated to the name.
617 int mlt_properties_set_int64( mlt_properties
this, const char *name
, int64_t value
)
621 // Fetch the property to work with
622 mlt_property property
= mlt_properties_fetch( this, name
);
624 // Set it if not NULL
625 if ( property
!= NULL
)
627 error
= mlt_property_set_int64( property
, value
);
628 mlt_properties_do_mirror( this, name
);
631 mlt_events_fire( this, "property-changed", name
, NULL
);
636 /** Get a value associated to the name.
639 double mlt_properties_get_double( mlt_properties
this, const char *name
)
641 mlt_property value
= mlt_properties_find( this, name
);
642 return value
== NULL ?
0 : mlt_property_get_double( value
);
645 /** Set a value associated to the name.
648 int mlt_properties_set_double( mlt_properties
this, const char *name
, double value
)
652 // Fetch the property to work with
653 mlt_property property
= mlt_properties_fetch( this, name
);
655 // Set it if not NULL
656 if ( property
!= NULL
)
658 error
= mlt_property_set_double( property
, value
);
659 mlt_properties_do_mirror( this, name
);
662 mlt_events_fire( this, "property-changed", name
, NULL
);
667 /** Get a value associated to the name.
670 mlt_position
mlt_properties_get_position( mlt_properties
this, const char *name
)
672 mlt_property value
= mlt_properties_find( this, name
);
673 return value
== NULL ?
0 : mlt_property_get_position( value
);
676 /** Set a value associated to the name.
679 int mlt_properties_set_position( mlt_properties
this, const char *name
, mlt_position value
)
683 // Fetch the property to work with
684 mlt_property property
= mlt_properties_fetch( this, name
);
686 // Set it if not NULL
687 if ( property
!= NULL
)
689 error
= mlt_property_set_position( property
, value
);
690 mlt_properties_do_mirror( this, name
);
693 mlt_events_fire( this, "property-changed", name
, NULL
);
698 /** Get a value associated to the name.
701 void *mlt_properties_get_data( mlt_properties
this, const char *name
, int *length
)
703 mlt_property value
= mlt_properties_find( this, name
);
704 return value
== NULL ? NULL
: mlt_property_get_data( value
, length
);
707 /** Set a value associated to the name.
710 int mlt_properties_set_data( mlt_properties
this, const char *name
, void *value
, int length
, mlt_destructor destroy
, mlt_serialiser serialise
)
714 // Fetch the property to work with
715 mlt_property property
= mlt_properties_fetch( this, name
);
717 // Set it if not NULL
718 if ( property
!= NULL
)
719 error
= mlt_property_set_data( property
, value
, length
, destroy
, serialise
);
721 mlt_events_fire( this, "property-changed", name
, NULL
);
726 /** Rename a property.
729 int mlt_properties_rename( mlt_properties
this, const char *source
, const char *dest
)
731 mlt_property value
= mlt_properties_find( this, dest
);
735 property_list
*list
= this->local
;
739 for ( i
= 0; i
< list
->count
; i
++ )
741 if ( !strcmp( list
->name
[ i
], source
) )
743 free( list
->name
[ i
] );
744 list
->name
[ i
] = strdup( dest
);
745 list
->hash
[ generate_hash( dest
) ] = i
+ 1;
751 return value
!= NULL
;
754 /** Dump the properties.
757 void mlt_properties_dump( mlt_properties
this, FILE *output
)
759 property_list
*list
= this->local
;
761 for ( i
= 0; i
< list
->count
; i
++ )
762 if ( mlt_properties_get( this, list
->name
[ i
] ) != NULL
)
763 fprintf( output
, "%s=%s\n", list
->name
[ i
], mlt_properties_get( this, list
->name
[ i
] ) );
766 void mlt_properties_debug( mlt_properties
this, const char *title
, FILE *output
)
768 if ( output
== NULL
) output
= stderr
;
769 fprintf( output
, "%s: ", title
);
772 property_list
*list
= this->local
;
774 fprintf( output
, "[ ref=%d", list
->ref_count
);
775 for ( i
= 0; i
< list
->count
; i
++ )
776 if ( mlt_properties_get( this, list
->name
[ i
] ) != NULL
)
777 fprintf( output
, ", %s=%s", list
->name
[ i
], mlt_properties_get( this, list
->name
[ i
] ) );
779 fprintf( output
, ", %s=%p", list
->name
[ i
], mlt_properties_get_data( this, list
->name
[ i
], NULL
) );
780 fprintf( output
, " ]" );
782 fprintf( output
, "\n" );
785 int mlt_properties_save( mlt_properties
this, const char *filename
)
788 FILE *f
= fopen( filename
, "w" );
791 mlt_properties_dump( this, f
);
798 /* This is a very basic cross platform fnmatch replacement - it will fail in
799 ** many cases, but for the basic *.XXX and YYY*.XXX, it will work ok.
802 static int mlt_fnmatch( const char *wild
, const char *file
)
807 while( f
< strlen( file
) && w
< strlen( wild
) )
809 if ( wild
[ w
] == '*' )
812 if ( w
== strlen( wild
) )
814 while ( f
!= strlen( file
) && tolower( file
[ f
] ) != tolower( wild
[ w
] ) )
817 else if ( wild
[ w
] == '?' || tolower( file
[ f
] ) == tolower( wild
[ w
] ) )
822 else if ( wild
[ 0 ] == '*' )
832 return strlen( file
) == f
&& strlen( wild
) == w
;
835 static int mlt_compare( const void *this, const void *that
)
837 return strcmp( mlt_property_get_string( *( mlt_property
* )this ), mlt_property_get_string( *( mlt_property
* )that
) );
840 /* Obtains an optionally sorted list of the files found in a directory with a specific wild card.
841 * Entries in the list have a numeric name (running from 0 to count - 1). Only values change
842 * position if sort is enabled. Designed to be posix compatible (linux, os/x, mingw etc).
845 int mlt_properties_dir_list( mlt_properties
this, const char *dirname
, const char *pattern
, int sort
)
847 DIR *dir
= opendir( dirname
);
852 struct dirent
*de
= readdir( dir
);
853 char fullname
[ 1024 ];
856 sprintf( key
, "%d", mlt_properties_count( this ) );
857 snprintf( fullname
, 1024, "%s/%s", dirname
, de
->d_name
);
858 if ( pattern
== NULL
)
859 mlt_properties_set( this, key
, fullname
);
860 else if ( de
->d_name
[ 0 ] != '.' && mlt_fnmatch( pattern
, de
->d_name
) )
861 mlt_properties_set( this, key
, fullname
);
868 if ( sort
&& mlt_properties_count( this ) )
870 property_list
*list
= this->local
;
871 qsort( list
->value
, mlt_properties_count( this ), sizeof( mlt_property
), mlt_compare
);
874 return mlt_properties_count( this );
880 void mlt_properties_close( mlt_properties
this )
882 if ( this != NULL
&& mlt_properties_dec_ref( this ) <= 0 )
884 if ( this->close
!= NULL
)
886 this->close( this->close_object
);
890 property_list
*list
= this->local
;
893 #if _MLT_PROPERTY_CHECKS_ == 1
895 mlt_properties_debug( this, "Closing", stderr
);
898 #ifdef _MLT_PROPERTY_CHECKS_
899 // Increment destroyed count
900 properties_destroyed
++;
902 // Show current stats - these should match when the app is closed
903 fprintf( stderr
, "Created %d, destroyed %d\n", properties_created
, properties_destroyed
);
906 // Clean up names and values
907 for ( index
= list
->count
- 1; index
>= 0; index
-- )
909 free( list
->name
[ index
] );
910 mlt_property_close( list
->value
[ index
] );
914 pthread_mutex_destroy( &list
->mutex
);
919 // Free this now if this has no child
920 if ( this->child
== NULL
)
926 /** Determine if the properties list is really just a sequence or ordered list.
927 Returns 0 if false or 1 if true.
929 int mlt_properties_is_sequence( mlt_properties properties
)
932 int n
= mlt_properties_count( properties
);
933 for ( i
= 0; i
< n
; i
++ )
934 if ( ! isdigit( mlt_properties_get_name( properties
, i
)[0] ) )
940 * YAML is a nifty text format popular in the Ruby world as a cleaner,
941 * less verbose alternative to XML. See this Wikipedia topic for an overview:
942 * http://en.wikipedia.org/wiki/YAML
943 * The YAML specification is at:
945 * YAML::Tiny is a Perl module that specifies a subset of YAML that we are
946 * using here (for the same reasons):
947 * http://search.cpan.org/~adamk/YAML-Tiny-1.25/lib/YAML/Tiny.pm
950 struct yaml_parser_context
957 unsigned int block_indent
;
960 typedef struct yaml_parser_context
*yaml_parser
;
962 static unsigned int ltrim( char **s
)
967 for ( i
= 0; i
< n
&& *c
== ' '; i
++, c
++ );
972 static unsigned int rtrim( char *s
)
976 for ( i
= n
; i
> 0 && s
[i
- 1] == ' '; --i
)
981 static int parse_yaml( yaml_parser context
, const char *namevalue
)
983 char *name_
= strdup( namevalue
);
987 char *ptr
= strchr( name
, ':' );
988 unsigned int indent
= ltrim( &name
);
989 mlt_properties properties
= mlt_deque_peek_front( context
->stack
);
991 // Ascending one more levels in the tree
992 if ( indent
< context
->level
)
995 unsigned int n
= ( context
->level
- indent
) / 2;
996 for ( i
= 0; i
< n
; i
++ )
997 mlt_deque_pop_front( context
->stack
);
998 properties
= mlt_deque_peek_front( context
->stack
);
999 context
->level
= indent
;
1002 // Descending a level in the tree
1003 else if ( indent
> context
->level
&& context
->block
== 0 )
1005 context
->level
= indent
;
1008 // If there is a colon that is not part of a block
1009 if ( ptr
&& ( indent
== context
->level
) )
1011 // Reset block processing
1012 if ( context
->block_name
)
1014 free( context
->block_name
);
1015 context
->block_name
= NULL
;
1019 // Terminate the name and setup the value pointer
1023 char *comment
= strchr( ptr
, '#' );
1029 // Trim leading and trailing spaces from bare value
1033 // No value means a child
1034 if ( strcmp( ptr
, "" ) == 0 )
1036 mlt_properties child
= mlt_properties_new();
1037 mlt_properties_set_data( properties
, name
, child
, 0,
1038 ( mlt_destructor
)mlt_properties_close
, NULL
);
1039 mlt_deque_push_front( context
->stack
, child
);
1045 // A dash indicates a sequence item
1046 if ( name
[0] == '-' )
1048 mlt_properties child
= mlt_properties_new();
1051 snprintf( key
, sizeof(key
), "%d", context
->index
++ );
1052 mlt_properties_set_data( properties
, key
, child
, 0,
1053 ( mlt_destructor
)mlt_properties_close
, NULL
);
1054 mlt_deque_push_front( context
->stack
, child
);
1057 context
->level
+= ltrim( &name
) + 1;
1065 value
= strdup( ptr
);
1066 if ( value
&& value
[ strlen( value
) - 1 ] == '\"' )
1067 value
[ strlen( value
) - 1 ] = 0;
1070 // Value is folded or unfolded block
1071 else if ( *ptr
== '|' || *ptr
== '>' )
1073 context
->block
= *ptr
;
1074 context
->block_name
= strdup( name
);
1075 context
->block_indent
= 0;
1076 value
= strdup( "" );
1082 value
= strdup( ptr
);
1086 // A list of scalars
1087 else if ( name
[0] == '-' )
1089 // Reset block processing
1090 if ( context
->block_name
)
1092 free( context
->block_name
);
1093 context
->block_name
= NULL
;
1099 snprintf( key
, sizeof(key
), "%d", context
->index
++ );
1103 char *comment
= strchr( ptr
, '#' );
1107 // Trim leading and trailing spaces from bare value
1115 value
= strdup( ptr
);
1116 if ( value
&& value
[ strlen( value
) - 1 ] == '\"' )
1117 value
[ strlen( value
) - 1 ] = 0;
1120 // Value is folded or unfolded block
1121 else if ( *ptr
== '|' || *ptr
== '>' )
1123 context
->block
= *ptr
;
1124 context
->block_name
= strdup( key
);
1125 context
->block_indent
= 0;
1126 value
= strdup( "" );
1132 value
= strdup( ptr
);
1136 name
= name_
= strdup( key
);
1140 else if ( context
->block
== '|' )
1142 if ( context
->block_indent
== 0 )
1143 context
->block_indent
= indent
;
1144 if ( indent
> context
->block_indent
)
1145 name
= &name_
[ context
->block_indent
];
1147 char *old_value
= mlt_properties_get( properties
, context
->block_name
);
1148 value
= calloc( 1, strlen( old_value
) + strlen( name
) + 2 );
1149 strcpy( value
, old_value
);
1150 if ( strcmp( old_value
, "" ) )
1151 strcat( value
, "\n" );
1152 strcat( value
, name
);
1153 name
= context
->block_name
;
1157 else if ( context
->block
== '>' )
1161 char *old_value
= mlt_properties_get( properties
, context
->block_name
);
1163 // Blank line (prepended with spaces) is new line
1164 if ( strcmp( name
, "" ) == 0 )
1166 value
= calloc( 1, strlen( old_value
) + 2 );
1167 strcat( value
, old_value
);
1168 strcat( value
, "\n" );
1170 // Concatenate with space
1173 value
= calloc( 1, strlen( old_value
) + strlen( name
) + 2 );
1174 strcat( value
, old_value
);
1175 if ( strcmp( old_value
, "" ) && old_value
[ strlen( old_value
) - 1 ] != '\n' )
1176 strcat( value
, " " );
1177 strcat( value
, name
);
1179 name
= context
->block_name
;
1184 value
= strdup( "" );
1187 error
= mlt_properties_set( properties
, name
, value
);
1195 mlt_properties
mlt_properties_parse_yaml( const char *filename
)
1197 // Construct a standalone properties object
1198 mlt_properties
this = mlt_properties_new( );
1203 FILE *file
= fopen( filename
, "r" );
1205 // Load contents of file
1210 char *ptemp
= &temp
[ 0 ];
1213 yaml_parser context
= calloc( 1, sizeof( struct yaml_parser_context
) );
1214 context
->stack
= mlt_deque_init();
1215 mlt_deque_push_front( context
->stack
, this );
1217 // Read each string from the file
1218 while( fgets( temp
, 1024, file
) )
1220 // Check for end-of-stream
1221 if ( strncmp( ptemp
, "...", 3 ) == 0 )
1225 temp
[ strlen( temp
) - 1 ] = '\0';
1227 // Skip blank lines, comment lines, and document separator
1228 if ( strcmp( ptemp
, "" ) && ptemp
[ 0 ] != '#' && strncmp( ptemp
, "---", 3 )
1229 && strncmp( ptemp
, "%YAML", 5 ) && strncmp( ptemp
, "% YAML", 6 ) )
1230 parse_yaml( context
, temp
);
1235 mlt_deque_close( context
->stack
);
1236 if ( context
->block_name
)
1237 free( context
->block_name
);
1242 // Return the pointer
1246 /** YAML Tiny Serialiser
1249 /* strbuf is a self-growing string buffer */
1251 #define STRBUF_GROWTH (1024)
1259 typedef struct strbuf_s
*strbuf
;
1261 static strbuf
strbuf_new( )
1263 strbuf buffer
= calloc( 1, sizeof( struct strbuf_s
) );
1264 buffer
->size
= STRBUF_GROWTH
;
1265 buffer
->string
= calloc( 1, buffer
->size
);
1269 static void strbuf_close( strbuf buffer
)
1271 // We do not free buffer->string; strbuf user must save that pointer
1277 static char *strbuf_printf( strbuf buffer
, const char *format
, ... )
1279 while ( buffer
->string
)
1282 va_start( ap
, format
);
1283 size_t len
= strlen( buffer
->string
);
1284 size_t remain
= buffer
->size
- len
- 1;
1285 int need
= vsnprintf( buffer
->string
+ len
, remain
, format
, ap
);
1287 if ( need
> -1 && need
< remain
)
1289 buffer
->string
[ len
] = 0;
1290 buffer
->size
+= need
+ STRBUF_GROWTH
;
1291 buffer
->string
= realloc( buffer
->string
, buffer
->size
);
1293 return buffer
->string
;
1296 static inline void indent_yaml( strbuf output
, int indent
)
1299 for ( j
= 0; j
< indent
; j
++ )
1300 strbuf_printf( output
, " " );
1303 static void output_yaml_block_literal( strbuf output
, const char *value
, int indent
)
1305 char *v
= strdup( value
);
1307 char *eol
= strchr( sol
, '\n' );
1311 indent_yaml( output
, indent
);
1313 strbuf_printf( output
, "%s\n", sol
);
1315 eol
= strchr( sol
, '\n' );
1317 indent_yaml( output
, indent
);
1318 strbuf_printf( output
, "%s\n", sol
);
1321 static void serialise_yaml( mlt_properties
this, strbuf output
, int indent
, int is_parent_sequence
)
1323 property_list
*list
= this->local
;
1326 for ( i
= 0; i
< list
->count
; i
++ )
1328 // This implementation assumes that all data elements are property lists.
1329 // Unfortunately, we do not have run time type identification.
1330 mlt_properties child
= mlt_property_get_data( list
->value
[ i
], NULL
);
1332 if ( mlt_properties_is_sequence( this ) )
1334 // Ignore hidden/non-serialisable items
1335 if ( list
->name
[ i
][ 0 ] != '_' )
1337 // Indicate a sequence item
1338 indent_yaml( output
, indent
);
1339 strbuf_printf( output
, "- " );
1341 // If the value can be represented as a string
1342 const char *value
= mlt_properties_get( this, list
->name
[ i
] );
1343 if ( value
&& strcmp( value
, "" ) )
1345 // Determine if this is an unfolded block literal
1346 if ( strchr( value
, '\n' ) )
1348 strbuf_printf( output
, "|\n" );
1349 output_yaml_block_literal( output
, value
, indent
+ strlen( list
->name
[ i
] ) + strlen( "|" ) );
1353 strbuf_printf( output
, "%s\n", value
);
1359 serialise_yaml( child
, output
, indent
+ 2, 1 );
1363 // Assume this is a normal map-oriented properties list
1364 const char *value
= mlt_properties_get( this, list
->name
[ i
] );
1366 // Ignore hidden/non-serialisable items
1367 // If the value can be represented as a string
1368 if ( list
->name
[ i
][ 0 ] != '_' && value
&& strcmp( value
, "" ) )
1370 if ( is_parent_sequence
== 0 )
1371 indent_yaml( output
, indent
);
1373 is_parent_sequence
= 0;
1375 // Determine if this is an unfolded block literal
1376 if ( strchr( value
, '\n' ) )
1378 strbuf_printf( output
, "%s: |\n", list
->name
[ i
] );
1379 output_yaml_block_literal( output
, value
, indent
+ strlen( list
->name
[ i
] ) + strlen( ": " ) );
1383 strbuf_printf( output
, "%s: %s\n", list
->name
[ i
], value
);
1387 // Output a child as a map item
1390 indent_yaml( output
, indent
);
1391 strbuf_printf( output
, "%s:\n", list
->name
[ i
] );
1394 serialise_yaml( child
, output
, indent
+ 2, 0 );
1400 /** Serialise the properties as a string of YAML Tiny.
1401 The caller must free the returned string.
1404 char *mlt_properties_serialise_yaml( mlt_properties
this )
1406 strbuf b
= strbuf_new();
1407 strbuf_printf( b
, "---\n" );
1408 serialise_yaml( this, b
, 0, 0 );
1409 strbuf_printf( b
, "...\n" );
1410 char *ret
= b
->string
;