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