src/stdlib/SDL_iconv.c
changeset 2135 0313af081a84
parent 1895 c121d94672cb
child 2136 5a1b6fe6e90f
equal deleted inserted replaced
2134:180fa05e98e2 2135:0313af081a84
    30 
    30 
    31 #include <errno.h>
    31 #include <errno.h>
    32 
    32 
    33 size_t
    33 size_t
    34 SDL_iconv(SDL_iconv_t cd,
    34 SDL_iconv(SDL_iconv_t cd,
    35           char **inbuf, size_t * inbytesleft,
    35           const char **inbuf, size_t * inbytesleft,
    36           char **outbuf, size_t * outbytesleft)
    36           char **outbuf, size_t * outbytesleft)
    37 {
    37 {
    38     size_t retCode = iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
    38     size_t retCode;
       
    39 #ifdef ICONV_REALLY_MODIFIES_INBUF
       
    40     if (inbuf && *inbuf && inbytesleft) {
       
    41         char *tmp = SDL_stack_alloc(char, *inbytesleft);
       
    42         char *ptr = tmp;
       
    43         SDL_memcpy(tmp, inbuf, *inbytesleft);
       
    44         retCode = iconv(cd, &ptr, inbytesleft, outbuf, outbytesleft);
       
    45         inbuf += (ptr - tmp);
       
    46         SDL_stack_free(tmp);
       
    47     } else {
       
    48         retCode = iconv(cd, NULL, inbytesleft, outbuf, outbytesleft);
       
    49     }
       
    50 #else
       
    51     retCode = iconv(cd, (char **) inbuf, inbytesleft, outbuf, outbytesleft);
       
    52 #endif
    39     if (retCode == (size_t) - 1) {
    53     if (retCode == (size_t) - 1) {
    40         switch (errno) {
    54         switch (errno) {
    41         case E2BIG:
    55         case E2BIG:
    42             return SDL_ICONV_E2BIG;
    56             return SDL_ICONV_E2BIG;
    43         case EILSEQ:
    57         case EILSEQ:
   152     return (SDL_iconv_t) - 1;
   166     return (SDL_iconv_t) - 1;
   153 }
   167 }
   154 
   168 
   155 size_t
   169 size_t
   156 SDL_iconv(SDL_iconv_t cd,
   170 SDL_iconv(SDL_iconv_t cd,
   157           char **inbuf, size_t * inbytesleft,
   171           const char **inbuf, size_t * inbytesleft,
   158           char **outbuf, size_t * outbytesleft)
   172           char **outbuf, size_t * outbytesleft)
   159 {
   173 {
   160     /* For simplicity, we'll convert everything to and from UCS-4 */
   174     /* For simplicity, we'll convert everything to and from UCS-4 */
   161     char *src, *dst;
   175     const char *src;
       
   176     char *dst;
   162     size_t srclen, dstlen;
   177     size_t srclen, dstlen;
   163     Uint32 ch;
   178     Uint32 ch = 0;
   164     size_t total;
   179     size_t total;
   165 
   180 
   166     if (!inbuf || !*inbuf) {
   181     if (!inbuf || !*inbuf) {
   167         /* Reset the context */
   182         /* Reset the context */
   168         return 0;
   183         return 0;
   753 }
   768 }
   754 
   769 
   755 #endif /* !HAVE_ICONV */
   770 #endif /* !HAVE_ICONV */
   756 
   771 
   757 char *
   772 char *
   758 SDL_iconv_string(const char *tocode, const char *fromcode, char *inbuf,
   773 SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf,
   759                  size_t inbytesleft)
   774                  size_t inbytesleft)
   760 {
   775 {
   761     SDL_iconv_t cd;
   776     SDL_iconv_t cd;
   762     char *string;
   777     char *string;
   763     size_t stringsize;
   778     size_t stringsize;