d047da0c2ae503d23fd2c32d64695ca5e3e6717b
2 * mlt_properties.c -- base properties class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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.
22 #include "mlt_properties.h"
23 #include "mlt_property.h"
29 /* ---------------- // Private Implementation // ---------------- */
31 /** Private implementation of the property list.
43 /** Basic implementation.
46 int mlt_properties_init( mlt_properties
this, void *child
)
49 memset( this, 0, sizeof( struct mlt_properties_s
) );
51 // Assign the child of the object
54 // Allocate the private structure
55 this->private = calloc( sizeof( property_list
), 1 );
57 return this->private == NULL
;
60 /** Constructor for stand alone object.
63 mlt_properties
mlt_properties_new( )
65 // Construct a standalone properties object
66 mlt_properties
this = calloc( sizeof( struct mlt_properties_s
), 1 );
69 mlt_properties_init( this, NULL
);
75 /** Inherit all serialisable properties from that into this.
78 int mlt_properties_inherit( mlt_properties
this, mlt_properties that
)
80 int count
= mlt_properties_count( that
);
83 char *value
= mlt_properties_get_value( that
, count
);
86 char *name
= mlt_properties_get_name( that
, count
);
87 mlt_properties_set( this, name
, value
);
93 /** Locate a property by name
96 static mlt_property
mlt_properties_find( mlt_properties
this, char *name
)
98 mlt_property value
= NULL
;
99 property_list
*list
= this->private;
103 for ( i
= 0; value
== NULL
&& i
< list
->count
; i
++ )
104 if ( !strcmp( list
->name
[ i
], name
) )
105 value
= list
->value
[ i
];
110 /** Add a new property.
113 static mlt_property
mlt_properties_add( mlt_properties
this, char *name
)
115 property_list
*list
= this->private;
117 // Check that we have space and resize if necessary
118 if ( list
->count
== list
->size
)
121 list
->name
= realloc( list
->name
, list
->size
* sizeof( char * ) );
122 list
->value
= realloc( list
->value
, list
->size
* sizeof( mlt_property
) );
125 // Assign name/value pair
126 list
->name
[ list
->count
] = strdup( name
);
127 list
->value
[ list
->count
] = mlt_property_init( );
129 // Return and increment count accordingly
130 return list
->value
[ list
->count
++ ];
133 /** Fetch a property by name - this includes add if not found.
136 static mlt_property
mlt_properties_fetch( mlt_properties
this, char *name
)
138 // Try to find an existing property first
139 mlt_property property
= mlt_properties_find( this, name
);
141 // If it wasn't found, create one
142 if ( property
== NULL
)
143 property
= mlt_properties_add( this, name
);
145 // Return the property
149 /** Set the property.
152 int mlt_properties_set( mlt_properties
this, char *name
, char *value
)
156 // Fetch the property to work with
157 mlt_property property
= mlt_properties_fetch( this, name
);
159 // Set it if not NULL
160 if ( property
!= NULL
)
161 error
= mlt_property_set_string( property
, value
);
166 /** Get a string value by name.
169 char *mlt_properties_get( mlt_properties
this, char *name
)
171 mlt_property value
= mlt_properties_find( this, name
);
172 return value
== NULL ? NULL
: mlt_property_get_string( value
);
175 /** Get a name by index.
178 char *mlt_properties_get_name( mlt_properties
this, int index
)
180 property_list
*list
= this->private;
181 if ( index
>= 0 && index
< list
->count
)
182 return list
->name
[ index
];
186 /** Get a string value by index.
189 char *mlt_properties_get_value( mlt_properties
this, int index
)
191 property_list
*list
= this->private;
192 if ( index
>= 0 && index
< list
->count
)
193 return mlt_property_get_string( list
->value
[ index
] );
197 /** Return the number of items in the list.
200 int mlt_properties_count( mlt_properties
this )
202 property_list
*list
= this->private;
206 /** Set a value by parsing a name=value string
209 int mlt_properties_parse( mlt_properties
this, char *namevalue
)
211 char *name
= strdup( namevalue
);
212 char *value
= strdup( namevalue
);
215 if ( strchr( name
, '=' ) )
217 *( strchr( name
, '=' ) ) = '\0';
218 strcpy( value
, strchr( value
, '=' ) + 1 );
225 error
= mlt_properties_set( this, name
, value
);
233 /** Get a value associated to the name.
236 int mlt_properties_get_int( mlt_properties
this, char *name
)
238 mlt_property value
= mlt_properties_find( this, name
);
239 return value
== NULL ?
0 : mlt_property_get_int( value
);
242 /** Set a value associated to the name.
245 int mlt_properties_set_int( mlt_properties
this, char *name
, int value
)
249 // Fetch the property to work with
250 mlt_property property
= mlt_properties_fetch( this, name
);
252 // Set it if not NULL
253 if ( property
!= NULL
)
254 error
= mlt_property_set_int( property
, value
);
259 /** Get a value associated to the name.
262 double mlt_properties_get_double( mlt_properties
this, char *name
)
264 mlt_property value
= mlt_properties_find( this, name
);
265 return value
== NULL ?
0 : mlt_property_get_double( value
);
268 /** Set a value associated to the name.
271 int mlt_properties_set_double( mlt_properties
this, char *name
, double value
)
275 // Fetch the property to work with
276 mlt_property property
= mlt_properties_fetch( this, name
);
278 // Set it if not NULL
279 if ( property
!= NULL
)
280 error
= mlt_property_set_double( property
, value
);
285 /** Get a value associated to the name.
288 mlt_position
mlt_properties_get_position( mlt_properties
this, char *name
)
290 mlt_property value
= mlt_properties_find( this, name
);
291 return value
== NULL ?
0 : mlt_property_get_position( value
);
294 /** Set a value associated to the name.
297 int mlt_properties_set_position( mlt_properties
this, char *name
, mlt_position value
)
301 // Fetch the property to work with
302 mlt_property property
= mlt_properties_fetch( this, name
);
304 // Set it if not NULL
305 if ( property
!= NULL
)
306 error
= mlt_property_set_position( property
, value
);
311 /** Get a value associated to the name.
314 void *mlt_properties_get_data( mlt_properties
this, char *name
, int *length
)
316 mlt_property value
= mlt_properties_find( this, name
);
317 return value
== NULL ? NULL
: mlt_property_get_data( value
, length
);
320 /** Set a value associated to the name.
323 int mlt_properties_set_data( mlt_properties
this, char *name
, void *value
, int length
, mlt_destructor destroy
, mlt_serialiser serialise
)
327 // Fetch the property to work with
328 mlt_property property
= mlt_properties_fetch( this, name
);
330 // Set it if not NULL
331 if ( property
!= NULL
)
332 error
= mlt_property_set_data( property
, value
, length
, destroy
, serialise
);
340 void mlt_properties_close( mlt_properties
this )
342 property_list
*list
= this->private;
345 int debug
= mlt_properties_get_int( this, "debug" );
347 // Clean up names and values
348 for ( index
= 0; index
< list
->count
; index
++ )
351 fprintf( stderr
, "closing %s\n", list
->name
[ index
] );
352 free( list
->name
[ index
] );
353 mlt_property_close( list
->value
[ index
] );
361 // Free this now if this has no child
362 if ( this->child
== NULL
)