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, 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 /** Get an int from this property.
159 int mlt_property_get_int( mlt_property
this )
161 if ( this->types
& mlt_prop_int
)
162 return this->prop_int
;
163 else if ( this->types
& mlt_prop_double
)
164 return ( int )this->prop_double
;
165 else if ( this->types
& mlt_prop_position
)
166 return ( int )this->prop_position
;
167 else if ( this->types
& mlt_prop_int64
)
168 return ( int )this->prop_int64
;
169 else if ( this->types
& mlt_prop_string
)
170 return atoi( this->prop_string
);
174 /** Get a double from this property.
177 double mlt_property_get_double( mlt_property
this )
179 if ( this->types
& mlt_prop_double
)
180 return this->prop_double
;
181 else if ( this->types
& mlt_prop_int
)
182 return ( double )this->prop_int
;
183 else if ( this->types
& mlt_prop_position
)
184 return ( double )this->prop_position
;
185 else if ( this->types
& mlt_prop_int64
)
186 return ( double )this->prop_int64
;
187 else if ( this->types
& mlt_prop_string
)
188 return atof( this->prop_string
);
192 /** Get a position from this property.
195 mlt_position
mlt_property_get_position( mlt_property
this )
197 if ( this->types
& mlt_prop_position
)
198 return this->prop_position
;
199 else if ( this->types
& mlt_prop_int
)
200 return ( mlt_position
)this->prop_int
;
201 else if ( this->types
& mlt_prop_double
)
202 return ( mlt_position
)this->prop_double
;
203 else if ( this->types
& mlt_prop_int64
)
204 return ( mlt_position
)this->prop_int64
;
205 else if ( this->types
& mlt_prop_string
)
206 return ( mlt_position
)atol( this->prop_string
);
210 /** Get an int64 from this property.
213 int64_t mlt_property_get_int64( mlt_property
this )
215 if ( this->types
& mlt_prop_int64
)
216 return this->prop_int64
;
217 else if ( this->types
& mlt_prop_int
)
218 return ( int64_t )this->prop_int
;
219 else if ( this->types
& mlt_prop_double
)
220 return ( int64_t )this->prop_double
;
221 else if ( this->types
& mlt_prop_position
)
222 return ( int64_t )this->prop_position
;
223 else if ( this->types
& mlt_prop_string
)
224 return ( int64_t )atol( this->prop_string
);
228 /** Get a string from this property.
231 char *mlt_property_get_string( mlt_property
this )
233 // Construct a string if need be
234 if ( ! ( this->types
& mlt_prop_string
) )
236 if ( this->types
& mlt_prop_int
)
238 this->types
|= mlt_prop_string
;
239 this->prop_string
= malloc( 32 );
240 sprintf( this->prop_string
, "%d", this->prop_int
);
242 else if ( this->types
& mlt_prop_double
)
244 this->types
|= mlt_prop_string
;
245 this->prop_string
= malloc( 32 );
246 sprintf( this->prop_string
, "%f", this->prop_double
);
248 else if ( this->types
& mlt_prop_position
)
250 this->types
|= mlt_prop_string
;
251 this->prop_string
= malloc( 32 );
252 sprintf( this->prop_string
, "%d", this->prop_position
);
254 else if ( this->types
& mlt_prop_int64
)
256 this->types
|= mlt_prop_string
;
257 this->prop_string
= malloc( 32 );
258 sprintf( this->prop_string
, "%lld", this->prop_int64
);
260 else if ( this->types
& mlt_prop_data
&& this->serialiser
!= NULL
)
262 this->types
|= mlt_prop_string
;
263 this->prop_string
= this->serialiser( this->data
, this->length
);
267 // Return the string (may be NULL)
268 return this->prop_string
;
271 /** Get a data and associated length.
274 void *mlt_property_get_data( mlt_property
this, int *length
)
276 // Assign length if not NULL
277 if ( length
!= NULL
)
278 *length
= this->length
;
280 // Return the data (note: there is no conversion here)
284 /** Close this property.
287 void mlt_property_close( mlt_property
this )
289 mlt_property_clear( this );