From f8cfe5e8806cfd84dc83714e495b58890ef4bb02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Sun, 10 Feb 2019 00:47:02 +0100 Subject: Add formatting macros. --- trie.inc.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'trie.inc.h') diff --git a/trie.inc.h b/trie.inc.h index 3bc19895..a2590b3e 100644 --- a/trie.inc.h +++ b/trie.inc.h @@ -2,6 +2,8 @@ #error "Set TYPE before including this file" #else +#include + #include "err.h" INIT_F ( TRIE(TYPE) ) { @@ -148,4 +150,46 @@ int TRIE_DOT_HELP(TYPE) ( TRIE_NODE(TYPE)* root, FILE* f ) { return 0; } +FMT_F(TRIE_NODE(TYPE)) { + + va_list ap; + va_start(ap, buf); + int argc = va_arg(ap, int); + int depth = argc >= 1 + ? va_arg(ap, int) + : 0; + va_end(ap); + + int seek = 0; + + TRIE_NODE(TYPE)* n = this; + + if (n == NULL) { fmtf("\n"); } + while (n != NULL) { + fmtf("|"); + FOR(int, i, depth) fmtf(" "); + fmtf("%c ", n->c == '\0' ? '0' : n->c); + if (n->value != NULL) { + seek += FMT(TYPE)(n->value, buf + seek); + fmtf("\n"); + } + + if (n->child != NULL) { + fmtf("\n"); + seek += FMT(TRIE_NODE(TYPE))(n->child, buf + seek, depth + 1); + } + n = n->next; + } + return seek; + +} + +FMT_F(TRIE(TYPE)) { + int seek = 0; + fmtf("Trie: %p: {\n", this); + seek += FMT(TRIE_NODE(TYPE))(this->root->child, buf + seek); + fmtf("}"); + return seek; +} + #endif /* TYPE */ -- cgit v1.2.3