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 mlt_property_clear( this );
116 this->types
= mlt_prop_string
;
118 this->prop_string
= strdup( value
);
119 return this->prop_string
== NULL
;
122 /** Set an int64 on this property.
125 int mlt_property_set_int64( mlt_property
this, int64_t value
)
127 mlt_property_clear( this );
128 this->types
= mlt_prop_int64
;
129 this->prop_int64
= value
;
133 /** Set a data on this property.
136 int mlt_property_set_data( mlt_property
this, void *value
, int length
, mlt_destructor destructor
, mlt_serialiser serialiser
)
138 if ( this->data
== value
)
139 this->destructor
= NULL
;
140 mlt_property_clear( this );
141 this->types
= mlt_prop_data
;
143 this->length
= length
;
144 this->destructor
= destructor
;
145 this->serialiser
= serialiser
;
149 /** Get an int from this property.
152 int mlt_property_get_int( mlt_property
this )
154 if ( this->types
& mlt_prop_int
)
155 return this->prop_int
;
156 else if ( this->types
& mlt_prop_double
)
157 return ( int )this->prop_double
;
158 else if ( this->types
& mlt_prop_position
)
159 return ( int )this->prop_position
;
160 else if ( this->types
& mlt_prop_int64
)
161 return ( int )this->prop_int64
;
162 else if ( this->types
& mlt_prop_string
)
163 return atoi( this->prop_string
);
167 /** Get a double from this property.
170 double mlt_property_get_double( mlt_property
this )
172 if ( this->types
& mlt_prop_double
)
173 return this->prop_double
;
174 else if ( this->types
& mlt_prop_int
)
175 return ( double )this->prop_int
;
176 else if ( this->types
& mlt_prop_position
)
177 return ( double )this->prop_position
;
178 else if ( this->types
& mlt_prop_int64
)
179 return ( double )this->prop_int64
;
180 else if ( this->types
& mlt_prop_string
)
181 return atof( this->prop_string
);
185 /** Get a position from this property.
188 mlt_position
mlt_property_get_position( mlt_property
this )
190 if ( this->types
& mlt_prop_position
)
191 return this->prop_position
;
192 else if ( this->types
& mlt_prop_int
)
193 return ( mlt_position
)this->prop_int
;
194 else if ( this->types
& mlt_prop_double
)
195 return ( mlt_position
)this->prop_double
;
196 else if ( this->types
& mlt_prop_int64
)
197 return ( mlt_position
)this->prop_int64
;
198 else if ( this->types
& mlt_prop_string
)
199 return ( mlt_position
)atol( this->prop_string
);
203 /** Get an int64 from this property.
206 int64_t mlt_property_get_int64( mlt_property
this )
208 if ( this->types
& mlt_prop_int64
)
209 return this->prop_int64
;
210 else if ( this->types
& mlt_prop_int
)
211 return ( int64_t )this->prop_int
;
212 else if ( this->types
& mlt_prop_double
)
213 return ( int64_t )this->prop_double
;
214 else if ( this->types
& mlt_prop_position
)
215 return ( int64_t )this->prop_position
;
216 else if ( this->types
& mlt_prop_string
)
217 return ( int64_t )atol( this->prop_string
);
221 /** Get a string from this property.
224 char *mlt_property_get_string( mlt_property
this )
226 // Construct a string if need be
227 if ( ! ( this->types
& mlt_prop_string
) )
229 if ( this->types
& mlt_prop_int
)
231 this->types
|= mlt_prop_string
;
232 this->prop_string
= malloc( 32 );
233 sprintf( this->prop_string
, "%d", this->prop_int
);
235 else if ( this->types
& mlt_prop_double
)
237 this->types
|= mlt_prop_string
;
238 this->prop_string
= malloc( 32 );
239 sprintf( this->prop_string
, "%f", this->prop_double
);
241 else if ( this->types
& mlt_prop_position
)
243 this->types
|= mlt_prop_string
;
244 this->prop_string
= malloc( 32 );
245 sprintf( this->prop_string
, "%d", this->prop_position
);
247 else if ( this->types
& mlt_prop_int64
)
249 this->types
|= mlt_prop_string
;
250 this->prop_string
= malloc( 32 );
251 sprintf( this->prop_string
, "%lld", this->prop_int64
);
253 else if ( this->types
& mlt_prop_data
&& this->serialiser
!= NULL
)
255 this->types
|= mlt_prop_string
;
256 this->prop_string
= this->serialiser( this->data
, this->length
);
260 // Return the string (may be NULL)
261 return this->prop_string
;
264 /** Get a data and associated length.
267 void *mlt_property_get_data( mlt_property
this, int *length
)
269 // Assign length if not NULL
270 if ( length
!= NULL
)
271 *length
= this->length
;
273 // Return the data (note: there is no conversion here)
277 /** Close this property.
280 void mlt_property_close( mlt_property
this )
282 mlt_property_clear( this );