oggvorbis: always allocate a tag object

Always allocate a new tag object before parsing the vorbis comments;
free it when it turns out to be empty.  This simplifies the code a
bit.
This commit is contained in:
Max Kellermann 2009-01-14 23:09:31 +01:00
parent b5cadc9c04
commit 5a26c949bb

View File

@ -37,6 +37,8 @@
#endif /* HAVE_TREMOR */ #endif /* HAVE_TREMOR */
#include <glib.h> #include <glib.h>
#include <assert.h>
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@ -140,11 +142,13 @@ static const char *VORBIS_COMMENT_TRACK_KEY = "tracknumber";
static const char *VORBIS_COMMENT_DISC_KEY = "discnumber"; static const char *VORBIS_COMMENT_DISC_KEY = "discnumber";
static bool static bool
vorbis_parse_comment(char *comment, enum tag_type tag_type, vorbis_parse_comment(struct tag *tag, char *comment, enum tag_type tag_type)
struct tag ** tag)
{ {
const char *needle; const char *needle;
unsigned int len; unsigned int len;
assert(tag != NULL);
switch (tag_type) { switch (tag_type) {
case TAG_ITEM_TRACK: case TAG_ITEM_TRACK:
needle = VORBIS_COMMENT_TRACK_KEY; needle = VORBIS_COMMENT_TRACK_KEY;
@ -158,10 +162,7 @@ vorbis_parse_comment(char *comment, enum tag_type tag_type,
len = strlen(needle); len = strlen(needle);
if (strncasecmp(comment, needle, len) == 0 && *(comment + len) == '=') { if (strncasecmp(comment, needle, len) == 0 && *(comment + len) == '=') {
if (!*tag) tag_add_item(tag, tag_type, comment + len + 1);
*tag = tag_new();
tag_add_item(*tag, tag_type, comment + len + 1);
return true; return true;
} }
@ -172,17 +173,22 @@ vorbis_parse_comment(char *comment, enum tag_type tag_type,
static struct tag * static struct tag *
vorbis_comments_to_tag(char **comments) vorbis_comments_to_tag(char **comments)
{ {
struct tag *tag = NULL; struct tag *tag = tag_new();
while (*comments) { while (*comments) {
int j; int j;
for (j = TAG_NUM_OF_ITEM_TYPES; --j >= 0;) { for (j = TAG_NUM_OF_ITEM_TYPES; --j >= 0;) {
if (vorbis_parse_comment(*comments, j, &tag)) if (vorbis_parse_comment(tag, *comments, j))
break; break;
} }
comments++; comments++;
} }
if (tag_is_empty(tag)) {
tag_free(tag);
tag = NULL;
}
return tag; return tag;
} }