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( sizeof( struct mlt_property_s
), 1 );
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 timecode on this property.
79 int mlt_property_set_timecode( mlt_property
this, mlt_timecode value
)
81 mlt_property_clear( this );
82 this->types
= mlt_prop_timecode
;
83 this->prop_timecode
= 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 a data on this property.
102 int mlt_property_set_data( mlt_property
this, void *value
, int length
, mlt_destructor destructor
, mlt_serialiser serialiser
)
104 mlt_property_clear( this );
105 this->types
= mlt_prop_data
;
107 this->length
= length
;
108 this->destructor
= destructor
;
109 this->serialiser
= serialiser
;
113 /** Get an int from this property.
116 int mlt_property_get_int( mlt_property
this )
118 if ( this->types
& mlt_prop_int
)
119 return this->prop_int
;
120 else if ( this->types
& mlt_prop_double
)
121 return ( int )this->prop_double
;
122 else if ( this->types
& mlt_prop_timecode
)
123 return ( int )this->prop_timecode
;
124 else if ( this->types
& mlt_prop_string
)
125 return atoi( this->prop_string
);
129 /** Get a double from this property.
132 double mlt_property_get_double( mlt_property
this )
134 if ( this->types
& mlt_prop_double
)
135 return this->prop_double
;
136 else if ( this->types
& mlt_prop_int
)
137 return ( double )this->prop_int
;
138 else if ( this->types
& mlt_prop_timecode
)
139 return ( double )this->prop_timecode
;
140 else if ( this->types
& mlt_prop_string
)
141 return atof( this->prop_string
);
145 /** Get a timecode from this property.
148 mlt_timecode
mlt_property_get_timecode( mlt_property
this )
150 if ( this->types
& mlt_prop_timecode
)
151 return this->prop_timecode
;
152 else if ( this->types
& mlt_prop_int
)
153 return ( mlt_timecode
)this->prop_int
;
154 else if ( this->types
& mlt_prop_double
)
155 return ( mlt_timecode
)this->prop_double
;
156 else if ( this->types
& mlt_prop_string
)
157 return ( mlt_timecode
)atof( this->prop_string
);
161 /** Get a string from this property.
164 char *mlt_property_get_string( mlt_property
this )
166 // Construct a string if need be
167 if ( ! ( this->types
& mlt_prop_string
) )
169 if ( this->types
& mlt_prop_int
)
171 this->types
|= mlt_prop_string
;
172 this->prop_string
= malloc( 32 );
173 sprintf( this->prop_string
, "%d", this->prop_int
);
175 else if ( this->types
& mlt_prop_double
)
177 this->types
|= mlt_prop_string
;
178 this->prop_string
= malloc( 32 );
179 sprintf( this->prop_string
, "%e", this->prop_double
);
181 else if ( this->types
& mlt_prop_timecode
)
183 this->types
|= mlt_prop_string
;
184 this->prop_string
= malloc( 32 );
185 sprintf( this->prop_string
, "%e", this->prop_timecode
);
187 else if ( this->types
& mlt_prop_data
&& this->serialiser
!= NULL
)
189 this->types
|= mlt_prop_string
;
190 this->prop_string
= this->serialiser( this->data
, this->length
);
194 // Return the string (may be NULL)
195 return this->prop_string
;
198 /** Get a data and associated length.
201 void *mlt_property_get_data( mlt_property
this, int *length
)
203 // Assign length if not NULL
204 if ( length
!= NULL
)
205 *length
= this->length
;
207 // Return the data (note: there is no conversion here)
211 /** Close this property.
214 void mlt_property_close( mlt_property
this )
216 mlt_property_clear( this );