From 9c8cee7a55619f842a13028a7ce58e60dcc9ea5a Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Mon, 13 Aug 2012 22:22:24 -0400 Subject: use a static array and loop to validate actions --- pulsemix.c | 60 ++++++++++++++++++++++++++---------------------------------- runtests | 4 ++++ 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/pulsemix.c b/pulsemix.c index 102f072..e2fdac3 100644 --- a/pulsemix.c +++ b/pulsemix.c @@ -78,6 +78,25 @@ enum action { ACTION_INVALID }; +static const char *actions[ACTION_INVALID] = { + [ACTION_DEFAULTS] = "defaults", + [ACTION_LIST] = "list", + [ACTION_GETVOL] = "get-volume", + [ACTION_SETVOL] = "set-volume", + [ACTION_GETBAL] = "get-balance", + [ACTION_SETBAL] = "set-balance", + [ACTION_ADJBAL] = "adj-balance", + [ACTION_INCREASE] = "increase", + [ACTION_DECREASE] = "decrease", + [ACTION_MUTE] = "mute", + [ACTION_UNMUTE] = "unmute", + [ACTION_TOGGLE] = "toggle", + [ACTION_ISMUTED] = "is-muted", + [ACTION_SETDEFAULT] = "set-default", + [ACTION_MOVE] = "move", + [ACTION_KILL] = "kill" +}; + struct io_t { uint32_t idx; char *name; @@ -648,40 +667,13 @@ static void __attribute__((__noreturn__)) usage(FILE *out) static enum action string_to_verb(const char *string) { - if (strcmp(string, "defaults") == 0) - return ACTION_DEFAULTS; - else if (strcmp(string, "list") == 0) - return ACTION_LIST; - else if (strcmp(string, "get-volume") == 0) - return ACTION_GETVOL; - else if (strcmp(string, "set-volume") == 0) - return ACTION_SETVOL; - else if (strcmp(string, "get-balance") == 0) - return ACTION_GETBAL; - else if (strcmp(string, "set-balance") == 0) - return ACTION_SETBAL; - else if (strcmp(string, "adj-balance") == 0) - return ACTION_ADJBAL; - else if (strcmp(string, "increase") == 0) - return ACTION_INCREASE; - else if (strcmp(string, "decrease") == 0) - return ACTION_DECREASE; - else if (strcmp(string, "mute") == 0) - return ACTION_MUTE; - else if (strcmp(string, "unmute") == 0) - return ACTION_UNMUTE; - else if (strcmp(string, "toggle") == 0) - return ACTION_TOGGLE; - else if (strcmp(string, "is-muted") == 0) - return ACTION_ISMUTED; - else if (strcmp(string, "move") == 0) - return ACTION_MOVE; - else if (strcmp(string, "kill") == 0) - return ACTION_KILL; - else if (strcmp(string, "set-default") == 0) - return ACTION_SETDEFAULT; - - return ACTION_INVALID; + size_t i; + + for (i = 0; i < ACTION_INVALID; i++) + if (strcmp(actions[i], string) == 0) + break; + + return i; } static int do_verb(struct pulseaudio_t *pulse, enum action verb, int value) diff --git a/runtests b/runtests index b2fb861..f5d523f 100755 --- a/runtests +++ b/runtests @@ -28,6 +28,10 @@ do_test() { fi } +# strictly invalid +do_test '' 'herp' +do_test '' 'derp' 100 + # volume do_test 50 'set-volume' 50 do_test 10 'decrease' 40 -- cgit v1.2.3