From 7146789f5fd7e837313f69da8c772ebb67b80ec1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?R=C3=A9mi=20Denis-Courmont?= Date: Wed, 8 Mar 2006 11:32:34 +0000 Subject: [PATCH] i18n_atof(): locale-agnostic atof() --- include/charset.h | 1 + src/Makefile.am | 14 ++++++++++++++ src/misc/charset.c | 39 +++++++++++++++++++++++++++++++++++---- src/test/i18n_atof.c | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 src/test/i18n_atof.c diff --git a/include/charset.h b/include/charset.h index f8cdd73cef..a7398598e9 100644 --- a/include/charset.h +++ b/include/charset.h @@ -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 } diff --git a/src/Makefile.am b/src/Makefile.am index 8f6da119e7..8e18be4780 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 + diff --git a/src/misc/charset.c b/src/misc/charset.c index 5b8d437e1c..a575f72136 100644 --- a/src/misc/charset.c +++ b/src/misc/charset.c @@ -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 + * Authors: Derk-Jan Hartman + * 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 index 0000000000..4ac8eeeeb1 --- /dev/null +++ b/src/test/i18n_atof.c @@ -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 +#include "charset.h" + +#undef NDEBUG +#include + +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; +} -- 2.39.2