2 * mlt_properties.c -- base properties class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "mlt_properties.h"
23 #include "mlt_property.h"
29 /* ---------------- // Private Implementation // ---------------- */
31 /** Private implementation of the property list.
41 mlt_properties mirror
;
45 /** Basic implementation.
48 int mlt_properties_init( mlt_properties
this, void *child
)
51 memset( this, 0, sizeof( struct mlt_properties_s
) );
53 // Assign the child of the object
56 // Allocate the private structure
57 this->private = calloc( sizeof( property_list
), 1 );
59 return this->private == NULL
;
62 /** Constructor for stand alone object.
65 mlt_properties
mlt_properties_new( )
67 // Construct a standalone properties object
68 mlt_properties
this = calloc( sizeof( struct mlt_properties_s
), 1 );
71 mlt_properties_init( this, NULL
);
77 static inline int generate_hash( char *name
)
82 hash
= ( hash
+ ( i
++ * ( *name
++ & 31 ) ) ) % 199;
86 /** Special case - when a container (such as fezzik) is protecting another
87 producer, we need to ensure that properties are passed through to the
91 static inline void mlt_properties_do_mirror( mlt_properties
this, char *name
)
93 property_list
*list
= this->private;
94 if ( list
->mirror
!= NULL
)
96 char *value
= mlt_properties_get( this, name
);
98 mlt_properties_set( list
->mirror
, name
, value
);
102 /** Allow the specification of a mirror.
105 void mlt_properties_mirror( mlt_properties
this, mlt_properties that
)
107 property_list
*list
= this->private;
111 /** Inherit all serialisable properties from that into this.
114 int mlt_properties_inherit( mlt_properties
this, mlt_properties that
)
116 int count
= mlt_properties_count( that
);
118 for ( i
= 0; i
< count
; i
++ )
120 char *value
= mlt_properties_get_value( that
, i
);
123 char *name
= mlt_properties_get_name( that
, i
);
124 mlt_properties_set( this, name
, value
);
130 /** Pass all properties from 'that' that match the prefix to 'this' (excluding the prefix).
133 int mlt_properties_pass( mlt_properties
this, mlt_properties that
, char *prefix
)
135 int count
= mlt_properties_count( that
);
136 int length
= strlen( prefix
);
138 for ( i
= 0; i
< count
; i
++ )
140 char *name
= mlt_properties_get_name( that
, i
);
141 if ( !strncmp( name
, prefix
, length
) )
143 char *value
= mlt_properties_get_value( that
, i
);
145 mlt_properties_set( this, name
+ length
, value
);
151 /** Locate a property by name
154 static inline mlt_property
mlt_properties_find( mlt_properties
this, char *name
)
156 property_list
*list
= this->private;
157 mlt_property value
= NULL
;
158 int key
= generate_hash( name
);
159 int i
= list
->hash
[ key
] - 1;
163 // Check if we're hashed
164 if ( list
->count
> 0 &&
165 name
[ 0 ] == list
->name
[ i
][ 0 ] &&
166 !strcmp( list
->name
[ i
], name
) )
167 value
= list
->value
[ i
];
170 for ( i
= list
->count
- 1; value
== NULL
&& i
>= 0; i
-- )
171 if ( name
[ 0 ] == list
->name
[ i
][ 0 ] && !strcmp( list
->name
[ i
], name
) )
172 value
= list
->value
[ i
];
178 /** Add a new property.
181 static mlt_property
mlt_properties_add( mlt_properties
this, char *name
)
183 property_list
*list
= this->private;
184 int key
= generate_hash( name
);
186 // Check that we have space and resize if necessary
187 if ( list
->count
== list
->size
)
190 list
->name
= realloc( list
->name
, list
->size
* sizeof( char * ) );
191 list
->value
= realloc( list
->value
, list
->size
* sizeof( mlt_property
) );
194 // Assign name/value pair
195 list
->name
[ list
->count
] = strdup( name
);
196 list
->value
[ list
->count
] = mlt_property_init( );
198 // Assign to hash table
199 if ( list
->hash
[ key
] == 0 )
200 list
->hash
[ key
] = list
->count
+ 1;
202 // Return and increment count accordingly
203 return list
->value
[ list
->count
++ ];
206 /** Fetch a property by name - this includes add if not found.
209 static mlt_property
mlt_properties_fetch( mlt_properties
this, char *name
)
211 // Try to find an existing property first
212 mlt_property property
= mlt_properties_find( this, name
);
214 // If it wasn't found, create one
215 if ( property
== NULL
)
216 property
= mlt_properties_add( this, name
);
218 // Return the property
222 /** Set the property.
225 int mlt_properties_set( mlt_properties
this, char *name
, char *value
)
229 // Fetch the property to work with
230 mlt_property property
= mlt_properties_fetch( this, name
);
232 // Set it if not NULL
233 if ( property
!= NULL
)
235 error
= mlt_property_set_string( property
, value
);
236 mlt_properties_do_mirror( this, name
);
242 /** Set or default the property.
245 int mlt_properties_set_or_default( mlt_properties
this, char *name
, char *value
, char *def
)
247 return mlt_properties_set( this, name
, value
== NULL ? def
: value
);
250 /** Get a string value by name.
253 char *mlt_properties_get( mlt_properties
this, char *name
)
255 mlt_property value
= mlt_properties_find( this, name
);
256 return value
== NULL ? NULL
: mlt_property_get_string( value
);
259 /** Get a name by index.
262 char *mlt_properties_get_name( mlt_properties
this, int index
)
264 property_list
*list
= this->private;
265 if ( index
>= 0 && index
< list
->count
)
266 return list
->name
[ index
];
270 /** Get a string value by index.
273 char *mlt_properties_get_value( mlt_properties
this, int index
)
275 property_list
*list
= this->private;
276 if ( index
>= 0 && index
< list
->count
)
277 return mlt_property_get_string( list
->value
[ index
] );
281 /** Get a data value by index.
284 void *mlt_properties_get_data_at( mlt_properties
this, int index
, int *size
)
286 property_list
*list
= this->private;
287 if ( index
>= 0 && index
< list
->count
)
288 return mlt_property_get_data( list
->value
[ index
], size
);
292 /** Return the number of items in the list.
295 int mlt_properties_count( mlt_properties
this )
297 property_list
*list
= this->private;
301 /** Set a value by parsing a name=value string
304 int mlt_properties_parse( mlt_properties
this, char *namevalue
)
306 char *name
= strdup( namevalue
);
307 char *value
= strdup( namevalue
);
310 if ( strchr( name
, '=' ) )
312 *( strchr( name
, '=' ) ) = '\0';
313 strcpy( value
, strchr( value
, '=' ) + 1 );
320 if ( strlen( value
) > 1 && value
[ 0 ] == '\"' )
322 strcpy( value
, value
+ 1 );
323 if ( value
[ strlen( value
) - 1 ] == '\"' )
324 value
[ strlen( value
) - 1 ] = '\0';
327 error
= mlt_properties_set( this, name
, value
);
335 /** Get a value associated to the name.
338 int mlt_properties_get_int( mlt_properties
this, char *name
)
340 mlt_property value
= mlt_properties_find( this, name
);
341 return value
== NULL ?
0 : mlt_property_get_int( value
);
344 /** Set a value associated to the name.
347 int mlt_properties_set_int( mlt_properties
this, char *name
, int value
)
351 // Fetch the property to work with
352 mlt_property property
= mlt_properties_fetch( this, name
);
354 // Set it if not NULL
355 if ( property
!= NULL
)
357 error
= mlt_property_set_int( property
, value
);
358 mlt_properties_do_mirror( this, name
);
364 /** Get a value associated to the name.
367 double mlt_properties_get_double( mlt_properties
this, char *name
)
369 mlt_property value
= mlt_properties_find( this, name
);
370 return value
== NULL ?
0 : mlt_property_get_double( value
);
373 /** Set a value associated to the name.
376 int mlt_properties_set_double( mlt_properties
this, char *name
, double value
)
380 // Fetch the property to work with
381 mlt_property property
= mlt_properties_fetch( this, name
);
383 // Set it if not NULL
384 if ( property
!= NULL
)
386 error
= mlt_property_set_double( property
, value
);
387 mlt_properties_do_mirror( this, name
);
393 /** Get a value associated to the name.
396 mlt_position
mlt_properties_get_position( mlt_properties
this, char *name
)
398 mlt_property value
= mlt_properties_find( this, name
);
399 return value
== NULL ?
0 : mlt_property_get_position( value
);
402 /** Set a value associated to the name.
405 int mlt_properties_set_position( mlt_properties
this, char *name
, mlt_position value
)
409 // Fetch the property to work with
410 mlt_property property
= mlt_properties_fetch( this, name
);
412 // Set it if not NULL
413 if ( property
!= NULL
)
415 error
= mlt_property_set_position( property
, value
);
416 mlt_properties_do_mirror( this, name
);
422 /** Get a value associated to the name.
425 void *mlt_properties_get_data( mlt_properties
this, char *name
, int *length
)
427 mlt_property value
= mlt_properties_find( this, name
);
428 return value
== NULL ? NULL
: mlt_property_get_data( value
, length
);
431 /** Set a value associated to the name.
434 int mlt_properties_set_data( mlt_properties
this, char *name
, void *value
, int length
, mlt_destructor destroy
, mlt_serialiser serialise
)
438 // Fetch the property to work with
439 mlt_property property
= mlt_properties_fetch( this, name
);
441 // Set it if not NULL
442 if ( property
!= NULL
)
443 error
= mlt_property_set_data( property
, value
, length
, destroy
, serialise
);
448 /** Rename a property.
451 int mlt_properties_rename( mlt_properties
this, char *source
, char *dest
)
453 mlt_property value
= mlt_properties_find( this, dest
);
457 property_list
*list
= this->private;
461 for ( i
= 0; i
< list
->count
; i
++ )
463 if ( !strcmp( list
->name
[ i
], source
) )
465 free( list
->name
[ i
] );
466 list
->name
[ i
] = strdup( dest
);
467 list
->hash
[ generate_hash( dest
) ] = i
+ 1;
473 return value
!= NULL
;
476 /** Dump the properties.
479 void mlt_properties_dump( mlt_properties
this, FILE *output
)
481 property_list
*list
= this->private;
483 for ( i
= 0; i
< list
->count
; i
++ )
484 fprintf( stderr
, "%s = %s\n", list
->name
[ i
], mlt_properties_get( this, list
->name
[ i
] ) );
490 void mlt_properties_close( mlt_properties
this )
492 property_list
*list
= this->private;
495 // Clean up names and values
496 for ( index
= list
->count
- 1; index
>= 0; index
-- )
498 free( list
->name
[ index
] );
499 mlt_property_close( list
->value
[ index
] );
507 // Free this now if this has no child
508 if ( this->child
== NULL
)