From 82107df9ddbbf739b6daa08c391e15a0b9b1cdfd Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 26 Aug 2008 08:27:14 +0200 Subject: wavpack: moved code to wavpack_open_wvc() Move everything related to finding and initializing the WVC stream to wavpack_open_wvc(). This greatly simplifies its error handling and the function wavpack_streamdecode(). --- src/inputPlugins/wavpack_plugin.c | 136 ++++++++++++++++++-------------------- 1 file changed, 66 insertions(+), 70 deletions(-) (limited to 'src/inputPlugins') diff --git a/src/inputPlugins/wavpack_plugin.c b/src/inputPlugins/wavpack_plugin.c index 4199dd946..5e593ad98 100644 --- a/src/inputPlugins/wavpack_plugin.c +++ b/src/inputPlugins/wavpack_plugin.c @@ -424,87 +424,84 @@ static unsigned int wavpack_trydecode(InputStream *is) return 1; } -/* - * Decodes a stream. - */ -static int wavpack_streamdecode(InputStream *is) +/* wvc being the "correction" file to supplement the original .wv */ +static int wavpack_open_wvc(InputStream *is_wvc) { - char error[ERRORLEN]; - WavpackContext *wpc; - InputStream is_wvc; - int open_flags = OPEN_2CH_MAX | OPEN_NORMALIZE /*| OPEN_STREAMING*/; - char *wvc_url = NULL; - int err; - InputStreamPlus isp, isp_wvc; - int canseek; + char wvc_url[MPD_PATH_MAX]; + size_t len; - /* Try to find wvc */ - /* wvc being the "correction" file to supplement the original .wv */ - do { - char tmp[MPD_PATH_MAX]; - const char *utf8url; - size_t len; - err = 1; + /* This is the only reader of dc.current_song */ + if (!get_song_url(wvc_url, dc.current_song)) + return 0; - /* This is the only reader of dc.current_song */ - if (!(utf8url = get_song_url(tmp, dc.current_song))) - break; + len = strlen(wvc_url); + if ((len + 2) >= MPD_PATH_MAX) + return 0; - if (!(len = strlen(utf8url))) - break; + /* convert the original ".wv" path to a ".wvc" path */ + assert(wvc_url[len - 3] == '.'); + assert(wvc_url[len - 2] == 'w' || wvc_url[len - 2] == 'w'); + assert(wvc_url[len - 1] == 'v' || wvc_url[len - 1] == 'V'); + assert(wvc_url[len] == '\0'); - wvc_url = (char *)xmalloc(len + sizeof("c")); - memcpy(wvc_url, utf8url, len); - wvc_url[len] = 'c'; - wvc_url[len + 1] = '\0'; + wvc_url[len] = 'c'; + wvc_url[len + 1] = '\0'; - if (openInputStream(&is_wvc, wvc_url)) + if (openInputStream(is_wvc, wvc_url) < 0) { + /* lowercase 'c' didn't work, maybe uppercase... */ + wvc_url[len] = 'C'; + if (openInputStream(is_wvc, wvc_url) < 0) + return 0; + } + + /* + * And we try to buffer in order to get know + * about a possible 404 error. + */ + for (;;) { + if (inputStreamAtEOF(is_wvc)) + /* + * EOF is reached even without + * a single byte is read... + * So, this is not good :/ + */ break; - /* - * And we try to buffer in order to get know - * about a possible 404 error. - */ - for (;;) { - if (inputStreamAtEOF(&is_wvc)) { - /* - * EOF is reached even without - * a single byte is read... - * So, this is not good :/ - */ - break; - } + /* FIXME: replace with future "peek" function */ + if (bufferInputStream(is_wvc) >= 0) { + DEBUG("wavpack: got wvc file: %s\n", wvc_url); + return 1; /* success */ + } - /* FIXME: replace with future "peek" function */ - if (bufferInputStream(&is_wvc) >= 0) { - err = 0; - break; - } + if (dc_intr()) + break; + /* Save some CPU */ + my_usleep(1000); /* FIXME: remove */ + } - if (dc_intr()) - break; + closeInputStream(is_wvc); + return 0; +} - /* Save some CPU */ - my_usleep(1000); /* FIXME: remove */ - } - if (err) { - closeInputStream(&is_wvc); - break; - } - open_flags |= OPEN_WVC; +/* + * Decodes a stream. + */ +static int wavpack_streamdecode(InputStream *is) +{ + char error[ERRORLEN]; + WavpackContext *wpc; + InputStream is_wvc; + int open_flags = OPEN_2CH_MAX | OPEN_NORMALIZE /*| OPEN_STREAMING*/; + InputStreamPlus isp, isp_wvc; + int canseek; - } while (0); + if (wavpack_open_wvc(&is_wvc)) { + initInputStreamPlus(&isp_wvc, &is_wvc); + open_flags |= OPEN_WVC; + } canseek = can_seek(&isp); - if (wvc_url != NULL) { - if (err) { - free(wvc_url); - wvc_url = NULL; - } else { - initInputStreamPlus(&isp_wvc, &is_wvc); - } - } initInputStreamPlus(&isp, is); wpc = WavpackOpenFileInputEx(&mpd_is_reader, &isp, &isp_wvc, error, @@ -512,17 +509,16 @@ static int wavpack_streamdecode(InputStream *is) if (wpc == NULL) { ERROR("failed to open WavPack stream: %s\n", error); + if (open_flags & OPEN_WVC) + closeInputStream(&is_wvc); return -1; } wavpack_decode(wpc, canseek, NULL); WavpackCloseFile(wpc); - if (wvc_url != NULL) { + if (open_flags & OPEN_WVC) closeInputStream(&is_wvc); - free(wvc_url); - } - closeInputStream(is); return 0; } -- cgit v1.2.3