comparison src/stdlib/SDL_iconv.c @ 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 e906da4414a3
children 9f31740cad2e
comparison
equal deleted inserted replaced
2181:791cdc226105 2182:cc2597da0840
109 { 109 {
110 const char *name; 110 const char *name;
111 int format; 111 int format;
112 } encodings[] = { 112 } encodings[] = {
113 /* *INDENT-OFF* */ 113 /* *INDENT-OFF* */
114 { "646", ENCODING_ASCII },
115 { "ASCII", ENCODING_ASCII }, 114 { "ASCII", ENCODING_ASCII },
116 { "US-ASCII", ENCODING_ASCII }, 115 { "US-ASCII", ENCODING_ASCII },
117 { "LATIN1", ENCODING_LATIN1 },
118 { "8859-1", ENCODING_LATIN1 }, 116 { "8859-1", ENCODING_LATIN1 },
119 { "ISO-8859-1", ENCODING_LATIN1 }, 117 { "ISO-8859-1", ENCODING_LATIN1 },
120 { "UTF8", ENCODING_UTF8 }, 118 { "UTF8", ENCODING_UTF8 },
121 { "UTF-8", ENCODING_UTF8 }, 119 { "UTF-8", ENCODING_UTF8 },
122 { "UTF16", ENCODING_UTF16 }, 120 { "UTF16", ENCODING_UTF16 },
136 { "UCS4", ENCODING_UCS4 }, 134 { "UCS4", ENCODING_UCS4 },
137 { "UCS-4", ENCODING_UCS4 }, 135 { "UCS-4", ENCODING_UCS4 },
138 /* *INDENT-ON* */ 136 /* *INDENT-ON* */
139 }; 137 };
140 138
139 static const char *
140 getlocale()
141 {
142 const char *lang;
143
144 lang = SDL_getenv("LC_ALL");
145 if (!lang) {
146 lang = SDL_getenv("LC_CTYPE");
147 }
148 if (!lang) {
149 lang = SDL_getenv("LC_MESSAGES");
150 }
151 if (!lang) {
152 lang = SDL_getenv("LANG");
153 }
154 if (!lang || !*lang || SDL_strcmp(lang, "C") == 0) {
155 lang = "ASCII";
156 }
157 return lang;
158 }
159
141 SDL_iconv_t 160 SDL_iconv_t
142 SDL_iconv_open(const char *tocode, const char *fromcode) 161 SDL_iconv_open(const char *tocode, const char *fromcode)
143 { 162 {
144 int src_fmt = ENCODING_UNKNOWN; 163 int src_fmt = ENCODING_UNKNOWN;
145 int dst_fmt = ENCODING_UNKNOWN; 164 int dst_fmt = ENCODING_UNKNOWN;
146 int i; 165 int i;
147 166
167 if (!fromcode || !*fromcode) {
168 fromcode = getlocale();
169 }
170 if (!tocode || !*tocode) {
171 fromcode = getlocale();
172 }
148 for (i = 0; i < SDL_arraysize(encodings); ++i) { 173 for (i = 0; i < SDL_arraysize(encodings); ++i) {
149 if (SDL_strcasecmp(fromcode, encodings[i].name) == 0) { 174 if (SDL_strcasecmp(fromcode, encodings[i].name) == 0) {
150 src_fmt = encodings[i].format; 175 src_fmt = encodings[i].format;
151 if (dst_fmt != ENCODING_UNKNOWN) { 176 if (dst_fmt != ENCODING_UNKNOWN) {
152 break; 177 break;
803 size_t stringsize; 828 size_t stringsize;
804 char *outbuf; 829 char *outbuf;
805 size_t outbytesleft; 830 size_t outbytesleft;
806 size_t retCode = 0; 831 size_t retCode = 0;
807 832
808 if (!fromcode || !*fromcode) {
809 fromcode = getlocale();
810 }
811 if (!tocode || !*tocode) {
812 tocode = getlocale();
813 }
814 cd = SDL_iconv_open(tocode, fromcode); 833 cd = SDL_iconv_open(tocode, fromcode);
834 if (cd == (SDL_iconv_t) - 1) {
835 /* See if we can recover here (fixes iconv on Solaris 11) */
836 if (!tocode || !*tocode) {
837 tocode = "UTF-8";
838 }
839 if (!fromcode || !*fromcode) {
840 tocode = "UTF-8";
841 }
842 cd = SDL_iconv_open(tocode, fromcode);
843 }
815 if (cd == (SDL_iconv_t) - 1) { 844 if (cd == (SDL_iconv_t) - 1) {
816 return NULL; 845 return NULL;
817 } 846 }
818 847
819 stringsize = inbytesleft > 4 ? inbytesleft : 4; 848 stringsize = inbytesleft > 4 ? inbytesleft : 4;