]> git.sesse.net Git - mlt/blob - src/framework/mlt_properties.c
1638200c0a85bce147359fd649206f64452147c7
[mlt] / src / framework / mlt_properties.c
1 /*
2  * mlt_properties.c -- base properties class
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "config.h"
22 #include "mlt_properties.h"
23 #include "mlt_property.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 /* ---------------- // Private Implementation // ---------------- */
30
31 /** Private implementation of the property list.
32 */
33
34 typedef struct
35 {
36         int hash[ 199 ];
37         char **name;
38         mlt_property *value;
39         int count;
40         int size;
41         mlt_properties mirror;
42 }
43 property_list;
44
45 /** Basic implementation.
46 */
47
48 int mlt_properties_init( mlt_properties this, void *child )
49 {
50         // NULL all methods
51         memset( this, 0, sizeof( struct mlt_properties_s ) );
52
53         // Assign the child of the object
54         this->child = child;
55
56         // Allocate the private structure
57         this->private = calloc( sizeof( property_list ), 1 );
58
59         return this->private == NULL;
60 }
61
62 /** Constructor for stand alone object.
63 */
64
65 mlt_properties mlt_properties_new( )
66 {
67         // Construct a standalone properties object
68         mlt_properties this = calloc( sizeof( struct mlt_properties_s ), 1 );
69
70         // Initialise this
71         mlt_properties_init( this, NULL );
72
73         // Return the pointer
74         return this;
75 }
76
77 static inline int generate_hash( char *name )
78 {
79         int hash = 0;
80         int i = 1;
81         while ( *name )
82                 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
83         return hash;
84 }
85
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
88         real producer.
89 */
90
91 static inline void mlt_properties_do_mirror( mlt_properties this, char *name )
92 {
93         property_list *list = this->private;
94         if ( list->mirror != NULL ) 
95         {
96                 char *value = mlt_properties_get( this, name );
97                 if ( value != NULL )
98                         mlt_properties_set( list->mirror, name, value );
99         }
100 }
101
102 /** Allow the specification of a mirror.
103 */
104
105 void mlt_properties_mirror( mlt_properties this, mlt_properties that )
106 {
107         property_list *list = this->private;
108         list->mirror = that;
109 }
110
111 /** Inherit all serialisable properties from that into this.
112 */
113
114 int mlt_properties_inherit( mlt_properties this, mlt_properties that )
115 {
116         int count = mlt_properties_count( that );
117         int i = 0;
118         for ( i = 0; i < count; i ++ )
119         {
120                 char *value = mlt_properties_get_value( that, i );
121                 if ( value != NULL )
122                 {
123                         char *name = mlt_properties_get_name( that, i );
124                         mlt_properties_set( this, name, value );
125                 }
126         }
127         return 0;
128 }
129
130 /** Pass all properties from 'that' that match the prefix to 'this' (excluding the prefix).
131 */
132
133 int mlt_properties_pass( mlt_properties this, mlt_properties that, char *prefix )
134 {
135         int count = mlt_properties_count( that );
136         int length = strlen( prefix );
137         int i = 0;
138         for ( i = 0; i < count; i ++ )
139         {
140                 char *name = mlt_properties_get_name( that, i );
141                 if ( !strncmp( name, prefix, length ) )
142                 {
143                         char *value = mlt_properties_get_value( that, i );
144                         if ( value != NULL )
145                                 mlt_properties_set( this, name + length, value );
146                 }
147         }
148         return 0;
149 }
150
151 /** Locate a property by name
152 */
153
154 static inline mlt_property mlt_properties_find( mlt_properties this, char *name )
155 {
156         property_list *list = this->private;
157         mlt_property value = NULL;
158         int key = generate_hash( name );
159         int i = list->hash[ key ] - 1;
160
161         if ( i >= 0 )
162         {
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 ];
168
169                 // Locate the item 
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 ];
173         }
174
175         return value;
176 }
177
178 /** Add a new property.
179 */
180
181 static mlt_property mlt_properties_add( mlt_properties this, char *name )
182 {
183         property_list *list = this->private;
184         int key = generate_hash( name );
185
186         // Check that we have space and resize if necessary
187         if ( list->count == list->size )
188         {
189                 list->size += 50;
190                 list->name = realloc( list->name, list->size * sizeof( char * ) );
191                 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
192         }
193
194         // Assign name/value pair
195         list->name[ list->count ] = strdup( name );
196         list->value[ list->count ] = mlt_property_init( );
197
198         // Assign to hash table
199         if ( list->hash[ key ] == 0 )
200                 list->hash[ key ] = list->count + 1;
201
202         // Return and increment count accordingly
203         return list->value[ list->count ++ ];
204 }
205
206 /** Fetch a property by name - this includes add if not found.
207 */
208
209 static mlt_property mlt_properties_fetch( mlt_properties this, char *name )
210 {
211         // Try to find an existing property first
212         mlt_property property = mlt_properties_find( this, name );
213
214         // If it wasn't found, create one
215         if ( property == NULL )
216                 property = mlt_properties_add( this, name );
217
218         // Return the property
219         return property;
220 }
221
222 /** Set the property.
223 */
224
225 int mlt_properties_set( mlt_properties this, char *name, char *value )
226 {
227         int error = 1;
228
229         // Fetch the property to work with
230         mlt_property property = mlt_properties_fetch( this, name );
231
232         // Set it if not NULL
233         if ( property != NULL )
234         {
235                 error = mlt_property_set_string( property, value );
236                 mlt_properties_do_mirror( this, name );
237         }
238
239         return error;
240 }
241
242 /** Set or default the property.
243 */
244
245 int mlt_properties_set_or_default( mlt_properties this, char *name, char *value, char *def )
246 {
247         return mlt_properties_set( this, name, value == NULL ? def : value );
248 }
249
250 /** Get a string value by name.
251 */
252
253 char *mlt_properties_get( mlt_properties this, char *name )
254 {
255         mlt_property value = mlt_properties_find( this, name );
256         return value == NULL ? NULL : mlt_property_get_string( value );
257 }
258
259 /** Get a name by index.
260 */
261
262 char *mlt_properties_get_name( mlt_properties this, int index )
263 {
264         property_list *list = this->private;
265         if ( index >= 0 && index < list->count )
266                 return list->name[ index ];
267         return NULL;
268 }
269
270 /** Get a string value by index.
271 */
272
273 char *mlt_properties_get_value( mlt_properties this, int index )
274 {
275         property_list *list = this->private;
276         if ( index >= 0 && index < list->count )
277                 return mlt_property_get_string( list->value[ index ] );
278         return NULL;
279 }
280
281 /** Get a data value by index.
282 */
283
284 void *mlt_properties_get_data_at( mlt_properties this, int index, int *size )
285 {
286         property_list *list = this->private;
287         if ( index >= 0 && index < list->count )
288                 return mlt_property_get_data( list->value[ index ], size );
289         return NULL;
290 }
291
292 /** Return the number of items in the list.
293 */
294
295 int mlt_properties_count( mlt_properties this )
296 {
297         property_list *list = this->private;
298         return list->count;
299 }
300
301 /** Set a value by parsing a name=value string
302 */
303
304 int mlt_properties_parse( mlt_properties this, char *namevalue )
305 {
306         char *name = strdup( namevalue );
307         char *value = strdup( namevalue );
308         int error = 0;
309
310         if ( strchr( name, '=' ) )
311         {
312                 *( strchr( name, '=' ) ) = '\0';
313                 strcpy( value, strchr( value, '=' ) + 1 );
314         }
315         else
316         {
317                 strcpy( value, "" );
318         }
319
320         if ( strlen( value ) > 1 && value[ 0 ] == '\"' )
321         {
322                 strcpy( value, value + 1 );
323                 if ( value[ strlen( value ) - 1 ] == '\"' )
324                         value[ strlen( value ) - 1 ] = '\0';
325         }
326
327         error = mlt_properties_set( this, name, value );
328
329         free( name );
330         free( value );
331
332         return error;
333 }
334
335 /** Get a value associated to the name.
336 */
337
338 int mlt_properties_get_int( mlt_properties this, char *name )
339 {
340         mlt_property value = mlt_properties_find( this, name );
341         return value == NULL ? 0 : mlt_property_get_int( value );
342 }
343
344 /** Set a value associated to the name.
345 */
346
347 int mlt_properties_set_int( mlt_properties this, char *name, int value )
348 {
349         int error = 1;
350
351         // Fetch the property to work with
352         mlt_property property = mlt_properties_fetch( this, name );
353
354         // Set it if not NULL
355         if ( property != NULL )
356         {
357                 error = mlt_property_set_int( property, value );
358                 mlt_properties_do_mirror( this, name );
359         }
360
361         return error;
362 }
363
364 /** Get a value associated to the name.
365 */
366
367 double mlt_properties_get_double( mlt_properties this, char *name )
368 {
369         mlt_property value = mlt_properties_find( this, name );
370         return value == NULL ? 0 : mlt_property_get_double( value );
371 }
372
373 /** Set a value associated to the name.
374 */
375
376 int mlt_properties_set_double( mlt_properties this, char *name, double value )
377 {
378         int error = 1;
379
380         // Fetch the property to work with
381         mlt_property property = mlt_properties_fetch( this, name );
382
383         // Set it if not NULL
384         if ( property != NULL )
385         {
386                 error = mlt_property_set_double( property, value );
387                 mlt_properties_do_mirror( this, name );
388         }
389
390         return error;
391 }
392
393 /** Get a value associated to the name.
394 */
395
396 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
397 {
398         mlt_property value = mlt_properties_find( this, name );
399         return value == NULL ? 0 : mlt_property_get_position( value );
400 }
401
402 /** Set a value associated to the name.
403 */
404
405 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
406 {
407         int error = 1;
408
409         // Fetch the property to work with
410         mlt_property property = mlt_properties_fetch( this, name );
411
412         // Set it if not NULL
413         if ( property != NULL )
414         {
415                 error = mlt_property_set_position( property, value );
416                 mlt_properties_do_mirror( this, name );
417         }
418
419         return error;
420 }
421
422 /** Get a value associated to the name.
423 */
424
425 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
426 {
427         mlt_property value = mlt_properties_find( this, name );
428         return value == NULL ? NULL : mlt_property_get_data( value, length );
429 }
430
431 /** Set a value associated to the name.
432 */
433
434 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
435 {
436         int error = 1;
437
438         // Fetch the property to work with
439         mlt_property property = mlt_properties_fetch( this, name );
440
441         // Set it if not NULL
442         if ( property != NULL )
443                 error = mlt_property_set_data( property, value, length, destroy, serialise );
444
445         return error;
446 }
447
448 /** Rename a property.
449 */
450
451 int mlt_properties_rename( mlt_properties this, char *source, char *dest )
452 {
453         mlt_property value = mlt_properties_find( this, dest );
454
455         if ( value == NULL )
456         {
457                 property_list *list = this->private;
458                 int i = 0;
459
460                 // Locate the item 
461                 for ( i = 0; i < list->count; i ++ )
462                 {
463                         if ( !strcmp( list->name[ i ], source ) )
464                         {
465                                 free( list->name[ i ] );
466                                 list->name[ i ] = strdup( dest );
467                                 list->hash[ generate_hash( dest ) ] = i + 1;
468                                 break;
469                         }
470                 }
471         }
472
473         return value != NULL;
474 }
475
476 /** Dump the properties.
477 */
478
479 void mlt_properties_dump( mlt_properties this, FILE *output )
480 {
481         property_list *list = this->private;
482         int i = 0;
483         for ( i = 0; i < list->count; i ++ )
484                 fprintf( stderr, "%s = %s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
485 }
486
487 /** Close the list.
488 */
489
490 void mlt_properties_close( mlt_properties this )
491 {
492         property_list *list = this->private;
493         int index = 0;
494
495         // Clean up names and values
496         for ( index = list->count - 1; index >= 0; index -- )
497         {
498                 free( list->name[ index ] );
499                 mlt_property_close( list->value[ index ] );
500         }
501
502         // Clear up the list
503         free( list->name );
504         free( list->value );
505         free( list );
506
507         // Free this now if this has no child
508         if ( this->child == NULL )
509                 free( this );
510 }
511