Rudimentary arithmetic property assignment
[melted] / src / framework / mlt_properties.c
index 27f4a27..895be28 100644 (file)
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
 
 /* ---------------- // Private Implementation // ---------------- */
 
 
 typedef struct
 {
+       int hash[ 199 ];
        char **name;
        mlt_property *value;
        int count;
        int size;
+       mlt_properties mirror;
 }
 property_list;
 
+/** Memory leak checks.
+*/
+
+#ifdef _MLT_PROPERTY_CHECKS_
+static int properties_created = 0;
+static int properties_destroyed = 0;
+#endif
+
 /** Basic implementation.
 */
 
 int mlt_properties_init( mlt_properties this, void *child )
 {
-       // NULL all methods
-       memset( this, 0, sizeof( struct mlt_properties_s ) );
+       if ( this != NULL )
+       {
+#ifdef _MLT_PROPERTY_CHECKS_
+               // Increment number of properties created
+               properties_created ++;
+#endif
+
+               // NULL all methods
+               memset( this, 0, sizeof( struct mlt_properties_s ) );
 
-       // Assign the child of the object
-       this->child = child;
+               // Assign the child of the object
+               this->child = child;
 
-       // Allocate the private structure
-       this->private = calloc( sizeof( property_list ), 1 );
+               // Allocate the local structure
+               this->local = calloc( sizeof( property_list ), 1 );
+       }
 
-       return this->private == NULL;
+       // Check that initialisation was successful
+       return this != NULL && this->local == NULL;
 }
 
 /** Constructor for stand alone object.
@@ -72,19 +92,67 @@ mlt_properties mlt_properties_new( )
        return this;
 }
 
+/** Load properties from a file.
+*/
+
+mlt_properties mlt_properties_load( char *filename )
+{
+       // Construct a standalone properties object
+       mlt_properties this = mlt_properties_new( );
+
+       if ( this != NULL )
+       {
+               // Open the file
+               FILE *file = fopen( filename, "r" );
+
+               // Load contents of file
+               if ( file != NULL )
+               {
+                       // Temp string
+                       char temp[ 1024 ];
+
+                       // Read each string from the file
+                       while( fgets( temp, 1024, file ) )
+                       {
+                               // Chomp the string
+                               temp[ strlen( temp ) - 1 ] = '\0';
+
+                               // Parse and set the property
+                               if ( strcmp( temp, "" ) && temp[ 0 ] != '#' )
+                                       mlt_properties_parse( this, temp );
+                       }
+
+                       // Close the file
+                       fclose( file );
+               }
+       }
+
+       // Return the pointer
+       return this;
+}
+
+static inline int generate_hash( char *name )
+{
+       int hash = 0;
+       int i = 1;
+       while ( *name )
+               hash = ( hash + ( i ++ * ( *name ++ & 31 ) ) ) % 199;
+       return hash;
+}
+
 /** Special case - when a container (such as fezzik) is protecting another 
        producer, we need to ensure that properties are passed through to the
        real producer.
 */
 
-static void mlt_properties_do_mirror( mlt_properties this, char *name )
+static inline void mlt_properties_do_mirror( mlt_properties this, char *name )
 {
-       mlt_properties mirror = mlt_properties_get_data( this, "mlt_mirror", NULL );
-       if ( mirror != NULL )
+       property_list *list = this->local;
+       if ( list->mirror != NULL ) 
        {
                char *value = mlt_properties_get( this, name );
                if ( value != NULL )
-                       mlt_properties_set( mirror, name, value );
+                       mlt_properties_set( list->mirror, name, value );
        }
 }
 
@@ -93,7 +161,8 @@ static void mlt_properties_do_mirror( mlt_properties this, char *name )
 
 void mlt_properties_mirror( mlt_properties this, mlt_properties that )
 {
-       mlt_properties_set_data( this, "mlt_mirror", that, 0, NULL, NULL );
+       property_list *list = this->local;
+       list->mirror = that;
 }
 
 /** Inherit all serialisable properties from that into this.
@@ -102,32 +171,64 @@ void mlt_properties_mirror( mlt_properties this, mlt_properties that )
 int mlt_properties_inherit( mlt_properties this, mlt_properties that )
 {
        int count = mlt_properties_count( that );
-       while ( count -- )
+       int i = 0;
+       for ( i = 0; i < count; i ++ )
        {
-               char *value = mlt_properties_get_value( that, count );
+               char *value = mlt_properties_get_value( that, i );
                if ( value != NULL )
                {
-                       char *name = mlt_properties_get_name( that, count );
+                       char *name = mlt_properties_get_name( that, i );
                        mlt_properties_set( this, name, value );
                }
        }
        return 0;
 }
 
+/** Pass all properties from 'that' that match the prefix to 'this' (excluding the prefix).
+*/
+
+int mlt_properties_pass( mlt_properties this, mlt_properties that, char *prefix )
+{
+       int count = mlt_properties_count( that );
+       int length = strlen( prefix );
+       int i = 0;
+       for ( i = 0; i < count; i ++ )
+       {
+               char *name = mlt_properties_get_name( that, i );
+               if ( !strncmp( name, prefix, length ) )
+               {
+                       char *value = mlt_properties_get_value( that, i );
+                       if ( value != NULL )
+                               mlt_properties_set( this, name + length, value );
+               }
+       }
+       return 0;
+}
+
 /** Locate a property by name
 */
 
-static mlt_property mlt_properties_find( mlt_properties this, char *name )
+static inline mlt_property mlt_properties_find( mlt_properties this, char *name )
 {
+       property_list *list = this->local;
        mlt_property value = NULL;
-       property_list *list = this->private;
-       int i = 0;
+       int key = generate_hash( name );
+       int i = list->hash[ key ] - 1;
 
-       // Locate the item 
-       for ( i = 0; value == NULL && i < list->count; i ++ )
-               if ( !strcmp( list->name[ i ], name ) )
+       if ( i >= 0 )
+       {
+               // Check if we're hashed
+               if ( list->count > 0 &&
+                       name[ 0 ] == list->name[ i ][ 0 ] && 
+                       !strcmp( list->name[ i ], name ) )
                        value = list->value[ i ];
 
+               // Locate the item 
+               for ( i = list->count - 1; value == NULL && i >= 0; i -- )
+                       if ( name[ 0 ] == list->name[ i ][ 0 ] && !strcmp( list->name[ i ], name ) )
+                               value = list->value[ i ];
+       }
+
        return value;
 }
 
@@ -136,12 +237,13 @@ static mlt_property mlt_properties_find( mlt_properties this, char *name )
 
 static mlt_property mlt_properties_add( mlt_properties this, char *name )
 {
-       property_list *list = this->private;
+       property_list *list = this->local;
+       int key = generate_hash( name );
 
        // Check that we have space and resize if necessary
        if ( list->count == list->size )
        {
-               list->size += 10;
+               list->size += 50;
                list->name = realloc( list->name, list->size * sizeof( char * ) );
                list->value = realloc( list->value, list->size * sizeof( mlt_property ) );
        }
@@ -150,6 +252,10 @@ static mlt_property mlt_properties_add( mlt_properties this, char *name )
        list->name[ list->count ] = strdup( name );
        list->value[ list->count ] = mlt_property_init( );
 
+       // Assign to hash table
+       if ( list->hash[ key ] == 0 )
+               list->hash[ key ] = list->count + 1;
+
        // Return and increment count accordingly
        return list->value[ list->count ++ ];
 }
@@ -181,15 +287,71 @@ int mlt_properties_set( mlt_properties this, char *name, char *value )
        mlt_property property = mlt_properties_fetch( this, name );
 
        // Set it if not NULL
-       if ( property != NULL )
+       if ( property != NULL && ( value == NULL || value[ 0 ] != '@' ) )
        {
                error = mlt_property_set_string( property, value );
                mlt_properties_do_mirror( this, name );
        }
+       else if ( property != NULL && value[ 0 ] == '@' )
+       {
+               int total = 0;
+               int current = 0;
+               char id[ 255 ];
+               char op = '+';
+
+               value ++;
+
+               while ( *value != '\0' )
+               {
+                       int length = strcspn( value, "+-*/" );
+
+                       // Get the identifier
+                       strncpy( id, value, length );
+                       id[ length ] = '\0';
+                       value += length;
+
+                       // Determine the value
+                       if ( isdigit( id[ 0 ] ) )
+                               current = atof( id );
+                       else
+                               current = mlt_properties_get_int( this, id );
+
+                       // Apply the operation
+                       switch( op )
+                       {
+                               case '+':
+                                       total += current;
+                                       break;
+                               case '-':
+                                       total -= current;
+                                       break;
+                               case '*':
+                                       total *= current;
+                                       break;
+                               case '/':
+                                       total /= current;
+                                       break;
+                       }
+
+                       // Get the next op
+                       op = *value != '\0' ? *value ++ : ' ';
+               }
+
+               error = mlt_property_set_int( property, total );
+               mlt_properties_do_mirror( this, name );
+       }
 
        return error;
 }
 
+/** Set or default the property.
+*/
+
+int mlt_properties_set_or_default( mlt_properties this, char *name, char *value, char *def )
+{
+       return mlt_properties_set( this, name, value == NULL ? def : value );
+}
+
 /** Get a string value by name.
 */
 
@@ -204,7 +366,7 @@ char *mlt_properties_get( mlt_properties this, char *name )
 
 char *mlt_properties_get_name( mlt_properties this, int index )
 {
-       property_list *list = this->private;
+       property_list *list = this->local;
        if ( index >= 0 && index < list->count )
                return list->name[ index ];
        return NULL;
@@ -215,18 +377,29 @@ char *mlt_properties_get_name( mlt_properties this, int index )
 
 char *mlt_properties_get_value( mlt_properties this, int index )
 {
-       property_list *list = this->private;
+       property_list *list = this->local;
        if ( index >= 0 && index < list->count )
                return mlt_property_get_string( list->value[ index ] );
        return NULL;
 }
 
+/** Get a data value by index.
+*/
+
+void *mlt_properties_get_data_at( mlt_properties this, int index, int *size )
+{
+       property_list *list = this->local;
+       if ( index >= 0 && index < list->count )
+               return mlt_property_get_data( list->value[ index ], size );
+       return NULL;
+}
+
 /** Return the number of items in the list.
 */
 
 int mlt_properties_count( mlt_properties this )
 {
-       property_list *list = this->private;
+       property_list *list = this->local;
        return list->count;
 }
 
@@ -236,17 +409,29 @@ int mlt_properties_count( mlt_properties this )
 int mlt_properties_parse( mlt_properties this, char *namevalue )
 {
        char *name = strdup( namevalue );
-       char *value = strdup( namevalue );
+       char *value = NULL;
        int error = 0;
+       char *ptr = strchr( name, '=' );
 
-       if ( strchr( name, '=' ) )
+       if ( ptr )
        {
-               *( strchr( name, '=' ) ) = '\0';
-               strcpy( value, strchr( value, '=' ) + 1 );
+               *( ptr ++ ) = '\0';
+
+               if ( *ptr != '\"' )
+               {
+                       value = strdup( ptr );
+               }
+               else
+               {
+                       ptr ++;
+                       value = strdup( ptr );
+                       if ( value != NULL && value[ strlen( value ) - 1 ] == '\"' )
+                               value[ strlen( value ) - 1 ] = '\0';
+               }
        }
        else
        {
-               strcpy( value, "" );
+               value = strdup( "" );
        }
 
        error = mlt_properties_set( this, name, value );
@@ -370,28 +555,79 @@ int mlt_properties_set_data( mlt_properties this, char *name, void *value, int l
        return error;
 }
 
-/** Close the list.
+/** Rename a property.
 */
 
-void mlt_properties_close( mlt_properties this )
+int mlt_properties_rename( mlt_properties this, char *source, char *dest )
 {
-       property_list *list = this->private;
-       int index = 0;
+       mlt_property value = mlt_properties_find( this, dest );
 
-       // Clean up names and values
-       for ( index = list->count - 1; index >= 0; index -- )
+       if ( value == NULL )
        {
-               free( list->name[ index ] );
-               mlt_property_close( list->value[ index ] );
+               property_list *list = this->local;
+               int i = 0;
+
+               // Locate the item 
+               for ( i = 0; i < list->count; i ++ )
+               {
+                       if ( !strcmp( list->name[ i ], source ) )
+                       {
+                               free( list->name[ i ] );
+                               list->name[ i ] = strdup( dest );
+                               list->hash[ generate_hash( dest ) ] = i + 1;
+                               break;
+                       }
+               }
        }
 
-       // Clear up the list
-       free( list->name );
-       free( list->value );
-       free( list );
+       return value != NULL;
+}
+
+/** Dump the properties.
+*/
+
+void mlt_properties_dump( mlt_properties this, FILE *output )
+{
+       property_list *list = this->local;
+       int i = 0;
+       for ( i = 0; i < list->count; i ++ )
+               if ( mlt_properties_get( this, list->name[ i ] ) != NULL )
+                       fprintf( output, "%s=%s\n", list->name[ i ], mlt_properties_get( this, list->name[ i ] ) );
+}
+
+/** Close the list.
+*/
 
-       // Free this now if this has no child
-       if ( this->child == NULL )
-               free( this );
+void mlt_properties_close( mlt_properties this )
+{
+       if ( this != NULL )
+       {
+               property_list *list = this->local;
+               int index = 0;
+
+               // Clean up names and values
+               for ( index = list->count - 1; index >= 0; index -- )
+               {
+                       free( list->name[ index ] );
+                       mlt_property_close( list->value[ index ] );
+               }
+
+               // Clear up the list
+               free( list->name );
+               free( list->value );
+               free( list );
+
+               // Free this now if this has no child
+               if ( this->child == NULL )
+                       free( this );
+
+#ifdef _MLT_PROPERTY_CHECKS_
+               // Increment destroyed count
+               properties_destroyed ++;
+
+               // Show current stats - these should match when the app is closed
+               fprintf( stderr, "Created %d, destroyed %d\n", properties_created, properties_destroyed );
+#endif
+       }
 }