From 0a28cb55e5dd6e8914e7dbc6c0a3b6284501a64a Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 4 Jul 2007 08:01:04 +0000 Subject: [PATCH] Fix for bug #447 merged from SDL 1.2 --- include/SDL_stdinc.h | 3 +-- src/stdlib/SDL_iconv.c | 27 +++++++++++++++++++++++++++ src/video/x11/SDL_x11window.c | 16 ++++++++-------- test/testiconv.c | 4 ++-- 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/include/SDL_stdinc.h b/include/SDL_stdinc.h index 548d7d858..f86bd85db 100644 --- a/include/SDL_stdinc.h +++ b/include/SDL_stdinc.h @@ -656,8 +656,7 @@ extern DECLSPEC char *SDLCALL SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf, size_t inbytesleft); -#define SDL_iconv_utf8_ascii(S) SDL_iconv_string("646", "UTF-8", S, SDL_strlen(S)+1) -#define SDL_iconv_utf8_latin1(S) SDL_iconv_string("8859-1", "UTF-8", S, SDL_strlen(S)+1) +#define SDL_iconv_utf8_locale(S) SDL_iconv_string("", "UTF-8", S, SDL_strlen(S)+1) #define SDL_iconv_utf8_ucs2(S) (Uint16 *)SDL_iconv_string("UCS-2", "UTF-8", S, SDL_strlen(S)+1) #define SDL_iconv_utf8_ucs4(S) (Uint32 *)SDL_iconv_string("UCS-4", "UTF-8", S, SDL_strlen(S)+1) diff --git a/src/stdlib/SDL_iconv.c b/src/stdlib/SDL_iconv.c index 54d3777fd..fc6acfb7f 100644 --- a/src/stdlib/SDL_iconv.c +++ b/src/stdlib/SDL_iconv.c @@ -773,6 +773,27 @@ SDL_iconv_close(SDL_iconv_t cd) #endif /* !HAVE_ICONV */ +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 || SDL_strcmp(lang, "C") == 0) { + lang = "ASCII"; + } + return lang; +} + char * SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf, size_t inbytesleft) @@ -784,6 +805,12 @@ SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf, size_t outbytesleft; size_t retCode = 0; + if (!fromcode || !*fromcode) { + fromcode = getlocale(); + } + if (!tocode || !*tocode) { + tocode = getlocale(); + } cd = SDL_iconv_open(tocode, fromcode); if (cd == (SDL_iconv_t) - 1) { return NULL; diff --git a/src/video/x11/SDL_x11window.c b/src/video/x11/SDL_x11window.c index 4d4b4f3dd..7082cdf5c 100644 --- a/src/video/x11/SDL_x11window.c +++ b/src/video/x11/SDL_x11window.c @@ -429,13 +429,13 @@ X11_SetWindowTitle(_THIS, SDL_Window * window) #endif if (title != NULL) { - char *title_latin1 = SDL_iconv_utf8_latin1((char *) title); - if (!title_latin1) { + char *title_locale = SDL_iconv_utf8_locale(title); + if (!title_locale) { SDL_OutOfMemory(); return; } - status = XStringListToTextProperty(&title_latin1, 1, &titleprop); - SDL_free(title_latin1); + status = XStringListToTextProperty(&title_locale, 1, &titleprop); + SDL_free(title_locale); if (status) { XSetTextProperty(display, data->window, &titleprop, XA_WM_NAME); XFree(titleprop.value); @@ -454,13 +454,13 @@ X11_SetWindowTitle(_THIS, SDL_Window * window) #endif } if (icon != NULL) { - char *icon_latin1 = SDL_iconv_utf8_latin1((char *) icon); - if (!icon_latin1) { + char *icon_locale = SDL_iconv_utf8_locale(icon); + if (!icon_locale) { SDL_OutOfMemory(); return; } - status = XStringListToTextProperty(&icon_latin1, 1, &iconprop); - SDL_free(icon_latin1); + status = XStringListToTextProperty(&icon_locale, 1, &iconprop); + SDL_free(icon_locale); if (status) { XSetTextProperty(display, data->window, &iconprop, XA_WM_ICON_NAME); diff --git a/test/testiconv.c b/test/testiconv.c index ee5808604..0ffce65b6 100644 --- a/test/testiconv.c +++ b/test/testiconv.c @@ -61,10 +61,10 @@ main(int argc, char *argv[]) fprintf(stderr, "FAIL: %s\n", formats[i]); ++errors; } - if(test[0]) { + if (test[0]) { SDL_free(test[0]); } - if(test[1]) { + if (test[1]) { SDL_free(test[1]); } }