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
;
52 /** Memory leak checks.
55 //#define _MLT_PROPERTY_CHECKS_ 2
57 #ifdef _MLT_PROPERTY_CHECKS_
58 static int properties_created
= 0;
59 static int properties_destroyed
= 0;
62 /** Basic implementation.
65 int mlt_properties_init( mlt_properties
this, void *child
)
69 #ifdef _MLT_PROPERTY_CHECKS_
70 // Increment number of properties created
71 properties_created
++;
75 memset( this, 0, sizeof( struct mlt_properties_s
) );
77 // Assign the child of the object
80 // Allocate the local structure
81 this->local
= calloc( sizeof( property_list
), 1 );
83 // Increment the ref count
84 ( ( property_list
* )this->local
)->ref_count
= 1;
87 // Check that initialisation was successful
88 return this != NULL
&& this->local
== NULL
;
91 /** Constructor for stand alone object.
94 mlt_properties
mlt_properties_new( )
96 // Construct a standalone properties object
97 mlt_properties
this = calloc( sizeof( struct mlt_properties_s
), 1 );
100 mlt_properties_init( this, NULL
);
102 // Return the pointer
106 /** Load properties from a .properties file (DEPRECATED).
109 mlt_properties
mlt_properties_load( const char *filename
)
111 // Construct a standalone properties object
112 mlt_properties
this = mlt_properties_new( );
117 FILE *file
= fopen( filename
, "r" );
119 // Load contents of file
124 char last
[ 1024 ] = "";
126 // Read each string from the file
127 while( fgets( temp
, 1024, file
) )
130 temp
[ strlen( temp
) - 1 ] = '\0';
132 // Check if the line starts with a .
133 if ( temp
[ 0 ] == '.' )
136 sprintf( temp2
, "%s%s", last
, temp
);
137 strcpy( temp
, temp2
);
139 else if ( strchr( temp
, '=' ) )
141 strcpy( last
, temp
);
142 *( strchr( last
, '=' ) ) = '\0';
145 // Parse and set the property
146 if ( strcmp( temp
, "" ) && temp
[ 0 ] != '#' )
147 mlt_properties_parse( this, temp
);
155 // Return the pointer
159 static inline int generate_hash( const char *name
)
164 hash
= ( hash
+ ( i
++ * ( *name
++ & 31 ) ) ) % 199;
168 /** Special case - when a container (such as fezzik) is protecting another
169 producer, we need to ensure that properties are passed through to the
173 static inline void mlt_properties_do_mirror( mlt_properties
this, const char *name
)
175 property_list
*list
= this->local
;
176 if ( list
->mirror
!= NULL
)
178 char *value
= mlt_properties_get( this, name
);
180 mlt_properties_set( list
->mirror
, name
, value
);
184 /** Maintain ref count to allow multiple uses of an mlt object.
187 int mlt_properties_inc_ref( mlt_properties
this )
191 property_list
*list
= this->local
;
192 return ++ list
->ref_count
;
197 /** Maintain ref count to allow multiple uses of an mlt object.
200 int mlt_properties_dec_ref( mlt_properties
this )
204 property_list
*list
= this->local
;
205 return -- list
->ref_count
;
210 /** Return the ref count of this object.
213 int mlt_properties_ref_count( mlt_properties
this )
217 property_list
*list
= this->local
;
218 return list
->ref_count
;
223 /** Mirror properties set on 'this' to 'that'.
226 void mlt_properties_mirror( mlt_properties
this, mlt_properties that
)
228 property_list
*list
= this->local
;
232 /** Inherit all serialisable properties from that into this.
235 int mlt_properties_inherit( mlt_properties
this, mlt_properties that
)
237 int count
= mlt_properties_count( that
);
239 for ( i
= 0; i
< count
; i
++ )
241 char *value
= mlt_properties_get_value( that
, i
);
244 char *name
= mlt_properties_get_name( that
, i
);
245 mlt_properties_set( this, name
, value
);
251 /** Pass all properties from 'that' that match the prefix to 'this' (excluding the prefix).
254 int mlt_properties_pass( mlt_properties
this, mlt_properties that
, const char *prefix
)
256 int count
= mlt_properties_count( that
);
257 int length
= strlen( prefix
);
259 for ( i
= 0; i
< count
; i
++ )
261 char *name
= mlt_properties_get_name( that
, i
);
262 if ( !strncmp( name
, prefix
, length
) )
264 char *value
= mlt_properties_get_value( that
, i
);
266 mlt_properties_set( this, name
+ length
, value
);
272 /** Locate a property by name
275 static inline mlt_property
mlt_properties_find( mlt_properties
this, const char *name
)
277 property_list
*list
= this->local
;
278 mlt_property value
= NULL
;
279 int key
= generate_hash( name
);
280 int i
= list
->hash
[ key
] - 1;
284 // Check if we're hashed
285 if ( list
->count
> 0 &&
286 name
[ 0 ] == list
->name
[ i
][ 0 ] &&
287 !strcmp( list
->name
[ i
], name
) )
288 value
= list
->value
[ i
];
291 for ( i
= list
->count
- 1; value
== NULL
&& i
>= 0; i
-- )
292 if ( name
[ 0 ] == list
->name
[ i
][ 0 ] && !strcmp( list
->name
[ i
], name
) )
293 value
= list
->value
[ i
];
299 /** Add a new property.
302 static mlt_property
mlt_properties_add( mlt_properties
this, const char *name
)
304 property_list
*list
= this->local
;
305 int key
= generate_hash( name
);
307 // Check that we have space and resize if necessary
308 if ( list
->count
== list
->size
)
311 list
->name
= realloc( list
->name
, list
->size
* sizeof( const char * ) );
312 list
->value
= realloc( list
->value
, list
->size
* sizeof( mlt_property
) );
315 // Assign name/value pair
316 list
->name
[ list
->count
] = strdup( name
);
317 list
->value
[ list
->count
] = mlt_property_init( );
319 // Assign to hash table
320 if ( list
->hash
[ key
] == 0 )
321 list
->hash
[ key
] = list
->count
+ 1;
323 // Return and increment count accordingly
324 return list
->value
[ list
->count
++ ];
327 /** Fetch a property by name - this includes add if not found.
330 static mlt_property
mlt_properties_fetch( mlt_properties
this, const char *name
)
332 // Try to find an existing property first
333 mlt_property property
= mlt_properties_find( this, name
);
335 // If it wasn't found, create one
336 if ( property
== NULL
)
337 property
= mlt_properties_add( this, name
);
339 // Return the property
343 /** Pass property 'name' from 'that' to 'this'
344 * Who to blame: Zach <zachary.drew@gmail.com>
347 void mlt_properties_pass_property( mlt_properties
this, mlt_properties that
, const char *name
)
349 // Make sure the source property isn't null.
350 mlt_property that_prop
= mlt_properties_find( that
, name
);
351 if( that_prop
== NULL
)
354 mlt_property_pass( mlt_properties_fetch( this, name
), that_prop
);
357 /** Pass all properties from 'that' to 'this' as found in comma seperated 'list'.
358 * Who to blame: Zach <zachary.drew@gmail.com>
361 int mlt_properties_pass_list( mlt_properties
this, mlt_properties that
, const char *list
)
363 char *props
= strdup( list
);
365 char *delim
= " ,\t\n"; // Any combination of spaces, commas, tabs, and newlines
370 count
= strcspn( ptr
, delim
);
372 if( ptr
[count
] == '\0' )
375 ptr
[count
] = '\0'; // Make it a real string
377 mlt_properties_pass_property( this, that
, ptr
);
380 ptr
+= strspn( ptr
, delim
);
389 /** Set the property.
392 int mlt_properties_set( mlt_properties
this, const char *name
, const char *value
)
396 // Fetch the property to work with
397 mlt_property property
= mlt_properties_fetch( this, name
);
399 // Set it if not NULL
400 if ( property
== NULL
)
402 fprintf( stderr
, "Whoops - %s not found (should never occur)\n", name
);
404 else if ( value
== NULL
)
406 error
= mlt_property_set_string( property
, value
);
407 mlt_properties_do_mirror( this, name
);
409 else if ( *value
!= '@' )
411 error
= mlt_property_set_string( property
, value
);
412 mlt_properties_do_mirror( this, name
);
414 else if ( value
[ 0 ] == '@' )
423 while ( *value
!= '\0' )
425 int length
= strcspn( value
, "+-*/" );
427 // Get the identifier
428 strncpy( id
, value
, length
);
432 // Determine the value
433 if ( isdigit( id
[ 0 ] ) )
434 current
= atof( id
);
436 current
= mlt_properties_get_int( this, id
);
438 // Apply the operation
456 op
= *value
!= '\0' ?
*value
++ : ' ';
459 error
= mlt_property_set_int( property
, total
);
460 mlt_properties_do_mirror( this, name
);
463 mlt_events_fire( this, "property-changed", name
, NULL
);
468 /** Set or default the property.
471 int mlt_properties_set_or_default( mlt_properties
this, const char *name
, const char *value
, const char *def
)
473 return mlt_properties_set( this, name
, value
== NULL ? def
: value
);
476 /** Get a string value by name.
479 char *mlt_properties_get( mlt_properties
this, const char *name
)
481 mlt_property value
= mlt_properties_find( this, name
);
482 return value
== NULL ? NULL
: mlt_property_get_string( value
);
485 /** Get a name by index.
488 char *mlt_properties_get_name( mlt_properties
this, int index
)
490 property_list
*list
= this->local
;
491 if ( index
>= 0 && index
< list
->count
)
492 return list
->name
[ index
];
496 /** Get a string value by index.
499 char *mlt_properties_get_value( mlt_properties
this, int index
)
501 property_list
*list
= this->local
;
502 if ( index
>= 0 && index
< list
->count
)
503 return mlt_property_get_string( list
->value
[ index
] );
507 /** Get a data value by index.
510 void *mlt_properties_get_data_at( mlt_properties
this, int index
, int *size
)
512 property_list
*list
= this->local
;
513 if ( index
>= 0 && index
< list
->count
)
514 return mlt_property_get_data( list
->value
[ index
], size
);
518 /** Return the number of items in the list.
521 int mlt_properties_count( mlt_properties
this )
523 property_list
*list
= this->local
;
527 /** Set a value by parsing a name=value string
530 int mlt_properties_parse( mlt_properties
this, const char *namevalue
)
532 char *name
= strdup( namevalue
);
535 char *ptr
= strchr( name
, '=' );
543 value
= strdup( ptr
);
548 value
= strdup( ptr
);
549 if ( value
!= NULL
&& value
[ strlen( value
) - 1 ] == '\"' )
550 value
[ strlen( value
) - 1 ] = '\0';
555 value
= strdup( "" );
558 error
= mlt_properties_set( this, name
, value
);
566 /** Get a value associated to the name.
569 int mlt_properties_get_int( mlt_properties
this, const char *name
)
571 mlt_property value
= mlt_properties_find( this, name
);
572 return value
== NULL ?
0 : mlt_property_get_int( value
);
575 /** Set a value associated to the name.
578 int mlt_properties_set_int( mlt_properties
this, const char *name
, int value
)
582 // Fetch the property to work with
583 mlt_property property
= mlt_properties_fetch( this, name
);
585 // Set it if not NULL
586 if ( property
!= NULL
)
588 error
= mlt_property_set_int( property
, value
);
589 mlt_properties_do_mirror( this, name
);
592 mlt_events_fire( this, "property-changed", name
, NULL
);
597 /** Get a value associated to the name.
600 int64_t mlt_properties_get_int64( mlt_properties
this, const char *name
)
602 mlt_property value
= mlt_properties_find( this, name
);
603 return value
== NULL ?
0 : mlt_property_get_int64( value
);
606 /** Set a value associated to the name.
609 int mlt_properties_set_int64( mlt_properties
this, const char *name
, int64_t value
)
613 // Fetch the property to work with
614 mlt_property property
= mlt_properties_fetch( this, name
);
616 // Set it if not NULL
617 if ( property
!= NULL
)
619 error
= mlt_property_set_int64( property
, value
);
620 mlt_properties_do_mirror( this, name
);
623 mlt_events_fire( this, "property-changed", name
, NULL
);
628 /** Get a value associated to the name.
631 double mlt_properties_get_double( mlt_properties
this, const char *name
)
633 mlt_property value
= mlt_properties_find( this, name
);
634 return value
== NULL ?
0 : mlt_property_get_double( value
);
637 /** Set a value associated to the name.
640 int mlt_properties_set_double( mlt_properties
this, const char *name
, double value
)
644 // Fetch the property to work with
645 mlt_property property
= mlt_properties_fetch( this, name
);
647 // Set it if not NULL
648 if ( property
!= NULL
)
650 error
= mlt_property_set_double( property
, value
);
651 mlt_properties_do_mirror( this, name
);
654 mlt_events_fire( this, "property-changed", name
, NULL
);
659 /** Get a value associated to the name.
662 mlt_position
mlt_properties_get_position( mlt_properties
this, const char *name
)
664 mlt_property value
= mlt_properties_find( this, name
);
665 return value
== NULL ?
0 : mlt_property_get_position( value
);
668 /** Set a value associated to the name.
671 int mlt_properties_set_position( mlt_properties
this, const char *name
, mlt_position value
)
675 // Fetch the property to work with
676 mlt_property property
= mlt_properties_fetch( this, name
);
678 // Set it if not NULL
679 if ( property
!= NULL
)
681 error
= mlt_property_set_position( property
, value
);
682 mlt_properties_do_mirror( this, name
);
685 mlt_events_fire( this, "property-changed", name
, NULL
);
690 /** Get a value associated to the name.
693 void *mlt_properties_get_data( mlt_properties
this, const char *name
, int *length
)
695 mlt_property value
= mlt_properties_find( this, name
);
696 return value
== NULL ? NULL
: mlt_property_get_data( value
, length
);
699 /** Set a value associated to the name.
702 int mlt_properties_set_data( mlt_properties
this, const char *name
, void *value
, int length
, mlt_destructor destroy
, mlt_serialiser serialise
)
706 // Fetch the property to work with
707 mlt_property property
= mlt_properties_fetch( this, name
);
709 // Set it if not NULL
710 if ( property
!= NULL
)
711 error
= mlt_property_set_data( property
, value
, length
, destroy
, serialise
);
713 mlt_events_fire( this, "property-changed", name
, NULL
);
718 /** Rename a property.
721 int mlt_properties_rename( mlt_properties
this, const char *source
, const char *dest
)
723 mlt_property value
= mlt_properties_find( this, dest
);
727 property_list
*list
= this->local
;
731 for ( i
= 0; i
< list
->count
; i
++ )
733 if ( !strcmp( list
->name
[ i
], source
) )
735 free( list
->name
[ i
] );
736 list
->name
[ i
] = strdup( dest
);
737 list
->hash
[ generate_hash( dest
) ] = i
+ 1;
743 return value
!= NULL
;
746 /** Dump the properties.
749 void mlt_properties_dump( mlt_properties
this, FILE *output
)
751 property_list
*list
= this->local
;
753 for ( i
= 0; i
< list
->count
; i
++ )
754 if ( mlt_properties_get( this, list
->name
[ i
] ) != NULL
)
755 fprintf( output
, "%s=%s\n", list
->name
[ i
], mlt_properties_get( this, list
->name
[ i
] ) );
758 void mlt_properties_debug( mlt_properties
this, const char *title
, FILE *output
)
760 if ( output
== NULL
) output
= stderr
;
761 fprintf( output
, "%s: ", title
);
764 property_list
*list
= this->local
;
766 fprintf( output
, "[ ref=%d", list
->ref_count
);
767 for ( i
= 0; i
< list
->count
; i
++ )
768 if ( mlt_properties_get( this, list
->name
[ i
] ) != NULL
)
769 fprintf( output
, ", %s=%s", list
->name
[ i
], mlt_properties_get( this, list
->name
[ i
] ) );
771 fprintf( output
, ", %s=%p", list
->name
[ i
], mlt_properties_get_data( this, list
->name
[ i
], NULL
) );
772 fprintf( output
, " ]" );
774 fprintf( output
, "\n" );
777 int mlt_properties_save( mlt_properties
this, const char *filename
)
780 FILE *f
= fopen( filename
, "w" );
783 mlt_properties_dump( this, f
);
790 /* This is a very basic cross platform fnmatch replacement - it will fail in
791 ** many cases, but for the basic *.XXX and YYY*.XXX, it will work ok.
794 static int mlt_fnmatch( const char *wild
, const char *file
)
799 while( f
< strlen( file
) && w
< strlen( wild
) )
801 if ( wild
[ w
] == '*' )
804 if ( w
== strlen( wild
) )
806 while ( f
!= strlen( file
) && tolower( file
[ f
] ) != tolower( wild
[ w
] ) )
809 else if ( wild
[ w
] == '?' || tolower( file
[ f
] ) == tolower( wild
[ w
] ) )
814 else if ( wild
[ 0 ] == '*' )
824 return strlen( file
) == f
&& strlen( wild
) == w
;
827 static int mlt_compare( const void *this, const void *that
)
829 return strcmp( mlt_property_get_string( *( mlt_property
* )this ), mlt_property_get_string( *( mlt_property
* )that
) );
832 /* Obtains an optionally sorted list of the files found in a directory with a specific wild card.
833 * Entries in the list have a numeric name (running from 0 to count - 1). Only values change
834 * position if sort is enabled. Designed to be posix compatible (linux, os/x, mingw etc).
837 int mlt_properties_dir_list( mlt_properties
this, const char *dirname
, const char *pattern
, int sort
)
839 DIR *dir
= opendir( dirname
);
844 struct dirent
*de
= readdir( dir
);
845 char fullname
[ 1024 ];
848 sprintf( key
, "%d", mlt_properties_count( this ) );
849 snprintf( fullname
, 1024, "%s/%s", dirname
, de
->d_name
);
850 if ( pattern
== NULL
)
851 mlt_properties_set( this, key
, fullname
);
852 else if ( de
->d_name
[ 0 ] != '.' && mlt_fnmatch( pattern
, de
->d_name
) )
853 mlt_properties_set( this, key
, fullname
);
860 if ( sort
&& mlt_properties_count( this ) )
862 property_list
*list
= this->local
;
863 qsort( list
->value
, mlt_properties_count( this ), sizeof( mlt_property
), mlt_compare
);
866 return mlt_properties_count( this );
872 void mlt_properties_close( mlt_properties
this )
874 if ( this != NULL
&& mlt_properties_dec_ref( this ) <= 0 )
876 if ( this->close
!= NULL
)
878 this->close( this->close_object
);
882 property_list
*list
= this->local
;
885 #if _MLT_PROPERTY_CHECKS_ == 1
887 mlt_properties_debug( this, "Closing", stderr
);
890 #ifdef _MLT_PROPERTY_CHECKS_
891 // Increment destroyed count
892 properties_destroyed
++;
894 // Show current stats - these should match when the app is closed
895 fprintf( stderr
, "Created %d, destroyed %d\n", properties_created
, properties_destroyed
);
898 // Clean up names and values
899 for ( index
= list
->count
- 1; index
>= 0; index
-- )
901 free( list
->name
[ index
] );
902 mlt_property_close( list
->value
[ index
] );
910 // Free this now if this has no child
911 if ( this->child
== NULL
)
917 /** Determine if the properties list is really just a sequence or ordered list.
918 Returns 0 if false or 1 if true.
920 int mlt_properties_is_sequence( mlt_properties properties
)
923 int n
= mlt_properties_count( properties
);
924 for ( i
= 0; i
< n
; i
++ )
925 if ( ! isdigit( mlt_properties_get_name( properties
, i
)[0] ) )
931 * YAML is a nifty text format popular in the Ruby world as a cleaner,
932 * less verbose alternative to XML. See this Wikipedia topic for an overview:
933 * http://en.wikipedia.org/wiki/YAML
934 * The YAML specification is at:
936 * YAML::Tiny is a Perl module that specifies a subset of YAML that we are
937 * using here (for the same reasons):
938 * http://search.cpan.org/~adamk/YAML-Tiny-1.25/lib/YAML/Tiny.pm
941 struct yaml_parser_context
948 unsigned int block_indent
;
951 typedef struct yaml_parser_context
*yaml_parser
;
953 static unsigned int ltrim( char **s
)
958 for ( i
= 0; i
< n
&& *c
== ' '; i
++, c
++ );
963 static unsigned int rtrim( char *s
)
967 for ( i
= n
; i
> 0 && s
[i
- 1] == ' '; --i
)
972 static int parse_yaml( yaml_parser context
, const char *namevalue
)
974 char *name_
= strdup( namevalue
);
978 char *ptr
= strchr( name
, ':' );
979 unsigned int indent
= ltrim( &name
);
980 mlt_properties properties
= mlt_deque_peek_front( context
->stack
);
982 // Ascending one more levels in the tree
983 if ( indent
< context
->level
)
986 unsigned int n
= ( context
->level
- indent
) / 2;
987 for ( i
= 0; i
< n
; i
++ )
988 mlt_deque_pop_front( context
->stack
);
989 properties
= mlt_deque_peek_front( context
->stack
);
990 context
->level
= indent
;
993 // Descending a level in the tree
994 else if ( indent
> context
->level
&& context
->block
== 0 )
996 context
->level
= indent
;
999 // If there is a colon that is not part of a block
1000 if ( ptr
&& ( indent
== context
->level
) )
1002 // Reset block processing
1003 if ( context
->block_name
)
1005 free( context
->block_name
);
1006 context
->block_name
= NULL
;
1010 // Terminate the name and setup the value pointer
1014 char *comment
= strchr( ptr
, '#' );
1020 // Trim leading and trailing spaces from bare value
1024 // No value means a child
1025 if ( strcmp( ptr
, "" ) == 0 )
1027 mlt_properties child
= mlt_properties_new();
1028 mlt_properties_set_data( properties
, name
, child
, 0,
1029 ( mlt_destructor
)mlt_properties_close
, NULL
);
1030 mlt_deque_push_front( context
->stack
, child
);
1036 // A dash indicates a sequence item
1037 if ( name
[0] == '-' )
1039 mlt_properties child
= mlt_properties_new();
1042 snprintf( key
, sizeof(key
), "%d", context
->index
++ );
1043 mlt_properties_set_data( properties
, key
, child
, 0,
1044 ( mlt_destructor
)mlt_properties_close
, NULL
);
1045 mlt_deque_push_front( context
->stack
, child
);
1048 context
->level
+= ltrim( &name
) + 1;
1056 value
= strdup( ptr
);
1057 if ( value
&& value
[ strlen( value
) - 1 ] == '\"' )
1058 value
[ strlen( value
) - 1 ] = 0;
1061 // Value is folded or unfolded block
1062 else if ( *ptr
== '|' || *ptr
== '>' )
1064 context
->block
= *ptr
;
1065 context
->block_name
= strdup( name
);
1066 context
->block_indent
= 0;
1067 value
= strdup( "" );
1073 value
= strdup( ptr
);
1077 // A list of scalars
1078 else if ( name
[0] == '-' )
1080 // Reset block processing
1081 if ( context
->block_name
)
1083 free( context
->block_name
);
1084 context
->block_name
= NULL
;
1090 snprintf( key
, sizeof(key
), "%d", context
->index
++ );
1094 char *comment
= strchr( ptr
, '#' );
1098 // Trim leading and trailing spaces from bare value
1106 value
= strdup( ptr
);
1107 if ( value
&& value
[ strlen( value
) - 1 ] == '\"' )
1108 value
[ strlen( value
) - 1 ] = 0;
1111 // Value is folded or unfolded block
1112 else if ( *ptr
== '|' || *ptr
== '>' )
1114 context
->block
= *ptr
;
1115 context
->block_name
= strdup( key
);
1116 context
->block_indent
= 0;
1117 value
= strdup( "" );
1123 value
= strdup( ptr
);
1127 name
= name_
= strdup( key
);
1131 else if ( context
->block
== '|' )
1133 if ( context
->block_indent
== 0 )
1134 context
->block_indent
= indent
;
1135 if ( indent
> context
->block_indent
)
1136 name
= &name_
[ context
->block_indent
];
1138 char *old_value
= mlt_properties_get( properties
, context
->block_name
);
1139 value
= calloc( 1, strlen( old_value
) + strlen( name
) + 2 );
1140 strcpy( value
, old_value
);
1141 if ( strcmp( old_value
, "" ) )
1142 strcat( value
, "\n" );
1143 strcat( value
, name
);
1144 name
= context
->block_name
;
1148 else if ( context
->block
== '>' )
1152 char *old_value
= mlt_properties_get( properties
, context
->block_name
);
1154 // Blank line (prepended with spaces) is new line
1155 if ( strcmp( name
, "" ) == 0 )
1157 value
= calloc( 1, strlen( old_value
) + 2 );
1158 strcat( value
, old_value
);
1159 strcat( value
, "\n" );
1161 // Concatenate with space
1164 value
= calloc( 1, strlen( old_value
) + strlen( name
) + 2 );
1165 strcat( value
, old_value
);
1166 if ( strcmp( old_value
, "" ) && old_value
[ strlen( old_value
) - 1 ] != '\n' )
1167 strcat( value
, " " );
1168 strcat( value
, name
);
1170 name
= context
->block_name
;
1175 value
= strdup( "" );
1178 error
= mlt_properties_set( properties
, name
, value
);
1186 mlt_properties
mlt_properties_parse_yaml( const char *filename
)
1188 // Construct a standalone properties object
1189 mlt_properties
this = mlt_properties_new( );
1194 FILE *file
= fopen( filename
, "r" );
1196 // Load contents of file
1201 char *ptemp
= &temp
[ 0 ];
1204 yaml_parser context
= calloc( 1, sizeof( struct yaml_parser_context
) );
1205 context
->stack
= mlt_deque_init();
1206 mlt_deque_push_front( context
->stack
, this );
1208 // Read each string from the file
1209 while( fgets( temp
, 1024, file
) )
1211 // Check for end-of-stream
1212 if ( strncmp( ptemp
, "...", 3 ) == 0 )
1216 temp
[ strlen( temp
) - 1 ] = '\0';
1218 // Skip blank lines, comment lines, and document separator
1219 if ( strcmp( ptemp
, "" ) && ptemp
[ 0 ] != '#' && strncmp( ptemp
, "---", 3 )
1220 && strncmp( ptemp
, "%YAML", 5 ) && strncmp( ptemp
, "% YAML", 6 ) )
1221 parse_yaml( context
, temp
);
1226 mlt_deque_close( context
->stack
);
1227 if ( context
->block_name
)
1228 free( context
->block_name
);
1233 // Return the pointer
1237 /** YAML Tiny Serialiser
1240 /* strbuf is a self-growing string buffer */
1242 #define STRBUF_GROWTH (1024)
1250 typedef struct strbuf_s
*strbuf
;
1252 static strbuf
strbuf_new( )
1254 strbuf buffer
= calloc( 1, sizeof( struct strbuf_s
) );
1255 buffer
->size
= STRBUF_GROWTH
;
1256 buffer
->string
= calloc( 1, buffer
->size
);
1260 static void strbuf_close( strbuf buffer
)
1262 // We do not free buffer->string; strbuf user must save that pointer
1268 static char *strbuf_printf( strbuf buffer
, const char *format
, ... )
1270 while ( buffer
->string
)
1273 va_start( ap
, format
);
1274 size_t len
= strlen( buffer
->string
);
1275 size_t remain
= buffer
->size
- len
- 1;
1276 int need
= vsnprintf( buffer
->string
+ len
, remain
, format
, ap
);
1278 if ( need
> -1 && need
< remain
)
1280 buffer
->string
[ len
] = 0;
1281 buffer
->size
+= need
+ STRBUF_GROWTH
;
1282 buffer
->string
= realloc( buffer
->string
, buffer
->size
);
1284 return buffer
->string
;
1287 static inline void indent_yaml( strbuf output
, int indent
)
1290 for ( j
= 0; j
< indent
; j
++ )
1291 strbuf_printf( output
, " " );
1294 static void output_yaml_block_literal( strbuf output
, const char *value
, int indent
)
1296 char *v
= strdup( value
);
1298 char *eol
= strchr( sol
, '\n' );
1302 indent_yaml( output
, indent
);
1304 strbuf_printf( output
, "%s\n", sol
);
1306 eol
= strchr( sol
, '\n' );
1308 indent_yaml( output
, indent
);
1309 strbuf_printf( output
, "%s\n", sol
);
1312 static void serialise_yaml( mlt_properties
this, strbuf output
, int indent
, int is_parent_sequence
)
1314 property_list
*list
= this->local
;
1317 for ( i
= 0; i
< list
->count
; i
++ )
1319 // This implementation assumes that all data elements are property lists.
1320 // Unfortunately, we do not have run time type identification.
1321 mlt_properties child
= mlt_property_get_data( list
->value
[ i
], NULL
);
1323 if ( mlt_properties_is_sequence( this ) )
1325 // Ignore hidden/non-serialisable items
1326 if ( list
->name
[ i
][ 0 ] != '_' )
1328 // Indicate a sequence item
1329 indent_yaml( output
, indent
);
1330 strbuf_printf( output
, "- " );
1332 // If the value can be represented as a string
1333 const char *value
= mlt_properties_get( this, list
->name
[ i
] );
1334 if ( value
&& strcmp( value
, "" ) )
1336 // Determine if this is an unfolded block literal
1337 if ( strchr( value
, '\n' ) )
1339 strbuf_printf( output
, "|\n" );
1340 output_yaml_block_literal( output
, value
, indent
+ strlen( list
->name
[ i
] ) + strlen( "|" ) );
1344 strbuf_printf( output
, "%s\n", value
);
1350 serialise_yaml( child
, output
, indent
+ 2, 1 );
1354 // Assume this is a normal map-oriented properties list
1355 const char *value
= mlt_properties_get( this, list
->name
[ i
] );
1357 // Ignore hidden/non-serialisable items
1358 // If the value can be represented as a string
1359 if ( list
->name
[ i
][ 0 ] != '_' && value
&& strcmp( value
, "" ) )
1361 if ( is_parent_sequence
== 0 )
1362 indent_yaml( output
, indent
);
1364 is_parent_sequence
= 0;
1366 // Determine if this is an unfolded block literal
1367 if ( strchr( value
, '\n' ) )
1369 strbuf_printf( output
, "%s: |\n", list
->name
[ i
] );
1370 output_yaml_block_literal( output
, value
, indent
+ strlen( list
->name
[ i
] ) + strlen( ": " ) );
1374 strbuf_printf( output
, "%s: %s\n", list
->name
[ i
], value
);
1378 // Output a child as a map item
1381 indent_yaml( output
, indent
);
1382 strbuf_printf( output
, "%s:\n", list
->name
[ i
] );
1385 serialise_yaml( child
, output
, indent
+ 2, 0 );
1391 /** Serialise the properties as a string of YAML Tiny.
1392 The caller must free the returned string.
1395 char *mlt_properties_serialise_yaml( mlt_properties
this )
1397 strbuf b
= strbuf_new();
1398 strbuf_printf( b
, "---\n" );
1399 serialise_yaml( this, b
, 0, 0 );
1400 strbuf_printf( b
, "...\n" );
1401 char *ret
= b
->string
;