replace more strcmp if then trees with string_option()

1

Change-Id: Ic89bbb2ab41068d09c7bd9caa5ba7f38749b9084
This commit is contained in:
William Wilgus 2022-03-13 14:31:02 -04:00
parent a62d36d9e7
commit 2352cef6d0
6 changed files with 82 additions and 80 deletions

View file

@ -1389,35 +1389,18 @@ int split_string(char *str, const char split_char, char *vector[], const int vec
* option list is array of char pointers with the final item set to null * 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} * 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 string_option(const char *option, const char *const oplist[], bool ignore_case)
{ {
int i;
int ifound = -1;
const char *op; 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 (cmp_fn(op, option) == 0)
{ return i;
if (strcasecmp(op, option) == 0)
{
ifound = i;
break;
} }
} return -1;
}
else
{
for (i=0; (op=oplist[i]) != NULL; i++)
{
if (strcmp(op, option) == 0)
{
ifound = i;
break;
}
}
}
return ifound;
} }
/** Open a UTF-8 file and set file descriptor to first byte after BOM. /** Open a UTF-8 file and set file descriptor to first byte after BOM.

View file

@ -312,18 +312,22 @@ static int readline_cb(int n, char *buf, void *parameters)
} }
else if (sc && settings_parseline(buf, &name, &value)) else if (sc && settings_parseline(buf, &name, &value))
{ {
if (!strcmp(name, "type")) static const char *nm_options[] = {"type", "name", "data",
"icon", "talkclip", NULL};
int nm_op = string_option(name, nm_options, false);
if (nm_op == 0) /*type*/
{ {
int t = 0; int t = 0;
for (t=0; t<SHORTCUT_TYPE_COUNT && sc->type == SHORTCUT_UNDEFINED; t++) for (t=0; t<SHORTCUT_TYPE_COUNT && sc->type == SHORTCUT_UNDEFINED; t++)
if (!strcmp(value, type_strings[t])) if (!strcmp(value, type_strings[t]))
sc->type = t; sc->type = t;
} }
else if (!strcmp(name, "name")) else if (nm_op == 1) /*name*/
{ {
strlcpy(sc->name, value, MAX_SHORTCUT_NAME); strlcpy(sc->name, value, MAX_SHORTCUT_NAME);
} }
else if (!strcmp(name, "data")) else if (nm_op == 2) /*data*/
{ {
switch (sc->type) switch (sc->type)
{ {
@ -357,7 +361,7 @@ static int readline_cb(int n, char *buf, void *parameters)
break; break;
} }
} }
else if (!strcmp(name, "icon")) else if (nm_op == 3) /*icon*/
{ {
if (!strcmp(value, "filetype") && sc->type != SHORTCUT_SETTING && sc->u.path[0]) if (!strcmp(value, "filetype") && sc->type != SHORTCUT_SETTING && sc->u.path[0])
{ {
@ -368,7 +372,7 @@ static int readline_cb(int n, char *buf, void *parameters)
sc->icon = atoi(value); sc->icon = atoi(value);
} }
} }
else if (!strcmp(name, "talkclip")) else if (nm_op == 4) /*talkclip*/
{ {
strlcpy(sc->talk_clip, value, MAX_PATH); strlcpy(sc->talk_clip, value, MAX_PATH);
} }

View file

@ -185,38 +185,41 @@ static bool parse_sap_header(int fd, struct mp3entry* id3, int file_len)
break; break;
} }
} }
static const char *tg_options[] = {"SAP", "AUTHOR", "NAME", "DATE",
"SONGS", "DEFSONG", "TIME", NULL};
/* parse tags */ /* parse tags */
if(strcmp(line, "SAP") == 0) int tg_op = string_option(line, tg_options, false);
if (tg_op == 0) /*SAP*/
sap_signature = 1; sap_signature = 1;
if (sap_signature == -1) if (sap_signature == -1)
return false; return false;
if (strcmp(line, "AUTHOR") == 0)
if (tg_op == 1) /*AUTHOR*/
{ {
if(read_asap_string(p, &buffer, &buffer_end, &id3->artist) == false) if(read_asap_string(p, &buffer, &buffer_end, &id3->artist) == false)
return 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) if(read_asap_string(p, &buffer, &buffer_end, &id3->title) == false)
return 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) if(read_asap_string(p, &buffer, &buffer_end, &id3->year_string) == false)
return false; return false;
} }
else if (strcmp(line, "SONGS") == 0) else if (tg_op == 4) /*SONGS*/
{ {
if (parse_dec(&numSongs, p, 1, MAX_SONGS) == false ) if (parse_dec(&numSongs, p, 1, MAX_SONGS) == false )
return false; return false;
} }
else if (strcmp(line, "DEFSONG") == 0) else if (tg_op == 5) /*DEFSONG*/
{ {
if (parse_dec(&defSong, p, 0, MAX_SONGS) == false) if (parse_dec(&defSong, p, 0, MAX_SONGS) == false)
return false; return false;
} }
else if (strcmp(line, "TIME") == 0) else if (tg_op == 6) /*TIME*/
{ {
int durationTemp = ASAP_ParseDuration(p); int durationTemp = ASAP_ParseDuration(p);
if (durationTemp < 0 || duration_index >= MAX_SONGS) if (durationTemp < 0 || duration_index >= MAX_SONGS)

View file

@ -437,6 +437,18 @@ static int asf_parse_header(int fd, struct mp3entry* id3,
read_uint16le(fd, &count); read_uint16le(fd, &count);
bytesleft -= 2; bytesleft -= 2;
//DEBUGF("extended metadata count = %u\n",count); //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++) { for (i=0; i < count; i++) {
uint16_t length, type; 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, &type);
read_uint16le(fd, &length); read_uint16le(fd, &length);
if (!strcmp("WM/TrackNumber",utf8buf)) { int itag = string_option(utf8buf, tagops, false);
if (itag == eWM_TrackNumber) {
if (type == 0) { if (type == 0) {
id3->track_string = id3buf; id3->track_string = id3buf;
asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
@ -460,19 +474,19 @@ static int asf_parse_header(int fd, struct mp3entry* id3,
} else { } else {
lseek(fd, length, SEEK_CUR); lseek(fd, length, SEEK_CUR);
} }
} else if ((!strcmp("WM/Genre", utf8buf)) && (type == 0)) { } else if ((itag == eWM_Genre) && (type == 0)) {
id3->genre_string = id3buf; id3->genre_string = id3buf;
asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); 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; id3->album = id3buf;
asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); 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; id3->albumartist = id3buf;
asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); 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; id3->composer = id3buf;
asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
} else if (!strcmp("WM/Year", utf8buf)) { } else if (itag == eWM_Year) {
if (type == 0) { if (type == 0) {
id3->year_string = id3buf; id3->year_string = id3buf;
asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
@ -482,15 +496,11 @@ static int asf_parse_header(int fd, struct mp3entry* id3,
} else { } else {
lseek(fd, length, SEEK_CUR); lseek(fd, length, SEEK_CUR);
} }
} else if (!strncmp("replaygain_", utf8buf, 11)) { } else if (itag == eWM_MusicBrainz_Track_Id) {
char *value = id3buf;
asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
parse_replaygain(utf8buf, value, id3);
} else if (!strcmp("MusicBrainz/Track Id", utf8buf)) {
id3->mb_track_id = id3buf; id3->mb_track_id = id3buf;
asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
#ifdef HAVE_ALBUMART #ifdef HAVE_ALBUMART
} else if (!strcmp("WM/Picture", utf8buf)) { } else if (itag == eWM_Picture) {
uint32_t datalength = 0; uint32_t datalength = 0;
uint32_t strlength; uint32_t strlength;
/* Expected is either "01 00 xx xx 03 yy yy yy yy" or /* 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); asf_utf16LEdecode(fd, 32, &utf8, &utf8length);
strlength = (strlen(utf8buf) + 2) * 2; strlength = (strlen(utf8buf) + 2) * 2;
lseek(fd, strlength-32, SEEK_CUR); 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; id3->albumart.type = AA_TYPE_JPG;
} else if (!strcmp("image/jpg", utf8buf)) { }
else if (aa_op == 1) /*image/jpg*/
{
/* image/jpg is technically invalid, /* image/jpg is technically invalid,
* but it does occur in the wild */ * but it does occur in the wild */
id3->albumart.type = AA_TYPE_JPG; 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; id3->albumart.type = AA_TYPE_PNG;
} else { } else {
id3->albumart.type = AA_TYPE_UNKNOWN; 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); lseek(fd, datalength, SEEK_CUR);
#endif #endif
} else if (!strncmp("replaygain_", utf8buf, 11)) {
char *value = id3buf;
asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
parse_replaygain(utf8buf, value, id3);
} else { } else {
lseek(fd, length, SEEK_CUR); lseek(fd, length, SEEK_CUR);
} }

View file

@ -308,24 +308,26 @@ static int parsealbumart( struct mp3entry* entry, char* tag, int bufferpos )
char *start = tag; char *start = tag;
/* skip text encoding */ /* skip text encoding */
tag += 1; tag += 1;
static const char *img_options[] = {"jpeg", "jpg", "png", NULL};
if (memcmp(tag, "image/", 6) == 0) if (memcmp(tag, "image/", 6) == 0)
{ {
int tg_op = string_option(tag, img_options, false);
/* ID3 v2.3+ */ /* ID3 v2.3+ */
tag += 6; tag += 6;
if (strcmp(tag, "jpeg") == 0) if (tg_op == 0) /*jpeg*/
{ {
entry->albumart.type = AA_TYPE_JPG; entry->albumart.type = AA_TYPE_JPG;
tag += 5; tag += 5;
} }
else if (strcmp(tag, "jpg") == 0) else if (tg_op == 1) /*jpg*/
{ {
/* image/jpg is technically invalid, but it does occur in /* image/jpg is technically invalid, but it does occur in
* the wild */ * the wild */
entry->albumart.type = AA_TYPE_JPG; entry->albumart.type = AA_TYPE_JPG;
tag += 4; tag += 4;
} }
else if (strcmp(tag, "png") == 0) else if (tg_op == 2) /*png*/
{ {
entry->albumart.type = AA_TYPE_PNG; entry->albumart.type = AA_TYPE_PNG;
tag += 4; 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; 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 /* Only accept non-track values if we don't have any previous
* value. * value.
*/ */

View file

@ -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 string_option(const char *option, const char *const oplist[], bool ignore_case)
{ {
int i;
int ifound = -1;
const char *op; 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 (cmp_fn(op, option) == 0)
{ return i;
if (strcasecmp(op, option) == 0)
{
ifound = i;
break;
} }
} return -1;
}
else
{
for (i=0; (op=oplist[i]) != NULL; i++)
{
if (strcmp(op, option) == 0)
{
ifound = i;
break;
}
}
}
return ifound;
} }
#endif #endif
/* Parse the tag (the name-value pair) and fill id3 and buffer accordingly. /* Parse the tag (the name-value pair) and fill id3 and buffer accordingly.