lwtarantool 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +84 -0
- data/ext/lwtarantool/conn.c +314 -0
- data/ext/lwtarantool/depend +23 -0
- data/ext/lwtarantool/errors.c +24 -0
- data/ext/lwtarantool/extconf.rb +8 -0
- data/ext/lwtarantool/lwtarantool.c +12 -0
- data/ext/lwtarantool/lwtarantool.h +38 -0
- data/ext/lwtarantool/request.c +124 -0
- data/ext/lwtarantool/vendor/msgpuck/CMakeLists.txt +73 -0
- data/ext/lwtarantool/vendor/msgpuck/hints.c +674 -0
- data/ext/lwtarantool/vendor/msgpuck/msgpuck.c +375 -0
- data/ext/lwtarantool/vendor/msgpuck/msgpuck.h +2195 -0
- data/ext/lwtarantool/vendor/msgpuck/test/CMakeLists.txt +25 -0
- data/ext/lwtarantool/vendor/msgpuck/test/msgpuck.c +1126 -0
- data/ext/lwtarantool/vendor/msgpuck/test/test.c +105 -0
- data/ext/lwtarantool/vendor/msgpuck/test/test.h +123 -0
- data/ext/lwtarantool/vendor/tarantool-c/CMakeLists.txt +89 -0
- data/ext/lwtarantool/vendor/tarantool-c/cmake/FindMsgPuck.cmake +18 -0
- data/ext/lwtarantool/vendor/tarantool-c/cmake/FindSphinx.cmake +10 -0
- data/ext/lwtarantool/vendor/tarantool-c/doc/CMakeLists.txt +15 -0
- data/ext/lwtarantool/vendor/tarantool-c/doc/source/example.c +340 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/CMakeLists.txt +6 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tarantool.h +67 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_auth.h +72 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_buf.h +88 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_call.h +83 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_delete.h +52 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_execute.h +24 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_insert.h +62 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_io.h +67 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_iob.h +62 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_iter.h +301 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_mem.h +102 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_net.h +242 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_object.h +248 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_opt.h +138 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_ping.h +49 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_proto.h +295 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_reply.h +204 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_request.h +377 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_schema.h +165 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_select.h +59 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_stream.h +79 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tarantool/tnt_update.h +226 -0
- data/ext/lwtarantool/vendor/tarantool-c/include/tp.h +1998 -0
- data/ext/lwtarantool/vendor/tarantool-c/test/CMakeLists.txt +36 -0
- data/ext/lwtarantool/vendor/tarantool-c/test/common.c +233 -0
- data/ext/lwtarantool/vendor/tarantool-c/test/common.h +28 -0
- data/ext/lwtarantool/vendor/tarantool-c/test/plain_test.c +152 -0
- data/ext/lwtarantool/vendor/tarantool-c/test/tarantool_call.c +57 -0
- data/ext/lwtarantool/vendor/tarantool-c/test/tarantool_disconnect.c +31 -0
- data/ext/lwtarantool/vendor/tarantool-c/test/tarantool_tcp.c +840 -0
- data/ext/lwtarantool/vendor/tarantool-c/test/tarantool_unix.c +384 -0
- data/ext/lwtarantool/vendor/tarantool-c/test/test.c +72 -0
- data/ext/lwtarantool/vendor/tarantool-c/test/test.h +94 -0
- data/ext/lwtarantool/vendor/tarantool-c/test/tnt_assoc.c +6 -0
- data/ext/lwtarantool/vendor/tarantool-c/test/tnt_assoc.h +67 -0
- data/ext/lwtarantool/vendor/tarantool-c/third_party/PMurHash.c +317 -0
- data/ext/lwtarantool/vendor/tarantool-c/third_party/PMurHash.h +68 -0
- data/ext/lwtarantool/vendor/tarantool-c/third_party/base64.c +300 -0
- data/ext/lwtarantool/vendor/tarantool-c/third_party/base64.h +91 -0
- data/ext/lwtarantool/vendor/tarantool-c/third_party/mhash.h +589 -0
- data/ext/lwtarantool/vendor/tarantool-c/third_party/sha1.c +219 -0
- data/ext/lwtarantool/vendor/tarantool-c/third_party/sha1.h +24 -0
- data/ext/lwtarantool/vendor/tarantool-c/third_party/uri.c +6528 -0
- data/ext/lwtarantool/vendor/tarantool-c/third_party/uri.h +81 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/CMakeLists.txt +83 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/pmatomic.h +494 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_assoc.c +9 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_assoc.h +172 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_auth.c +118 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_buf.c +171 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_call.c +79 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_delete.c +52 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_execute.c +60 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_insert.c +60 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_io.c +520 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_iob.c +75 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_iter.c +293 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_mem.c +82 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_net.c +336 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_object.c +476 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_opt.c +117 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_ping.c +38 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_proto_internal.h +43 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_reply.c +300 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_request.c +336 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_schema.c +334 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_select.c +58 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_stream.c +71 -0
- data/ext/lwtarantool/vendor/tarantool-c/tnt/tnt_update.c +247 -0
- data/ext/lwtarantool/vendor/tarantool-c/tntrpl/CMakeLists.txt +69 -0
- data/ext/lwtarantool/vendor/tarantool-c/tntrpl/tnt_dir.c +173 -0
- data/ext/lwtarantool/vendor/tarantool-c/tntrpl/tnt_dir.h +58 -0
- data/ext/lwtarantool/vendor/tarantool-c/tntrpl/tnt_log.c +329 -0
- data/ext/lwtarantool/vendor/tarantool-c/tntrpl/tnt_log.h +119 -0
- data/ext/lwtarantool/vendor/tarantool-c/tntrpl/tnt_rpl.c +189 -0
- data/ext/lwtarantool/vendor/tarantool-c/tntrpl/tnt_rpl.h +47 -0
- data/ext/lwtarantool/vendor/tarantool-c/tntrpl/tnt_snapshot.c +163 -0
- data/ext/lwtarantool/vendor/tarantool-c/tntrpl/tnt_snapshot.h +50 -0
- data/ext/lwtarantool/vendor/tarantool-c/tntrpl/tnt_xlog.c +163 -0
- data/ext/lwtarantool/vendor/tarantool-c/tntrpl/tnt_xlog.h +50 -0
- data/lib/lwtarantool/connection.rb +84 -0
- data/lib/lwtarantool/request.rb +58 -0
- data/lib/lwtarantool.rb +23 -0
- metadata +164 -0
@@ -0,0 +1,375 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2013-2017 MsgPuck Authors
|
3
|
+
* All rights reserved.
|
4
|
+
*
|
5
|
+
* Redistribution and use in source and binary forms, with or
|
6
|
+
* without modification, are permitted provided that the following
|
7
|
+
* conditions are met:
|
8
|
+
*
|
9
|
+
* 1. Redistributions of source code must retain the above
|
10
|
+
* copyright notice, this list of conditions and the
|
11
|
+
* following disclaimer.
|
12
|
+
*
|
13
|
+
* 2. Redistributions in binary form must reproduce the above
|
14
|
+
* copyright notice, this list of conditions and the following
|
15
|
+
* disclaimer in the documentation and/or other materials
|
16
|
+
* provided with the distribution.
|
17
|
+
*
|
18
|
+
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
|
19
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
20
|
+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
21
|
+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
22
|
+
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
23
|
+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
24
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
25
|
+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
26
|
+
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
27
|
+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28
|
+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
29
|
+
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
30
|
+
* SUCH DAMAGE.
|
31
|
+
*/
|
32
|
+
|
33
|
+
#define MP_LIBRARY 1
|
34
|
+
#include "msgpuck.h"
|
35
|
+
|
36
|
+
size_t
|
37
|
+
mp_vformat(char *data, size_t data_size, const char *format, va_list vl)
|
38
|
+
{
|
39
|
+
size_t result = 0;
|
40
|
+
const char *f = NULL;
|
41
|
+
|
42
|
+
for (f = format; *f; f++) {
|
43
|
+
if (f[0] == '[') {
|
44
|
+
uint32_t size = 0;
|
45
|
+
int level = 1;
|
46
|
+
const char *e = NULL;
|
47
|
+
|
48
|
+
for (e = f + 1; level && *e; e++) {
|
49
|
+
if (*e == '[' || *e == '{') {
|
50
|
+
if (level == 1)
|
51
|
+
size++;
|
52
|
+
level++;
|
53
|
+
} else if (*e == ']' || *e == '}') {
|
54
|
+
level--;
|
55
|
+
/* opened '[' must be closed by ']' */
|
56
|
+
assert(level || *e == ']');
|
57
|
+
} else if (*e == '%') {
|
58
|
+
if (e[1] == '%')
|
59
|
+
e++;
|
60
|
+
else if (level == 1)
|
61
|
+
size++;
|
62
|
+
} else if (*e == 'N' && e[1] == 'I'
|
63
|
+
&& e[2] == 'L' && level == 1) {
|
64
|
+
size++;
|
65
|
+
}
|
66
|
+
}
|
67
|
+
/* opened '[' must be closed */
|
68
|
+
assert(level == 0);
|
69
|
+
result += mp_sizeof_array(size);
|
70
|
+
if (result <= data_size)
|
71
|
+
data = mp_encode_array(data, size);
|
72
|
+
} else if (f[0] == '{') {
|
73
|
+
uint32_t count = 0;
|
74
|
+
int level = 1;
|
75
|
+
const char *e = NULL;
|
76
|
+
|
77
|
+
for (e = f + 1; level && *e; e++) {
|
78
|
+
if (*e == '[' || *e == '{') {
|
79
|
+
if (level == 1)
|
80
|
+
count++;
|
81
|
+
level++;
|
82
|
+
} else if (*e == ']' || *e == '}') {
|
83
|
+
level--;
|
84
|
+
/* opened '{' must be closed by '}' */
|
85
|
+
assert(level || *e == '}');
|
86
|
+
} else if (*e == '%') {
|
87
|
+
if (e[1] == '%')
|
88
|
+
e++;
|
89
|
+
else if (level == 1)
|
90
|
+
count++;
|
91
|
+
} else if (*e == 'N' && e[1] == 'I'
|
92
|
+
&& e[2] == 'L' && level == 1) {
|
93
|
+
count++;
|
94
|
+
}
|
95
|
+
}
|
96
|
+
/* opened '{' must be closed */
|
97
|
+
assert(level == 0);
|
98
|
+
/* since map is a pair list, count must be even */
|
99
|
+
assert(count % 2 == 0);
|
100
|
+
uint32_t size = count / 2;
|
101
|
+
result += mp_sizeof_map(size);
|
102
|
+
if (result <= data_size)
|
103
|
+
data = mp_encode_map(data, size);
|
104
|
+
} else if (f[0] == '%') {
|
105
|
+
f++;
|
106
|
+
assert(f[0]);
|
107
|
+
int64_t int_value = 0;
|
108
|
+
int int_status = 0; /* 1 - signed, 2 - unsigned */
|
109
|
+
|
110
|
+
if (f[0] == 'd' || f[0] == 'i') {
|
111
|
+
int_value = va_arg(vl, int);
|
112
|
+
int_status = 1;
|
113
|
+
} else if (f[0] == 'u') {
|
114
|
+
int_value = va_arg(vl, unsigned int);
|
115
|
+
int_status = 2;
|
116
|
+
} else if (f[0] == 's') {
|
117
|
+
const char *str = va_arg(vl, const char *);
|
118
|
+
uint32_t len = (uint32_t)strlen(str);
|
119
|
+
result += mp_sizeof_str(len);
|
120
|
+
if (result <= data_size)
|
121
|
+
data = mp_encode_str(data, str, len);
|
122
|
+
} else if (f[0] == '.' && f[1] == '*' && f[2] == 's') {
|
123
|
+
uint32_t len = va_arg(vl, uint32_t);
|
124
|
+
const char *str = va_arg(vl, const char *);
|
125
|
+
result += mp_sizeof_str(len);
|
126
|
+
if (result <= data_size)
|
127
|
+
data = mp_encode_str(data, str, len);
|
128
|
+
f += 2;
|
129
|
+
} else if (f[0] == 'p') {
|
130
|
+
const char *p = va_arg(vl, const char *);
|
131
|
+
const char *end = p;
|
132
|
+
mp_next(&end);
|
133
|
+
uint32_t len = end - p;
|
134
|
+
result += len;
|
135
|
+
if (result <= data_size) {
|
136
|
+
memcpy(data, p, len);
|
137
|
+
data += len;
|
138
|
+
}
|
139
|
+
} else if (f[0] == '.' && f[1] == '*' && f[2] == 'p') {
|
140
|
+
uint32_t len = va_arg(vl, uint32_t);
|
141
|
+
const char *p = va_arg(vl, const char *);
|
142
|
+
assert(len > 0);
|
143
|
+
result += len;
|
144
|
+
if (result <= data_size) {
|
145
|
+
memcpy(data, p, len);
|
146
|
+
data += len;
|
147
|
+
}
|
148
|
+
f += 2;
|
149
|
+
} else if(f[0] == 'f') {
|
150
|
+
float v = (float)va_arg(vl, double);
|
151
|
+
result += mp_sizeof_float(v);
|
152
|
+
if (result <= data_size)
|
153
|
+
data = mp_encode_float(data, v);
|
154
|
+
} else if(f[0] == 'l' && f[1] == 'f') {
|
155
|
+
double v = va_arg(vl, double);
|
156
|
+
result += mp_sizeof_double(v);
|
157
|
+
if (result <= data_size)
|
158
|
+
data = mp_encode_double(data, v);
|
159
|
+
f++;
|
160
|
+
} else if(f[0] == 'b') {
|
161
|
+
bool v = (bool)va_arg(vl, int);
|
162
|
+
result += mp_sizeof_bool(v);
|
163
|
+
if (result <= data_size)
|
164
|
+
data = mp_encode_bool(data, v);
|
165
|
+
} else if (f[0] == 'l'
|
166
|
+
&& (f[1] == 'd' || f[1] == 'i')) {
|
167
|
+
int_value = va_arg(vl, long);
|
168
|
+
int_status = 1;
|
169
|
+
f++;
|
170
|
+
} else if (f[0] == 'l' && f[1] == 'u') {
|
171
|
+
int_value = va_arg(vl, unsigned long);
|
172
|
+
int_status = 2;
|
173
|
+
f++;
|
174
|
+
} else if (f[0] == 'l' && f[1] == 'l'
|
175
|
+
&& (f[2] == 'd' || f[2] == 'i')) {
|
176
|
+
int_value = va_arg(vl, long long);
|
177
|
+
int_status = 1;
|
178
|
+
f += 2;
|
179
|
+
} else if (f[0] == 'l' && f[1] == 'l' && f[2] == 'u') {
|
180
|
+
int_value = va_arg(vl, unsigned long long);
|
181
|
+
int_status = 2;
|
182
|
+
f += 2;
|
183
|
+
} else if (f[0] == 'h'
|
184
|
+
&& (f[1] == 'd' || f[1] == 'i')) {
|
185
|
+
int_value = va_arg(vl, int);
|
186
|
+
int_status = 1;
|
187
|
+
f++;
|
188
|
+
} else if (f[0] == 'h' && f[1] == 'u') {
|
189
|
+
int_value = va_arg(vl, unsigned int);
|
190
|
+
int_status = 2;
|
191
|
+
f++;
|
192
|
+
} else if (f[0] == 'h' && f[1] == 'h'
|
193
|
+
&& (f[2] == 'd' || f[2] == 'i')) {
|
194
|
+
int_value = va_arg(vl, int);
|
195
|
+
int_status = 1;
|
196
|
+
f += 2;
|
197
|
+
} else if (f[0] == 'h' && f[1] == 'h' && f[2] == 'u') {
|
198
|
+
int_value = va_arg(vl, unsigned int);
|
199
|
+
int_status = 2;
|
200
|
+
f += 2;
|
201
|
+
} else if (f[0] != '%') {
|
202
|
+
/* unexpected format specifier */
|
203
|
+
assert(false);
|
204
|
+
}
|
205
|
+
|
206
|
+
if (int_status == 1 && int_value < 0) {
|
207
|
+
result += mp_sizeof_int(int_value);
|
208
|
+
if (result <= data_size)
|
209
|
+
data = mp_encode_int(data, int_value);
|
210
|
+
} else if(int_status) {
|
211
|
+
result += mp_sizeof_uint(int_value);
|
212
|
+
if (result <= data_size)
|
213
|
+
data = mp_encode_uint(data, int_value);
|
214
|
+
}
|
215
|
+
} else if (f[0] == 'N' && f[1] == 'I' && f[2] == 'L') {
|
216
|
+
result += mp_sizeof_nil();
|
217
|
+
if (result <= data_size)
|
218
|
+
data = mp_encode_nil(data);
|
219
|
+
f += 2;
|
220
|
+
}
|
221
|
+
}
|
222
|
+
return result;
|
223
|
+
}
|
224
|
+
|
225
|
+
size_t
|
226
|
+
mp_format(char *data, size_t data_size, const char *format, ...)
|
227
|
+
{
|
228
|
+
va_list args;
|
229
|
+
va_start(args, format);
|
230
|
+
size_t res = mp_vformat(data, data_size, format, args);
|
231
|
+
va_end(args);
|
232
|
+
return res;
|
233
|
+
}
|
234
|
+
|
235
|
+
#define MP_PRINT(SELF, PRINTF) \
|
236
|
+
{ \
|
237
|
+
switch (mp_typeof(**data)) { \
|
238
|
+
case MP_NIL: \
|
239
|
+
mp_decode_nil(data); \
|
240
|
+
PRINTF("null"); \
|
241
|
+
break; \
|
242
|
+
case MP_UINT: \
|
243
|
+
PRINTF("%llu", (unsigned long long) mp_decode_uint(data)); \
|
244
|
+
break; \
|
245
|
+
case MP_INT: \
|
246
|
+
PRINTF("%lld", (long long) mp_decode_int(data)); \
|
247
|
+
break; \
|
248
|
+
case MP_STR: \
|
249
|
+
case MP_BIN: \
|
250
|
+
{ \
|
251
|
+
uint32_t len = mp_typeof(**data) == MP_STR ? \
|
252
|
+
mp_decode_strl(data) : mp_decode_binl(data); \
|
253
|
+
PRINTF("\""); \
|
254
|
+
const char *s; \
|
255
|
+
for (s = *data; s < *data + len; s++) { \
|
256
|
+
unsigned char c = (unsigned char ) *s; \
|
257
|
+
if (c < 128 && mp_char2escape[c] != NULL) { \
|
258
|
+
/* Escape character */ \
|
259
|
+
PRINTF("%s", mp_char2escape[c]); \
|
260
|
+
} else { \
|
261
|
+
PRINTF("%c", c); \
|
262
|
+
} \
|
263
|
+
} \
|
264
|
+
PRINTF("\""); \
|
265
|
+
*data += len; \
|
266
|
+
break; \
|
267
|
+
} \
|
268
|
+
case MP_ARRAY: \
|
269
|
+
{ \
|
270
|
+
uint32_t count = mp_decode_array(data); \
|
271
|
+
PRINTF("["); \
|
272
|
+
uint32_t i; \
|
273
|
+
for (i = 0; i < count; i++) { \
|
274
|
+
if (i) \
|
275
|
+
PRINTF(", "); \
|
276
|
+
SELF(data); \
|
277
|
+
} \
|
278
|
+
PRINTF("]"); \
|
279
|
+
break; \
|
280
|
+
} \
|
281
|
+
case MP_MAP: \
|
282
|
+
{ \
|
283
|
+
uint32_t count = mp_decode_map(data); \
|
284
|
+
PRINTF("{"); \
|
285
|
+
uint32_t i; \
|
286
|
+
for (i = 0; i < count; i++) { \
|
287
|
+
if (i) \
|
288
|
+
PRINTF(", "); \
|
289
|
+
SELF(data); \
|
290
|
+
PRINTF(": "); \
|
291
|
+
SELF(data); \
|
292
|
+
} \
|
293
|
+
PRINTF("}"); \
|
294
|
+
break; \
|
295
|
+
} \
|
296
|
+
case MP_BOOL: \
|
297
|
+
PRINTF(mp_decode_bool(data) ? "true" : "false"); \
|
298
|
+
break; \
|
299
|
+
case MP_FLOAT: \
|
300
|
+
PRINTF("%g", mp_decode_float(data)); \
|
301
|
+
break; \
|
302
|
+
case MP_DOUBLE: \
|
303
|
+
PRINTF("%lg", mp_decode_double(data)); \
|
304
|
+
break; \
|
305
|
+
case MP_EXT: \
|
306
|
+
mp_next(data); \
|
307
|
+
PRINTF("undefined"); \
|
308
|
+
break; \
|
309
|
+
default: \
|
310
|
+
mp_unreachable(); \
|
311
|
+
return -1; \
|
312
|
+
} \
|
313
|
+
}
|
314
|
+
|
315
|
+
static inline int
|
316
|
+
mp_fprint_internal(FILE *file, const char **data)
|
317
|
+
{
|
318
|
+
int total_bytes = 0;
|
319
|
+
#define HANDLE(FUN, ...) do { \
|
320
|
+
int bytes = FUN(file, __VA_ARGS__); \
|
321
|
+
if (mp_unlikely(bytes < 0)) \
|
322
|
+
return -1; \
|
323
|
+
total_bytes += bytes; \
|
324
|
+
} while (0)
|
325
|
+
#define PRINT(...) HANDLE(fprintf, __VA_ARGS__)
|
326
|
+
#define SELF(...) HANDLE(mp_fprint_internal, __VA_ARGS__)
|
327
|
+
MP_PRINT(SELF, PRINT)
|
328
|
+
#undef HANDLE
|
329
|
+
#undef SELF
|
330
|
+
#undef PRINT
|
331
|
+
return total_bytes;
|
332
|
+
}
|
333
|
+
|
334
|
+
int
|
335
|
+
mp_fprint(FILE *file, const char *data)
|
336
|
+
{
|
337
|
+
if (!file)
|
338
|
+
file = stdout;
|
339
|
+
int res = mp_fprint_internal(file, &data);
|
340
|
+
return res;
|
341
|
+
}
|
342
|
+
|
343
|
+
static inline int
|
344
|
+
mp_snprint_internal(char *buf, int size, const char **data)
|
345
|
+
{
|
346
|
+
int total_bytes = 0;
|
347
|
+
#define HANDLE(FUN, ...) do { \
|
348
|
+
int bytes = FUN(buf, size, __VA_ARGS__); \
|
349
|
+
if (mp_unlikely(bytes < 0)) \
|
350
|
+
return -1; \
|
351
|
+
total_bytes += bytes; \
|
352
|
+
if (bytes < size) { \
|
353
|
+
buf += bytes; \
|
354
|
+
size -= bytes; \
|
355
|
+
} else { \
|
356
|
+
/* Calculate the number of bytes needed */ \
|
357
|
+
buf = NULL; \
|
358
|
+
size = 0; \
|
359
|
+
} \
|
360
|
+
} while (0)
|
361
|
+
#define PRINT(...) HANDLE(snprintf, __VA_ARGS__)
|
362
|
+
#define SELF(...) HANDLE(mp_snprint_internal, __VA_ARGS__)
|
363
|
+
MP_PRINT(SELF, PRINT)
|
364
|
+
#undef HANDLE
|
365
|
+
#undef SELF
|
366
|
+
#undef PRINT
|
367
|
+
return total_bytes;
|
368
|
+
}
|
369
|
+
#undef MP_PRINT
|
370
|
+
|
371
|
+
int
|
372
|
+
mp_snprint(char *buf, int size, const char *data)
|
373
|
+
{
|
374
|
+
return mp_snprint_internal(buf, size, &data);
|
375
|
+
}
|