aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Reisner <dreisner@archlinux.org>2012-08-13 22:22:24 -0400
committerDave Reisner <dreisner@archlinux.org>2012-08-13 22:22:24 -0400
commit9c8cee7a55619f842a13028a7ce58e60dcc9ea5a (patch)
tree3879df074e4418fa5b3d656bd76f07c736d2cd26
parent2d285f37b8c9ef785d6697b5ed374607bd420618 (diff)
downloadmirror-ponymix-9c8cee7a55619f842a13028a7ce58e60dcc9ea5a.tar.gz
mirror-ponymix-9c8cee7a55619f842a13028a7ce58e60dcc9ea5a.tar.bz2
mirror-ponymix-9c8cee7a55619f842a13028a7ce58e60dcc9ea5a.zip
use a static array and loop to validate actions
-rw-r--r--pulsemix.c60
-rwxr-xr-xruntests4
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