]> git.sesse.net Git - mlt/commitdiff
Add mlt_properties_set_lcnumeric, mlt_property_get_double_l, and
authorDan Dennedy <dan@dennedy.org>
Sat, 9 Jul 2011 19:32:46 +0000 (12:32 -0700)
committerDan Dennedy <dan@dennedy.org>
Sat, 9 Jul 2011 19:38:07 +0000 (12:38 -0700)
mlt_property_get_string_l.

Locale-specific variants of key properties functions.

src/framework/mlt_properties.c
src/framework/mlt_properties.h
src/framework/mlt_property.c
src/framework/mlt_property.h

index fd674e1aad82b807d4d0aeb20e5c0ddbd3dc74a1..02dbd7f215b869256c800c3b465a4edc596447bf 100644 (file)
@@ -38,6 +38,7 @@
 #include <dirent.h>
 #include <sys/stat.h>
 #include <errno.h>
+#include <locale.h>
 
 /** \brief private implementation of the property list */
 
@@ -51,6 +52,7 @@ typedef struct
        mlt_properties mirror;
        int ref_count;
        pthread_mutex_t mutex;
+       locale_t locale;
 }
 property_list;
 
@@ -118,6 +120,33 @@ mlt_properties mlt_properties_new( )
        return self;
 }
 
+/** Set the numeric locale used for string/double conversions.
+ *
+ * \public \memberof mlt_properties_s
+ * \param self a properties list
+ * \param locale the locale name
+ * \return true if error
+ */
+
+int mlt_properties_set_lcnumeric( mlt_properties self, const char *locale )
+{
+       int error = 0;
+
+       if ( self && locale )
+       {
+               property_list *list = self->local;
+
+               if ( list->locale )
+                       freelocale( list->locale );
+               list->locale = newlocale( LC_NUMERIC, locale, NULL );
+               error = list->locale == NULL;
+       }
+       else
+               error = 1;
+
+       return error;
+}
+
 static int load_properties( mlt_properties self, const char *filename )
 {
        // Open the file
@@ -689,7 +718,8 @@ int mlt_properties_set_or_default( mlt_properties self, const char *name, const
 char *mlt_properties_get( mlt_properties self, const char *name )
 {
        mlt_property value = mlt_properties_find( self, name );
-       return value == NULL ? NULL : mlt_property_get_string( value );
+       property_list *list = self->local;
+       return value == NULL ? NULL : mlt_property_get_string_l( value, list->locale );
 }
 
 /** Get a property name by index.
@@ -722,7 +752,7 @@ char *mlt_properties_get_value( mlt_properties self, int index )
 {
        property_list *list = self->local;
        if ( index >= 0 && index < list->count )
-               return mlt_property_get_string( list->value[ index ] );
+               return mlt_property_get_string_l( list->value[ index ], list->locale );
        return NULL;
 }
 
@@ -896,7 +926,8 @@ int mlt_properties_set_int64( mlt_properties self, const char *name, int64_t val
 double mlt_properties_get_double( mlt_properties self, const char *name )
 {
        mlt_property value = mlt_properties_find( self, name );
-       return value == NULL ? 0 : mlt_property_get_double( value );
+       property_list *list = self->local;
+       return value == NULL ? 0 : mlt_property_get_double_l( value, list->locale );
 }
 
 /** Set a property to a floating point value.
@@ -1169,7 +1200,7 @@ static int mlt_fnmatch( const char *wild, const char *file )
 
 static int mlt_compare( const void *self, const void *that )
 {
-       return strcmp( mlt_property_get_string( *( const mlt_property * )self ), mlt_property_get_string( *( const mlt_property * )that ) );
+    return strcmp( mlt_property_get_string( *( const mlt_property * )self ), mlt_property_get_string( *( const mlt_property * )that ) );
 }
 
 /** Get the contents of a directory.
@@ -1259,6 +1290,10 @@ void mlt_properties_close( mlt_properties self )
                                free( list->name[ index ] );
                        }
 
+                       // Cleanup locale
+                       if ( list->locale )
+                               freelocale( list->locale );
+
                        // Clear up the list
                        pthread_mutex_destroy( &list->mutex );
                        free( list->name );
index 5e17b15087906dcfbc3ea92abb034d950730ec26..d08c9b77b7819d2d7baaa3aba00425b327a6d1c9 100644 (file)
@@ -47,6 +47,7 @@ struct mlt_properties_s
 
 extern int mlt_properties_init( mlt_properties, void *child );
 extern mlt_properties mlt_properties_new( );
+extern int mlt_properties_set_lcnumeric( mlt_properties, const char *locale );
 extern mlt_properties mlt_properties_load( const char *file );
 extern int mlt_properties_preset( mlt_properties self, const char *name );
 extern int mlt_properties_inc_ref( mlt_properties self );
index aea00e72bdfda1766bb66a114cb827b88ac04542..c9c178c3b644b4f6417deaea09108f890881184a 100644 (file)
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+// For strtod_l
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
 #include "mlt_property.h"
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <locale.h>
 
 
 /** Bit pattern used internally to indicated representations available.
@@ -326,6 +332,31 @@ double mlt_property_get_double( mlt_property self )
        return 0;
 }
 
+/** Get the property (with locale) as a floating point.
+ *
+ * \public \memberof mlt_property_s
+ * \param self a property
+ * \param locale the locale to use for this conversion
+ * \return a floating point value
+ */
+
+double mlt_property_get_double_l( mlt_property self, locale_t locale )
+{
+       if ( self->types & mlt_prop_double )
+               return self->prop_double;
+       else if ( self->types & mlt_prop_int )
+               return ( double )self->prop_int;
+       else if ( self->types & mlt_prop_position )
+               return ( double )self->prop_position;
+       else if ( self->types & mlt_prop_int64 )
+               return ( double )self->prop_int64;
+       else if ( locale && ( self->types & mlt_prop_string ) && self->prop_string )
+               return strtod_l( self->prop_string, NULL, locale );
+       else if ( ( self->types & mlt_prop_string ) && self->prop_string )
+               return strtod( self->prop_string, NULL );
+       return 0;
+}
+
 /** Get the property as a position.
  *
  * A position is an offset time in terms of frame units.
@@ -440,6 +471,76 @@ char *mlt_property_get_string( mlt_property self )
        return self->prop_string;
 }
 
+/** Get the property as a string (with locale).
+ *
+ * The caller is not responsible for deallocating the returned string!
+ * The string is deallocated when the Property is closed.
+ * This tries its hardest to convert the property to string including using
+ * a serialization function for binary data, if supplied.
+ * \public \memberof mlt_property_s
+ * \param self a property
+ * \param locale the locale to use for this conversion
+ * \return a string representation of the property or NULL if failed
+ */
+
+char *mlt_property_get_string_l( mlt_property self, locale_t locale )
+{
+       // Optimization for no locale
+       if ( !locale )
+               return mlt_property_get_string( self );
+
+       // Construct a string if need be
+       if ( ! ( self->types & mlt_prop_string ) )
+       {
+               // TODO: when glibc gets sprintf_l, start using it! For now, hack on setlocale.
+               // Save the current locale
+#ifdef querylocale
+               const char *localename = querylocale( LC_NUMERIC, locale );
+#else
+               const char *localename = locale->__names[ LC_NUMERIC ];
+#endif
+               const char *orig_localename = setlocale( LC_NUMERIC, NULL );
+
+               // Set the new locale
+               setlocale( LC_NUMERIC, localename );
+
+               if ( self->types & mlt_prop_int )
+               {
+                       self->types |= mlt_prop_string;
+                       self->prop_string = malloc( 32 );
+                       sprintf( self->prop_string, "%d", self->prop_int );
+               }
+               else if ( self->types & mlt_prop_double )
+               {
+                       self->types |= mlt_prop_string;
+                       self->prop_string = malloc( 32 );
+                       sprintf( self->prop_string, "%f", self->prop_double );
+               }
+               else if ( self->types & mlt_prop_position )
+               {
+                       self->types |= mlt_prop_string;
+                       self->prop_string = malloc( 32 );
+                       sprintf( self->prop_string, "%d", (int)self->prop_position );
+               }
+               else if ( self->types & mlt_prop_int64 )
+               {
+                       self->types |= mlt_prop_string;
+                       self->prop_string = malloc( 32 );
+                       sprintf( self->prop_string, "%"PRId64, self->prop_int64 );
+               }
+               else if ( self->types & mlt_prop_data && self->serialiser != NULL )
+               {
+                       self->types |= mlt_prop_string;
+                       self->prop_string = self->serialiser( self->data, self->length );
+               }
+               // Restore the current locale
+               setlocale( LC_NUMERIC, orig_localename );
+       }
+
+       // Return the string (may be NULL)
+       return self->prop_string;
+}
+
 /** Get the binary data from a property.
  *
  * This only works if you previously put binary data into the property.
index ecc9f4eed20a930ba8b16de00efa9fd227b4d105..fcf075401bd31fb48bff235f20408e94009b5129 100644 (file)
@@ -25,6 +25,7 @@
 #define _MLT_PROPERTY_H_
 
 #include "mlt_types.h"
+#include <xlocale.h>
 
 extern mlt_property mlt_property_init( );
 extern int mlt_property_set_int( mlt_property self, int value );
@@ -35,9 +36,11 @@ extern int mlt_property_set_string( mlt_property self, const char *value );
 extern int mlt_property_set_data( mlt_property self, void *value, int length, mlt_destructor destructor, mlt_serialiser serialiser );
 extern int mlt_property_get_int( mlt_property self );
 extern double mlt_property_get_double( mlt_property self );
+extern double mlt_property_get_double_l( mlt_property self, locale_t );
 extern mlt_position mlt_property_get_position( mlt_property self );
 extern int64_t mlt_property_get_int64( mlt_property self );
 extern char *mlt_property_get_string( mlt_property self );
+extern char *mlt_property_get_string_l( mlt_property self, locale_t );
 extern void *mlt_property_get_data( mlt_property self, int *length );
 extern void mlt_property_close( mlt_property self );