4 * @author Sebastian Hack
6 * Copyright (C) 2005 Universitaet Karlsruhe
7 * Released under the GPL
9 * Enum callback and dump implementation.
18 #if defined(__FreeBSD__)
26 #include "lc_opts_t.h"
27 #include "lc_opts_enum.h"
29 static const char *delim = " \t|,";
31 #define DECL_CB(N, op) \
32 int lc_opt_enum_ ## N ## _cb(LC_UNUSED(const char *name), LC_UNUSED(lc_opt_type_t type), void *data, size_t len, ...) \
34 lc_opt_enum_ ## N ## _var_t *var = data; \
35 const lc_opt_enum_ ## N ## _items_t *items = var->items; \
43 va_start(args, len); \
44 arg = va_arg(args, const char *); \
48 tmp = s = malloc((end + 1) * sizeof(arg[0])); \
53 while(arg[end] != '\0') { \
56 begin = end + strspn(arg + end, delim); \
57 end = begin + strcspn(arg + begin, delim); \
59 s[end - begin] = '\0'; \
61 for(i = 0; items[i].name != NULL; ++i) { \
62 if(strcmp(s, items[i].name) == 0) { \
63 *var->value op items[i].value; \
78 #define DECL_DUMP(T, N, cond) \
79 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)) \
81 lc_opt_enum_ ## N ## _var_t *var = data; \
82 const lc_opt_enum_ ## N ## _items_t *items = var->items; \
83 const char *prefix = ""; \
84 TYPE(value) = *var->value; \
86 size_t l = strlen(buf); \
92 for (i = 0; items[i].name != NULL; ++i) { \
93 TYPE(item_value) = items[i].value; \
97 strcat(buf, prefix); \
98 l = strlen(items[i].name); \
101 strcat(buf, items[i].name); \
106 return strlen(buf); \
110 #define DECL_DUMP_VALS(T, N) \
111 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)) \
113 lc_opt_enum_ ## N ## _var_t *var = data; \
114 const lc_opt_enum_ ## N ## _items_t *items = var->items; \
115 const char *prefix = ""; \
117 size_t l = strlen(buf); \
123 for (i = 0; items[i].name != NULL; ++i) { \
126 strcat(buf, prefix); n -= 2; \
127 l = strlen(items[i].name); \
130 strcat(buf, items[i].name); \
135 return strlen(buf); \
140 #define TYPE(x) int x
141 DECL_DUMP(int, int, item_value == value)
142 DECL_DUMP_VALS(int, int)
145 #define TYPE(x) unsigned x
146 DECL_DUMP(unsigned, mask, (item_value & value) == item_value)
147 DECL_DUMP_VALS(unsigned, mask)
150 #define TYPE(x) void *x
151 DECL_DUMP(void *, ptr, item_value == value)
152 DECL_DUMP_VALS(void *, ptr)
155 #define TYPE(x) const void *x
156 DECL_DUMP(const void *, const_ptr, item_value == value)
157 DECL_DUMP_VALS(const void *, const_ptr)
160 #define TYPE(x) int (*x)(void)
161 DECL_DUMP(int (*)(void), func_ptr, item_value == value)
162 DECL_DUMP_VALS(int (*)(void), func_ptr)