torrentd

simple torrent daemon
git clone git@git.2f30.org/torrentd.git
Log | Files | Refs | LICENSE

commit 16145cd3fcbc8a51170f8f06cad2eabcedd3ff4d
parent 0804b3301b521e8f346641e8ad27bc9c0115b78d
Author: sin <sin@2f30.org>
Date:   Thu, 17 Dec 2015 14:29:59 +0000

Add bprint()

Diffstat:
Mben.c | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msbtd.h | 1+
2 files changed, 60 insertions(+), 0 deletions(-)

diff --git a/ben.c b/ben.c @@ -267,3 +267,62 @@ bfree(struct ben *b) break; } } + +static void +printstr(struct ben *b) +{ + int64_t i; + + for (i = 0; i < b->len; i++) + putchar(b->s[i]); +} + +static void +printindent(int indent) +{ + while (indent--) + putchar(' '); +} + +void +bprint(struct ben *b, int indent) +{ + struct ben *bl; + struct ben *bd; + + if (!b) + return; + switch (b->type) { + case 's': + printindent(indent); + printstr(b); + putchar('\n'); + break; + case 'i': + printindent(indent); + printf("%lld\n", (long long)b->i); + break; + case 'l': + printindent(indent); + printf("[\n"); + for (bl = b; bl; bl = bl->next) + bprint(bl->v, indent + 1); + printindent(indent); + printf("]\n"); + break; + case 'd': + printindent(indent); + printf("{\n"); + for (bd = b; bd; bd = bd->next) { + if (bd->k) { + printindent(indent + 1); + printstr(bd->k); + printf(" :\n"); + bprint(bd->v, indent + 2); + } + } + printindent(indent); + printf("}\n"); + break; + } +} diff --git a/sbtd.h b/sbtd.h @@ -33,6 +33,7 @@ int bstrcmp(struct ben *, struct ben *); struct ben *dlook(struct ben *, struct ben *); struct ben *dlookstr(struct ben *, char *); void bfree(struct ben *); +void bprint(struct ben *, int); /* util.c */ void *emalloc(size_t);