Fixed bug #455
authorSam Lantinga
Thu, 12 Jul 2007 07:52:50 +0000
changeset 2182cc2597da0840
parent 2181 791cdc226105
child 2183 9f31740cad2e
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
src/stdlib/SDL_iconv.c
     1.1 --- a/src/stdlib/SDL_iconv.c	Thu Jul 12 07:02:44 2007 +0000
     1.2 +++ b/src/stdlib/SDL_iconv.c	Thu Jul 12 07:52:50 2007 +0000
     1.3 @@ -111,10 +111,8 @@
     1.4      int format;
     1.5  } encodings[] = {
     1.6  /* *INDENT-OFF* */
     1.7 -    { "646", ENCODING_ASCII },
     1.8      { "ASCII", ENCODING_ASCII },
     1.9      { "US-ASCII", ENCODING_ASCII },
    1.10 -    { "LATIN1", ENCODING_LATIN1 },
    1.11      { "8859-1", ENCODING_LATIN1 },
    1.12      { "ISO-8859-1", ENCODING_LATIN1 },
    1.13      { "UTF8", ENCODING_UTF8 },
    1.14 @@ -138,6 +136,27 @@
    1.15  /* *INDENT-ON* */
    1.16  };
    1.17  
    1.18 +static const char *
    1.19 +getlocale()
    1.20 +{
    1.21 +    const char *lang;
    1.22 +
    1.23 +    lang = SDL_getenv("LC_ALL");
    1.24 +    if (!lang) {
    1.25 +        lang = SDL_getenv("LC_CTYPE");
    1.26 +    }
    1.27 +    if (!lang) {
    1.28 +        lang = SDL_getenv("LC_MESSAGES");
    1.29 +    }
    1.30 +    if (!lang) {
    1.31 +        lang = SDL_getenv("LANG");
    1.32 +    }
    1.33 +    if (!lang || !*lang || SDL_strcmp(lang, "C") == 0) {
    1.34 +        lang = "ASCII";
    1.35 +    }
    1.36 +    return lang;
    1.37 +}
    1.38 +
    1.39  SDL_iconv_t
    1.40  SDL_iconv_open(const char *tocode, const char *fromcode)
    1.41  {
    1.42 @@ -145,6 +164,12 @@
    1.43      int dst_fmt = ENCODING_UNKNOWN;
    1.44      int i;
    1.45  
    1.46 +    if (!fromcode || !*fromcode) {
    1.47 +        fromcode = getlocale();
    1.48 +    }
    1.49 +    if (!tocode || !*tocode) {
    1.50 +        fromcode = getlocale();
    1.51 +    }
    1.52      for (i = 0; i < SDL_arraysize(encodings); ++i) {
    1.53          if (SDL_strcasecmp(fromcode, encodings[i].name) == 0) {
    1.54              src_fmt = encodings[i].format;
    1.55 @@ -805,13 +830,17 @@
    1.56      size_t outbytesleft;
    1.57      size_t retCode = 0;
    1.58  
    1.59 -    if (!fromcode || !*fromcode) {
    1.60 -        fromcode = getlocale();
    1.61 +    cd = SDL_iconv_open(tocode, fromcode);
    1.62 +    if (cd == (SDL_iconv_t) - 1) {
    1.63 +        /* See if we can recover here (fixes iconv on Solaris 11) */
    1.64 +        if (!tocode || !*tocode) {
    1.65 +            tocode = "UTF-8";
    1.66 +        }
    1.67 +        if (!fromcode || !*fromcode) {
    1.68 +            tocode = "UTF-8";
    1.69 +        }
    1.70 +        cd = SDL_iconv_open(tocode, fromcode);
    1.71      }
    1.72 -    if (!tocode || !*tocode) {
    1.73 -        tocode = getlocale();
    1.74 -    }
    1.75 -    cd = SDL_iconv_open(tocode, fromcode);
    1.76      if (cd == (SDL_iconv_t) - 1) {
    1.77          return NULL;
    1.78      }