diff options
author | William Wilgus <wilgus.william@gmail.com> | 2022-03-13 14:31:02 -0400 |
---|---|---|
committer | William Wilgus <wilgus.william@gmail.com> | 2022-03-13 14:31:02 -0400 |
commit | 2352cef6d0757a4d31a18561a09a10f031388e12 (patch) | |
tree | 81d555acb1f7382d7c8c7c4180cfb9aa5b028d8b | |
parent | a62d36d9e7fd89adfd55ae2428fa9df4243bc435 (diff) | |
download | rockbox-2352cef6d0757a4d31a18561a09a10f031388e12.tar.gz rockbox-2352cef6d0757a4d31a18561a09a10f031388e12.zip |
replace more strcmp if then trees with string_option()
1
Change-Id: Ic89bbb2ab41068d09c7bd9caa5ba7f38749b9084
-rw-r--r-- | apps/misc.c | 31 | ||||
-rw-r--r-- | apps/shortcuts.c | 14 | ||||
-rw-r--r-- | lib/rbcodec/metadata/asap.c | 19 | ||||
-rw-r--r-- | lib/rbcodec/metadata/asf.c | 54 | ||||
-rw-r--r-- | lib/rbcodec/metadata/id3tags.c | 14 | ||||
-rw-r--r-- | lib/rbcodec/metadata/metadata_common.c | 30 |
6 files changed, 82 insertions, 80 deletions
diff --git a/apps/misc.c b/apps/misc.c index 350537dc31..a4958a59ea 100644 --- a/apps/misc.c +++ b/apps/misc.c | |||
@@ -1389,35 +1389,18 @@ int split_string(char *str, const char split_char, char *vector[], const int vec | |||
1389 | * option list is array of char pointers with the final item set to null | 1389 | * option list is array of char pointers with the final item set to null |
1390 | * ex - const char *option[] = { "op_a", "op_b", "op_c", NULL} | 1390 | * ex - const char *option[] = { "op_a", "op_b", "op_c", NULL} |
1391 | */ | 1391 | */ |
1392 | |||
1393 | int string_option(const char *option, const char *const oplist[], bool ignore_case) | 1392 | int string_option(const char *option, const char *const oplist[], bool ignore_case) |
1394 | { | 1393 | { |
1395 | int i; | ||
1396 | int ifound = -1; | ||
1397 | const char *op; | 1394 | const char *op; |
1398 | if (ignore_case) | 1395 | int (*cmp_fn)(const char*, const char*) = &strcasecmp; |
1396 | if (!ignore_case) | ||
1397 | cmp_fn = strcmp; | ||
1398 | for (int i=0; (op=oplist[i]) != NULL; i++) | ||
1399 | { | 1399 | { |
1400 | for (i=0; (op=oplist[i]) != NULL; i++) | 1400 | if (cmp_fn(op, option) == 0) |
1401 | { | 1401 | return i; |
1402 | if (strcasecmp(op, option) == 0) | ||
1403 | { | ||
1404 | ifound = i; | ||
1405 | break; | ||
1406 | } | ||
1407 | } | ||
1408 | } | ||
1409 | else | ||
1410 | { | ||
1411 | for (i=0; (op=oplist[i]) != NULL; i++) | ||
1412 | { | ||
1413 | if (strcmp(op, option) == 0) | ||
1414 | { | ||
1415 | ifound = i; | ||
1416 | break; | ||
1417 | } | ||
1418 | } | ||
1419 | } | 1402 | } |
1420 | return ifound; | 1403 | return -1; |
1421 | } | 1404 | } |
1422 | 1405 | ||
1423 | /** Open a UTF-8 file and set file descriptor to first byte after BOM. | 1406 | /** Open a UTF-8 file and set file descriptor to first byte after BOM. |
diff --git a/apps/shortcuts.c b/apps/shortcuts.c index f573d6fe4e..754bd83b80 100644 --- a/apps/shortcuts.c +++ b/apps/shortcuts.c | |||
@@ -312,18 +312,22 @@ static int readline_cb(int n, char *buf, void *parameters) | |||
312 | } | 312 | } |
313 | else if (sc && settings_parseline(buf, &name, &value)) | 313 | else if (sc && settings_parseline(buf, &name, &value)) |
314 | { | 314 | { |
315 | if (!strcmp(name, "type")) | 315 | static const char *nm_options[] = {"type", "name", "data", |
316 | "icon", "talkclip", NULL}; | ||
317 | int nm_op = string_option(name, nm_options, false); | ||
318 | |||
319 | if (nm_op == 0) /*type*/ | ||
316 | { | 320 | { |
317 | int t = 0; | 321 | int t = 0; |
318 | for (t=0; t<SHORTCUT_TYPE_COUNT && sc->type == SHORTCUT_UNDEFINED; t++) | 322 | for (t=0; t<SHORTCUT_TYPE_COUNT && sc->type == SHORTCUT_UNDEFINED; t++) |
319 | if (!strcmp(value, type_strings[t])) | 323 | if (!strcmp(value, type_strings[t])) |
320 | sc->type = t; | 324 | sc->type = t; |
321 | } | 325 | } |
322 | else if (!strcmp(name, "name")) | 326 | else if (nm_op == 1) /*name*/ |
323 | { | 327 | { |
324 | strlcpy(sc->name, value, MAX_SHORTCUT_NAME); | 328 | strlcpy(sc->name, value, MAX_SHORTCUT_NAME); |
325 | } | 329 | } |
326 | else if (!strcmp(name, "data")) | 330 | else if (nm_op == 2) /*data*/ |
327 | { | 331 | { |
328 | switch (sc->type) | 332 | switch (sc->type) |
329 | { | 333 | { |
@@ -357,7 +361,7 @@ static int readline_cb(int n, char *buf, void *parameters) | |||
357 | break; | 361 | break; |
358 | } | 362 | } |
359 | } | 363 | } |
360 | else if (!strcmp(name, "icon")) | 364 | else if (nm_op == 3) /*icon*/ |
361 | { | 365 | { |
362 | if (!strcmp(value, "filetype") && sc->type != SHORTCUT_SETTING && sc->u.path[0]) | 366 | if (!strcmp(value, "filetype") && sc->type != SHORTCUT_SETTING && sc->u.path[0]) |
363 | { | 367 | { |
@@ -368,7 +372,7 @@ static int readline_cb(int n, char *buf, void *parameters) | |||
368 | sc->icon = atoi(value); | 372 | sc->icon = atoi(value); |
369 | } | 373 | } |
370 | } | 374 | } |
371 | else if (!strcmp(name, "talkclip")) | 375 | else if (nm_op == 4) /*talkclip*/ |
372 | { | 376 | { |
373 | strlcpy(sc->talk_clip, value, MAX_PATH); | 377 | strlcpy(sc->talk_clip, value, MAX_PATH); |
374 | } | 378 | } |
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) | |||
185 | break; | 185 | break; |
186 | } | 186 | } |
187 | } | 187 | } |
188 | 188 | static const char *tg_options[] = {"SAP", "AUTHOR", "NAME", "DATE", | |
189 | "SONGS", "DEFSONG", "TIME", NULL}; | ||
189 | /* parse tags */ | 190 | /* parse tags */ |
190 | if(strcmp(line, "SAP") == 0) | 191 | int tg_op = string_option(line, tg_options, false); |
192 | if (tg_op == 0) /*SAP*/ | ||
191 | sap_signature = 1; | 193 | sap_signature = 1; |
192 | if (sap_signature == -1) | 194 | if (sap_signature == -1) |
193 | return false; | 195 | return false; |
194 | if (strcmp(line, "AUTHOR") == 0) | 196 | |
197 | if (tg_op == 1) /*AUTHOR*/ | ||
195 | { | 198 | { |
196 | if(read_asap_string(p, &buffer, &buffer_end, &id3->artist) == false) | 199 | if(read_asap_string(p, &buffer, &buffer_end, &id3->artist) == false) |
197 | return false; | 200 | return false; |
198 | } | 201 | } |
199 | else if(strcmp(line, "NAME") == 0) | 202 | else if(tg_op == 2) /*NAME*/ |
200 | { | 203 | { |
201 | if(read_asap_string(p, &buffer, &buffer_end, &id3->title) == false) | 204 | if(read_asap_string(p, &buffer, &buffer_end, &id3->title) == false) |
202 | return false; | 205 | return false; |
203 | } | 206 | } |
204 | else if(strcmp(line, "DATE") == 0) | 207 | else if(tg_op == 3) /*DATE*/ |
205 | { | 208 | { |
206 | if(read_asap_string(p, &buffer, &buffer_end, &id3->year_string) == false) | 209 | if(read_asap_string(p, &buffer, &buffer_end, &id3->year_string) == false) |
207 | return false; | 210 | return false; |
208 | } | 211 | } |
209 | else if (strcmp(line, "SONGS") == 0) | 212 | else if (tg_op == 4) /*SONGS*/ |
210 | { | 213 | { |
211 | if (parse_dec(&numSongs, p, 1, MAX_SONGS) == false ) | 214 | if (parse_dec(&numSongs, p, 1, MAX_SONGS) == false ) |
212 | return false; | 215 | return false; |
213 | } | 216 | } |
214 | else if (strcmp(line, "DEFSONG") == 0) | 217 | else if (tg_op == 5) /*DEFSONG*/ |
215 | { | 218 | { |
216 | if (parse_dec(&defSong, p, 0, MAX_SONGS) == false) | 219 | if (parse_dec(&defSong, p, 0, MAX_SONGS) == false) |
217 | return false; | 220 | return false; |
218 | } | 221 | } |
219 | else if (strcmp(line, "TIME") == 0) | 222 | else if (tg_op == 6) /*TIME*/ |
220 | { | 223 | { |
221 | int durationTemp = ASAP_ParseDuration(p); | 224 | int durationTemp = ASAP_ParseDuration(p); |
222 | if (durationTemp < 0 || duration_index >= MAX_SONGS) | 225 | 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, | |||
437 | read_uint16le(fd, &count); | 437 | read_uint16le(fd, &count); |
438 | bytesleft -= 2; | 438 | bytesleft -= 2; |
439 | //DEBUGF("extended metadata count = %u\n",count); | 439 | //DEBUGF("extended metadata count = %u\n",count); |
440 | enum | ||
441 | { | ||
442 | eWM_TrackNumber, eWM_Genre, eWM_AlbumTitle, | ||
443 | eWM_AlbumArtist, eWM_Composer, eWM_Year, | ||
444 | eWM_MusicBrainz_Track_Id, eWM_Picture | ||
445 | }; | ||
446 | |||
447 | static const char *tagops[] = | ||
448 | { "WM/TrackNumber", "WM/Genre", "WM/AlbumTitle", | ||
449 | "WM/AlbumArtist", "WM/Composer", "WM/Year", | ||
450 | "MusicBrainz/Track Id", "WM/Picture", NULL | ||
451 | }; | ||
440 | 452 | ||
441 | for (i=0; i < count; i++) { | 453 | for (i=0; i < count; i++) { |
442 | uint16_t length, type; | 454 | uint16_t length, type; |
@@ -450,7 +462,9 @@ static int asf_parse_header(int fd, struct mp3entry* id3, | |||
450 | read_uint16le(fd, &type); | 462 | read_uint16le(fd, &type); |
451 | read_uint16le(fd, &length); | 463 | read_uint16le(fd, &length); |
452 | 464 | ||
453 | if (!strcmp("WM/TrackNumber",utf8buf)) { | 465 | int itag = string_option(utf8buf, tagops, false); |
466 | |||
467 | if (itag == eWM_TrackNumber) { | ||
454 | if (type == 0) { | 468 | if (type == 0) { |
455 | id3->track_string = id3buf; | 469 | id3->track_string = id3buf; |
456 | asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); | 470 | asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); |
@@ -460,19 +474,19 @@ static int asf_parse_header(int fd, struct mp3entry* id3, | |||
460 | } else { | 474 | } else { |
461 | lseek(fd, length, SEEK_CUR); | 475 | lseek(fd, length, SEEK_CUR); |
462 | } | 476 | } |
463 | } else if ((!strcmp("WM/Genre", utf8buf)) && (type == 0)) { | 477 | } else if ((itag == eWM_Genre) && (type == 0)) { |
464 | id3->genre_string = id3buf; | 478 | id3->genre_string = id3buf; |
465 | asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); | 479 | asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); |
466 | } else if ((!strcmp("WM/AlbumTitle", utf8buf)) && (type == 0)) { | 480 | } else if ((itag == eWM_AlbumTitle) && (type == 0)) { |
467 | id3->album = id3buf; | 481 | id3->album = id3buf; |
468 | asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); | 482 | asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); |
469 | } else if ((!strcmp("WM/AlbumArtist", utf8buf)) && (type == 0)) { | 483 | } else if ((itag == eWM_AlbumArtist) && (type == 0)) { |
470 | id3->albumartist = id3buf; | 484 | id3->albumartist = id3buf; |
471 | asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); | 485 | asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); |
472 | } else if ((!strcmp("WM/Composer", utf8buf)) && (type == 0)) { | 486 | } else if ((itag == eWM_Composer) && (type == 0)) { |
473 | id3->composer = id3buf; | 487 | id3->composer = id3buf; |
474 | asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); | 488 | asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); |
475 | } else if (!strcmp("WM/Year", utf8buf)) { | 489 | } else if (itag == eWM_Year) { |
476 | if (type == 0) { | 490 | if (type == 0) { |
477 | id3->year_string = id3buf; | 491 | id3->year_string = id3buf; |
478 | asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); | 492 | asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); |
@@ -482,15 +496,11 @@ static int asf_parse_header(int fd, struct mp3entry* id3, | |||
482 | } else { | 496 | } else { |
483 | lseek(fd, length, SEEK_CUR); | 497 | lseek(fd, length, SEEK_CUR); |
484 | } | 498 | } |
485 | } else if (!strncmp("replaygain_", utf8buf, 11)) { | 499 | } else if (itag == eWM_MusicBrainz_Track_Id) { |
486 | char *value = id3buf; | ||
487 | asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); | ||
488 | parse_replaygain(utf8buf, value, id3); | ||
489 | } else if (!strcmp("MusicBrainz/Track Id", utf8buf)) { | ||
490 | id3->mb_track_id = id3buf; | 500 | id3->mb_track_id = id3buf; |
491 | asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); | 501 | asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); |
492 | #ifdef HAVE_ALBUMART | 502 | #ifdef HAVE_ALBUMART |
493 | } else if (!strcmp("WM/Picture", utf8buf)) { | 503 | } else if (itag == eWM_Picture) { |
494 | uint32_t datalength = 0; | 504 | uint32_t datalength = 0; |
495 | uint32_t strlength; | 505 | uint32_t strlength; |
496 | /* Expected is either "01 00 xx xx 03 yy yy yy yy" or | 506 | /* 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, | |||
521 | asf_utf16LEdecode(fd, 32, &utf8, &utf8length); | 531 | asf_utf16LEdecode(fd, 32, &utf8, &utf8length); |
522 | strlength = (strlen(utf8buf) + 2) * 2; | 532 | strlength = (strlen(utf8buf) + 2) * 2; |
523 | lseek(fd, strlength-32, SEEK_CUR); | 533 | lseek(fd, strlength-32, SEEK_CUR); |
524 | if (!strcmp("image/jpeg", utf8buf)) { | 534 | |
535 | static const char *aa_options[] = {"image/jpeg", | ||
536 | "image/jpg","image/png", NULL}; | ||
537 | int aa_op = string_option(utf8buf, aa_options, false); | ||
538 | |||
539 | if (aa_op == 0) /*image/jpeg*/ | ||
540 | { | ||
525 | id3->albumart.type = AA_TYPE_JPG; | 541 | id3->albumart.type = AA_TYPE_JPG; |
526 | } else if (!strcmp("image/jpg", utf8buf)) { | 542 | } |
543 | else if (aa_op == 1) /*image/jpg*/ | ||
544 | { | ||
527 | /* image/jpg is technically invalid, | 545 | /* image/jpg is technically invalid, |
528 | * but it does occur in the wild */ | 546 | * but it does occur in the wild */ |
529 | id3->albumart.type = AA_TYPE_JPG; | 547 | id3->albumart.type = AA_TYPE_JPG; |
530 | } else if (!strcmp("image/png", utf8buf)) { | 548 | } |
549 | else if (aa_op == 2) /*image/png*/ | ||
550 | { | ||
531 | id3->albumart.type = AA_TYPE_PNG; | 551 | id3->albumart.type = AA_TYPE_PNG; |
532 | } else { | 552 | } else { |
533 | id3->albumart.type = AA_TYPE_UNKNOWN; | 553 | id3->albumart.type = AA_TYPE_UNKNOWN; |
@@ -543,6 +563,10 @@ static int asf_parse_header(int fd, struct mp3entry* id3, | |||
543 | 563 | ||
544 | lseek(fd, datalength, SEEK_CUR); | 564 | lseek(fd, datalength, SEEK_CUR); |
545 | #endif | 565 | #endif |
566 | } else if (!strncmp("replaygain_", utf8buf, 11)) { | ||
567 | char *value = id3buf; | ||
568 | asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); | ||
569 | parse_replaygain(utf8buf, value, id3); | ||
546 | } else { | 570 | } else { |
547 | lseek(fd, length, SEEK_CUR); | 571 | lseek(fd, length, SEEK_CUR); |
548 | } | 572 | } |
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 ) | |||
308 | char *start = tag; | 308 | char *start = tag; |
309 | /* skip text encoding */ | 309 | /* skip text encoding */ |
310 | tag += 1; | 310 | tag += 1; |
311 | static const char *img_options[] = {"jpeg", "jpg", "png", NULL}; | ||
311 | 312 | ||
312 | if (memcmp(tag, "image/", 6) == 0) | 313 | if (memcmp(tag, "image/", 6) == 0) |
313 | { | 314 | { |
315 | int tg_op = string_option(tag, img_options, false); | ||
314 | /* ID3 v2.3+ */ | 316 | /* ID3 v2.3+ */ |
315 | tag += 6; | 317 | tag += 6; |
316 | if (strcmp(tag, "jpeg") == 0) | 318 | if (tg_op == 0) /*jpeg*/ |
317 | { | 319 | { |
318 | entry->albumart.type = AA_TYPE_JPG; | 320 | entry->albumart.type = AA_TYPE_JPG; |
319 | tag += 5; | 321 | tag += 5; |
320 | } | 322 | } |
321 | else if (strcmp(tag, "jpg") == 0) | 323 | else if (tg_op == 1) /*jpg*/ |
322 | { | 324 | { |
323 | /* image/jpg is technically invalid, but it does occur in | 325 | /* image/jpg is technically invalid, but it does occur in |
324 | * the wild */ | 326 | * the wild */ |
325 | entry->albumart.type = AA_TYPE_JPG; | 327 | entry->albumart.type = AA_TYPE_JPG; |
326 | tag += 4; | 328 | tag += 4; |
327 | } | 329 | } |
328 | else if (strcmp(tag, "png") == 0) | 330 | else if (tg_op == 2) /*png*/ |
329 | { | 331 | { |
330 | entry->albumart.type = AA_TYPE_PNG; | 332 | entry->albumart.type = AA_TYPE_PNG; |
331 | tag += 4; | 333 | tag += 4; |
@@ -434,9 +436,11 @@ static int parserva2( struct mp3entry* entry, char* tag, int bufferpos) | |||
434 | } | 436 | } |
435 | } | 437 | } |
436 | 438 | ||
437 | if (strcasecmp(tag, "album") == 0) { | 439 | static const char *tg_options[] = {"album", "track", NULL}; |
440 | int tg_op = string_option(tag, tg_options, true); | ||
441 | if (tg_op == 0) { /*album*/ | ||
438 | album = true; | 442 | album = true; |
439 | } else if (strcasecmp(tag, "track") != 0) { | 443 | } else if (tg_op != 1) { /*!track*/ |
440 | /* Only accept non-track values if we don't have any previous | 444 | /* Only accept non-track values if we don't have any previous |
441 | * value. | 445 | * value. |
442 | */ | 446 | */ |
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) | |||
260 | */ | 260 | */ |
261 | int string_option(const char *option, const char *const oplist[], bool ignore_case) | 261 | int string_option(const char *option, const char *const oplist[], bool ignore_case) |
262 | { | 262 | { |
263 | int i; | ||
264 | int ifound = -1; | ||
265 | const char *op; | 263 | const char *op; |
266 | if (ignore_case) | 264 | int (*cmp_fn)(const char*, const char*) = &strcasecmp; |
265 | if (!ignore_case) | ||
266 | cmp_fn = strcmp; | ||
267 | for (int i=0; (op=oplist[i]) != NULL; i++) | ||
267 | { | 268 | { |
268 | for (i=0; (op=oplist[i]) != NULL; i++) | 269 | if (cmp_fn(op, option) == 0) |
269 | { | 270 | return i; |
270 | if (strcasecmp(op, option) == 0) | ||
271 | { | ||
272 | ifound = i; | ||
273 | break; | ||
274 | } | ||
275 | } | ||
276 | } | ||
277 | else | ||
278 | { | ||
279 | for (i=0; (op=oplist[i]) != NULL; i++) | ||
280 | { | ||
281 | if (strcmp(op, option) == 0) | ||
282 | { | ||
283 | ifound = i; | ||
284 | break; | ||
285 | } | ||
286 | } | ||
287 | } | 271 | } |
288 | return ifound; | 272 | return -1; |
289 | } | 273 | } |
290 | #endif | 274 | #endif |
291 | /* Parse the tag (the name-value pair) and fill id3 and buffer accordingly. | 275 | /* Parse the tag (the name-value pair) and fill id3 and buffer accordingly. |