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 +++++++------------------------ apps/shortcuts.c | 14 +++++++++----- 2 files changed, 16 insertions(+), 29 deletions(-) (limited to 'apps') 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. 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) } 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; ttype == 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); } -- cgit v1.2.3