~ubuntu-branches/ubuntu/karmic/libsdl1.2/karmic

« back to all changes in this revision

Viewing changes to src/stdlib/SDL_iconv.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-12-05 20:29:43 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20071205202943-ryogi07hodn5cdif
Tags: 1.2.12-1ubuntu1
* Merge with Debian; remaining changes:
  - Remove svgalib support.
  - Prefer libgl1-mesa-dev build-dependency over xlibmesa-gl-dev.
  - Build for lpia as for i386.
* Link using -Wl,-Bsymbolic-functions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
#ifdef HAVE_ICONV
30
30
 
 
31
/* Depending on which standard the iconv() was implemented with,
 
32
   iconv() may or may not use const char ** for the inbuf param.
 
33
   If we get this wrong, it's just a warning, so no big deal.
 
34
*/
 
35
#if defined(_XGP6) || \
 
36
    defined(__GLIBC__) && ((__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2))
 
37
#define ICONV_INBUF_NONCONST
 
38
#endif
 
39
 
31
40
#include <errno.h>
32
41
 
33
42
size_t SDL_iconv(SDL_iconv_t cd,
34
 
                 char **inbuf, size_t *inbytesleft,
 
43
                 const char **inbuf, size_t *inbytesleft,
35
44
                 char **outbuf, size_t *outbytesleft)
36
45
{
37
 
        size_t retCode = iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
 
46
        size_t retCode;
 
47
#ifdef ICONV_INBUF_NONCONST
 
48
        retCode = iconv(cd, (char **)inbuf, inbytesleft, outbuf, outbytesleft);
 
49
#else
 
50
        retCode = iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
 
51
#endif
38
52
        if ( retCode == (size_t)-1 ) {
39
53
                switch(errno) {
40
54
                    case E2BIG:
95
109
} encodings[] = {
96
110
        { "ASCII",      ENCODING_ASCII },
97
111
        { "US-ASCII",   ENCODING_ASCII },
98
 
        { "LATIN1",     ENCODING_LATIN1 },
 
112
        { "8859-1",     ENCODING_LATIN1 },
99
113
        { "ISO-8859-1", ENCODING_LATIN1 },
100
114
        { "UTF8",       ENCODING_UTF8 },
101
115
        { "UTF-8",      ENCODING_UTF8 },
117
131
        { "UCS-4",      ENCODING_UCS4 },
118
132
};
119
133
 
 
134
static const char *getlocale(char *buffer, size_t bufsize)
 
135
{
 
136
        const char *lang;
 
137
        char *ptr;
 
138
 
 
139
        lang = SDL_getenv("LC_ALL");
 
140
        if ( !lang ) {
 
141
                lang = SDL_getenv("LC_CTYPE");
 
142
        }
 
143
        if ( !lang ) {
 
144
                lang = SDL_getenv("LC_MESSAGES");
 
145
        }
 
146
        if ( !lang ) {
 
147
                lang = SDL_getenv("LANG");
 
148
        }
 
149
        if ( !lang || !*lang || SDL_strcmp(lang, "C") == 0 ) {
 
150
                lang = "ASCII";
 
151
        }
 
152
 
 
153
        /* We need to trim down strings like "en_US.UTF-8@blah" to "UTF-8" */
 
154
        ptr = SDL_strchr(lang, '.');
 
155
        if (ptr != NULL) {
 
156
                lang = ptr + 1;
 
157
        }
 
158
 
 
159
        SDL_strlcpy(buffer, lang, bufsize);
 
160
        ptr = SDL_strchr(buffer, '@');
 
161
        if (ptr != NULL) {
 
162
                *ptr = '\0';  /* chop end of string. */
 
163
        }
 
164
 
 
165
        return buffer;
 
166
}
 
167
 
120
168
SDL_iconv_t SDL_iconv_open(const char *tocode, const char *fromcode)
121
169
{
122
170
        int src_fmt = ENCODING_UNKNOWN;
123
171
        int dst_fmt = ENCODING_UNKNOWN;
124
172
        int i;
 
173
        char fromcode_buffer[64];
 
174
        char tocode_buffer[64];
125
175
 
 
176
        if ( !fromcode || !*fromcode ) {
 
177
                fromcode = getlocale(fromcode_buffer, sizeof(fromcode_buffer));
 
178
        }
 
179
        if ( !tocode || !*tocode ) {
 
180
                tocode = getlocale(tocode_buffer, sizeof(tocode_buffer));
 
181
        }
126
182
        for ( i = 0; i < SDL_arraysize(encodings); ++i ) {
127
183
                if ( SDL_strcasecmp(fromcode, encodings[i].name) == 0 ) {
128
184
                        src_fmt = encodings[i].format;
149
205
}
150
206
 
151
207
size_t SDL_iconv(SDL_iconv_t cd,
152
 
                 char **inbuf, size_t *inbytesleft,
 
208
                 const char **inbuf, size_t *inbytesleft,
153
209
                 char **outbuf, size_t *outbytesleft)
154
210
{
155
211
        /* For simplicity, we'll convert everything to and from UCS-4 */
156
 
        char *src, *dst;
 
212
        const char *src;
 
213
        char *dst;
157
214
        size_t srclen, dstlen;
158
215
        Uint32 ch = 0;
159
216
        size_t total;
755
812
 
756
813
#endif /* !HAVE_ICONV */
757
814
 
758
 
char *SDL_iconv_string(const char *tocode, const char *fromcode, char *inbuf, size_t inbytesleft)
 
815
char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf, size_t inbytesleft)
759
816
{
760
817
        SDL_iconv_t cd;
761
818
        char *string;
766
823
 
767
824
        cd = SDL_iconv_open(tocode, fromcode);
768
825
        if ( cd == (SDL_iconv_t)-1 ) {
 
826
                /* See if we can recover here (fixes iconv on Solaris 11) */
 
827
                if ( !tocode || !*tocode ) {
 
828
                        tocode = "UTF-8";
 
829
                }
 
830
                if ( !fromcode || !*fromcode ) {
 
831
                        tocode = "UTF-8";
 
832
                }
 
833
                cd = SDL_iconv_open(tocode, fromcode);
 
834
        }
 
835
        if ( cd == (SDL_iconv_t)-1 ) {
769
836
                return NULL;
770
837
        }
771
838