]> git.sesse.net Git - vlc/commitdiff
i18n_atof(): locale-agnostic atof()
authorRémi Denis-Courmont <rem@videolan.org>
Wed, 8 Mar 2006 11:32:34 +0000 (11:32 +0000)
committerRémi Denis-Courmont <rem@videolan.org>
Wed, 8 Mar 2006 11:32:34 +0000 (11:32 +0000)
include/charset.h
src/Makefile.am
src/misc/charset.c
src/test/i18n_atof.c [new file with mode: 0644]

index f8cdd73cefffa058c493e1f3e7aed29ea7968cd8..a7398598e9dd72c561f617af5fe88c13cca43294 100644 (file)
@@ -49,6 +49,7 @@ VLC_EXPORT( char *, EnsureUTF8, ( char * ) );
 VLC_EXPORT( char *, FromUTF32, ( const wchar_t * ) );
 VLC_EXPORT( char *, __vlc_fix_readdir_charset, ( vlc_object_t *, const char * ) );
 #define vlc_fix_readdir_charset(a,b) __vlc_fix_readdir_charset(VLC_OBJECT(a),b)
+extern double i18n_atof( const char * );
 
 # ifdef __cplusplus
 }
index 8f6da119e7203527e46bc515688c52e33feb8383..8e18be47803e8177999e3270f6b65cd75b622618 100644 (file)
@@ -372,3 +372,17 @@ stamp-api: Makefile.in $(HEADERS_include) ../vlc-api.pl
          top_srcdir="$(top_srcdir)" perl $(top_srcdir)/vlc-api.pl
        touch stamp-api
 
+###############################################################################
+# Unit/regression test
+###############################################################################
+if USE_LIBTOOL
+check_PROGRAMS = test_i18n_atof
+TESTS = $(check_PROGRAMS)
+
+CFLAGS_tests = `$(VLC_CONFIG) --cflags vlc`
+
+test_i18n_atof_SOURCES = test/i18n_atof.c
+test_i18n_atof_LDADD = libvlc.la
+test_i18n_atof_CFLAGS = $(CFLAGS_tests)
+endif
+
index 5b8d437e1c416335e305f4810955b90f10ca1025..a575f72136e69e70ca1c56cf2c105e1ae727f47a 100644 (file)
@@ -1,11 +1,14 @@
 /*****************************************************************************
- * charset.c: Determine a canonical name for the current locale's character
- *            encoding.
+ * charset.c: Locale's character encoding stuff.
  *****************************************************************************
- * Copyright (C) 2003-2005 the VideoLAN team
+ * See also unicode.c for Unicode to locale conversion helpers.
+ *
+ * Copyright (C) 2003-2006 the VideoLAN team
  * $Id$
  *
- * Author: Derk-Jan Hartman <thedj at users.sf.net>
+ * Authors: Derk-Jan Hartman <thedj at users.sf.net>
+ *          Christophe Massiot
+ *          Rémi Denis-Courmont
  *
  * vlc_current_charset() an adaption of mp_locale_charset():
  *
@@ -371,3 +374,31 @@ char *__vlc_fix_readdir_charset( vlc_object_t *p_this, const char *psz_string )
 
     return strdup( psz_string );
 }
+
+/**
+ * There are two decimal separators in the computer world-wide locales:
+ * dot (which is the american default), and comma (which is used in France,
+ * the country with the most VLC developers, among others).
+ *
+ * i18n_atof() has the same prototype as ANSI C atof() but it accepts
+ * either decimal separator when deserializing the string to a float number,
+ * independant of the local computer setting.
+ */
+double i18n_atof( const char *str )
+{
+    char *end;
+    double d = strtod( str, &end );
+
+    if(( *end == ',' ) || ( *end == '.' ))
+    {
+        char *dup = strdup( str );
+
+        if( dup == NULL )
+            return d;
+
+        dup[end - str] = ( *end == ',' ) ? '.' : ',';
+        d = strtod( dup, &end );
+        free( dup );
+    }
+    return d;
+}
diff --git a/src/test/i18n_atof.c b/src/test/i18n_atof.c
new file mode 100644 (file)
index 0000000..4ac8eee
--- /dev/null
@@ -0,0 +1,41 @@
+/*****************************************************************************
+ * i18n_atof.c: Test for i18n_atof
+ *****************************************************************************
+ * Copyright (C) 2006 Rémi Denis-Courmont
+ * $Id$
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#include <vlc/vlc.h>
+#include "charset.h"
+
+#undef NDEBUG
+#include <assert.h>
+
+int main (void)
+{
+    assert (i18n_atof("0") == 0.);
+    assert (i18n_atof("1") == 1.);
+    assert (i18n_atof("1.") == 1.);
+    assert (i18n_atof("1,") == 1.);
+    assert (i18n_atof("1#") == 1.);
+    assert (i18n_atof("999999.999999") == 999999.999999);
+    assert (i18n_atof("999999,999999") == 999999.999999);
+    assert (i18n_atof("999999#999999") == 999999.);
+
+    assert (i18n_atof("invalid") == 0.);
+    return 0;
+}