From bbe4560b180e8c432c7eb7f53664605390bf67ca Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 1 Sep 2008 20:01:41 -0700 Subject: provide a generic deconst_ptr function This is generic enough to be used for various purposes. It will only deconst their argument to work around various braindead APIs without having to write a new wrapper each time we use one of those braindead APIs. It does not cast nor do do anything other than quietly remove the const qualifier for those braindead APIs. --- src/utils.h | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src') diff --git a/src/utils.h b/src/utils.h index 6a6e562cf..0001ba3c8 100644 --- a/src/utils.h +++ b/src/utils.h @@ -90,4 +90,15 @@ void xpthread_mutex_destroy(pthread_mutex_t *mutex); void xpthread_cond_destroy(pthread_cond_t *cond); +/* + * Work-arounds for braindead APIs that require non-const pointers: + * ao_play(), free(), vorbis_comment_add_tag(), iconv() + */ +static inline void * deconst_ptr(const void *ptr) +{ + union { const void *in; void *out; } u; + u.in = ptr; + return u.out; +} + #endif -- cgit v1.2.3 From 925843cfedcde221207ec1ab78f778dab22325f9 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 1 Sep 2008 20:08:55 -0700 Subject: use deconst_ptr instead of duplicating deconst logic --- src/audioOutputs/audioOutput_ao.c | 21 +++------------------ src/audioOutputs/audioOutput_shout.c | 12 ++++++------ src/charConv.c | 23 ++++------------------- src/list.c | 9 ++------- src/mpd_types.h | 5 ----- 5 files changed, 15 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/src/audioOutputs/audioOutput_ao.c b/src/audioOutputs/audioOutput_ao.c index e7e201add..ed8eaa796 100644 --- a/src/audioOutputs/audioOutput_ao.c +++ b/src/audioOutputs/audioOutput_ao.c @@ -199,23 +199,6 @@ static int audioOutputAo_openDevice(AudioOutput * audioOutput) return 0; } -/** - * For whatever reason, libao wants a non-const pointer. Let's hope - * it does not write to the buffer, and use the union deconst hack to - * work around this API misdesign. - */ -static int ao_play_deconst(ao_device *device, const void *output_samples, - uint_32 num_bytes) -{ - union { - const void *in; - void *out; - } u; - - u.in = output_samples; - return ao_play(device, u.out, num_bytes); -} - static int audioOutputAo_play(AudioOutput * audioOutput, const char *playChunk, size_t size) { @@ -229,7 +212,9 @@ static int audioOutputAo_play(AudioOutput * audioOutput, chunk_size = (size_t)ad->writeSize > size ? size : (size_t)ad->writeSize; - if (ao_play_deconst(ad->device, playChunk, chunk_size) == 0) { + if (!ao_play(ad->device, + (char *)deconst_ptr(playChunk), + (uint_32)chunk_size)) { audioOutputAo_error(); ERROR("closing audio device due to write error\n"); audioOutputAo_closeDevice(audioOutput); diff --git a/src/audioOutputs/audioOutput_shout.c b/src/audioOutputs/audioOutput_shout.c index 49d69eebd..c1f784986 100644 --- a/src/audioOutputs/audioOutput_shout.c +++ b/src/audioOutputs/audioOutput_shout.c @@ -390,11 +390,10 @@ static void myShout_closeDevice(AudioOutput * audioOutput) static void addTag(ShoutData *sd, const char *name, char *value) { - if (value) { - union const_hack u; - u.in = name; - vorbis_comment_add_tag(&(sd->vc), u.out, value); - } + if (value) + vorbis_comment_add_tag(&(sd->vc), + (char *)deconst_ptr(name), + value); } static void copyTagToVorbisComment(ShoutData * sd) @@ -637,7 +636,8 @@ static int myShout_play(AudioOutput * audioOutput, for (i = 0; i < samples; i++) { for (j = 0; j < sd->audioFormat->channels; j++) { - vorbbuf[j][i] = (*((mpd_sint16 *) playChunk)) / 32768.0; + vorbbuf[j][i] = + (*((mpd_sint16 *)deconst_ptr(playChunk))) / 32768.0; playChunk += bytes; } } diff --git a/src/charConv.c b/src/charConv.c index c205e93e4..60bcc655a 100644 --- a/src/charConv.c +++ b/src/charConv.c @@ -95,22 +95,6 @@ int setCharSetConversion(const char *to, const char *from) #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) { if (!char_conv_to) @@ -132,11 +116,12 @@ char *char_conv_str(char *dest, const char *string) dest[0] = '\0'; while (inleft) { + char *inbuf = deconst_ptr(string); + bufferPtr = buffer; outleft = BUFFER_SIZE; - err = - deconst_iconv(char_conv_iconv, &string, &inleft, - &bufferPtr, &outleft); + err = iconv(char_conv_iconv, &inbuf, + &inleft, &bufferPtr, &outleft); if (outleft == BUFFER_SIZE || (err == (size_t)-1L && errno != E2BIG)) { return NULL; diff --git a/src/list.c b/src/list.c index 0f1f31ad4..bc158c513 100644 --- a/src/list.c +++ b/src/list.c @@ -301,11 +301,6 @@ int deleteFromList(List * list, const char *key) return 1; } -static void free_const_string(const char *p) -{ - free((char *)p); -} - void deleteNodeFromList(List * list, ListNode * node) { assert(list != NULL); @@ -326,7 +321,7 @@ void deleteNodeFromList(List * list, ListNode * node) } if (list->strdupKeys) - free_const_string(node->key); + free(deconst_ptr(node->key)); free(node); list->numberOfNodes--; @@ -353,7 +348,7 @@ void freeList(void *list) while (tmpNode != NULL) { tmpNode2 = tmpNode->nextNode; if (((List *) list)->strdupKeys) - free_const_string(tmpNode->key); + free(deconst_ptr(tmpNode->key)); if (((List *) list)->freeDataFunc) { ((List *) list)->freeDataFunc(tmpNode->data); } diff --git a/src/mpd_types.h b/src/mpd_types.h index ed573c4a4..dbdfc6865 100644 --- a/src/mpd_types.h +++ b/src/mpd_types.h @@ -40,9 +40,4 @@ typedef unsigned long mpd_uint32; typedef signed long mpd_sint32; #endif -union const_hack { - const char *in; - char *out; -}; - #endif -- cgit v1.2.3