2 * UCW Library: Reading and writing Varints on Fastbuf Streams
4 * (c) 2013 Tomas Valla <tom@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include <ucw/fastbuf.h>
12 #include <ucw/varint.h>
13 #include <ucw/ff-varint.h>
14 #include <ucw/ff-binary.h>
17 u64 bget_varint_slow(struct fastbuf *b, u64 repl)
20 uns l = varint_len(h);
24 if (breadb(b, buf+1, l) < l)
26 varint_get(buf, &repl);
30 void bput_varint_slow(struct fastbuf *b, u64 u)
33 uns l = varint_put(buf, u);
42 int main(int argc, char **argv)
45 F(BGET_VARINT) F(BPUT_VARINT)
48 #define F(x) FUNC_##x,
53 #define F(x) [FUNC_##x] = #x,
60 for (uns i = 0; i < ARRAY_SIZE(names); i++)
61 if (!strcasecmp(names[i], argv[1]))
64 fprintf(stderr, "Invalid usage!\n");
68 struct fastbuf *b = fbgrow_create(8);
73 case FUNC_BGET_VARINT:
74 while (scanf("%x", &u) == 1)
77 while (bpeekc(b) >= 0) {
80 r = bget_varint_slow(b, ~0LLU);
86 case FUNC_BPUT_VARINT:
88 while (scanf("%jx", &r) == 1)
89 bput_varint_slow(b, r);
91 while (bpeekc(b) >= 0) {
94 printf("%02x", bgetc(b));