Commit 7146789f authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

i18n_atof(): locale-agnostic atof()

parent d3de78aa
...@@ -49,6 +49,7 @@ VLC_EXPORT( char *, EnsureUTF8, ( char * ) ); ...@@ -49,6 +49,7 @@ VLC_EXPORT( char *, EnsureUTF8, ( char * ) );
VLC_EXPORT( char *, FromUTF32, ( const wchar_t * ) ); VLC_EXPORT( char *, FromUTF32, ( const wchar_t * ) );
VLC_EXPORT( char *, __vlc_fix_readdir_charset, ( vlc_object_t *, const char * ) ); 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) #define vlc_fix_readdir_charset(a,b) __vlc_fix_readdir_charset(VLC_OBJECT(a),b)
extern double i18n_atof( const char * );
# ifdef __cplusplus # ifdef __cplusplus
} }
......
...@@ -372,3 +372,17 @@ stamp-api: Makefile.in $(HEADERS_include) ../vlc-api.pl ...@@ -372,3 +372,17 @@ stamp-api: Makefile.in $(HEADERS_include) ../vlc-api.pl
top_srcdir="$(top_srcdir)" perl $(top_srcdir)/vlc-api.pl top_srcdir="$(top_srcdir)" perl $(top_srcdir)/vlc-api.pl
touch stamp-api 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
/***************************************************************************** /*****************************************************************************
* charset.c: Determine a canonical name for the current locale's character * charset.c: Locale's character encoding stuff.
* encoding.
***************************************************************************** *****************************************************************************
* 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$ * $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(): * 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 ) ...@@ -371,3 +374,31 @@ char *__vlc_fix_readdir_charset( vlc_object_t *p_this, const char *psz_string )
return strdup( 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;
}
/*****************************************************************************
* 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;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment