deconstify input buffer for iconv()

Unfortunately, the function iconv() wants a non-const input buffer.
In this context, we only have a const pointer, which emits a correct
gcc warning.  Work around this ugliness with an union-deconst hack.
This is optimized away in the binary.

git-svn-id: https://svn.musicpd.org/mpd/trunk@7229 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
Max Kellermann 2008-04-12 04:06:45 +00:00 committed by Eric Wong
parent 51f5bf932c
commit c011ab8105

View File

@ -93,6 +93,22 @@ int setCharSetConversion(const char *to, const char *from)
#endif #endif
} }
#ifdef HAVE_ICONV
static inline size_t deconst_iconv(iconv_t cd,
const char **inbuf, size_t *inbytesleft,
char **outbuf, size_t *outbytesleft)
{
union {
const char **a;
char **b;
} deconst;
deconst.a = inbuf;
return iconv(cd, deconst.b, inbytesleft, outbuf, outbytesleft);
}
#endif
char *char_conv_str(char *dest, const char *string) char *char_conv_str(char *dest, const char *string)
{ {
if (!char_conv_to) if (!char_conv_to)
@ -117,8 +133,8 @@ char *char_conv_str(char *dest, const char *string)
bufferPtr = buffer; bufferPtr = buffer;
outleft = BUFFER_SIZE; outleft = BUFFER_SIZE;
err = err =
iconv(char_conv_iconv, &string, &inleft, &bufferPtr, deconst_iconv(char_conv_iconv, &string, &inleft,
&outleft); &bufferPtr, &outleft);
if (outleft == BUFFER_SIZE if (outleft == BUFFER_SIZE
|| (err == (size_t)-1L && errno != E2BIG)) { || (err == (size_t)-1L && errno != E2BIG)) {
return NULL; return NULL;