]> git.sesse.net Git - vlc/blobdiff - src/misc/charset.c
* i18n_strtod: locale-agnostic strtod() (accepts both comma and dot as decimal separ...
[vlc] / src / misc / charset.c
index 491d6546606009e254b8209e080770e63e48518e..2a66d6732670a3845831aaf7a4500053cb6c03e3 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():
  *
@@ -64,7 +67,7 @@ typedef struct VLCCharsetAlias
  * a lot of basic aliases (check it first by iconv -l).
  *
  */
-#if defined WIN32 || defined OS2 || !HAVE_LANGINFO_CODESET
+#if (defined OS2 || !HAVE_LANGINFO_CODESET) && !defined WIN32
 static const char* vlc_encoding_from_language( const char *l )
 {
     /* check for language (and perhaps country) codes */
@@ -189,12 +192,9 @@ static const char* vlc_charset_aliases( const char *psz_name )
     VLCCharsetAlias aliases[] = {{NULL, NULL}};
 #endif
 
-    if( aliases )
-    {
-        for (a = aliases; a->psz_alias; a++)
-            if (strcasecmp (a->psz_alias, psz_name) == 0)
-                return a->psz_name;
-    }
+    for (a = aliases; a->psz_alias; a++)
+        if (strcasecmp (a->psz_alias, psz_name) == 0)
+            return a->psz_name;
 
     /* we return original name beacuse iconv() probably will know
      * something better about name if we don't know it :-) */
@@ -202,7 +202,7 @@ static const char* vlc_charset_aliases( const char *psz_name )
 }
 
 /* Returns charset from "language_COUNTRY.charset@modifier" string */
-#if defined WIN32 || defined OS2 || !HAVE_LANGINFO_CODESET
+#if (defined OS2 || !HAVE_LANGINFO_CODESET) && !defined WIN32
 static void vlc_encoding_from_locale( char *psz_locale, char *psz_charset )
 {
     char *psz_dot = strchr( psz_locale, '.' );
@@ -374,3 +374,78 @@ 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_strtod() has the same prototype as ANSI C strtod() but it accepts
+ * either decimal separator when deserializing the string to a float number,
+ * independant of the local computer setting.
+ */
+double i18n_strtod( const char *str, char **end )
+{
+    char *end_buf, e;
+    double d;
+
+    if( end == NULL )
+        end = &end_buf;
+    d = strtod( str, end );
+
+    e = **end;
+    if(( e == ',' ) || ( e == '.' ))
+    {
+        char dup[strlen( str ) + 1];
+        strcpy( dup, str );
+
+        if( dup == NULL )
+            return d;
+
+        dup[*end - str] = ( e == ',' ) ? '.' : ',';
+        d = strtod( dup, end );
+    }
+    return d;
+}
+
+/**
+ * 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 )
+{
+    return i18n_strtod( str, NULL );
+}
+
+
+/**
+ * us_strtod() has the same prototype as ANSI C strtod() but it expects
+ * a dot as decimal separator regardless of the system locale.
+ */
+double us_strtod( const char *str, char **end )
+{
+    char dup[strlen( str ) + 1], *ptr;
+    double d;
+    strcpy( dup, str );
+
+    ptr = strchr( dup, ',' );
+    if( ptr != NULL )
+        *ptr = '\0';
+
+    d = strtod( dup, &ptr );
+    if( end != NULL )
+        *end = (char *)&str[ptr - dup];
+
+    return d;
+}
+
+/**
+ * us_atof() has the same prototype as ANSI C atof() but it expects a dot
+ * as decimal separator, regardless of the system locale.
+ */
+double us_atof( const char *str )
+{
+    return us_strtod( str, NULL );
+}
+