changeset 2182:cc2597da0840

Fixed bug #455 If passed "" for the character set, let iconv_open() interpret it as locale. This was merged from revision 3234,3235 from SDL 1.2
author Sam Lantinga <slouken@libsdl.org>
date Thu, 12 Jul 2007 07:52:50 +0000
parents 791cdc226105
children 9f31740cad2e
files src/stdlib/SDL_iconv.c
diffstat 1 files changed, 37 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/stdlib/SDL_iconv.c	Thu Jul 12 07:02:44 2007 +0000
+++ b/src/stdlib/SDL_iconv.c	Thu Jul 12 07:52:50 2007 +0000
@@ -111,10 +111,8 @@
     int format;
 } encodings[] = {
 /* *INDENT-OFF* */
-    { "646", ENCODING_ASCII },
     { "ASCII", ENCODING_ASCII },
     { "US-ASCII", ENCODING_ASCII },
-    { "LATIN1", ENCODING_LATIN1 },
     { "8859-1", ENCODING_LATIN1 },
     { "ISO-8859-1", ENCODING_LATIN1 },
     { "UTF8", ENCODING_UTF8 },
@@ -138,6 +136,27 @@
 /* *INDENT-ON* */
 };
 
+static const char *
+getlocale()
+{
+    const char *lang;
+
+    lang = SDL_getenv("LC_ALL");
+    if (!lang) {
+        lang = SDL_getenv("LC_CTYPE");
+    }
+    if (!lang) {
+        lang = SDL_getenv("LC_MESSAGES");
+    }
+    if (!lang) {
+        lang = SDL_getenv("LANG");
+    }
+    if (!lang || !*lang || SDL_strcmp(lang, "C") == 0) {
+        lang = "ASCII";
+    }
+    return lang;
+}
+
 SDL_iconv_t
 SDL_iconv_open(const char *tocode, const char *fromcode)
 {
@@ -145,6 +164,12 @@
     int dst_fmt = ENCODING_UNKNOWN;
     int i;
 
+    if (!fromcode || !*fromcode) {
+        fromcode = getlocale();
+    }
+    if (!tocode || !*tocode) {
+        fromcode = getlocale();
+    }
     for (i = 0; i < SDL_arraysize(encodings); ++i) {
         if (SDL_strcasecmp(fromcode, encodings[i].name) == 0) {
             src_fmt = encodings[i].format;
@@ -805,13 +830,17 @@
     size_t outbytesleft;
     size_t retCode = 0;
 
-    if (!fromcode || !*fromcode) {
-        fromcode = getlocale();
+    cd = SDL_iconv_open(tocode, fromcode);
+    if (cd == (SDL_iconv_t) - 1) {
+        /* See if we can recover here (fixes iconv on Solaris 11) */
+        if (!tocode || !*tocode) {
+            tocode = "UTF-8";
+        }
+        if (!fromcode || !*fromcode) {
+            tocode = "UTF-8";
+        }
+        cd = SDL_iconv_open(tocode, fromcode);
     }
-    if (!tocode || !*tocode) {
-        tocode = getlocale();
-    }
-    cd = SDL_iconv_open(tocode, fromcode);
     if (cd == (SDL_iconv_t) - 1) {
         return NULL;
     }