]> git.sesse.net Git - mlt/blob - src/framework/mlt_properties.c
more small optimisations
[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 && strcmp( name, "in" ) && strcmp( name, "out" ) )
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 /** Get a string value by name.
243 */
244
245 char *mlt_properties_get( mlt_properties this, char *name )
246 {
247         mlt_property value = mlt_properties_find( this, name );
248         return value == NULL ? NULL : mlt_property_get_string( value );
249 }
250
251 /** Get a name by index.
252 */
253
254 char *mlt_properties_get_name( mlt_properties this, int index )
255 {
256         property_list *list = this->private;
257         if ( index >= 0 && index < list->count )
258                 return list->name[ index ];
259         return NULL;
260 }
261
262 /** Get a string value by index.
263 */
264
265 char *mlt_properties_get_value( mlt_properties this, int index )
266 {
267         property_list *list = this->private;
268         if ( index >= 0 && index < list->count )
269                 return mlt_property_get_string( list->value[ index ] );
270         return NULL;
271 }
272
273 /** Get a data value by index.
274 */
275
276 void *mlt_properties_get_data_at( mlt_properties this, int index, int *size )
277 {
278         property_list *list = this->private;
279         if ( index >= 0 && index < list->count )
280                 return mlt_property_get_data( list->value[ index ], size );
281         return NULL;
282 }
283
284 /** Return the number of items in the list.
285 */
286
287 int mlt_properties_count( mlt_properties this )
288 {
289         property_list *list = this->private;
290         return list->count;
291 }
292
293 /** Set a value by parsing a name=value string
294 */
295
296 int mlt_properties_parse( mlt_properties this, char *namevalue )
297 {
298         char *name = strdup( namevalue );
299         char *value = strdup( namevalue );
300         int error = 0;
301
302         if ( strchr( name, '=' ) )
303         {
304                 *( strchr( name, '=' ) ) = '\0';
305                 strcpy( value, strchr( value, '=' ) + 1 );
306         }
307         else
308         {
309                 strcpy( value, "" );
310         }
311
312         if ( strlen( value ) > 1 && value[ 0 ] == '\"' )
313         {
314                 strcpy( value, value + 1 );
315                 if ( value[ strlen( value ) - 1 ] == '\"' )
316                         value[ strlen( value ) - 1 ] = '\0';
317         }
318
319         error = mlt_properties_set( this, name, value );
320
321         free( name );
322         free( value );
323
324         return error;
325 }
326
327 /** Get a value associated to the name.
328 */
329
330 int mlt_properties_get_int( mlt_properties this, char *name )
331 {
332         mlt_property value = mlt_properties_find( this, name );
333         return value == NULL ? 0 : mlt_property_get_int( value );
334 }
335
336 /** Set a value associated to the name.
337 */
338
339 int mlt_properties_set_int( mlt_properties this, char *name, int value )
340 {
341         int error = 1;
342
343         // Fetch the property to work with
344         mlt_property property = mlt_properties_fetch( this, name );
345
346         // Set it if not NULL
347         if ( property != NULL )
348         {
349                 error = mlt_property_set_int( property, value );
350                 mlt_properties_do_mirror( this, name );
351         }
352
353         return error;
354 }
355
356 /** Get a value associated to the name.
357 */
358
359 double mlt_properties_get_double( mlt_properties this, char *name )
360 {
361         mlt_property value = mlt_properties_find( this, name );
362         return value == NULL ? 0 : mlt_property_get_double( value );
363 }
364
365 /** Set a value associated to the name.
366 */
367
368 int mlt_properties_set_double( mlt_properties this, char *name, double value )
369 {
370         int error = 1;
371
372         // Fetch the property to work with
373         mlt_property property = mlt_properties_fetch( this, name );
374
375         // Set it if not NULL
376         if ( property != NULL )
377         {
378                 error = mlt_property_set_double( property, value );
379                 mlt_properties_do_mirror( this, name );
380         }
381
382         return error;
383 }
384
385 /** Get a value associated to the name.
386 */
387
388 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
389 {
390         mlt_property value = mlt_properties_find( this, name );
391         return value == NULL ? 0 : mlt_property_get_position( value );
392 }
393
394 /** Set a value associated to the name.
395 */
396
397 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
398 {
399         int error = 1;
400
401         // Fetch the property to work with
402         mlt_property property = mlt_properties_fetch( this, name );
403
404         // Set it if not NULL
405         if ( property != NULL )
406         {
407                 error = mlt_property_set_position( property, value );
408                 mlt_properties_do_mirror( this, name );
409         }
410
411         return error;
412 }
413
414 /** Get a value associated to the name.
415 */
416
417 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
418 {
419         mlt_property value = mlt_properties_find( this, name );
420         return value == NULL ? NULL : mlt_property_get_data( value, length );
421 }
422
423 /** Set a value associated to the name.
424 */
425
426 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
427 {
428         int error = 1;
429
430         // Fetch the property to work with
431         mlt_property property = mlt_properties_fetch( this, name );
432
433         // Set it if not NULL
434         if ( property != NULL )
435                 error = mlt_property_set_data( property, value, length, destroy, serialise );
436
437         return error;
438 }
439
440 /** Rename a property.
441 */
442
443 int mlt_properties_rename( mlt_properties this, char *source, char *dest )
444 {
445         mlt_property value = mlt_properties_find( this, dest );
446
447         if ( value == NULL )
448         {
449                 property_list *list = this->private;
450                 int i = 0;
451
452                 // Locate the item 
453                 for ( i = 0; i < list->count; i ++ )
454                 {
455                         if ( !strcmp( list->name[ i ], source ) )
456                         {
457                                 free( list->name[ i ] );
458                                 list->name[ i ] = strdup( dest );
459                                 list->hash[ generate_hash( dest ) ] = i + 1;
460                                 break;
461                         }
462                 }
463         }
464
465         return value != NULL;
466 }
467
468 /** Dump the properties.
469 */
470
471 void mlt_properties_dump( mlt_properties this, FILE *output )
472 {
473         property_list *list = this->private;
474         int i = 0;
475         for ( i = 0; i < list->count; i ++ )
476                 fprintf( stderr, "%s = %s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
477 }
478
479 /** Close the list.
480 */
481
482 void mlt_properties_close( mlt_properties this )
483 {
484         property_list *list = this->private;
485         int index = 0;
486
487         // Clean up names and values
488         for ( index = list->count - 1; index >= 0; index -- )
489         {
490                 free( list->name[ index ] );
491                 mlt_property_close( list->value[ index ] );
492         }
493
494         // Clear up the list
495         free( list->name );
496         free( list->value );
497         free( list );
498
499         // Free this now if this has no child
500         if ( this->child == NULL )
501                 free( this );
502 }
503