From 77e4dd81f5abedda802b8b6e895849995907b484 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Sun, 13 Mar 2022 10:55:47 -0400 Subject: option_string clean-up and consolidate with metadata_common Change-Id: I2649f6af37bd871fb8f181ae2f716ff0bcf1f65c --- lib/rbcodec/metadata/metadata_common.c | 36 ++++++++++++++++++++++++++-------- lib/rbcodec/metadata/metadata_common.h | 1 + lib/rbcodec/metadata/mp4.c | 13 ++++++++---- lib/rbcodec/metadata/replaygain.c | 31 +++++++++++++++-------------- 4 files changed, 54 insertions(+), 27 deletions(-) (limited to 'lib') diff --git a/lib/rbcodec/metadata/metadata_common.c b/lib/rbcodec/metadata/metadata_common.c index 3677599c30..2586b2b65e 100644 --- a/lib/rbcodec/metadata/metadata_common.c +++ b/lib/rbcodec/metadata/metadata_common.c @@ -252,22 +252,42 @@ bool skip_id3v2(int fd, struct mp3entry *id3) return success; } -static int get_tag_option(const char *option, const char *const oplist[]) +#ifndef ROCKBOX /*codecs can be built without rockbox */ +/* returns match index from option list + * returns -1 if option was not found + * option list is array of char pointers with the final item set to null + * ex - const char *option[] = { "op_a", "op_b", "op_c", NULL} + */ +int string_option(const char *option, const char *const oplist[], bool ignore_case) { int i; int ifound = -1; const char *op; - for (i=0; (op=oplist[i]) != NULL; i++) + if (ignore_case) { - if (strcasecmp(op, option) == 0) + for (i=0; (op=oplist[i]) != NULL; i++) { - ifound = i; - break; + if (strcasecmp(op, option) == 0) + { + ifound = i; + break; + } + } + } + else + { + for (i=0; (op=oplist[i]) != NULL; i++) + { + if (strcmp(op, option) == 0) + { + ifound = i; + break; + } } } return ifound; } - +#endif /* Parse the tag (the name-value pair) and fill id3 and buffer accordingly. * String values to keep are written to buf. Returns number of bytes written * to buf (including end nil). @@ -287,7 +307,7 @@ long parse_tag(const char* name, char* value, struct mp3entry* id3, eMUSICBRAINZ1, eMUSICBRAINZ2 }; - const char *tagops[] = + static const char *tagops[] = { "track", "tracknumber", "discnumber", "disc", "year","date","title", "artist", "album", "genre" "composer","comment","albumartist","album artist", @@ -295,7 +315,7 @@ long parse_tag(const char* name, char* value, struct mp3entry* id3, "musicbrainz_trackid", "http://musicbrainz.org", NULL }; - int item = get_tag_option(name, tagops); + int item = string_option(name, tagops, true); if (((item == eTRACK && (type == TAGTYPE_APE))) || (item == eTRACKNUMBER && (type == TAGTYPE_VORBIS))) diff --git a/lib/rbcodec/metadata/metadata_common.h b/lib/rbcodec/metadata/metadata_common.h index db91729de4..14115b745a 100644 --- a/lib/rbcodec/metadata/metadata_common.h +++ b/lib/rbcodec/metadata/metadata_common.h @@ -38,6 +38,7 @@ bool read_ape_tags(int fd, struct mp3entry* id3); long read_vorbis_tags(int fd, struct mp3entry *id3, long tag_remaining); +int string_option(const char *option, const char *const oplist[], bool ignore_case); bool skip_id3v2(int fd, struct mp3entry *id3); long read_string(int fd, char* buf, long buf_size, int eos, long size); diff --git a/lib/rbcodec/metadata/mp4.c b/lib/rbcodec/metadata/mp4.c index 41f38480b1..accc5cd662 100644 --- a/lib/rbcodec/metadata/mp4.c +++ b/lib/rbcodec/metadata/mp4.c @@ -533,13 +533,18 @@ static bool read_mp4_tags(int fd, struct mp3entry* id3, rd_ret = 0; tag_name[rd_ret] = 0; + static const char *tn_options[] = {"composer", "iTunSMPB", + "musicbrainz track id", "album artist", NULL}; - if ((strcasecmp(tag_name, "composer") == 0) && !cwrt) + int tn_op = string_option(tag_name, tn_options, true); + + + if (tn_op == 0 && !cwrt) /*composer*/ { read_mp4_tag_string(fd, size, &buffer, &buffer_left, &id3->composer); } - else if (strcasecmp(tag_name, "iTunSMPB") == 0) + else if (tn_op == 1) /*iTunSMPB*/ { char value[TAG_VALUE_LENGTH]; char* value_p = value; @@ -552,12 +557,12 @@ static bool read_mp4_tags(int fd, struct mp3entry* id3, DEBUGF("AAC: lead_trim %d, tail_trim %d\n", id3->lead_trim, id3->tail_trim); } - else if (strcasecmp(tag_name, "musicbrainz track id") == 0) + else if (tn_op == 2) /*musicbrainz track id*/ { read_mp4_tag_string(fd, size, &buffer, &buffer_left, &id3->mb_track_id); } - else if ((strcasecmp(tag_name, "album artist") == 0)) + else if (tn_op == 3) /*album artist*/ { read_mp4_tag_string(fd, size, &buffer, &buffer_left, &id3->albumartist); diff --git a/lib/rbcodec/metadata/replaygain.c b/lib/rbcodec/metadata/replaygain.c index 7aa77cfedd..eb98bbc342 100644 --- a/lib/rbcodec/metadata/replaygain.c +++ b/lib/rbcodec/metadata/replaygain.c @@ -32,6 +32,7 @@ #include "debug.h" #include "replaygain.h" #include "fixedpoint.h" +#include "metadata_common.h" #define FP_BITS (12) #define FP_ONE (1 << FP_BITS) @@ -167,29 +168,29 @@ long get_replaygain_int(long int_gain) void parse_replaygain(const char* key, const char* value, struct mp3entry* entry) { - if (((strcasecmp(key, "replaygain_track_gain") == 0) || - (strcasecmp(key, "rg_radio") == 0)) && - !entry->track_gain) - { + static const char *rg_options[] = {"replaygain_track_gain", "rg_radio", + "replaygain_album_gain", "rg_audiophile", + "replaygain_track_peak", "rg_peak", + "replaygain_album_peak", NULL}; + + int rg_op = string_option(key, rg_options, true); + + if ((rg_op == 0 || rg_op == 1) && !entry->track_gain) + { /*replaygain_track_gain||rg_radio*/ entry->track_level = get_replaygain(value); entry->track_gain = convert_gain(entry->track_level); } - else if (((strcasecmp(key, "replaygain_album_gain") == 0) || - (strcasecmp(key, "rg_audiophile") == 0)) && - !entry->album_gain) - { + else if ((rg_op == 2 || rg_op == 3) && !entry->album_gain) + { /*replaygain_album_gain||rg_audiophile*/ entry->album_level = get_replaygain(value); entry->album_gain = convert_gain(entry->album_level); } - else if (((strcasecmp(key, "replaygain_track_peak") == 0) || - (strcasecmp(key, "rg_peak") == 0)) && - !entry->track_peak) - { + else if ((rg_op == 4 || rg_op == 5) && !entry->track_peak) + { /*replaygain_track_peak||rg_peak*/ entry->track_peak = get_replaypeak(value); } - else if ((strcasecmp(key, "replaygain_album_peak") == 0) && - !entry->album_peak) - { + else if ((rg_op == 6) && !entry->album_peak) + { /*replaygain_album_peak*/ entry->album_peak = get_replaypeak(value); } } -- cgit v1.2.3