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