comparison src/stdlib/SDL_iconv.c @ 4053:0aadbc81c497 SDL-1.2

Fixed bug #455 If passed "" for the character set, let iconv_open() interpret it as locale.
author Sam Lantinga <slouken@libsdl.org>
date Thu, 12 Jul 2007 07:29:19 +0000
parents 098ac044cd2f
children 64bf737873a2
comparison
equal deleted inserted replaced
4052:b004c6c24a98 4053:0aadbc81c497
105 105
106 static struct { 106 static struct {
107 const char *name; 107 const char *name;
108 int format; 108 int format;
109 } encodings[] = { 109 } encodings[] = {
110 { "646", ENCODING_ASCII },
111 { "ASCII", ENCODING_ASCII }, 110 { "ASCII", ENCODING_ASCII },
112 { "US-ASCII", ENCODING_ASCII }, 111 { "US-ASCII", ENCODING_ASCII },
113 { "LATIN1", ENCODING_LATIN1 },
114 { "8859-1", ENCODING_LATIN1 }, 112 { "8859-1", ENCODING_LATIN1 },
115 { "ISO-8859-1", ENCODING_LATIN1 }, 113 { "ISO-8859-1", ENCODING_LATIN1 },
116 { "UTF8", ENCODING_UTF8 }, 114 { "UTF8", ENCODING_UTF8 },
117 { "UTF-8", ENCODING_UTF8 }, 115 { "UTF-8", ENCODING_UTF8 },
118 { "UTF16", ENCODING_UTF16 }, 116 { "UTF16", ENCODING_UTF16 },
131 { "UCS-2", ENCODING_UCS2 }, 129 { "UCS-2", ENCODING_UCS2 },
132 { "UCS4", ENCODING_UCS4 }, 130 { "UCS4", ENCODING_UCS4 },
133 { "UCS-4", ENCODING_UCS4 }, 131 { "UCS-4", ENCODING_UCS4 },
134 }; 132 };
135 133
134 static const char *getlocale()
135 {
136 const char *lang;
137
138 lang = SDL_getenv("LC_ALL");
139 if ( !lang ) {
140 lang = SDL_getenv("LC_CTYPE");
141 }
142 if ( !lang ) {
143 lang = SDL_getenv("LC_MESSAGES");
144 }
145 if ( !lang ) {
146 lang = SDL_getenv("LANG");
147 }
148 if ( !lang || !*lang || SDL_strcmp(lang, "C") == 0 ) {
149 lang = "ASCII";
150 }
151 return lang;
152 }
153
136 SDL_iconv_t SDL_iconv_open(const char *tocode, const char *fromcode) 154 SDL_iconv_t SDL_iconv_open(const char *tocode, const char *fromcode)
137 { 155 {
138 int src_fmt = ENCODING_UNKNOWN; 156 int src_fmt = ENCODING_UNKNOWN;
139 int dst_fmt = ENCODING_UNKNOWN; 157 int dst_fmt = ENCODING_UNKNOWN;
140 int i; 158 int i;
141 159
160 if ( !fromcode || !*fromcode ) {
161 fromcode = getlocale();
162 }
163 if ( !tocode || !*tocode ) {
164 fromcode = getlocale();
165 }
142 for ( i = 0; i < SDL_arraysize(encodings); ++i ) { 166 for ( i = 0; i < SDL_arraysize(encodings); ++i ) {
143 if ( SDL_strcasecmp(fromcode, encodings[i].name) == 0 ) { 167 if ( SDL_strcasecmp(fromcode, encodings[i].name) == 0 ) {
144 src_fmt = encodings[i].format; 168 src_fmt = encodings[i].format;
145 if ( dst_fmt != ENCODING_UNKNOWN ) { 169 if ( dst_fmt != ENCODING_UNKNOWN ) {
146 break; 170 break;
770 return 0; 794 return 0;
771 } 795 }
772 796
773 #endif /* !HAVE_ICONV */ 797 #endif /* !HAVE_ICONV */
774 798
775 static const char *getlocale()
776 {
777 const char *lang;
778
779 lang = SDL_getenv("LC_ALL");
780 if ( !lang ) {
781 lang = SDL_getenv("LC_CTYPE");
782 }
783 if ( !lang ) {
784 lang = SDL_getenv("LC_MESSAGES");
785 }
786 if ( !lang ) {
787 lang = SDL_getenv("LANG");
788 }
789 if ( !lang || SDL_strcmp(lang, "C") == 0 ) {
790 lang = "ASCII";
791 }
792 return lang;
793 }
794
795 char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf, size_t inbytesleft) 799 char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf, size_t inbytesleft)
796 { 800 {
797 SDL_iconv_t cd; 801 SDL_iconv_t cd;
798 char *string; 802 char *string;
799 size_t stringsize; 803 size_t stringsize;
800 char *outbuf; 804 char *outbuf;
801 size_t outbytesleft; 805 size_t outbytesleft;
802 size_t retCode = 0; 806 size_t retCode = 0;
803 807
804 if ( !fromcode || !*fromcode ) {
805 fromcode = getlocale();
806 }
807 if ( !tocode || !*tocode ) {
808 tocode = getlocale();
809 }
810 cd = SDL_iconv_open(tocode, fromcode); 808 cd = SDL_iconv_open(tocode, fromcode);
811 if ( cd == (SDL_iconv_t)-1 ) { 809 if ( cd == (SDL_iconv_t)-1 ) {
812 return NULL; 810 return NULL;
813 } 811 }
814 812