summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugene Syromyatnikov <evgsyr@gmail.com>2018-09-04 13:22:11 +0200
committerDmitry V. Levin <ldv@altlinux.org>2019-08-06 13:38:20 +0000
commit930cec83b62972f0b94e19fcd10e2f553861cf8a (patch)
tree3c2d23365cdc35d623fbcaaef920a73e93eb0c6c
parent1475a76e96b29977fe8271f81adb398904017d6f (diff)
downloadstrace-930cec83b62972f0b94e19fcd10e2f553861cf8a.tar.gz
count: rewrite sort function selection using a table
* count.c (set_sortby): Replace nested if's with iteration over a table. Co-Authored-by: Dmitry V. Levin <ldv@altlinux.org>
-rw-r--r--count.c27
1 files changed, 17 insertions, 10 deletions
diff --git a/count.c b/count.c
index 3adde7a36..329909ec3 100644
--- a/count.c
+++ b/count.c
@@ -91,17 +91,24 @@ static int (*sortfun)(const void *, const void *);
void
set_sortby(const char *sortby)
{
- if (strcmp(sortby, "time") == 0)
- sortfun = time_cmp;
- else if (strcmp(sortby, "calls") == 0)
- sortfun = count_cmp;
- else if (strcmp(sortby, "name") == 0)
- sortfun = syscall_cmp;
- else if (strcmp(sortby, "nothing") == 0)
- sortfun = NULL;
- else {
- error_msg_and_help("invalid sortby: '%s'", sortby);
+ static const struct {
+ int (*fn)(const void *, const void *);
+ const char *name;
+ } sort_fns[] = {
+ { time_cmp, "time" },
+ { count_cmp, "calls" },
+ { syscall_cmp, "name" },
+ { NULL, "nothing" },
+ };
+
+ for (size_t i = 0; i < ARRAY_SIZE(sort_fns); ++i) {
+ if (!strcmp(sort_fns[i].name, sortby)) {
+ sortfun = sort_fns[i].fn;
+ return;
+ }
}
+
+ error_msg_and_help("invalid sortby: '%s'", sortby);
}
int