Corrections after valgrinding
[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 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 /** Get an int from this property.
157 */
158
159 int mlt_property_get_int( mlt_property this )
160 {
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 );
171 return 0;
172 }
173
174 /** Get a double from this property.
175 */
176
177 double mlt_property_get_double( mlt_property this )
178 {
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 );
189 return 0;
190 }
191
192 /** Get a position from this property.
193 */
194
195 mlt_position mlt_property_get_position( mlt_property this )
196 {
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 );
207 return 0;
208 }
209
210 /** Get an int64 from this property.
211 */
212
213 int64_t mlt_property_get_int64( mlt_property this )
214 {
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 );
225 return 0;
226 }
227
228 /** Get a string from this property.
229 */
230
231 char *mlt_property_get_string( mlt_property this )
232 {
233 // Construct a string if need be
234 if ( ! ( this->types & mlt_prop_string ) )
235 {
236 if ( this->types & mlt_prop_int )
237 {
238 this->types |= mlt_prop_string;
239 this->prop_string = malloc( 32 );
240 sprintf( this->prop_string, "%d", this->prop_int );
241 }
242 else if ( this->types & mlt_prop_double )
243 {
244 this->types |= mlt_prop_string;
245 this->prop_string = malloc( 32 );
246 sprintf( this->prop_string, "%f", this->prop_double );
247 }
248 else if ( this->types & mlt_prop_position )
249 {
250 this->types |= mlt_prop_string;
251 this->prop_string = malloc( 32 );
252 sprintf( this->prop_string, "%d", this->prop_position );
253 }
254 else if ( this->types & mlt_prop_int64 )
255 {
256 this->types |= mlt_prop_string;
257 this->prop_string = malloc( 32 );
258 sprintf( this->prop_string, "%lld", this->prop_int64 );
259 }
260 else if ( this->types & mlt_prop_data && this->serialiser != NULL )
261 {
262 this->types |= mlt_prop_string;
263 this->prop_string = this->serialiser( this->data, this->length );
264 }
265 }
266
267 // Return the string (may be NULL)
268 return this->prop_string;
269 }
270
271 /** Get a data and associated length.
272 */
273
274 void *mlt_property_get_data( mlt_property this, int *length )
275 {
276 // Assign length if not NULL
277 if ( length != NULL )
278 *length = this->length;
279
280 // Return the data (note: there is no conversion here)
281 return this->data;
282 }
283
284 /** Close this property.
285 */
286
287 void mlt_property_close( mlt_property this )
288 {
289 mlt_property_clear( this );
290 free( this );
291 }
292
293