]> 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 a7980e028ee0be890daa056fce53255ba64b07bb..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-2004 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():
  *
@@ -24,7 +27,7 @@
  *
  * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #include <stdlib.h>
@@ -43,7 +46,7 @@
 # include <windows.h>
 #endif
 
-#ifdef SYS_DARWIN
+#ifdef __APPLE__
 #   include <errno.h>
 #   include <string.h>
 #endif
@@ -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,15 +202,14 @@ 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
-static const char *vlc_encoding_from_locale( char *psz_locale )
+#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, '.' );
 
     if( psz_dot != NULL )
     {
         const char *psz_modifier;
-        char buf[2 + 10 + 1];
 
         psz_dot++;
 
@@ -218,17 +217,20 @@ static const char *vlc_encoding_from_locale( char *psz_locale )
         psz_modifier = strchr( psz_dot, '@' );
 
         if( psz_modifier == NULL )
-            return psz_dot;
+        {
+            strcpy( psz_charset, psz_dot );
+            return;
+        }
         if( 0 < ( psz_modifier - psz_dot )
-             && ( psz_modifier - psz_dot ) < sizeof( buf ))
+             && ( psz_modifier - psz_dot ) < 2 + 10 + 1 )
         {
-            memcpy( buf, psz_dot, psz_modifier - psz_dot );
-            buf[ psz_modifier - psz_dot ] = '\0';
-            return buf;
+            memcpy( psz_charset, psz_dot, psz_modifier - psz_dot );
+            psz_charset[ psz_modifier - psz_dot ] = '\0';
+            return;
         }
     }
     /* try language mapping */
-    return vlc_encoding_from_language( psz_locale );
+    strcpy( psz_charset, vlc_encoding_from_language( psz_locale ) );
 }
 #endif
 
@@ -236,20 +238,23 @@ vlc_bool_t vlc_current_charset( char **psz_charset )
 {
     const char *psz_codeset;
 
-#if !(defined WIN32 || defined OS2 || defined SYS_DARWIN)
+#if !(defined WIN32 || defined OS2 || defined __APPLE__)
 
 # if HAVE_LANGINFO_CODESET
     /* Most systems support nl_langinfo( CODESET ) nowadays.  */
     psz_codeset = nl_langinfo( CODESET );
+    if( !strcmp( psz_codeset, "ANSI_X3.4-1968" ) )
+        psz_codeset = "ASCII";
 # else
     /* On old systems which lack it, use setlocale or getenv.  */
     const char *psz_locale = NULL;
+    char buf[2 + 10 + 1];
 
     /* But most old systems don't have a complete set of locales.  Some
      * (like SunOS 4 or DJGPP) have only the C locale.  Therefore we don't
      * use setlocale here; it would return "C" when it doesn't support the
      * locale name the user has set. Darwin's setlocale is broken. */
-#  if HAVE_SETLOCALE && !SYS_DARWIN
+#  if HAVE_SETLOCALE && !__APPLE__
     psz_locale = setlocale( LC_ALL, NULL );
 #  endif
     if( psz_locale == NULL || psz_locale[0] == '\0' )
@@ -265,10 +270,11 @@ vlc_bool_t vlc_current_charset( char **psz_charset )
 
     /* On some old systems, one used to set locale = "iso8859_1". On others,
      * you set it to "language_COUNTRY.charset". Darwin only has LANG :( */
-    psz_codeset = vlc_encoding_from_locale( (char *)psz_locale );
+    vlc_encoding_from_locale( (char *)psz_locale, buf );
+    psz_codeset =  buf;
 # endif /* HAVE_LANGINFO_CODESET */
 
-#elif defined SYS_DARWIN
+#elif defined __APPLE__
 
     /* Darwin is always using UTF-8 internally. */
     psz_codeset = "UTF-8";
@@ -298,7 +304,8 @@ vlc_bool_t vlc_current_charset( char **psz_charset )
             locale = getenv( "LANG" );
     }
     if( psz_locale != NULL && psz_locale[0] != '\0' )
-        psz_codeset = vlc_encoding_from_locale( psz_locale );
+        vlc_encoding_from_locale( psz_locale, buf );
+        psz_codeset = buf;
     else
     {
         /* OS/2 has a function returning the locale's codepage as a number. */
@@ -338,7 +345,7 @@ vlc_bool_t vlc_current_charset( char **psz_charset )
 
 char *__vlc_fix_readdir_charset( vlc_object_t *p_this, const char *psz_string )
 {
-#ifdef SYS_DARWIN
+#ifdef __APPLE__
     if ( p_this->p_libvlc->iconv_macosx != (vlc_iconv_t)-1 )
     {
         const char *psz_in = psz_string;
@@ -367,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 );
+}
+