Fix for bug #447 merged from SDL 1.2
authorSam Lantinga <slouken@libsdl.org>
Wed, 04 Jul 2007 08:01:04 +0000
changeset 2143e906da4414a3
parent 2142 ca80c942e69c
child 2144 665c2669e6a3
Fix for bug #447 merged from SDL 1.2
include/SDL_stdinc.h
src/stdlib/SDL_iconv.c
src/video/x11/SDL_x11window.c
test/testiconv.c
     1.1 --- a/include/SDL_stdinc.h	Wed Jul 04 07:08:16 2007 +0000
     1.2 +++ b/include/SDL_stdinc.h	Wed Jul 04 08:01:04 2007 +0000
     1.3 @@ -656,8 +656,7 @@
     1.4                                                 const char *fromcode,
     1.5                                                 const char *inbuf,
     1.6                                                 size_t inbytesleft);
     1.7 -#define SDL_iconv_utf8_ascii(S)		SDL_iconv_string("646", "UTF-8", S, SDL_strlen(S)+1)
     1.8 -#define SDL_iconv_utf8_latin1(S)	SDL_iconv_string("8859-1", "UTF-8", S, SDL_strlen(S)+1)
     1.9 +#define SDL_iconv_utf8_locale(S)	SDL_iconv_string("", "UTF-8", S, SDL_strlen(S)+1)
    1.10  #define SDL_iconv_utf8_ucs2(S)		(Uint16 *)SDL_iconv_string("UCS-2", "UTF-8", S, SDL_strlen(S)+1)
    1.11  #define SDL_iconv_utf8_ucs4(S)		(Uint32 *)SDL_iconv_string("UCS-4", "UTF-8", S, SDL_strlen(S)+1)
    1.12  
     2.1 --- a/src/stdlib/SDL_iconv.c	Wed Jul 04 07:08:16 2007 +0000
     2.2 +++ b/src/stdlib/SDL_iconv.c	Wed Jul 04 08:01:04 2007 +0000
     2.3 @@ -773,6 +773,27 @@
     2.4  
     2.5  #endif /* !HAVE_ICONV */
     2.6  
     2.7 +static const char *
     2.8 +getlocale()
     2.9 +{
    2.10 +    const char *lang;
    2.11 +
    2.12 +    lang = SDL_getenv("LC_ALL");
    2.13 +    if (!lang) {
    2.14 +        lang = SDL_getenv("LC_CTYPE");
    2.15 +    }
    2.16 +    if (!lang) {
    2.17 +        lang = SDL_getenv("LC_MESSAGES");
    2.18 +    }
    2.19 +    if (!lang) {
    2.20 +        lang = SDL_getenv("LANG");
    2.21 +    }
    2.22 +    if (!lang || SDL_strcmp(lang, "C") == 0) {
    2.23 +        lang = "ASCII";
    2.24 +    }
    2.25 +    return lang;
    2.26 +}
    2.27 +
    2.28  char *
    2.29  SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf,
    2.30                   size_t inbytesleft)
    2.31 @@ -784,6 +805,12 @@
    2.32      size_t outbytesleft;
    2.33      size_t retCode = 0;
    2.34  
    2.35 +    if (!fromcode || !*fromcode) {
    2.36 +        fromcode = getlocale();
    2.37 +    }
    2.38 +    if (!tocode || !*tocode) {
    2.39 +        tocode = getlocale();
    2.40 +    }
    2.41      cd = SDL_iconv_open(tocode, fromcode);
    2.42      if (cd == (SDL_iconv_t) - 1) {
    2.43          return NULL;
     3.1 --- a/src/video/x11/SDL_x11window.c	Wed Jul 04 07:08:16 2007 +0000
     3.2 +++ b/src/video/x11/SDL_x11window.c	Wed Jul 04 08:01:04 2007 +0000
     3.3 @@ -429,13 +429,13 @@
     3.4  #endif
     3.5  
     3.6      if (title != NULL) {
     3.7 -        char *title_latin1 = SDL_iconv_utf8_latin1((char *) title);
     3.8 -        if (!title_latin1) {
     3.9 +        char *title_locale = SDL_iconv_utf8_locale(title);
    3.10 +        if (!title_locale) {
    3.11              SDL_OutOfMemory();
    3.12              return;
    3.13          }
    3.14 -        status = XStringListToTextProperty(&title_latin1, 1, &titleprop);
    3.15 -        SDL_free(title_latin1);
    3.16 +        status = XStringListToTextProperty(&title_locale, 1, &titleprop);
    3.17 +        SDL_free(title_locale);
    3.18          if (status) {
    3.19              XSetTextProperty(display, data->window, &titleprop, XA_WM_NAME);
    3.20              XFree(titleprop.value);
    3.21 @@ -454,13 +454,13 @@
    3.22  #endif
    3.23      }
    3.24      if (icon != NULL) {
    3.25 -        char *icon_latin1 = SDL_iconv_utf8_latin1((char *) icon);
    3.26 -        if (!icon_latin1) {
    3.27 +        char *icon_locale = SDL_iconv_utf8_locale(icon);
    3.28 +        if (!icon_locale) {
    3.29              SDL_OutOfMemory();
    3.30              return;
    3.31          }
    3.32 -        status = XStringListToTextProperty(&icon_latin1, 1, &iconprop);
    3.33 -        SDL_free(icon_latin1);
    3.34 +        status = XStringListToTextProperty(&icon_locale, 1, &iconprop);
    3.35 +        SDL_free(icon_locale);
    3.36          if (status) {
    3.37              XSetTextProperty(display, data->window, &iconprop,
    3.38                               XA_WM_ICON_NAME);
     4.1 --- a/test/testiconv.c	Wed Jul 04 07:08:16 2007 +0000
     4.2 +++ b/test/testiconv.c	Wed Jul 04 08:01:04 2007 +0000
     4.3 @@ -61,10 +61,10 @@
     4.4                  fprintf(stderr, "FAIL: %s\n", formats[i]);
     4.5                  ++errors;
     4.6              }
     4.7 -            if(test[0]) {
     4.8 +            if (test[0]) {
     4.9                  SDL_free(test[0]);
    4.10              }
    4.11 -            if(test[1]) {
    4.12 +            if (test[1]) {
    4.13                  SDL_free(test[1]);
    4.14              }
    4.15          }