lc_opts_enum.c 2.63 KB
Newer Older
1
2
3
/*
 * This file is part of libFirm.
 * Copyright (C) 2012 Universitaet Karlsruhe
4
5
6
7
8
9
10
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "lc_opts_t.h"
#include "lc_opts_enum.h"
Christoph Mallon's avatar
Christoph Mallon committed
11
#include "util.h"
12
13

#define DECL_CB(N, op) \
14
bool lc_opt_enum_##N##_cb(void *const data, size_t const len, char const *const arg) \
15
{ \
16
17
	lc_opt_enum_##N##_var_t const *const var = (lc_opt_enum_##N##_var_t*)data; \
	(void)len; \
18
	\
19
20
21
22
23
24
25
26
	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; \
27
				res = true; \
28
29
			} \
		} \
30
		a = end; \
31
32
33
34
35
36
37
38
39
	} \
	return res; \
} \

DECL_CB(int, =)
DECL_CB(mask, |=)
DECL_CB(func_ptr, =)

#define DECL_DUMP(T, N, cond) \
40
int lc_opt_enum_ ## N ## _dump(char *buf, size_t n, void *data) \
41
{ \
42
	lc_opt_enum_ ## N ## _var_t *var           = (lc_opt_enum_ ## N ## _var_t*)data;       \
43
44
	const lc_opt_enum_ ## N ## _items_t *items = var->items; \
	const char *prefix                         = "";         \
45
46
	TYPE(value) = *var->value; \
	int i; \
47
	size_t l = strlen(buf); \
48
 \
49
50
51
52
53
	if (l >= n) \
		return (int)l; \
	n -= l; \
	n += 2; \
	for (i = 0; items[i].name != NULL; ++i) { \
54
		TYPE(item_value) = items[i].value; \
55
56
57
58
59
60
61
62
		if (cond) { \
			if (n <= 2) \
				break; \
			strcat(buf, prefix); \
			l = strlen(items[i].name); \
			if (n <= l) \
				break; \
			strcat(buf, items[i].name); \
63
64
65
66
			prefix = ", "; \
		} \
	} \
 \
67
	return (int)strlen(buf); \
68
69
70
71
} \


#define DECL_DUMP_VALS(T, N) \
72
int lc_opt_enum_ ## N ## _dump_vals(char *buf, size_t n, void *data) \
73
{ \
74
	lc_opt_enum_ ## N ## _var_t *var           = (lc_opt_enum_ ## N ## _var_t*) data;       \
75
76
	const lc_opt_enum_ ## N ## _items_t *items = var->items; \
	const char *prefix                         = "";         \
77
	int i; \
78
	size_t l = strlen(buf); \
79
 \
80
81
82
83
84
85
86
87
88
89
90
91
92
	if (l >= n) \
		return (int)l; \
	n -= l; \
	n += 2; \
	for (i = 0; items[i].name != NULL; ++i) { \
		if (n <= 2) \
			break; \
		strcat(buf, prefix); n -= 2; \
		l = strlen(items[i].name); \
		if (n <= l) \
			break; \
		strcat(buf, items[i].name); \
		n -= l; \
93
94
95
		prefix = ", "; \
	} \
 \
96
	return (int)strlen(buf); \
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
} \



#define TYPE(x) int x
DECL_DUMP(int, int, item_value == value)
DECL_DUMP_VALS(int, int)
#undef TYPE

#define TYPE(x) unsigned x
DECL_DUMP(unsigned, mask, (item_value & value) == item_value)
DECL_DUMP_VALS(unsigned, mask)
#undef TYPE

#define TYPE(x) int (*x)(void)
DECL_DUMP(int (*)(void), func_ptr, item_value == value)
DECL_DUMP_VALS(int (*)(void), func_ptr)
#undef TYPE