in point fix, low latency sdl, minor fixes
[melted] / 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 /** Load properties from a file.
78 */
79
80 mlt_properties mlt_properties_load( char *filename )
81 {
82 // Construct a standalone properties object
83 mlt_properties this = mlt_properties_new( );
84
85 if ( this != NULL )
86 {
87 // Open the file
88 FILE *file = fopen( filename, "r" );
89
90 // Load contents of file
91 if ( file != NULL )
92 {
93 // Temp string
94 char temp[ 1024 ];
95
96 // Read each string from the file
97 while( fgets( temp, 1024, file ) )
98 {
99 // Chomp the string
100 temp[ strlen( temp ) - 1 ] = '\0';
101
102 // Parse and set the property
103 if ( strcmp( temp, "" ) && temp[ 0 ] != '#' )
104 mlt_properties_parse( this, temp );
105 }
106
107 // Close the file
108 fclose( file );
109 }
110 }
111
112 // Return the pointer
113 return this;
114 }
115
116 static inline int generate_hash( char *name )
117 {
118 int hash = 0;
119 int i = 1;
120 while ( *name )
121 hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
122 return hash;
123 }
124
125 /** Special case - when a container (such as fezzik) is protecting another
126 producer, we need to ensure that properties are passed through to the
127 real producer.
128 */
129
130 static inline void mlt_properties_do_mirror( mlt_properties this, char *name )
131 {
132 property_list *list = this->private;
133 if ( list->mirror != NULL )
134 {
135 char *value = mlt_properties_get( this, name );
136 if ( value != NULL )
137 mlt_properties_set( list->mirror, name, value );
138 }
139 }
140
141 /** Allow the specification of a mirror.
142 */
143
144 void mlt_properties_mirror( mlt_properties this, mlt_properties that )
145 {
146 property_list *list = this->private;
147 list->mirror = that;
148 }
149
150 /** Inherit all serialisable properties from that into this.
151 */
152
153 int mlt_properties_inherit( mlt_properties this, mlt_properties that )
154 {
155 int count = mlt_properties_count( that );
156 int i = 0;
157 for ( i = 0; i < count; i ++ )
158 {
159 char *value = mlt_properties_get_value( that, i );
160 if ( value != NULL )
161 {
162 char *name = mlt_properties_get_name( that, i );
163 mlt_properties_set( this, name, value );
164 }
165 }
166 return 0;
167 }
168
169 /** Pass all properties from 'that' that match the prefix to 'this' (excluding the prefix).
170 */
171
172 int mlt_properties_pass( mlt_properties this, mlt_properties that, char *prefix )
173 {
174 int count = mlt_properties_count( that );
175 int length = strlen( prefix );
176 int i = 0;
177 for ( i = 0; i < count; i ++ )
178 {
179 char *name = mlt_properties_get_name( that, i );
180 if ( !strncmp( name, prefix, length ) )
181 {
182 char *value = mlt_properties_get_value( that, i );
183 if ( value != NULL )
184 mlt_properties_set( this, name + length, value );
185 }
186 }
187 return 0;
188 }
189
190 /** Locate a property by name
191 */
192
193 static inline mlt_property mlt_properties_find( mlt_properties this, char *name )
194 {
195 property_list *list = this->private;
196 mlt_property value = NULL;
197 int key = generate_hash( name );
198 int i = list->hash[ key ] - 1;
199
200 if ( i >= 0 )
201 {
202 // Check if we're hashed
203 if ( list->count > 0 &&
204 name[ 0 ] == list->name[ i ][ 0 ] &&
205 !strcmp( list->name[ i ], name ) )
206 value = list->value[ i ];
207
208 // Locate the item
209 for ( i = list->count - 1; value == NULL && i >= 0; i -- )
210 if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
211 value = list->value[ i ];
212 }
213
214 return value;
215 }
216
217 /** Add a new property.
218 */
219
220 static mlt_property mlt_properties_add( mlt_properties this, char *name )
221 {
222 property_list *list = this->private;
223 int key = generate_hash( name );
224
225 // Check that we have space and resize if necessary
226 if ( list->count == list->size )
227 {
228 list->size += 50;
229 list->name = realloc( list->name, list->size * sizeof( char * ) );
230 list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
231 }
232
233 // Assign name/value pair
234 list->name[ list->count ] = strdup( name );
235 list->value[ list->count ] = mlt_property_init( );
236
237 // Assign to hash table
238 if ( list->hash[ key ] == 0 )
239 list->hash[ key ] = list->count + 1;
240
241 // Return and increment count accordingly
242 return list->value[ list->count ++ ];
243 }
244
245 /** Fetch a property by name - this includes add if not found.
246 */
247
248 static mlt_property mlt_properties_fetch( mlt_properties this, char *name )
249 {
250 // Try to find an existing property first
251 mlt_property property = mlt_properties_find( this, name );
252
253 // If it wasn't found, create one
254 if ( property == NULL )
255 property = mlt_properties_add( this, name );
256
257 // Return the property
258 return property;
259 }
260
261 /** Set the property.
262 */
263
264 int mlt_properties_set( mlt_properties this, char *name, char *value )
265 {
266 int error = 1;
267
268 // Fetch the property to work with
269 mlt_property property = mlt_properties_fetch( this, name );
270
271 // Set it if not NULL
272 if ( property != NULL )
273 {
274 error = mlt_property_set_string( property, value );
275 mlt_properties_do_mirror( this, name );
276 }
277
278 return error;
279 }
280
281 /** Set or default the property.
282 */
283
284 int mlt_properties_set_or_default( mlt_properties this, char *name, char *value, char *def )
285 {
286 return mlt_properties_set( this, name, value == NULL ? def : value );
287 }
288
289 /** Get a string value by name.
290 */
291
292 char *mlt_properties_get( mlt_properties this, char *name )
293 {
294 mlt_property value = mlt_properties_find( this, name );
295 return value == NULL ? NULL : mlt_property_get_string( value );
296 }
297
298 /** Get a name by index.
299 */
300
301 char *mlt_properties_get_name( mlt_properties this, int index )
302 {
303 property_list *list = this->private;
304 if ( index >= 0 && index < list->count )
305 return list->name[ index ];
306 return NULL;
307 }
308
309 /** Get a string value by index.
310 */
311
312 char *mlt_properties_get_value( mlt_properties this, int index )
313 {
314 property_list *list = this->private;
315 if ( index >= 0 && index < list->count )
316 return mlt_property_get_string( list->value[ index ] );
317 return NULL;
318 }
319
320 /** Get a data value by index.
321 */
322
323 void *mlt_properties_get_data_at( mlt_properties this, int index, int *size )
324 {
325 property_list *list = this->private;
326 if ( index >= 0 && index < list->count )
327 return mlt_property_get_data( list->value[ index ], size );
328 return NULL;
329 }
330
331 /** Return the number of items in the list.
332 */
333
334 int mlt_properties_count( mlt_properties this )
335 {
336 property_list *list = this->private;
337 return list->count;
338 }
339
340 /** Set a value by parsing a name=value string
341 */
342
343 int mlt_properties_parse( mlt_properties this, char *namevalue )
344 {
345 char *name = strdup( namevalue );
346 char *value = strdup( namevalue );
347 int error = 0;
348
349 if ( strchr( name, '=' ) )
350 {
351 *( strchr( name, '=' ) ) = '\0';
352 strcpy( value, strchr( value, '=' ) + 1 );
353 }
354 else
355 {
356 strcpy( value, "" );
357 }
358
359 if ( strlen( value ) > 1 && value[ 0 ] == '\"' )
360 {
361 strcpy( value, value + 1 );
362 if ( value[ strlen( value ) - 1 ] == '\"' )
363 value[ strlen( value ) - 1 ] = '\0';
364 }
365
366 error = mlt_properties_set( this, name, value );
367
368 free( name );
369 free( value );
370
371 return error;
372 }
373
374 /** Get a value associated to the name.
375 */
376
377 int mlt_properties_get_int( mlt_properties this, char *name )
378 {
379 mlt_property value = mlt_properties_find( this, name );
380 return value == NULL ? 0 : mlt_property_get_int( value );
381 }
382
383 /** Set a value associated to the name.
384 */
385
386 int mlt_properties_set_int( mlt_properties this, char *name, int value )
387 {
388 int error = 1;
389
390 // Fetch the property to work with
391 mlt_property property = mlt_properties_fetch( this, name );
392
393 // Set it if not NULL
394 if ( property != NULL )
395 {
396 error = mlt_property_set_int( property, value );
397 mlt_properties_do_mirror( this, name );
398 }
399
400 return error;
401 }
402
403 /** Get a value associated to the name.
404 */
405
406 double mlt_properties_get_double( mlt_properties this, char *name )
407 {
408 mlt_property value = mlt_properties_find( this, name );
409 return value == NULL ? 0 : mlt_property_get_double( value );
410 }
411
412 /** Set a value associated to the name.
413 */
414
415 int mlt_properties_set_double( mlt_properties this, char *name, double value )
416 {
417 int error = 1;
418
419 // Fetch the property to work with
420 mlt_property property = mlt_properties_fetch( this, name );
421
422 // Set it if not NULL
423 if ( property != NULL )
424 {
425 error = mlt_property_set_double( property, value );
426 mlt_properties_do_mirror( this, name );
427 }
428
429 return error;
430 }
431
432 /** Get a value associated to the name.
433 */
434
435 mlt_position mlt_properties_get_position( mlt_properties this, char *name )
436 {
437 mlt_property value = mlt_properties_find( this, name );
438 return value == NULL ? 0 : mlt_property_get_position( value );
439 }
440
441 /** Set a value associated to the name.
442 */
443
444 int mlt_properties_set_position( mlt_properties this, char *name, mlt_position value )
445 {
446 int error = 1;
447
448 // Fetch the property to work with
449 mlt_property property = mlt_properties_fetch( this, name );
450
451 // Set it if not NULL
452 if ( property != NULL )
453 {
454 error = mlt_property_set_position( property, value );
455 mlt_properties_do_mirror( this, name );
456 }
457
458 return error;
459 }
460
461 /** Get a value associated to the name.
462 */
463
464 void *mlt_properties_get_data( mlt_properties this, char *name, int *length )
465 {
466 mlt_property value = mlt_properties_find( this, name );
467 return value == NULL ? NULL : mlt_property_get_data( value, length );
468 }
469
470 /** Set a value associated to the name.
471 */
472
473 int mlt_properties_set_data( mlt_properties this, char *name, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
474 {
475 int error = 1;
476
477 // Fetch the property to work with
478 mlt_property property = mlt_properties_fetch( this, name );
479
480 // Set it if not NULL
481 if ( property != NULL )
482 error = mlt_property_set_data( property, value, length, destroy, serialise );
483
484 return error;
485 }
486
487 /** Rename a property.
488 */
489
490 int mlt_properties_rename( mlt_properties this, char *source, char *dest )
491 {
492 mlt_property value = mlt_properties_find( this, dest );
493
494 if ( value == NULL )
495 {
496 property_list *list = this->private;
497 int i = 0;
498
499 // Locate the item
500 for ( i = 0; i < list->count; i ++ )
501 {
502 if ( !strcmp( list->name[ i ], source ) )
503 {
504 free( list->name[ i ] );
505 list->name[ i ] = strdup( dest );
506 list->hash[ generate_hash( dest ) ] = i + 1;
507 break;
508 }
509 }
510 }
511
512 return value != NULL;
513 }
514
515 /** Dump the properties.
516 */
517
518 void mlt_properties_dump( mlt_properties this, FILE *output )
519 {
520 property_list *list = this->private;
521 int i = 0;
522 for ( i = 0; i < list->count; i ++ )
523 fprintf( stderr, "%s = %s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
524 }
525
526 /** Close the list.
527 */
528
529 void mlt_properties_close( mlt_properties this )
530 {
531 property_list *list = this->private;
532 int index = 0;
533
534 // Clean up names and values
535 for ( index = list->count - 1; index >= 0; index -- )
536 {
537 free( list->name[ index ] );
538 mlt_property_close( list->value[ index ] );
539 }
540
541 // Clear up the list
542 free( list->name );
543 free( list->value );
544 free( list );
545
546 // Free this now if this has no child
547 if ( this->child == NULL )
548 free( this );
549 }
550