]> git.tue.mpg.de Git - paraslash.git/commitdiff
mp4: Rename and simplify set_metadata_name()
authorAndre Noll <maan@tuebingen.mpg.de>
Wed, 11 Aug 2021 20:52:11 +0000 (22:52 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Mon, 30 May 2022 19:37:35 +0000 (21:37 +0200)
This function is an atrocious mess. For one, the naming is confusing
because the function does not set the atom name, it *returns* it.
More importantly, the function defines a static array for no good
reason and then hard-codes the array indices in a large switch
statement.

It's much easier to simply return a pointer to a string literal and
perform the strdup operation in the single caller.

mp4.c

diff --git a/mp4.c b/mp4.c
index b8746e327ece4b4b192d0565cf9859bf71547ea2..c97fa477931985bb039e18b023bcd4e67ce8c6d2 100644 (file)
--- a/mp4.c
+++ b/mp4.c
@@ -668,118 +668,41 @@ static char *read_string(struct mp4 *f, uint32_t length)
        return str;
 }
 
-static int32_t set_metadata_name(uint8_t atom_type, char **name)
-{
-       static char *tag_names[] = {
-               "unknown", "title", "artist", "writer", "album",
-               "date", "tool", "comment", "genre", "track",
-               "disc", "compilation", "genre", "tempo", "cover",
-               "album_artist", "contentgroup", "lyrics", "description",
-               "network", "show", "episodename",
-               "sorttitle", "sortalbum", "sortartist", "sortalbumartist",
-               "sortwriter", "sortshow",
-               "season", "episode", "podcast"
-       };
-       uint8_t tag_idx = 0;
-
+static const char *get_metadata_name(uint8_t atom_type)
+{
        switch (atom_type) {
-       case ATOM_TITLE:
-               tag_idx = 1;
-               break;
-       case ATOM_ARTIST:
-               tag_idx = 2;
-               break;
-       case ATOM_WRITER:
-               tag_idx = 3;
-               break;
-       case ATOM_ALBUM:
-               tag_idx = 4;
-               break;
-       case ATOM_DATE:
-               tag_idx = 5;
-               break;
-       case ATOM_TOOL:
-               tag_idx = 6;
-               break;
-       case ATOM_COMMENT:
-               tag_idx = 7;
-               break;
-       case ATOM_GENRE1:
-               tag_idx = 8;
-               break;
-       case ATOM_TRACK:
-               tag_idx = 9;
-               break;
-       case ATOM_DISC:
-               tag_idx = 10;
-               break;
-       case ATOM_COMPILATION:
-               tag_idx = 11;
-               break;
-       case ATOM_GENRE2:
-               tag_idx = 12;
-               break;
-       case ATOM_TEMPO:
-               tag_idx = 13;
-               break;
-       case ATOM_COVER:
-               tag_idx = 14;
-               break;
-       case ATOM_ALBUM_ARTIST:
-               tag_idx = 15;
-               break;
-       case ATOM_CONTENTGROUP:
-               tag_idx = 16;
-               break;
-       case ATOM_LYRICS:
-               tag_idx = 17;
-               break;
-       case ATOM_DESCRIPTION:
-               tag_idx = 18;
-               break;
-       case ATOM_NETWORK:
-               tag_idx = 19;
-               break;
-       case ATOM_SHOW:
-               tag_idx = 20;
-               break;
-       case ATOM_EPISODENAME:
-               tag_idx = 21;
-               break;
-       case ATOM_SORTTITLE:
-               tag_idx = 22;
-               break;
-       case ATOM_SORTALBUM:
-               tag_idx = 23;
-               break;
-       case ATOM_SORTARTIST:
-               tag_idx = 24;
-               break;
-       case ATOM_SORTALBUMARTIST:
-               tag_idx = 25;
-               break;
-       case ATOM_SORTWRITER:
-               tag_idx = 26;
-               break;
-       case ATOM_SORTSHOW:
-               tag_idx = 27;
-               break;
-       case ATOM_SEASON:
-               tag_idx = 28;
-               break;
-       case ATOM_EPISODE:
-               tag_idx = 29;
-               break;
-       case ATOM_PODCAST:
-               tag_idx = 30;
-               break;
-       default:
-               tag_idx = 0;
-               break;
+       case ATOM_TITLE: return "title";
+       case ATOM_ARTIST: return "artist";
+       case ATOM_WRITER: return "writer";
+       case ATOM_ALBUM: return "album";
+       case ATOM_DATE: return "date";
+       case ATOM_TOOL: return "tool";
+       case ATOM_COMMENT: return "comment";
+       case ATOM_GENRE1: return "genre";
+       case ATOM_TRACK: return "track";
+       case ATOM_DISC: return "disc";
+       case ATOM_COMPILATION: return "compilation";
+       case ATOM_GENRE2: return "genre";
+       case ATOM_TEMPO: return "tempo";
+       case ATOM_COVER: return "cover";
+       case ATOM_ALBUM_ARTIST: return "album_artist";
+       case ATOM_CONTENTGROUP: return "contentgroup";
+       case ATOM_LYRICS: return "lyrics";
+       case ATOM_DESCRIPTION: return "description";
+       case ATOM_NETWORK: return "network";
+       case ATOM_SHOW: return "show";
+       case ATOM_EPISODENAME: return "episodename";
+       case ATOM_SORTTITLE: return "sorttitle";
+       case ATOM_SORTALBUM: return "sortalbum";
+       case ATOM_SORTARTIST: return "sortartist";
+       case ATOM_SORTALBUMARTIST: return "sortalbumartist";
+       case ATOM_SORTWRITER: return "sortwriter";
+       case ATOM_SORTSHOW: return "sortshow";
+       case ATOM_SEASON: return "season";
+       case ATOM_EPISODE: return "episode";
+       case ATOM_PODCAST: return "podcast";
+       default: return "unknown";
        }
-
-       *name = para_strdup(tag_names[tag_idx]);
-       return 0;
 }
 
 static uint32_t min_body_size(uint8_t atom_type)
@@ -884,12 +807,10 @@ static int32_t parse_tag(struct mp4 *f, uint8_t parent, int32_t size)
        }
        if (data) {
                if (!done) {
-                       if (name == NULL)
-                               set_metadata_name(parent , &name);
-                       if (name)
-                               tag_add_field(&(f->tags), name, data, len);
+                       if (!name)
+                               name = para_strdup(get_metadata_name(parent));
+                       tag_add_field(&(f->tags), name, data, len);
                }
-
                free(data);
        }
        free(name);