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 return calloc( 1, sizeof( struct mlt_property_s
) );
40 void mlt_property_clear( mlt_property
this )
42 // Special case data handling
43 if ( this->types
& mlt_prop_data
&& this->destructor
!= NULL
)
44 this->destructor( this->data
);
46 // Special case string handling
47 if ( this->types
& mlt_prop_string
)
48 free( this->prop_string
);
50 // We can wipe it now.
51 memset( this, 0, sizeof( struct mlt_property_s
) );
54 /** Set an int on this property.
57 int mlt_property_set_int( mlt_property
this, int value
)
59 mlt_property_clear( this );
60 this->types
= mlt_prop_int
;
61 this->prop_int
= value
;
65 /** Set a double on this property.
68 int mlt_property_set_double( mlt_property
this, double value
)
70 mlt_property_clear( this );
71 this->types
= mlt_prop_double
;
72 this->prop_double
= value
;
76 /** Set a position on this property.
79 int mlt_property_set_position( mlt_property
this, mlt_position value
)
81 mlt_property_clear( this );
82 this->types
= mlt_prop_position
;
83 this->prop_position
= value
;
87 /** Set a string on this property.
90 int mlt_property_set_string( mlt_property
this, char *value
)
92 mlt_property_clear( this );
93 this->types
= mlt_prop_string
;
95 this->prop_string
= strdup( value
);
96 return this->prop_string
!= NULL
;
99 /** Set an int64 on this property.
102 int mlt_property_set_int64( mlt_property
this, int64_t value
)
104 mlt_property_clear( this );
105 this->types
= mlt_prop_int64
;
106 this->prop_int64
= value
;
110 /** Set a data on this property.
113 int mlt_property_set_data( mlt_property
this, void *value
, int length
, mlt_destructor destructor
, mlt_serialiser serialiser
)
115 if ( this->data
== value
)
116 this->destructor
= NULL
;
117 mlt_property_clear( this );
118 this->types
= mlt_prop_data
;
120 this->length
= length
;
121 this->destructor
= destructor
;
122 this->serialiser
= serialiser
;
126 /** Get an int from this property.
129 int mlt_property_get_int( mlt_property
this )
131 if ( this->types
& mlt_prop_int
)
132 return this->prop_int
;
133 else if ( this->types
& mlt_prop_double
)
134 return ( int )this->prop_double
;
135 else if ( this->types
& mlt_prop_position
)
136 return ( int )this->prop_position
;
137 else if ( this->types
& mlt_prop_int64
)
138 return ( int )this->prop_int64
;
139 else if ( this->types
& mlt_prop_string
)
140 return atoi( this->prop_string
);
144 /** Get a double from this property.
147 double mlt_property_get_double( mlt_property
this )
149 if ( this->types
& mlt_prop_double
)
150 return this->prop_double
;
151 else if ( this->types
& mlt_prop_int
)
152 return ( double )this->prop_int
;
153 else if ( this->types
& mlt_prop_position
)
154 return ( double )this->prop_position
;
155 else if ( this->types
& mlt_prop_int64
)
156 return ( double )this->prop_int64
;
157 else if ( this->types
& mlt_prop_string
)
158 return atof( this->prop_string
);
162 /** Get a position from this property.
165 mlt_position
mlt_property_get_position( mlt_property
this )
167 if ( this->types
& mlt_prop_position
)
168 return this->prop_position
;
169 else if ( this->types
& mlt_prop_int
)
170 return ( mlt_position
)this->prop_int
;
171 else if ( this->types
& mlt_prop_double
)
172 return ( mlt_position
)this->prop_double
;
173 else if ( this->types
& mlt_prop_int64
)
174 return ( mlt_position
)this->prop_int64
;
175 else if ( this->types
& mlt_prop_string
)
176 return ( mlt_position
)atol( this->prop_string
);
180 /** Get an int64 from this property.
183 int64_t mlt_property_get_int64( mlt_property
this )
185 if ( this->types
& mlt_prop_int64
)
186 return this->prop_int64
;
187 else if ( this->types
& mlt_prop_int
)
188 return ( int64_t )this->prop_int
;
189 else if ( this->types
& mlt_prop_double
)
190 return ( int64_t )this->prop_double
;
191 else if ( this->types
& mlt_prop_position
)
192 return ( int64_t )this->prop_position
;
193 else if ( this->types
& mlt_prop_string
)
194 return ( int64_t )atol( this->prop_string
);
198 /** Get a string from this property.
201 char *mlt_property_get_string( mlt_property
this )
203 // Construct a string if need be
204 if ( ! ( this->types
& mlt_prop_string
) )
206 if ( this->types
& mlt_prop_int
)
208 this->types
|= mlt_prop_string
;
209 this->prop_string
= malloc( 32 );
210 sprintf( this->prop_string
, "%d", this->prop_int
);
212 else if ( this->types
& mlt_prop_double
)
214 this->types
|= mlt_prop_string
;
215 this->prop_string
= malloc( 32 );
216 sprintf( this->prop_string
, "%e", this->prop_double
);
218 else if ( this->types
& mlt_prop_position
)
220 this->types
|= mlt_prop_string
;
221 this->prop_string
= malloc( 32 );
222 sprintf( this->prop_string
, "%d", this->prop_position
);
224 else if ( this->types
& mlt_prop_int64
)
226 this->types
|= mlt_prop_string
;
227 this->prop_string
= malloc( 32 );
228 sprintf( this->prop_string
, "%lld", this->prop_int64
);
230 else if ( this->types
& mlt_prop_data
&& this->serialiser
!= NULL
)
232 this->types
|= mlt_prop_string
;
233 this->prop_string
= this->serialiser( this->data
, this->length
);
237 // Return the string (may be NULL)
238 return this->prop_string
;
241 /** Get a data and associated length.
244 void *mlt_property_get_data( mlt_property
this, int *length
)
246 // Assign length if not NULL
247 if ( length
!= NULL
)
248 *length
= this->length
;
250 // Return the data (note: there is no conversion here)
254 /** Close this property.
257 void mlt_property_close( mlt_property
this )
259 mlt_property_clear( this );