2 * mlt_property.c -- property 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.
23 #include "mlt_property.h"
29 /** Construct and uninitialised property.
32 mlt_property mlt_property_init( )
34 mlt_property this = malloc( sizeof( struct mlt_property_s ) );
39 this->prop_position = 0;
40 this->prop_double = 0;
42 this->prop_string = NULL;
45 this->destructor = NULL;
46 this->serialiser = NULL;
54 static inline void mlt_property_clear( mlt_property this )
56 // Special case data handling
57 if ( this->types & mlt_prop_data && this->destructor != NULL )
58 this->destructor( this->data );
60 // Special case string handling
61 if ( this->types & mlt_prop_string )
62 free( this->prop_string );
67 this->prop_position = 0;
68 this->prop_double = 0;
70 this->prop_string = NULL;
73 this->destructor = NULL;
74 this->serialiser = NULL;
77 /** Set an int on this property.
80 int mlt_property_set_int( mlt_property this, int value )
82 mlt_property_clear( this );
83 this->types = mlt_prop_int;
84 this->prop_int = value;
88 /** Set a double on this property.
91 int mlt_property_set_double( mlt_property this, double value )
93 mlt_property_clear( this );
94 this->types = mlt_prop_double;
95 this->prop_double = value;
99 /** Set a position on this property.
102 int mlt_property_set_position( mlt_property this, mlt_position value )
104 mlt_property_clear( this );
105 this->types = mlt_prop_position;
106 this->prop_position = value;
110 /** Set a string on this property.
113 int mlt_property_set_string( mlt_property this, const char *value )
115 if ( value != this->prop_string )
117 mlt_property_clear( this );
118 this->types = mlt_prop_string;
120 this->prop_string = strdup( value );
124 this->types = mlt_prop_string;
126 return this->prop_string == NULL;
129 /** Set an int64 on this property.
132 int mlt_property_set_int64( mlt_property this, int64_t value )
134 mlt_property_clear( this );
135 this->types = mlt_prop_int64;
136 this->prop_int64 = value;
140 /** Set a data on this property.
143 int mlt_property_set_data( mlt_property this, void *value, int length, mlt_destructor destructor, mlt_serialiser serialiser )
145 if ( this->data == value )
146 this->destructor = NULL;
147 mlt_property_clear( this );
148 this->types = mlt_prop_data;
150 this->length = length;
151 this->destructor = destructor;
152 this->serialiser = serialiser;
156 static inline int mlt_property_atoi( const char *value )
160 else if ( value[0] == '0' && value[1] == 'x' )
161 return strtol( value + 2, NULL, 16 );
163 return strtol( value, NULL, 10 );
166 /** Get an int from this property.
169 int mlt_property_get_int( mlt_property this )
171 if ( this->types & mlt_prop_int )
172 return this->prop_int;
173 else if ( this->types & mlt_prop_double )
174 return ( int )this->prop_double;
175 else if ( this->types & mlt_prop_position )
176 return ( int )this->prop_position;
177 else if ( this->types & mlt_prop_int64 )
178 return ( int )this->prop_int64;
179 else if ( this->types & mlt_prop_string )
180 return mlt_property_atoi( this->prop_string );
184 /** Get a double from this property.
187 double mlt_property_get_double( mlt_property this )
189 if ( this->types & mlt_prop_double )
190 return this->prop_double;
191 else if ( this->types & mlt_prop_int )
192 return ( double )this->prop_int;
193 else if ( this->types & mlt_prop_position )
194 return ( double )this->prop_position;
195 else if ( this->types & mlt_prop_int64 )
196 return ( double )this->prop_int64;
197 else if ( this->types & mlt_prop_string )
198 return atof( this->prop_string );
202 /** Get a position from this property.
205 mlt_position mlt_property_get_position( mlt_property this )
207 if ( this->types & mlt_prop_position )
208 return this->prop_position;
209 else if ( this->types & mlt_prop_int )
210 return ( mlt_position )this->prop_int;
211 else if ( this->types & mlt_prop_double )
212 return ( mlt_position )this->prop_double;
213 else if ( this->types & mlt_prop_int64 )
214 return ( mlt_position )this->prop_int64;
215 else if ( this->types & mlt_prop_string )
216 return ( mlt_position )atol( this->prop_string );
220 static inline int64_t mlt_property_atoll( const char *value )
224 else if ( value[0] == '0' && value[1] == 'x' )
225 return strtoll( value + 2, NULL, 16 );
227 return strtoll( value, NULL, 10 );
230 /** Get an int64 from this property.
233 int64_t mlt_property_get_int64( mlt_property this )
235 if ( this->types & mlt_prop_int64 )
236 return this->prop_int64;
237 else if ( this->types & mlt_prop_int )
238 return ( int64_t )this->prop_int;
239 else if ( this->types & mlt_prop_double )
240 return ( int64_t )this->prop_double;
241 else if ( this->types & mlt_prop_position )
242 return ( int64_t )this->prop_position;
243 else if ( this->types & mlt_prop_string )
244 return mlt_property_atoll( this->prop_string );
248 /** Get a string from this property.
251 char *mlt_property_get_string( mlt_property this )
253 // Construct a string if need be
254 if ( ! ( this->types & mlt_prop_string ) )
256 if ( this->types & mlt_prop_int )
258 this->types |= mlt_prop_string;
259 this->prop_string = malloc( 32 );
260 sprintf( this->prop_string, "%d", this->prop_int );
262 else if ( this->types & mlt_prop_double )
264 this->types |= mlt_prop_string;
265 this->prop_string = malloc( 32 );
266 sprintf( this->prop_string, "%f", this->prop_double );
268 else if ( this->types & mlt_prop_position )
270 this->types |= mlt_prop_string;
271 this->prop_string = malloc( 32 );
272 sprintf( this->prop_string, "%d", this->prop_position );
274 else if ( this->types & mlt_prop_int64 )
276 this->types |= mlt_prop_string;
277 this->prop_string = malloc( 32 );
278 sprintf( this->prop_string, "%lld", this->prop_int64 );
280 else if ( this->types & mlt_prop_data && this->serialiser != NULL )
282 this->types |= mlt_prop_string;
283 this->prop_string = this->serialiser( this->data, this->length );
287 // Return the string (may be NULL)
288 return this->prop_string;
291 /** Get a data and associated length.
294 void *mlt_property_get_data( mlt_property this, int *length )
296 // Assign length if not NULL
297 if ( length != NULL )
298 *length = this->length;
300 // Return the data (note: there is no conversion here)
304 /** Close this property.
307 void mlt_property_close( mlt_property this )
309 mlt_property_clear( this );