-
Notifications
You must be signed in to change notification settings - Fork 1
/
vlbdb_convenience.c
164 lines (154 loc) · 5.77 KB
/
vlbdb_convenience.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "vlbdb.h"
#include <assert.h>
#include <stdlib.h>
#include <limits.h>
void * vlbdb_specializef (vlbdb_unit_t * unit, void * function,
const char * format, ...)
{
va_list values;
va_start(values, format);
return vlbdb_vspecializef(unit, function, format, values);
}
void * vlbdb_block_specializef (vlbdb_unit_t * unit, void * block,
const char * format, ...)
{
va_list values;
va_start(values, format);
return vlbdb_vblock_specializef(unit, block, format, values);
}
void * vlbdb_vspecializef (vlbdb_unit_t * unit, void * function,
const char * format, va_list values)
{
vlbdb_binder_t * binder = vlbdb_binder_create(unit, function);
vlbdb_vbindf(binder, format, values);
return vlbdb_specialize(binder);
}
void * vlbdb_vblock_specializef (vlbdb_unit_t * unit, void * block,
const char * format, va_list values)
{
vlbdb_binder_t * binder = vlbdb_binder_create_block(unit, block);
vlbdb_vbindf(binder, format, values);
return vlbdb_specialize(binder);
}
void * vlbdb_block_specialize(vlbdb_unit_t * unit, void * block)
{
vlbdb_binder_t * binder = vlbdb_binder_create_block(unit, block);
return vlbdb_specialize(binder);
}
void vlbdb_bindf (vlbdb_binder_t * binder, const char * format, ...)
{
va_list values;
va_start(values, format);
return vlbdb_vbindf(binder, format, values);
}
static const char *
bind_one_argf (vlbdb_binder_t * binder, const char * format, va_list values)
{
int have_width = 0;
long width = 0;
enum {normal, l, z, ll} modifier = normal;
int after_point = 0;
for (; *format != '\0'; format++) {
switch(*format) {
case '*':
case '-': case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
int value = 0;
have_width = 1;
if (*format == '*') {
value = va_arg(values, int);
} else {
value = strtol(format, (char**)&format, 10);
format--;
}
if (!after_point)
width = value;
break;
}
case '.':
after_point = 1;
break;
case 'h': break;
case 'l':
if (modifier == normal)
modifier = l;
else modifier = ll;
break;
case 'j':
modifier = ll;
break;
case 'z':
case 't':
modifier = z;
break;
case 'd': case 'i':
{
long long i = 0;
if (modifier == normal)
i = va_arg(values, int);
else if (modifier < ll)
i = va_arg(values, long);
else i = va_arg(values, long long);
vlbdb_bind_int(binder, i);
return format;
}
case 'c':
assert(modifier == normal);
case 'o': case 'u': case 'x': case 'X':
{
unsigned long long u = 0;
if (modifier == normal)
u = va_arg(values, unsigned);
else if (modifier < ll)
u = va_arg(values, unsigned long);
else u = va_arg(values, unsigned long long);
vlbdb_bind_uint(binder, u);
return format+1;
}
case 'f': case 'F': case 'e': case 'E':
case 'g': case 'G': case 'a': case 'A':
{
double fp = va_arg(values, double);
vlbdb_bind_fp(binder, fp);
return format;
}
case 'p':
{
void * ptr = va_arg(values, void*);
if ((!have_width) || (width == 0)) {
vlbdb_bind_ptr(binder, ptr);
} else if (width < 0) {
assert (0);
/* vlbdb_register_range(binder->unit, */
/* ptr, -width); */
/* vlbdb_bind_ptr(binder, ptr); */
} else if (width > 0) {
vlbdb_bind_range(binder, ptr, width);
} else {
assert(0);
}
return format;
}
case '%': break;
default:
assert(0);
break;
}
}
return format;
}
void vlbdb_vbindf (vlbdb_binder_t * binder, const char * format, va_list values)
{
for (; *format != '\0'; format++) {
if (*format == '%')
format = bind_one_argf(binder, format+1, values);
}
va_end(values);
}
void * vlbdb_specialize (vlbdb_binder_t * binder)
{
void * ret = vlbdb_specialize_retain(binder);
vlbdb_binder_destroy(binder);
return ret;
}