summaryrefslogtreecommitdiffhomepage
path: root/ir/libcore
diff options
context:
space:
mode:
authorChristoph Mallon <christoph.mallon@gmx.de>2015-12-08 12:53:13 +0100
committerChristoph Mallon <christoph.mallon@gmx.de>2015-12-08 12:53:13 +0100
commit621b7436b374b4420fbedb8b85b2bd956f37af8f (patch)
tree5aec060f965feeaf8edd185b2d4c6396c56b731a /ir/libcore
parentef64d1056826f160f309916bcb5d17803b814468 (diff)
libcore: Simplify 'DECL_CB()'.
Diffstat (limited to 'ir/libcore')
-rw-r--r--ir/libcore/lc_opts_enum.c42
1 files changed, 12 insertions, 30 deletions
diff --git a/ir/libcore/lc_opts_enum.c b/ir/libcore/lc_opts_enum.c
index 932256f..ecddfb9 100644
--- a/ir/libcore/lc_opts_enum.c
+++ b/ir/libcore/lc_opts_enum.c
@@ -9,44 +9,26 @@
#include "lc_opts_t.h"
#include "lc_opts_enum.h"
#include "util.h"
-#include "xmalloc.h"
-
-static const char *delim = " \t|,";
#define DECL_CB(N, op) \
-bool lc_opt_enum_ ## N ## _cb(void *const data, size_t const len, char const *const arg) \
+bool lc_opt_enum_##N##_cb(void *const data, size_t const len, char const *const arg) \
{ \
- lc_opt_enum_ ## N ## _var_t *var = (lc_opt_enum_ ## N ## _var_t*)data; \
- const lc_opt_enum_ ## N ## _items_t *items = var->items; \
- \
- char *s, *tmp; \
- size_t begin, end; \
- bool res = false; \
- \
- (void) len; \
- \
- end = strlen(arg); \
- tmp = s = (char*)malloc((end + 1) * sizeof(arg[0])); \
- strcpy(s, arg); \
- s[end] = '\0'; \
+ lc_opt_enum_##N##_var_t const *const var = (lc_opt_enum_##N##_var_t*)data; \
+ (void)len; \
\
- end = 0; \
- while (arg[end] != '\0') { \
- unsigned int i; \
- \
- begin = end + strspn(arg + end, delim); \
- end = begin + strcspn(arg + begin, delim); \
- s = tmp + begin; \
- s[end - begin] = '\0'; \
- \
- for (i = 0; items[i].name != NULL; ++i) { \
- if (streq(s, items[i].name)) { \
- *var->value op items[i].value; \
+ bool res = false; \
+ for (char const *a = arg; *a != '\0';) { \
+ char const *const delim = " \t|,"; \
+ a += strspn(a, delim); \
+ char const *const end = a + strcspn(a, delim); \
+ for (lc_opt_enum_##N##_items_t const *i = var->items; i->name; ++i) { \
+ if (strstart(a, i->name) == end) { \
+ *var->value op i->value; \
res = true; \
} \
} \
+ a = end; \
} \
- free(tmp); \
return res; \
} \