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 = {
.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)
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;