2 * This file is part of libFirm.
3 * Copyright (C) 2012 Universitaet Karlsruhe
12 #include "lc_opts_t.h"
13 #include "lc_opts_enum.h"
16 static const char *delim = " \t|,";
18 #define DECL_CB(N, op) \
19 int lc_opt_enum_ ## N ## _cb(const char *name, lc_opt_type_t type, void *data, size_t len, ...) \
21 lc_opt_enum_ ## N ## _var_t *var = (lc_opt_enum_ ## N ## _var_t*)data; \
22 const lc_opt_enum_ ## N ## _items_t *items = var->items; \
32 va_start(args, len); \
33 arg = va_arg(args, const char *); \
37 tmp = s = (char*)malloc((end + 1) * sizeof(arg[0])); \
42 while (arg[end] != '\0') { \
45 begin = end + strspn(arg + end, delim); \
46 end = begin + strcspn(arg + begin, delim); \
48 s[end - begin] = '\0'; \
50 for (i = 0; items[i].name != NULL; ++i) { \
51 if (strcmp(s, items[i].name) == 0) { \
52 *var->value op items[i].value; \
67 #define DECL_DUMP(T, N, cond) \
68 int lc_opt_enum_ ## N ## _dump(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t len) \
70 lc_opt_enum_ ## N ## _var_t *var = (lc_opt_enum_ ## N ## _var_t*)data; \
71 const lc_opt_enum_ ## N ## _items_t *items = var->items; \
72 const char *prefix = ""; \
73 TYPE(value) = *var->value; \
75 size_t l = strlen(buf); \
84 for (i = 0; items[i].name != NULL; ++i) { \
85 TYPE(item_value) = items[i].value; \
89 strcat(buf, prefix); \
90 l = strlen(items[i].name); \
93 strcat(buf, items[i].name); \
98 return (int)strlen(buf); \
102 #define DECL_DUMP_VALS(T, N) \
103 int lc_opt_enum_ ## N ## _dump_vals(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t len) \
105 lc_opt_enum_ ## N ## _var_t *var = (lc_opt_enum_ ## N ## _var_t*) data; \
106 const lc_opt_enum_ ## N ## _items_t *items = var->items; \
107 const char *prefix = ""; \
109 size_t l = strlen(buf); \
118 for (i = 0; items[i].name != NULL; ++i) { \
121 strcat(buf, prefix); n -= 2; \
122 l = strlen(items[i].name); \
125 strcat(buf, items[i].name); \
130 return (int)strlen(buf); \
135 #define TYPE(x) int x
136 DECL_DUMP(int, int, item_value == value)
137 DECL_DUMP_VALS(int, int)
140 #define TYPE(x) unsigned x
141 DECL_DUMP(unsigned, mask, (item_value & value) == item_value)
142 DECL_DUMP_VALS(unsigned, mask)
145 #define TYPE(x) void *x
146 DECL_DUMP(void *, ptr, item_value == value)
147 DECL_DUMP_VALS(void *, ptr)
150 #define TYPE(x) const void *x
151 DECL_DUMP(const void *, const_ptr, item_value == value)
152 DECL_DUMP_VALS(const void *, const_ptr)
155 #define TYPE(x) int (*x)(void)
156 DECL_DUMP(int (*)(void), func_ptr, item_value == value)
157 DECL_DUMP_VALS(int (*)(void), func_ptr)