changeset 2135:0313af081a84

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?
author Sam Lantinga <slouken@libsdl.org>
date Thu, 28 Jun 2007 06:57:08 +0000
parents 180fa05e98e2
children 5a1b6fe6e90f
files include/SDL_stdinc.h src/stdlib/SDL_iconv.c
diffstat 2 files changed, 24 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/include/SDL_stdinc.h	Wed Jun 27 10:17:35 2007 +0000
+++ b/include/SDL_stdinc.h	Thu Jun 28 06:57:08 2007 +0000
@@ -640,24 +640,21 @@
 #define SDL_iconv_t     iconv_t
 #define SDL_iconv_open  iconv_open
 #define SDL_iconv_close iconv_close
-extern DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, char **inbuf,
-                                         size_t * inbytesleft, char **outbuf,
-                                         size_t * outbytesleft);
 #else
 typedef struct _SDL_iconv_t *SDL_iconv_t;
 extern DECLSPEC SDL_iconv_t SDLCALL SDL_iconv_open(const char *tocode,
                                                    const char *fromcode);
 extern DECLSPEC int SDLCALL SDL_iconv_close(SDL_iconv_t cd);
-extern DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, char **inbuf,
+#endif
+extern DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, const char **inbuf,
                                          size_t * inbytesleft, char **outbuf,
                                          size_t * outbytesleft);
-#endif
 /* This function converts a string between encodings in one pass, returning a
    string that must be freed with SDL_free() or NULL on error.
 */
 extern DECLSPEC char *SDLCALL SDL_iconv_string(const char *tocode,
                                                const char *fromcode,
-                                               char *inbuf,
+                                               const char *inbuf,
                                                size_t inbytesleft);
 #define SDL_iconv_utf8_ascii(S)		SDL_iconv_string("ASCII", "UTF-8", S, SDL_strlen(S)+1)
 #define SDL_iconv_utf8_latin1(S)	SDL_iconv_string("LATIN1", "UTF-8", S, SDL_strlen(S)+1)
--- a/src/stdlib/SDL_iconv.c	Wed Jun 27 10:17:35 2007 +0000
+++ b/src/stdlib/SDL_iconv.c	Thu Jun 28 06:57:08 2007 +0000
@@ -32,10 +32,24 @@
 
 size_t
 SDL_iconv(SDL_iconv_t cd,
-          char **inbuf, size_t * inbytesleft,
+          const char **inbuf, size_t * inbytesleft,
           char **outbuf, size_t * outbytesleft)
 {
-    size_t retCode = iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
+    size_t retCode;
+#ifdef ICONV_REALLY_MODIFIES_INBUF
+    if (inbuf && *inbuf && inbytesleft) {
+        char *tmp = SDL_stack_alloc(char, *inbytesleft);
+        char *ptr = tmp;
+        SDL_memcpy(tmp, inbuf, *inbytesleft);
+        retCode = iconv(cd, &ptr, inbytesleft, outbuf, outbytesleft);
+        inbuf += (ptr - tmp);
+        SDL_stack_free(tmp);
+    } else {
+        retCode = iconv(cd, NULL, inbytesleft, outbuf, outbytesleft);
+    }
+#else
+    retCode = iconv(cd, (char **) inbuf, inbytesleft, outbuf, outbytesleft);
+#endif
     if (retCode == (size_t) - 1) {
         switch (errno) {
         case E2BIG:
@@ -154,13 +168,14 @@
 
 size_t
 SDL_iconv(SDL_iconv_t cd,
-          char **inbuf, size_t * inbytesleft,
+          const char **inbuf, size_t * inbytesleft,
           char **outbuf, size_t * outbytesleft)
 {
     /* For simplicity, we'll convert everything to and from UCS-4 */
-    char *src, *dst;
+    const char *src;
+    char *dst;
     size_t srclen, dstlen;
-    Uint32 ch;
+    Uint32 ch = 0;
     size_t total;
 
     if (!inbuf || !*inbuf) {
@@ -755,7 +770,7 @@
 #endif /* !HAVE_ICONV */
 
 char *
-SDL_iconv_string(const char *tocode, const char *fromcode, char *inbuf,
+SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf,
                  size_t inbytesleft)
 {
     SDL_iconv_t cd;