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. --- linked_list.h | 2 ++ linked_list.inc.h | 11 +++++++++++ macro.h | 15 +++++++++++++++ strbuf.c | 10 ++++++++++ strbuf.h | 4 ++++ trie.h | 3 +++ trie.inc.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ vcal.c | 12 ++++++++++++ vcal.h | 2 ++ 9 files changed, 103 insertions(+) diff --git a/linked_list.h b/linked_list.h index c46304cf..35e90d9e 100644 --- a/linked_list.h +++ b/linked_list.h @@ -59,6 +59,8 @@ int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ); LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new); +FMT_F(LLIST(TYPE)); + /* Iterator */ // #define __BEG_LLIST(v, set) v = (set)->head diff --git a/linked_list.inc.h b/linked_list.inc.h index 6a0546c7..6bf3d158 100644 --- a/linked_list.inc.h +++ b/linked_list.inc.h @@ -164,4 +164,15 @@ int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ) { return 0; } +FMT_F(LLIST (TYPE)) { + int seek = 0; + fmtf("("); + FOR(LLIST(TYPE), link, this) { + seek += FMT(TYPE)(link->value, buf + seek); + } + fmtf(")"); + + return seek; +} + #endif /* TYPE */ diff --git a/macro.h b/macro.h index bd421260..e1bff786 100644 --- a/macro.h +++ b/macro.h @@ -109,5 +109,20 @@ #define GET(T) TEMPL(get , T) #define RESET(T) TEMPL(reset , T) +/* + * Formatting macros. + * Transform objects into string representation of themselves. + * buf should be a suffisiently large memmory location, if it's to + * small then bad stuff might happen. + * + * Should return the number of bytes written (like sprintf). + */ + +#define FMT_T(T) TEMPL(format , T) +#define FMT_F(T) int FMT_T(T)(T* this, char* buf, ...) +// TODO change order of buf and item +#define __FMT_HELP(item, buf, ...) ((item), (buf), VA_ARGS_NUM(__VA_ARGS__), ## __VA_ARGS__) +#define FMT(T) FMT_T(T) __FMT_HELP +#define fmtf(...) seek += sprintf(buf + seek, __VA_ARGS__) #endif /* MACRO_H */ diff --git a/strbuf.c b/strbuf.c index 8d1c7f15..dcfcc826 100644 --- a/strbuf.c +++ b/strbuf.c @@ -1,6 +1,7 @@ #include "strbuf.h" #include +#include #ifdef SAFE_STR #include "err.h" @@ -153,3 +154,12 @@ int strbuf_soft_reset(strbuf* s) { s->ptr = s->len = 0; return 0; } + +strbuf* RESOLVE(strbuf)(strbuf* dest, strbuf* new) { + if (dest == NULL) return new; + else return dest; +} + +FMT_F(strbuf) { + return sprintf(buf, "%s", this->mem); +} diff --git a/strbuf.h b/strbuf.h index 77a401b5..e6f59896 100644 --- a/strbuf.h +++ b/strbuf.h @@ -103,4 +103,8 @@ int strbuf_realloc_copy(strbuf* dest, strbuf* src); */ int strbuf_init_copy(strbuf* dest, strbuf* src); +strbuf* RESOLVE(strbuf)(strbuf*, strbuf*); + +FMT_F(strbuf); + #endif /* STRBUF_H */ diff --git a/trie.h b/trie.h index 9acbe300..293b4841 100644 --- a/trie.h +++ b/trie.h @@ -51,4 +51,7 @@ int TRIE_DOT_HELP(TYPE) ( TRIE_NODE(TYPE)*, FILE* ); int EMPTY(TRIE(TYPE))(TRIE(TYPE)*); +FMT_F(TRIE_NODE(TYPE)); +FMT_F(TRIE(TYPE)); + #endif /* TYPE */ 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 */ diff --git a/vcal.c b/vcal.c index 3cb46b5f..b54a0370 100644 --- a/vcal.c +++ b/vcal.c @@ -141,3 +141,15 @@ int DEEP_COPY(vcomponent)(vcomponent* a, vcomponent* b) { ERR("Deep copy not implemented for vcomponent"); return -1; } + +FMT_F(vcomponent) { + return sprintf(buf, "vcomp (%p)", this); +} + +FMT_F(content_line) { + char str_a[100], str_b[100], str_c[100];; + FMT(strbuf)(&this->key, str_a); + FMT(LLIST(strbuf))(&this->params, str_b); + FMT(LLIST(strbuf))(&this->vals, str_c); + return sprintf(buf, "[[cl|%s] params := %s vals := %s]", str_a, str_b, str_c); +} diff --git a/vcal.h b/vcal.h index 761448c9..dc639232 100644 --- a/vcal.h +++ b/vcal.h @@ -87,5 +87,7 @@ int PUSH(vcomponent)(vcomponent*, vcomponent*); int DEEP_COPY(vcomponent)(vcomponent*, vcomponent*); +FMT_F(content_line); +FMT_F(vcomponent); #endif /* VCAL_H */ -- cgit v1.2.3