move binary modules to libdir - affects MLT_REPOSITORY
[melted] / src / framework / mlt_property.c
1 /*
2 * mlt_property.c -- property class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "mlt_property.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 /** Construct and uninitialised property.
28 */
29
30 mlt_property mlt_property_init( )
31 {
32 mlt_property this = malloc( sizeof( struct mlt_property_s ) );
33 if ( this != NULL )
34 {
35 this->types = 0;
36 this->prop_int = 0;
37 this->prop_position = 0;
38 this->prop_double = 0;
39 this->prop_int64 = 0;
40 this->prop_string = NULL;
41 this->data = NULL;
42 this->length = 0;
43 this->destructor = NULL;
44 this->serialiser = NULL;
45 }
46 return this;
47 }
48
49 /** Clear a property.
50 */
51
52 static inline void mlt_property_clear( mlt_property this )
53 {
54 // Special case data handling
55 if ( this->types & mlt_prop_data && this->destructor != NULL )
56 this->destructor( this->data );
57
58 // Special case string handling
59 if ( this->types & mlt_prop_string )
60 free( this->prop_string );
61
62 // Wipe stuff
63 this->types = 0;
64 this->prop_int = 0;
65 this->prop_position = 0;
66 this->prop_double = 0;
67 this->prop_int64 = 0;
68 this->prop_string = NULL;
69 this->data = NULL;
70 this->length = 0;
71 this->destructor = NULL;
72 this->serialiser = NULL;
73 }
74
75 /** Set an int on this property.
76 */
77
78 int mlt_property_set_int( mlt_property this, int value )
79 {
80 mlt_property_clear( this );
81 this->types = mlt_prop_int;
82 this->prop_int = value;
83 return 0;
84 }
85
86 /** Set a double on this property.
87 */
88
89 int mlt_property_set_double( mlt_property this, double value )
90 {
91 mlt_property_clear( this );
92 this->types = mlt_prop_double;
93 this->prop_double = value;
94 return 0;
95 }
96
97 /** Set a position on this property.
98 */
99
100 int mlt_property_set_position( mlt_property this, mlt_position value )
101 {
102 mlt_property_clear( this );
103 this->types = mlt_prop_position;
104 this->prop_position = value;
105 return 0;
106 }
107
108 /** Set a string on this property.
109 */
110
111 int mlt_property_set_string( mlt_property this, const char *value )
112 {
113 if ( value != this->prop_string )
114 {
115 mlt_property_clear( this );
116 this->types = mlt_prop_string;
117 if ( value != NULL )
118 this->prop_string = strdup( value );
119 }
120 else
121 {
122 this->types = mlt_prop_string;
123 }
124 return this->prop_string == NULL;
125 }
126
127 /** Set an int64 on this property.
128 */
129
130 int mlt_property_set_int64( mlt_property this, int64_t value )
131 {
132 mlt_property_clear( this );
133 this->types = mlt_prop_int64;
134 this->prop_int64 = value;
135 return 0;
136 }
137
138 /** Set a data on this property.
139 */
140
141 int mlt_property_set_data( mlt_property this, void *value, int length, mlt_destructor destructor, mlt_serialiser serialiser )
142 {
143 if ( this->data == value )
144 this->destructor = NULL;
145 mlt_property_clear( this );
146 this->types = mlt_prop_data;
147 this->data = value;
148 this->length = length;
149 this->destructor = destructor;
150 this->serialiser = serialiser;
151 return 0;
152 }
153
154 static inline int mlt_property_atoi( const char *value )
155 {
156 if ( value == NULL )
157 return 0;
158 else if ( value[0] == '0' && value[1] == 'x' )
159 return strtol( value + 2, NULL, 16 );
160 else
161 return strtol( value, NULL, 10 );
162 }
163
164 /** Get an int from this property.
165 */
166
167 int mlt_property_get_int( mlt_property this )
168 {
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 );
179 return 0;
180 }
181
182 /** Get a double from this property.
183 */
184
185 double mlt_property_get_double( mlt_property this )
186 {
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 );
197 return 0;
198 }
199
200 /** Get a position from this property.
201 */
202
203 mlt_position mlt_property_get_position( mlt_property this )
204 {
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 );
215 return 0;
216 }
217
218 static inline int64_t mlt_property_atoll( const char *value )
219 {
220 if ( value == NULL )
221 return 0;
222 else if ( value[0] == '0' && value[1] == 'x' )
223 return strtoll( value + 2, NULL, 16 );
224 else
225 return strtoll( value, NULL, 10 );
226 }
227
228 /** Get an int64 from this property.
229 */
230
231 int64_t mlt_property_get_int64( mlt_property this )
232 {
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 );
243 return 0;
244 }
245
246 /** Get a string from this property.
247 */
248
249 char *mlt_property_get_string( mlt_property this )
250 {
251 // Construct a string if need be
252 if ( ! ( this->types & mlt_prop_string ) )
253 {
254 if ( this->types & mlt_prop_int )
255 {
256 this->types |= mlt_prop_string;
257 this->prop_string = malloc( 32 );
258 sprintf( this->prop_string, "%d", this->prop_int );
259 }
260 else if ( this->types & mlt_prop_double )
261 {
262 this->types |= mlt_prop_string;
263 this->prop_string = malloc( 32 );
264 sprintf( this->prop_string, "%f", this->prop_double );
265 }
266 else if ( this->types & mlt_prop_position )
267 {
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 */
271 }
272 else if ( this->types & mlt_prop_int64 )
273 {
274 this->types |= mlt_prop_string;
275 this->prop_string = malloc( 32 );
276 sprintf( this->prop_string, "%lld", this->prop_int64 );
277 }
278 else if ( this->types & mlt_prop_data && this->serialiser != NULL )
279 {
280 this->types |= mlt_prop_string;
281 this->prop_string = this->serialiser( this->data, this->length );
282 }
283 }
284
285 // Return the string (may be NULL)
286 return this->prop_string;
287 }
288
289 /** Get a data and associated length.
290 */
291
292 void *mlt_property_get_data( mlt_property this, int *length )
293 {
294 // Assign length if not NULL
295 if ( length != NULL )
296 *length = this->length;
297
298 // Return the data (note: there is no conversion here)
299 return this->data;
300 }
301
302 /** Close this property.
303 */
304
305 void mlt_property_close( mlt_property this )
306 {
307 mlt_property_clear( this );
308 free( this );
309 }
310
311 /** Pass the property 'that' to 'this'.
312 * Who to blame: Zach <zachary.drew@gmail.com>
313 */
314 void mlt_property_pass( mlt_property this, mlt_property that )
315 {
316 mlt_property_clear( this );
317
318 this->types = that->types;
319
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 )
329 {
330 if ( that->prop_string != NULL )
331 this->prop_string = strdup( that->prop_string );
332 }
333 else if ( this->types & mlt_prop_data && this->serialiser != NULL )
334 {
335 this->types = mlt_prop_string;
336 this->prop_string = this->serialiser( this->data, this->length );
337 }
338 }