Commit 621b7436 authored by Christoph Mallon's avatar Christoph Mallon
Browse files

libcore: Simplify 'DECL_CB()'.

parent ef64d105
...@@ -9,44 +9,26 @@ ...@@ -9,44 +9,26 @@
#include "lc_opts_t.h" #include "lc_opts_t.h"
#include "lc_opts_enum.h" #include "lc_opts_enum.h"
#include "util.h" #include "util.h"
#include "xmalloc.h"
static const char *delim = " \t|,";
#define DECL_CB(N, op) \ #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; \ lc_opt_enum_##N##_var_t const *const var = (lc_opt_enum_##N##_var_t*)data; \
const lc_opt_enum_ ## N ## _items_t *items = var->items; \ (void)len; \
\
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'; \
\ \
end = 0; \ bool res = false; \
while (arg[end] != '\0') { \ for (char const *a = arg; *a != '\0';) { \
unsigned int i; \ char const *const delim = " \t|,"; \
\ a += strspn(a, delim); \
begin = end + strspn(arg + end, delim); \ char const *const end = a + strcspn(a, delim); \
end = begin + strcspn(arg + begin, delim); \ for (lc_opt_enum_##N##_items_t const *i = var->items; i->name; ++i) { \
s = tmp + begin; \ if (strstart(a, i->name) == end) { \
s[end - begin] = '\0'; \ *var->value op i->value; \
\
for (i = 0; items[i].name != NULL; ++i) { \
if (streq(s, items[i].name)) { \
*var->value op items[i].value; \
res = true; \ res = true; \
} \ } \
} \ } \
a = end; \
} \ } \
free(tmp); \
return res; \ return res; \
} \ } \
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment