2 * mlt_property.c -- property class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "mlt_property.h"
27 /** Construct and uninitialised property.
30 mlt_property mlt_property_init( )
32 mlt_property this = malloc( sizeof( struct mlt_property_s ) );
37 this->prop_position = 0;
38 this->prop_double = 0;
40 this->prop_string = NULL;
43 this->destructor = NULL;
44 this->serialiser = NULL;
52 static inline void mlt_property_clear( mlt_property this )
54 // Special case data handling
55 if ( this->types & mlt_prop_data && this->destructor != NULL )
56 this->destructor( this->data );
58 // Special case string handling
59 if ( this->types & mlt_prop_string )
60 free( this->prop_string );
65 this->prop_position = 0;
66 this->prop_double = 0;
68 this->prop_string = NULL;
71 this->destructor = NULL;
72 this->serialiser = NULL;
75 /** Set an int on this property.
78 int mlt_property_set_int( mlt_property this, int value )
80 mlt_property_clear( this );
81 this->types = mlt_prop_int;
82 this->prop_int = value;
86 /** Set a double on this property.
89 int mlt_property_set_double( mlt_property this, double value )
91 mlt_property_clear( this );
92 this->types = mlt_prop_double;
93 this->prop_double = value;
97 /** Set a position on this property.
100 int mlt_property_set_position( mlt_property this, mlt_position value )
102 mlt_property_clear( this );
103 this->types = mlt_prop_position;
104 this->prop_position = value;
108 /** Set a string on this property.
111 int mlt_property_set_string( mlt_property this, const char *value )
113 if ( value != this->prop_string )
115 mlt_property_clear( this );
116 this->types = mlt_prop_string;
118 this->prop_string = strdup( value );
122 this->types = mlt_prop_string;
124 return this->prop_string == NULL;
127 /** Set an int64 on this property.
130 int mlt_property_set_int64( mlt_property this, int64_t value )
132 mlt_property_clear( this );
133 this->types = mlt_prop_int64;
134 this->prop_int64 = value;
138 /** Set a data on this property.
141 int mlt_property_set_data( mlt_property this, void *value, int length, mlt_destructor destructor, mlt_serialiser serialiser )
143 if ( this->data == value )
144 this->destructor = NULL;
145 mlt_property_clear( this );
146 this->types = mlt_prop_data;
148 this->length = length;
149 this->destructor = destructor;
150 this->serialiser = serialiser;
154 static inline int mlt_property_atoi( const char *value )
158 else if ( value[0] == '0' && value[1] == 'x' )
159 return strtol( value + 2, NULL, 16 );
161 return strtol( value, NULL, 10 );
164 /** Get an int from this property.
167 int mlt_property_get_int( mlt_property this )
169 if ( this->types & mlt_prop_int )
170 return this->prop_int;
171 else if ( this->types & mlt_prop_double )
172 return ( int )this->prop_double;
173 else if ( this->types & mlt_prop_position )
174 return ( int )this->prop_position;
175 else if ( this->types & mlt_prop_int64 )
176 return ( int )this->prop_int64;
177 else if ( this->types & mlt_prop_string )
178 return mlt_property_atoi( this->prop_string );
182 /** Get a double from this property.
185 double mlt_property_get_double( mlt_property this )
187 if ( this->types & mlt_prop_double )
188 return this->prop_double;
189 else if ( this->types & mlt_prop_int )
190 return ( double )this->prop_int;
191 else if ( this->types & mlt_prop_position )
192 return ( double )this->prop_position;
193 else if ( this->types & mlt_prop_int64 )
194 return ( double )this->prop_int64;
195 else if ( this->types & mlt_prop_string )
196 return atof( this->prop_string );
200 /** Get a position from this property.
203 mlt_position mlt_property_get_position( mlt_property this )
205 if ( this->types & mlt_prop_position )
206 return this->prop_position;
207 else if ( this->types & mlt_prop_int )
208 return ( mlt_position )this->prop_int;
209 else if ( this->types & mlt_prop_double )
210 return ( mlt_position )this->prop_double;
211 else if ( this->types & mlt_prop_int64 )
212 return ( mlt_position )this->prop_int64;
213 else if ( this->types & mlt_prop_string )
214 return ( mlt_position )atol( this->prop_string );
218 static inline int64_t mlt_property_atoll( const char *value )
222 else if ( value[0] == '0' && value[1] == 'x' )
223 return strtoll( value + 2, NULL, 16 );
225 return strtoll( value, NULL, 10 );
228 /** Get an int64 from this property.
231 int64_t mlt_property_get_int64( mlt_property this )
233 if ( this->types & mlt_prop_int64 )
234 return this->prop_int64;
235 else if ( this->types & mlt_prop_int )
236 return ( int64_t )this->prop_int;
237 else if ( this->types & mlt_prop_double )
238 return ( int64_t )this->prop_double;
239 else if ( this->types & mlt_prop_position )
240 return ( int64_t )this->prop_position;
241 else if ( this->types & mlt_prop_string )
242 return mlt_property_atoll( this->prop_string );
246 /** Get a string from this property.
249 char *mlt_property_get_string( mlt_property this )
251 // Construct a string if need be
252 if ( ! ( this->types & mlt_prop_string ) )
254 if ( this->types & mlt_prop_int )
256 this->types |= mlt_prop_string;
257 this->prop_string = malloc( 32 );
258 sprintf( this->prop_string, "%d", this->prop_int );
260 else if ( this->types & mlt_prop_double )
262 this->types |= mlt_prop_string;
263 this->prop_string = malloc( 32 );
264 sprintf( this->prop_string, "%f", this->prop_double );
266 else if ( this->types & mlt_prop_position )
268 this->types |= mlt_prop_string;
269 this->prop_string = malloc( 32 );
270 sprintf( this->prop_string, "%d", (int)this->prop_position ); /* I don't know if this is wanted. -Zach */
272 else if ( this->types & mlt_prop_int64 )
274 this->types |= mlt_prop_string;
275 this->prop_string = malloc( 32 );
276 sprintf( this->prop_string, "%lld", this->prop_int64 );
278 else if ( this->types & mlt_prop_data && this->serialiser != NULL )
280 this->types |= mlt_prop_string;
281 this->prop_string = this->serialiser( this->data, this->length );
285 // Return the string (may be NULL)
286 return this->prop_string;
289 /** Get a data and associated length.
292 void *mlt_property_get_data( mlt_property this, int *length )
294 // Assign length if not NULL
295 if ( length != NULL )
296 *length = this->length;
298 // Return the data (note: there is no conversion here)
302 /** Close this property.
305 void mlt_property_close( mlt_property this )
307 mlt_property_clear( this );
311 /** Pass the property 'that' to 'this'.
312 * Who to blame: Zach <zachary.drew@gmail.com>
314 void mlt_property_pass( mlt_property this, mlt_property that )
316 mlt_property_clear( this );
318 this->types = that->types;
320 if ( this->types & mlt_prop_int64 )
321 this->prop_int64 = that->prop_int64;
322 else if ( this->types & mlt_prop_int )
323 this->prop_int = that->prop_int;
324 else if ( this->types & mlt_prop_double )
325 this->prop_double = that->prop_double;
326 else if ( this->types & mlt_prop_position )
327 this->prop_position = that->prop_position;
328 else if ( this->types & mlt_prop_string )
330 if ( that->prop_string != NULL )
331 this->prop_string = strdup( that->prop_string );
333 else if ( this->types & mlt_prop_data && this->serialiser != NULL )
335 this->types = mlt_prop_string;
336 this->prop_string = this->serialiser( this->data, this->length );