]> mj.ucw.cz Git - libucw.git/blob - ucw/ff-varint.c
Extended types: Cleaned up unit name parsing
[libucw.git] / ucw / ff-varint.c
1 /*
2  *      UCW Library: Reading and writing Varints on Fastbuf Streams
3  *
4  *      (c) 2013 Tomas Valla <tom@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include <ucw/lib.h>
11 #include <ucw/fastbuf.h>
12 #include <ucw/varint.h>
13 #include <ucw/ff-varint.h>
14 #include <ucw/ff-binary.h>
15
16
17 u64 bget_varint_slow(struct fastbuf *b, u64 repl)
18 {
19         uint h = bgetc(b);
20         uint l = varint_len(h);
21         byte buf[l];
22         buf[0] = h;
23         l--;
24         if (breadb(b, buf+1, l) < l)
25                 return repl;
26         varint_get(buf, &repl);
27         return repl;
28 }
29
30 void bput_varint_slow(struct fastbuf *b, u64 u)
31 {
32         byte buf[9];
33         uint l = varint_put(buf, u);
34         bwrite(b, buf, l);
35 }
36
37 #ifdef TEST
38
39 #include <stdlib.h>
40 #include <stdio.h>
41
42 int main(int argc, char **argv)
43 {
44         #define FUNCS \
45                 F(BGET_VARINT) F(BPUT_VARINT)
46
47         enum {
48                 #define F(x) FUNC_##x,
49                 FUNCS
50                 #undef F
51         };
52         char *names[] = {
53                 #define F(x) [FUNC_##x] = #x,
54                 FUNCS
55                 #undef F
56         };
57
58         uint func = ~0U;
59         if (argc > 1)
60                 for (uint i = 0; i < ARRAY_SIZE(names); i++)
61                         if (!strcasecmp(names[i], argv[1]))
62                                 func = i;
63         if (!~func) {
64                 fprintf(stderr, "Invalid usage!\n");
65                 return 1;
66         }
67
68         struct fastbuf *b = fbgrow_create(8);
69         switch (func) {
70                 uint u;
71                 uintmax_t r;
72                 int i;
73                 case FUNC_BGET_VARINT:
74                         while (scanf("%x", &u) == 1)
75                                 bputc(b, u);
76                         fbgrow_rewind(b);
77                         while (bpeekc(b) >= 0) {
78                                 if (btell(b))
79                                         putchar(' ');
80                                 r = bget_varint_slow(b, ~0LLU);
81                                 printf("%jx", r);
82                         }
83                         putchar('\n');
84                         break;
85
86                 case FUNC_BPUT_VARINT:
87                         i = 0;
88                         while (scanf("%jx", &r) == 1)
89                                 bput_varint_slow(b, r);
90                         fbgrow_rewind(b);
91                         while (bpeekc(b) >= 0) {
92                                 if (i++)
93                                         putchar(' ');
94                                 printf("%02x", bgetc(b));
95                         }
96                         putchar('\n');
97                         break;
98                 default:
99                         ASSERT(0);
100         }
101
102         bclose(b);
103         return 0;
104 }
105
106 #endif