Merge iconv const changes from 1.2
authorSam Lantinga <slouken@libsdl.org>
Thu, 28 Jun 2007 06:57:08 +0000
changeset 21350313af081a84
parent 2134 180fa05e98e2
child 2136 5a1b6fe6e90f
Merge iconv const changes from 1.2
I'm not entirely happy with them. Maybe the right way to go is to leave
SDL_iconv() taking a non-const inbuf? How often are we converting const
strings anyway?
include/SDL_stdinc.h
src/stdlib/SDL_iconv.c
     1.1 --- a/include/SDL_stdinc.h	Wed Jun 27 10:17:35 2007 +0000
     1.2 +++ b/include/SDL_stdinc.h	Thu Jun 28 06:57:08 2007 +0000
     1.3 @@ -640,24 +640,21 @@
     1.4  #define SDL_iconv_t     iconv_t
     1.5  #define SDL_iconv_open  iconv_open
     1.6  #define SDL_iconv_close iconv_close
     1.7 -extern DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, char **inbuf,
     1.8 -                                         size_t * inbytesleft, char **outbuf,
     1.9 -                                         size_t * outbytesleft);
    1.10  #else
    1.11  typedef struct _SDL_iconv_t *SDL_iconv_t;
    1.12  extern DECLSPEC SDL_iconv_t SDLCALL SDL_iconv_open(const char *tocode,
    1.13                                                     const char *fromcode);
    1.14  extern DECLSPEC int SDLCALL SDL_iconv_close(SDL_iconv_t cd);
    1.15 -extern DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, char **inbuf,
    1.16 +#endif
    1.17 +extern DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, const char **inbuf,
    1.18                                           size_t * inbytesleft, char **outbuf,
    1.19                                           size_t * outbytesleft);
    1.20 -#endif
    1.21  /* This function converts a string between encodings in one pass, returning a
    1.22     string that must be freed with SDL_free() or NULL on error.
    1.23  */
    1.24  extern DECLSPEC char *SDLCALL SDL_iconv_string(const char *tocode,
    1.25                                                 const char *fromcode,
    1.26 -                                               char *inbuf,
    1.27 +                                               const char *inbuf,
    1.28                                                 size_t inbytesleft);
    1.29  #define SDL_iconv_utf8_ascii(S)		SDL_iconv_string("ASCII", "UTF-8", S, SDL_strlen(S)+1)
    1.30  #define SDL_iconv_utf8_latin1(S)	SDL_iconv_string("LATIN1", "UTF-8", S, SDL_strlen(S)+1)
     2.1 --- a/src/stdlib/SDL_iconv.c	Wed Jun 27 10:17:35 2007 +0000
     2.2 +++ b/src/stdlib/SDL_iconv.c	Thu Jun 28 06:57:08 2007 +0000
     2.3 @@ -32,10 +32,24 @@
     2.4  
     2.5  size_t
     2.6  SDL_iconv(SDL_iconv_t cd,
     2.7 -          char **inbuf, size_t * inbytesleft,
     2.8 +          const char **inbuf, size_t * inbytesleft,
     2.9            char **outbuf, size_t * outbytesleft)
    2.10  {
    2.11 -    size_t retCode = iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
    2.12 +    size_t retCode;
    2.13 +#ifdef ICONV_REALLY_MODIFIES_INBUF
    2.14 +    if (inbuf && *inbuf && inbytesleft) {
    2.15 +        char *tmp = SDL_stack_alloc(char, *inbytesleft);
    2.16 +        char *ptr = tmp;
    2.17 +        SDL_memcpy(tmp, inbuf, *inbytesleft);
    2.18 +        retCode = iconv(cd, &ptr, inbytesleft, outbuf, outbytesleft);
    2.19 +        inbuf += (ptr - tmp);
    2.20 +        SDL_stack_free(tmp);
    2.21 +    } else {
    2.22 +        retCode = iconv(cd, NULL, inbytesleft, outbuf, outbytesleft);
    2.23 +    }
    2.24 +#else
    2.25 +    retCode = iconv(cd, (char **) inbuf, inbytesleft, outbuf, outbytesleft);
    2.26 +#endif
    2.27      if (retCode == (size_t) - 1) {
    2.28          switch (errno) {
    2.29          case E2BIG:
    2.30 @@ -154,13 +168,14 @@
    2.31  
    2.32  size_t
    2.33  SDL_iconv(SDL_iconv_t cd,
    2.34 -          char **inbuf, size_t * inbytesleft,
    2.35 +          const char **inbuf, size_t * inbytesleft,
    2.36            char **outbuf, size_t * outbytesleft)
    2.37  {
    2.38      /* For simplicity, we'll convert everything to and from UCS-4 */
    2.39 -    char *src, *dst;
    2.40 +    const char *src;
    2.41 +    char *dst;
    2.42      size_t srclen, dstlen;
    2.43 -    Uint32 ch;
    2.44 +    Uint32 ch = 0;
    2.45      size_t total;
    2.46  
    2.47      if (!inbuf || !*inbuf) {
    2.48 @@ -755,7 +770,7 @@
    2.49  #endif /* !HAVE_ICONV */
    2.50  
    2.51  char *
    2.52 -SDL_iconv_string(const char *tocode, const char *fromcode, char *inbuf,
    2.53 +SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf,
    2.54                   size_t inbytesleft)
    2.55  {
    2.56      SDL_iconv_t cd;