From 5e686add91a7c2a89b886d04136983480793a26f Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 11 Nov 2008 16:21:09 +0100 Subject: wavpack: added wavpack_tag_float() The function simplifies wavpack_replaygain(), because it already contains the float parser, and it works with a fixed buffer instead of doing expensive heap allocations. --- src/decoder/wavpack_plugin.c | 66 +++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 41 deletions(-) (limited to 'src/decoder/wavpack_plugin.c') diff --git a/src/decoder/wavpack_plugin.c b/src/decoder/wavpack_plugin.c index 07d363495..10dd7fd02 100644 --- a/src/decoder/wavpack_plugin.c +++ b/src/decoder/wavpack_plugin.c @@ -213,20 +213,22 @@ wavpack_decode(struct decoder * decoder, WavpackContext *wpc, bool canseek, } while (samplesgot == samplesreq); } -static char * -wavpack_tag(WavpackContext *wpc, const char *key) +/** + * Locate and parse a floating point tag. Returns true if it was + * found. + */ +static bool +wavpack_tag_float(WavpackContext *wpc, const char *key, float *value_r) { - char *value = NULL; - int size; - - size = WavpackGetTagItem(wpc, key, NULL, 0); - if (size > 0) { - size++; - value = g_malloc(size); - WavpackGetTagItem(wpc, key, value, size); - } + char buffer[64]; + int ret; - return value; + ret = WavpackGetTagItem(wpc, key, buffer, sizeof(buffer)); + if (ret <= 0) + return false; + + *value_r = atof(buffer); + return true; } static struct replay_gain_info * @@ -234,38 +236,20 @@ wavpack_replaygain(WavpackContext *wpc) { struct replay_gain_info *replay_gain_info; bool found = false; - char *value; replay_gain_info = replay_gain_info_new(); - value = wavpack_tag(wpc, "replaygain_track_gain"); - if (value) { - replay_gain_info->track_gain = atof(value); - free(value); - found = true; - } - - value = wavpack_tag(wpc, "replaygain_album_gain"); - if (value) { - replay_gain_info->album_gain = atof(value); - free(value); - found = true; - } - - value = wavpack_tag(wpc, "replaygain_track_peak"); - if (value) { - replay_gain_info->track_peak = atof(value); - free(value); - found = true; - } - - value = wavpack_tag(wpc, "replaygain_album_peak"); - if (value) { - replay_gain_info->album_peak = atof(value); - free(value); - found = true; - } - + found = wavpack_tag_float(wpc, "replaygain_track_gain", + &replay_gain_info->track_gain) + || + wavpack_tag_float(wpc, "replaygain_track_peak", + &replay_gain_info->track_peak) + || + wavpack_tag_float(wpc, "replaygain_album_gain", + &replay_gain_info->album_gain) + || + wavpack_tag_float(wpc, "replaygain_album_peak", + &replay_gain_info->album_peak); if (found) { return replay_gain_info; -- cgit v1.2.3