more small optimisations
[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 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, char *value )
114 {
115 mlt_property_clear( this );
116 this->types = mlt_prop_string;
117 if ( value != NULL )
118 this->prop_string = strdup( value );
119 return this->prop_string != NULL;
120 }
121
122 /** Set an int64 on this property.
123 */
124
125 int mlt_property_set_int64( mlt_property this, int64_t value )
126 {
127 mlt_property_clear( this );
128 this->types = mlt_prop_int64;
129 this->prop_int64 = value;
130 return 0;
131 }
132
133 /** Set a data on this property.
134 */
135
136 int mlt_property_set_data( mlt_property this, void *value, int length, mlt_destructor destructor, mlt_serialiser serialiser )
137 {
138 if ( this->data == value )
139 this->destructor = NULL;
140 mlt_property_clear( this );
141 this->types = mlt_prop_data;
142 this->data = value;
143 this->length = length;
144 this->destructor = destructor;
145 this->serialiser = serialiser;
146 return 0;
147 }
148
149 /** Get an int from this property.
150 */
151
152 int mlt_property_get_int( mlt_property this )
153 {
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 );
164 return 0;
165 }
166
167 /** Get a double from this property.
168 */
169
170 double mlt_property_get_double( mlt_property this )
171 {
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 );
182 return 0;
183 }
184
185 /** Get a position from this property.
186 */
187
188 mlt_position mlt_property_get_position( mlt_property this )
189 {
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 );
200 return 0;
201 }
202
203 /** Get an int64 from this property.
204 */
205
206 int64_t mlt_property_get_int64( mlt_property this )
207 {
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 );
218 return 0;
219 }
220
221 /** Get a string from this property.
222 */
223
224 char *mlt_property_get_string( mlt_property this )
225 {
226 // Construct a string if need be
227 if ( ! ( this->types & mlt_prop_string ) )
228 {
229 if ( this->types & mlt_prop_int )
230 {
231 this->types |= mlt_prop_string;
232 this->prop_string = malloc( 32 );
233 sprintf( this->prop_string, "%d", this->prop_int );
234 }
235 else if ( this->types & mlt_prop_double )
236 {
237 this->types |= mlt_prop_string;
238 this->prop_string = malloc( 32 );
239 sprintf( this->prop_string, "%e", this->prop_double );
240 }
241 else if ( this->types & mlt_prop_position )
242 {
243 this->types |= mlt_prop_string;
244 this->prop_string = malloc( 32 );
245 sprintf( this->prop_string, "%d", this->prop_position );
246 }
247 else if ( this->types & mlt_prop_int64 )
248 {
249 this->types |= mlt_prop_string;
250 this->prop_string = malloc( 32 );
251 sprintf( this->prop_string, "%lld", this->prop_int64 );
252 }
253 else if ( this->types & mlt_prop_data && this->serialiser != NULL )
254 {
255 this->types |= mlt_prop_string;
256 this->prop_string = this->serialiser( this->data, this->length );
257 }
258 }
259
260 // Return the string (may be NULL)
261 return this->prop_string;
262 }
263
264 /** Get a data and associated length.
265 */
266
267 void *mlt_property_get_data( mlt_property this, int *length )
268 {
269 // Assign length if not NULL
270 if ( length != NULL )
271 *length = this->length;
272
273 // Return the data (note: there is no conversion here)
274 return this->data;
275 }
276
277 /** Close this property.
278 */
279
280 void mlt_property_close( mlt_property this )
281 {
282 mlt_property_clear( this );
283 free( this );
284 }
285
286