From: Andre Noll Date: Thu, 19 Aug 2021 20:13:47 +0000 (+0200) Subject: aac_afh: Simplify aac_afh_rewrite_tags(). X-Git-Tag: v0.7.1~7^2~81 X-Git-Url: http://git.tue.mpg.de/?a=commitdiff_plain;h=3716862c92d1a7cbb9d3ebc2687da98cedef03f0;p=paraslash.git aac_afh: Simplify aac_afh_rewrite_tags(). This function was disgusting because it listed the five standard tags in three different ways. The new replace_or_add_tag() avoids this issue. --- diff --git a/aac_afh.c b/aac_afh.c index 3c8ba867..30e7164e 100644 --- a/aac_afh.c +++ b/aac_afh.c @@ -203,24 +203,33 @@ static uint32_t aac_afh_meta_truncate_cb(void *user_data) return ftruncate(fd, offset); } -static void replace_tag(struct mp4_tag *tag, const char *new_val, bool *found) +static void replace_or_add_tag(const char *item, const char *value, + struct mp4_metadata *meta) { - free(tag->value); - tag->value = para_strdup(new_val); - *found = true; -} - -static void add_tag(struct mp4_metadata *md, const char *item, const char *value) -{ - md->tags[md->count].item = para_strdup(item); - md->tags[md->count].value = para_strdup(value); - md->count++; + uint32_t n; + struct mp4_tag *t; + + for (n = 0; n < meta->count; n++) { + t = meta->tags + n; + if (strcasecmp(t->item, item)) + continue; + free(t->value); + t->value = para_strdup(value); + return; + } + /* item not found, add new tag */ + meta->tags = para_realloc(meta->tags, (meta->count + 1) + * sizeof(struct mp4_tag)); + t = meta->tags + meta->count; + t->item = para_strdup(item); + t->value = para_strdup(value); + meta->count++; } static int aac_afh_rewrite_tags(const char *map, size_t mapsize, struct taginfo *tags, int fd, __a_unused const char *filename) { - int ret, i; + int ret; struct mp4_metadata *metadata; struct mp4 *mp4; struct mp4_callback cb = { @@ -230,8 +239,6 @@ static int aac_afh_rewrite_tags(const char *map, size_t mapsize, .truncate = aac_afh_meta_truncate_cb, .user_data = &fd }; - bool found_artist = false, found_title = false, found_album = false, - found_year = false, found_comment = false; ret = write_all(fd, map, mapsize); if (ret < 0) @@ -243,31 +250,11 @@ static int aac_afh_rewrite_tags(const char *map, size_t mapsize, return -E_MP4_OPEN; metadata = mp4_get_meta(mp4); PARA_NOTICE_LOG("%u metadata item(s) found\n", metadata->count); - metadata->tags = para_realloc(metadata->tags, - (metadata->count + 5) * sizeof(struct mp4_tag)); - for (i = 0; i < metadata->count; i++) { - struct mp4_tag *tag = metadata->tags + i; - if (!strcmp(tag->item, "artist")) - replace_tag(tag, tags->artist, &found_artist); - else if (!strcmp(tag->item, "title")) - replace_tag(tag, tags->title, &found_title); - else if (!strcmp(tag->item, "album")) - replace_tag(tag, tags->album, &found_album); - else if (!strcmp(tag->item, "date")) - replace_tag(tag, tags->year, &found_year); - else if (!strcmp(tag->item, "comment")) - replace_tag(tag, tags->comment, &found_comment); - } - if (!found_artist) - add_tag(metadata, "artist", tags->artist); - if (!found_title) - add_tag(metadata, "title", tags->title); - if (!found_album) - add_tag(metadata, "album", tags->album); - if (!found_year) - add_tag(metadata, "date", tags->year); - if (!found_comment) - add_tag(metadata, "comment", tags->comment); + replace_or_add_tag("artist", tags->artist, metadata); + replace_or_add_tag("title", tags->title, metadata); + replace_or_add_tag("album", tags->album, metadata); + replace_or_add_tag("date", tags->year, metadata); + replace_or_add_tag("comment", tags->comment, metadata); ret = -E_MP4_META_WRITE; if (!mp4_meta_update(mp4)) goto close;