4 * @author Sebastian Hack
6 * Copyright (C) 2005 Universitaet Karlsruhe
7 * Released under the GPL
9 * Enum callback and dump implementation.
22 #include "lc_opts_t.h"
23 #include "lc_opts_enum.h"
25 static const char *delim = " \t|,";
27 #define DECL_CB(N, op) \
28 int lc_opt_enum_ ## N ## _cb(LC_UNUSED(const char *name), LC_UNUSED(lc_opt_type_t type), void *data, size_t len, ...) \
30 lc_opt_enum_ ## N ## _var_t *var = (lc_opt_enum_ ## N ## _var_t*)data; \
31 const lc_opt_enum_ ## N ## _items_t *items = var->items; \
39 va_start(args, len); \
40 arg = va_arg(args, const char *); \
44 tmp = s = (char*)malloc((end + 1) * sizeof(arg[0])); \
49 while (arg[end] != '\0') { \
52 begin = end + strspn(arg + end, delim); \
53 end = begin + strcspn(arg + begin, delim); \
55 s[end - begin] = '\0'; \
57 for (i = 0; items[i].name != NULL; ++i) { \
58 if (strcmp(s, items[i].name) == 0) { \
59 *var->value op items[i].value; \
74 #define DECL_DUMP(T, N, cond) \
75 int lc_opt_enum_ ## N ## _dump(char *buf, size_t n, LC_UNUSED(const char *name), LC_UNUSED(lc_opt_type_t type), void *data, LC_UNUSED(size_t len)) \
77 lc_opt_enum_ ## N ## _var_t *var = (lc_opt_enum_ ## N ## _var_t*)data; \
78 const lc_opt_enum_ ## N ## _items_t *items = var->items; \
79 const char *prefix = ""; \
80 TYPE(value) = *var->value; \
82 size_t l = strlen(buf); \
88 for (i = 0; items[i].name != NULL; ++i) { \
89 TYPE(item_value) = items[i].value; \
93 strcat(buf, prefix); \
94 l = strlen(items[i].name); \
97 strcat(buf, items[i].name); \
102 return (int)strlen(buf); \
106 #define DECL_DUMP_VALS(T, N) \
107 int lc_opt_enum_ ## N ## _dump_vals(char *buf, size_t n, LC_UNUSED(const char *name), LC_UNUSED(lc_opt_type_t type), void *data, LC_UNUSED(size_t len)) \
109 lc_opt_enum_ ## N ## _var_t *var = (lc_opt_enum_ ## N ## _var_t*) data; \
110 const lc_opt_enum_ ## N ## _items_t *items = var->items; \
111 const char *prefix = ""; \
113 size_t l = strlen(buf); \
119 for (i = 0; items[i].name != NULL; ++i) { \
122 strcat(buf, prefix); n -= 2; \
123 l = strlen(items[i].name); \
126 strcat(buf, items[i].name); \
131 return (int)strlen(buf); \
136 #define TYPE(x) int x
137 DECL_DUMP(int, int, item_value == value)
138 DECL_DUMP_VALS(int, int)
141 #define TYPE(x) unsigned x
142 DECL_DUMP(unsigned, mask, (item_value & value) == item_value)
143 DECL_DUMP_VALS(unsigned, mask)
146 #define TYPE(x) void *x
147 DECL_DUMP(void *, ptr, item_value == value)
148 DECL_DUMP_VALS(void *, ptr)
151 #define TYPE(x) const void *x
152 DECL_DUMP(const void *, const_ptr, item_value == value)
153 DECL_DUMP_VALS(const void *, const_ptr)
156 #define TYPE(x) int (*x)(void)
157 DECL_DUMP(int (*)(void), func_ptr, item_value == value)
158 DECL_DUMP_VALS(int (*)(void), func_ptr)