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 --- apps/misc.c | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) (limited to 'apps/misc.c') 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 * 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; - } - } - } - 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; } /** Open a UTF-8 file and set file descriptor to first byte after BOM. -- cgit v1.2.3