-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathben.h
More file actions
50 lines (40 loc) · 947 Bytes
/
ben.h
File metadata and controls
50 lines (40 loc) · 947 Bytes
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
#ifndef BEN_H_
#define BEN_H_
#include <ccan/darray/darray.h>
#include <assert.h>
#include <limits.h>
/* TODO: provide checking for
* - improper dict ordering
* - not specing a string as the dict key
* TODO: support output into a non-darray
*/
static inline void ben_dict_begin(darray_char *d)
{
darray_append(*d, 'd');
}
static inline void ben_dict_end(darray_char *d)
{
darray_append(*d, 'e');
}
static inline void ben_list_begin(darray_char *d)
{
darray_append(*d, 'l');
}
static inline void ben_list_end(darray_char *d)
{
darray_append(*d, 'e');
}
static inline void ben_string(darray_char *d, const char *bytes, size_t len)
{
assert(len <= INT_MAX);
darray_printf(*d, "%zu:%.*s", len, (int)len, bytes);
}
static inline void ben_integer(darray_char *d, long long value)
{
darray_printf(*d, "i%llde", value);
}
static inline void ben_string_c(darray_char *d, const char *str)
{
ben_string(d, str, strlen(str));
}
#endif