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
* 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;
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;
if (cmp_fn(op, option) == 0)
return i;
}
}
}
else
{
for (i=0; (op=oplist[i]) != NULL; i++)
{
if (strcmp(op, option) == 0)
{
ifound = i;
break;
}
}
}
return ifound;
return -1;
}
/** 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))
{
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;
for (t=0; t<SHORTCUT_TYPE_COUNT && sc->type == SHORTCUT_UNDEFINED; t++)
if (!strcmp(value, type_strings[t]))
sc->type = t;
}
else if (!strcmp(name, "name"))
else if (nm_op == 1) /*name*/
{
strlcpy(sc->name, value, MAX_SHORTCUT_NAME);
}
else if (!strcmp(name, "data"))
else if (nm_op == 2) /*data*/
{
switch (sc->type)
{
@ -357,7 +361,7 @@ static int readline_cb(int n, char *buf, void *parameters)
break;
}
}
else if (!strcmp(name, "icon"))
else if (nm_op == 3) /*icon*/
{
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);
}
}
else if (!strcmp(name, "talkclip"))
else if (nm_op == 4) /*talkclip*/
{
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;
}
}
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)

View file

@ -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);
}

View file

@ -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.
*/

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