]> git.sesse.net Git - vlc/blobdiff - src/text/charset.c
Include xlocale.h on OS X for locale_t
[vlc] / src / text / charset.c
index 57a25db04dc380ebd2a1e59141a9ba65732a7bc6..7d090240cb9f36fbd5997880039dfc7bba1c371f 100644 (file)
  *  Copyright (C) 2001-2003 The Mape Project
  *  Written by Karel Zak  <zakkr@zf.jcu.cz>.
  *
+ * which itself is an adaptation of locale_charset():
+ *
+ *  Copyright (C) 2000-2002 Free Software Foundation, Inc.
+ *  Written by Bruno Haible <bruno@clisp.org>.
+ *
  * 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
@@ -48,6 +53,7 @@
 #ifdef __APPLE__
 #   include <errno.h>
 #   include <string.h>
+#   include <xlocale.h>
 #endif
 
 #include <vlc_charset.h>
@@ -66,7 +72,7 @@ typedef struct VLCCharsetAlias
  * a lot of basic aliases (check it first by iconv -l).
  *
  */
-#if (defined OS2 || !HAVE_LANGINFO_CODESET) && !defined WIN32
+#if (defined OS2 || !defined(HAVE_LANGINFO_CODESET)) && !defined WIN32
 static const char* vlc_encoding_from_language( const char *l )
 {
     /* check for language (and perhaps country) codes */
@@ -201,7 +207,7 @@ static const char* vlc_charset_aliases( const char *psz_name )
 }
 
 /* Returns charset from "language_COUNTRY.charset@modifier" string */
-#if (defined OS2 || !HAVE_LANGINFO_CODESET) && !defined WIN32
+#if (defined OS2 || !defined(HAVE_LANGINFO_CODESET)) && !defined WIN32
 static void vlc_encoding_from_locale( char *psz_locale, char *psz_charset )
 {
     char *psz_dot = strchr( psz_locale, '.' );
@@ -233,13 +239,13 @@ static void vlc_encoding_from_locale( char *psz_locale, char *psz_charset )
 }
 #endif
 
-vlc_bool_t vlc_current_charset( char **psz_charset )
+bool vlc_current_charset( char **psz_charset )
 {
     const char *psz_codeset;
 
 #if !(defined WIN32 || defined OS2 || defined __APPLE__)
 
-# if HAVE_LANGINFO_CODESET
+# ifdef HAVE_LANGINFO_CODESET
     /* Most systems support nl_langinfo( CODESET ) nowadays.  */
     psz_codeset = nl_langinfo( CODESET );
     if( !strcmp( psz_codeset, "ANSI_X3.4-1968" ) )
@@ -253,7 +259,7 @@ vlc_bool_t vlc_current_charset( char **psz_charset )
      * (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 && !__APPLE__
+#  if defined (HAVE_SETLOCALE) && !defined (__APPLE__)
     psz_locale = setlocale( LC_ALL, NULL );
 #  endif
     if( psz_locale == NULL || psz_locale[0] == '\0' )
@@ -337,9 +343,9 @@ vlc_bool_t vlc_current_charset( char **psz_charset )
         *psz_charset = strdup(psz_codeset);
 
     if( !strcasecmp(psz_codeset, "UTF8") || !strcasecmp(psz_codeset, "UTF-8") )
-        return VLC_TRUE;
+        return true;
 
-    return VLC_FALSE;
+    return false;
 }
 
 
@@ -373,68 +379,21 @@ char *vlc_fix_readdir( const char *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.
+ * us_strtod() has the same prototype as ANSI C strtod() but it uses the
+ * POSIX/C decimal format, regardless of the current numeric locale.
  */
-double i18n_strtod( const char *str, char **end )
+double us_strtod( const char *str, char **end )
 {
-    char *end_buf, e;
-    double d;
-
-    if( end == NULL )
-        end = &end_buf;
-    d = strtod( str, end );
+    locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
+    locale_t oldloc = uselocale (loc);
+    double res = strtod (str, end);
 
-    e = **end;
-    if(( e == ',' ) || ( e == '.' ))
+    if (loc != (locale_t)0)
     {
-        char dup[strlen( str ) + 1];
-        strcpy( dup, str );
-
-        if( dup == NULL )
-            return d;
-
-        dup[*end - str] = ( e == ',' ) ? '.' : ',';
-        d = strtod( dup, end );
+        uselocale (oldloc);
+        freelocale (loc);
     }
-    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;
+    return res;
 }
 
 /**