From 2352cef6d0757a4d31a18561a09a10f031388e12 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Sun, 13 Mar 2022 14:31:02 -0400 Subject: replace more strcmp if then trees with string_option() 1 Change-Id: Ic89bbb2ab41068d09c7bd9caa5ba7f38749b9084 --- lib/rbcodec/metadata/asap.c | 19 +++++++----- lib/rbcodec/metadata/asf.c | 54 ++++++++++++++++++++++++---------- lib/rbcodec/metadata/id3tags.c | 14 +++++---- lib/rbcodec/metadata/metadata_common.c | 30 +++++-------------- 4 files changed, 66 insertions(+), 51 deletions(-) (limited to 'lib') diff --git a/lib/rbcodec/metadata/asap.c b/lib/rbcodec/metadata/asap.c index 47eb2a3d50..db23dd69fa 100644 --- a/lib/rbcodec/metadata/asap.c +++ b/lib/rbcodec/metadata/asap.c @@ -185,38 +185,41 @@ static bool parse_sap_header(int fd, struct mp3entry* id3, int file_len) break; } } - + static const char *tg_options[] = {"SAP", "AUTHOR", "NAME", "DATE", + "SONGS", "DEFSONG", "TIME", NULL}; /* parse tags */ - if(strcmp(line, "SAP") == 0) + int tg_op = string_option(line, tg_options, false); + if (tg_op == 0) /*SAP*/ sap_signature = 1; if (sap_signature == -1) return false; - if (strcmp(line, "AUTHOR") == 0) + + if (tg_op == 1) /*AUTHOR*/ { if(read_asap_string(p, &buffer, &buffer_end, &id3->artist) == false) return false; } - else if(strcmp(line, "NAME") == 0) + else if(tg_op == 2) /*NAME*/ { if(read_asap_string(p, &buffer, &buffer_end, &id3->title) == false) return false; } - else if(strcmp(line, "DATE") == 0) + else if(tg_op == 3) /*DATE*/ { if(read_asap_string(p, &buffer, &buffer_end, &id3->year_string) == false) return false; } - else if (strcmp(line, "SONGS") == 0) + else if (tg_op == 4) /*SONGS*/ { if (parse_dec(&numSongs, p, 1, MAX_SONGS) == false ) return false; } - else if (strcmp(line, "DEFSONG") == 0) + else if (tg_op == 5) /*DEFSONG*/ { if (parse_dec(&defSong, p, 0, MAX_SONGS) == false) return false; } - else if (strcmp(line, "TIME") == 0) + else if (tg_op == 6) /*TIME*/ { int durationTemp = ASAP_ParseDuration(p); if (durationTemp < 0 || duration_index >= MAX_SONGS) diff --git a/lib/rbcodec/metadata/asf.c b/lib/rbcodec/metadata/asf.c index d90487b36b..82873a43a6 100644 --- a/lib/rbcodec/metadata/asf.c +++ b/lib/rbcodec/metadata/asf.c @@ -437,6 +437,18 @@ static int asf_parse_header(int fd, struct mp3entry* id3, read_uint16le(fd, &count); bytesleft -= 2; //DEBUGF("extended metadata count = %u\n",count); + enum + { + eWM_TrackNumber, eWM_Genre, eWM_AlbumTitle, + eWM_AlbumArtist, eWM_Composer, eWM_Year, + eWM_MusicBrainz_Track_Id, eWM_Picture + }; + + static const char *tagops[] = + { "WM/TrackNumber", "WM/Genre", "WM/AlbumTitle", + "WM/AlbumArtist", "WM/Composer", "WM/Year", + "MusicBrainz/Track Id", "WM/Picture", NULL + }; for (i=0; i < count; i++) { uint16_t length, type; @@ -450,7 +462,9 @@ static int asf_parse_header(int fd, struct mp3entry* id3, read_uint16le(fd, &type); read_uint16le(fd, &length); - if (!strcmp("WM/TrackNumber",utf8buf)) { + int itag = string_option(utf8buf, tagops, false); + + if (itag == eWM_TrackNumber) { if (type == 0) { id3->track_string = id3buf; asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); @@ -460,19 +474,19 @@ static int asf_parse_header(int fd, struct mp3entry* id3, } else { lseek(fd, length, SEEK_CUR); } - } else if ((!strcmp("WM/Genre", utf8buf)) && (type == 0)) { + } else if ((itag == eWM_Genre) && (type == 0)) { id3->genre_string = id3buf; asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); - } else if ((!strcmp("WM/AlbumTitle", utf8buf)) && (type == 0)) { + } else if ((itag == eWM_AlbumTitle) && (type == 0)) { id3->album = id3buf; asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); - } else if ((!strcmp("WM/AlbumArtist", utf8buf)) && (type == 0)) { + } else if ((itag == eWM_AlbumArtist) && (type == 0)) { id3->albumartist = id3buf; asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); - } else if ((!strcmp("WM/Composer", utf8buf)) && (type == 0)) { + } else if ((itag == eWM_Composer) && (type == 0)) { id3->composer = id3buf; asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); - } else if (!strcmp("WM/Year", utf8buf)) { + } else if (itag == eWM_Year) { if (type == 0) { id3->year_string = id3buf; asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); @@ -482,15 +496,11 @@ static int asf_parse_header(int fd, struct mp3entry* id3, } else { lseek(fd, length, SEEK_CUR); } - } else if (!strncmp("replaygain_", utf8buf, 11)) { - char *value = id3buf; - asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); - parse_replaygain(utf8buf, value, id3); - } else if (!strcmp("MusicBrainz/Track Id", utf8buf)) { + } else if (itag == eWM_MusicBrainz_Track_Id) { id3->mb_track_id = id3buf; asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); #ifdef HAVE_ALBUMART - } else if (!strcmp("WM/Picture", utf8buf)) { + } else if (itag == eWM_Picture) { uint32_t datalength = 0; uint32_t strlength; /* Expected is either "01 00 xx xx 03 yy yy yy yy" or @@ -521,13 +531,23 @@ static int asf_parse_header(int fd, struct mp3entry* id3, asf_utf16LEdecode(fd, 32, &utf8, &utf8length); strlength = (strlen(utf8buf) + 2) * 2; lseek(fd, strlength-32, SEEK_CUR); - if (!strcmp("image/jpeg", utf8buf)) { + + static const char *aa_options[] = {"image/jpeg", + "image/jpg","image/png", NULL}; + int aa_op = string_option(utf8buf, aa_options, false); + + if (aa_op == 0) /*image/jpeg*/ + { id3->albumart.type = AA_TYPE_JPG; - } else if (!strcmp("image/jpg", utf8buf)) { + } + else if (aa_op == 1) /*image/jpg*/ + { /* image/jpg is technically invalid, * but it does occur in the wild */ id3->albumart.type = AA_TYPE_JPG; - } else if (!strcmp("image/png", utf8buf)) { + } + else if (aa_op == 2) /*image/png*/ + { id3->albumart.type = AA_TYPE_PNG; } else { id3->albumart.type = AA_TYPE_UNKNOWN; @@ -543,6 +563,10 @@ static int asf_parse_header(int fd, struct mp3entry* id3, lseek(fd, datalength, SEEK_CUR); #endif + } else if (!strncmp("replaygain_", utf8buf, 11)) { + char *value = id3buf; + asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); + parse_replaygain(utf8buf, value, id3); } else { lseek(fd, length, SEEK_CUR); } diff --git a/lib/rbcodec/metadata/id3tags.c b/lib/rbcodec/metadata/id3tags.c index 458e24cf61..18258c73ac 100644 --- a/lib/rbcodec/metadata/id3tags.c +++ b/lib/rbcodec/metadata/id3tags.c @@ -308,24 +308,26 @@ static int parsealbumart( struct mp3entry* entry, char* tag, int bufferpos ) char *start = tag; /* skip text encoding */ tag += 1; + static const char *img_options[] = {"jpeg", "jpg", "png", NULL}; if (memcmp(tag, "image/", 6) == 0) { + int tg_op = string_option(tag, img_options, false); /* ID3 v2.3+ */ tag += 6; - if (strcmp(tag, "jpeg") == 0) + if (tg_op == 0) /*jpeg*/ { entry->albumart.type = AA_TYPE_JPG; tag += 5; } - else if (strcmp(tag, "jpg") == 0) + else if (tg_op == 1) /*jpg*/ { /* image/jpg is technically invalid, but it does occur in * the wild */ entry->albumart.type = AA_TYPE_JPG; tag += 4; } - else if (strcmp(tag, "png") == 0) + else if (tg_op == 2) /*png*/ { entry->albumart.type = AA_TYPE_PNG; tag += 4; @@ -434,9 +436,11 @@ static int parserva2( struct mp3entry* entry, char* tag, int bufferpos) } } - if (strcasecmp(tag, "album") == 0) { + static const char *tg_options[] = {"album", "track", NULL}; + int tg_op = string_option(tag, tg_options, true); + if (tg_op == 0) { /*album*/ album = true; - } else if (strcasecmp(tag, "track") != 0) { + } else if (tg_op != 1) { /*!track*/ /* Only accept non-track values if we don't have any previous * value. */ diff --git a/lib/rbcodec/metadata/metadata_common.c b/lib/rbcodec/metadata/metadata_common.c index 202da49522..59c2f01840 100644 --- a/lib/rbcodec/metadata/metadata_common.c +++ b/lib/rbcodec/metadata/metadata_common.c @@ -260,32 +260,16 @@ bool skip_id3v2(int fd, struct mp3entry *id3) */ int string_option(const char *option, const char *const oplist[], bool ignore_case) { - int i; - int ifound = -1; const char *op; - if (ignore_case) + int (*cmp_fn)(const char*, const char*) = &strcasecmp; + if (!ignore_case) + cmp_fn = strcmp; + for (int i=0; (op=oplist[i]) != NULL; i++) { - for (i=0; (op=oplist[i]) != NULL; i++) - { - 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; - } - } + if (cmp_fn(op, option) == 0) + return i; } - return ifound; + return -1; } #endif /* Parse the tag (the name-value pair) and fill id3 and buffer accordingly. -- cgit v1.2.3