From 60c7d789ce21cb77bdf9dd2c8cb22e86232d903f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 11 Feb 2019 01:34:40 +0100 Subject: Rewrote content_line, and how parameters are stored. --- graphs.c | 44 +++++++++++--------------- linked_list.h | 2 ++ macro.h | 2 +- main.c | 10 ++++-- pair.h | 4 +-- pair.inc.h | 21 ++++++------ parse.c | 94 ++++++++++++++++++++++++++++++++++-------------------- scheme.scm.c | 4 +-- strbuf.c | 6 +++- strbuf.h | 2 ++ vcal.c | 100 ++++++++++++++++++---------------------------------------- vcal.h | 59 ++++++++++++++++++++++++++-------- vector.h | 9 ++++++ vector.inc.h | 2 +- 14 files changed, 197 insertions(+), 162 deletions(-) diff --git a/graphs.c b/graphs.c index 083bfea2..3550ddf5 100644 --- a/graphs.c +++ b/graphs.c @@ -92,52 +92,44 @@ int trie_to_dot ( TRIE(T)* trie, FILE* f ) { } int trie_to_dot_helper ( TRIE_NODE(T)* root, FILE* f ) { - if (root->value == NULL) { + if (L(root) == NULL) { fprintf(f, "\"%p\"[label = \"%c\" style=filled fillcolor=white];\n", (void*) root, root->c); } else { fprintf(f, "\"%p\"[label = \"%c [%i]\" style=filled fillcolor=green];\n", (void*) root, root->c, - SIZE(LLIST(strbuf))(&root->value->vals)); + SIZE(LLIST(content_set))(&L(root)->val) + ); } TRIE_NODE(T)* child = root->child; // ---------------------------------------- if (root->value != NULL) { - FOR(LLIST(strbuf), link, &root->value->vals) { + FOR(LLIST(content_set), link, &L(root)->val) { char buf[0x100]; - FMT(strbuf)(link->value, buf); + FMT(strbuf)(&L(link)->key, buf); fprintf(f, "\"%p\" [label=\"%s\" shape=rectangle color=darkgreen];\n", link->value, buf); - fprintf(f, "\"%p\" -> \"%p\";\n", root, link->value); - } + fprintf(f, "\"%p\" -> \"%p\";\n", root, L(link)); + /* Parameters */ + FOR(LLIST(param_set), param_link, &L(link)->val) { + strbuf* param_key = &L(param_link)->key; - /* Parameters */ - if (! EMPTY(LLIST(PAIR(strbuf,strbuf)))(&root->value->params)) { - fprintf(f, "subgraph \"cluster_param_%p\"{\n color=blue;\n", root); - FOR(LLIST(PAIR(strbuf,strbuf)), link, &root->value->params) { - fprintf(f, "\"%p\" [shape=rectangle, label=\"%s := %s\"];", link, - link->value->left.mem, - link->value->right.mem); - } - fputs("}", f); + fprintf(f, "\"%p\" [label=\"%s\" color=blue];\n", + param_key, param_key->mem); + fprintf(f, "\"%p\" -> \"%p\";", L(link), param_key); - /* - * TODO - * Params should actually belong to each value. Not to - * each content_line (since a content line is a collection - * of lines in the original file). This is a temporary - * hack. - */ - fprintf(f, "\"link_%p\" [label=params style=filled fillcolor=lightblue];\n", root); - fprintf(f, "\"%p\" -> \"link_%p\";\n", root, root); - FOR(LLIST(PAIR(strbuf,strbuf)), link, &root->value->params) { - fprintf(f, "\"link_%p\" -> \"%p\";\n", root, link); + FOR(LLIST(strbuf), str, &L(param_link)->val) { + fprintf(f, "\"%p\" [label=\"%s\" color=orange];", + str, L(str)->mem); + fprintf(f, "\"%p\" -> \"%p\";", param_key, str); + } } } } + // ---------------------------------------- while (child != NULL) { diff --git a/linked_list.h b/linked_list.h index 4e9ca042..8c09d5e0 100644 --- a/linked_list.h +++ b/linked_list.h @@ -17,6 +17,8 @@ typedef struct LINK(TYPE) { TYPE* value; } LINK(TYPE); +#define L(link) (link)->value + typedef struct { LINK(TYPE)* head; LINK(TYPE)* tail; diff --git a/macro.h b/macro.h index 55bff012..ae69ddd3 100644 --- a/macro.h +++ b/macro.h @@ -99,7 +99,7 @@ * Actuall implementation and type signature is mostly left to * individual implementations. */ -#define DEEP_COPY(T) TEMPL(deep_copy , T) +#define DEEP_COPY(T) TEMPL(copy , T) #define RESOLVE(T) TEMPL(resolve , T) #define APPEND(T) TEMPL(append , T) #define SIZE(T) TEMPL(size , T) diff --git a/main.c b/main.c index 042cc83d..e06d7d4f 100644 --- a/main.c +++ b/main.c @@ -37,7 +37,7 @@ int main (int argc, char* argv[argc]) { arg_shift(&args); if (args.argc == 0 || strcmp(args.argv[0], "-p") == 0) { - INFO_F("\nParsed calendar file containing [%u] events", + INFO_F("Parsed calendar file containing [%u] events", root.components.length); for (size_t i = 0; i < root.components.length; i++) { vcomponent* cal = GET(VECT(vcomponent))(&root.components, i); @@ -52,7 +52,7 @@ int main (int argc, char* argv[argc]) { printf("%3lu | %s | %s\n", i + 1, filename, - get_property(ev, "SUMMARY")->vals.cur->value->mem); + get_property(ev, "SUMMARY")->val.cur->value->key.mem); } } } else if (strcmp(args.argv[0], "-g") == 0) { @@ -79,5 +79,11 @@ int main (int argc, char* argv[argc]) { } } + /* + char buf[0x20000]; + FMT(vcomponent)(&root, buf); + puts(buf); + */ + FREE(vcomponent)(&root); } diff --git a/pair.h b/pair.h index e3822a9b..e96cf180 100644 --- a/pair.h +++ b/pair.h @@ -7,8 +7,8 @@ #if defined(T) && defined(V) typedef struct { - T left; - V right; + T key; + V val; } PAIR(T, V); INIT_F(PAIR(T, V)); diff --git a/pair.inc.h b/pair.inc.h index 53f746bd..5f746f1b 100644 --- a/pair.inc.h +++ b/pair.inc.h @@ -3,30 +3,31 @@ #else INIT_F(PAIR(T, V)) { - INIT(T, &this->left); - INIT(V, &this->right); + INIT(T, &this->key); + INIT(V, &this->val); return 0; } FREE_F(PAIR(T, V)) { - FREE(T)(&this->left); - FREE(V)(&this->right); + FREE(T)(&this->key); + FREE(V)(&this->val); + return 0; } FMT_F(PAIR(T, V)) { - char lbuf[100]; - char rbuf[100]; - FMT(T)(&this->left, lbuf); - FMT(V)(&this->right, rbuf); + char lbuf[0x100]; + char rbuf[0x1000]; + FMT(T)(&this->key, lbuf); + FMT(V)(&this->val, rbuf); return sprintf(buf, "<%s, %s>", lbuf, rbuf); } int DEEP_COPY(PAIR(T, V)) (PAIR(T, V)* dest, PAIR(T, V)* src) { - DEEP_COPY(T)(&dest->left, &src->left); - DEEP_COPY(V)(&dest->right, &src->right); + DEEP_COPY(T)(&dest->key, &src->key); + DEEP_COPY(V)(&dest->val, &src->val); return 0; } diff --git a/parse.c b/parse.c index a37c9874..f531671a 100644 --- a/parse.c +++ b/parse.c @@ -13,6 +13,13 @@ #include "linked_list.inc.h" #undef TYPE +#define T strbuf +#define V strbuf +#include "pair.h" +#include "pair.inc.h" +#undef T +#undef V + /* * name *(";" param) ":" value CRLF */ @@ -22,11 +29,7 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { SNEW(parse_ctx, ctx, filename); PUSH(LLIST(vcomponent))(&ctx.comp_stack, root); - // vallen -+ - // keylen -+ | - // | | - SNEW(content_line, cline, 100, 100); - SNEW(PAIR(strbuf,strbuf), kv); + SNEW(content_line, cline); char c; while ( (c = fgetc(f)) != EOF) { @@ -64,8 +67,10 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { exit (2); } - DEEP_COPY(strbuf)(cline.vals.cur->value, &ctx.str); - strbuf_cap(cline.vals.cur->value); + strbuf* target = CLINE_CUR_VAL(&cline); + + DEEP_COPY(strbuf)(target, &ctx.str); + strbuf_cap(target); strbuf_soft_reset(&ctx.str); ++ctx.line; @@ -77,13 +82,15 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { } - /* - * Border between param {key, value} - */ + /* Border between param {key, value} */ } else if (p_ctx == p_param_name && c == '=') { - DEEP_COPY(strbuf)(&kv.left, &ctx.str); - strbuf_cap(&kv.right); + LLIST(param_set)* params = CLINE_CUR_PARAMS(&cline); + + NEW(param_set, ps); + DEEP_COPY(strbuf)(&ps->key, &ctx.str); + strbuf_cap(&ps->key); strbuf_soft_reset(&ctx.str); + PUSH(LLIST(param_set))(params, ps); p_ctx = p_param_value; @@ -95,19 +102,28 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { * 4) ,, param, ,, value */ } else if ((p_ctx == p_key || p_ctx == p_param_value) && (c == ':' || c == ';')) { - strbuf* dest; - if (p_ctx == p_key) dest = &cline.key; - else if (p_ctx == p_param_value) dest = &kv.right; - - DEEP_COPY(strbuf)(dest, &ctx.str); - strbuf_cap(dest); - strbuf_soft_reset(&ctx.str); if (p_ctx == p_param_value) { /* push kv pair */ - NEW (PAIR(strbuf,strbuf), _kv); - DEEP_COPY(PAIR(strbuf,strbuf))(_kv, &kv); - PUSH(LLIST(PAIR(strbuf,strbuf)))(&cline.params, _kv); + + NEW(strbuf, s); + + DEEP_COPY(strbuf)(s, &ctx.str); + strbuf_cap(s); + strbuf_soft_reset(&ctx.str); + + LLIST(strbuf)* ls = & CLINE_CUR_PARAMS(&cline)->cur->value->val; + PUSH(LLIST(strbuf))(ls, s); + + } + + if (p_ctx == p_key) { + DEEP_COPY(strbuf)(&cline.key, &ctx.str); + strbuf_cap(&cline.key); + strbuf_soft_reset(&ctx.str); + + NEW(content_set, p); + PUSH(LLIST(content_set))(&cline.val, p); } if (c == ':') p_ctx = p_value; @@ -121,15 +137,23 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { if (! feof(f)) { ERR("Error parsing"); - } else if (cline.vals.cur->value->len != 0 && ctx.str.ptr != 0) { + } + /* Check to see if empty line */ + else if (ctx.str.ptr != 0) { /* * The standard (3.4, l. 2675) says that each icalobject must * end with CRLF. My files however does not, so we also parse * the end here. */ - DEEP_COPY(strbuf)(cline.vals.cur->value, &ctx.str); - strbuf_cap(cline.vals.cur->value); + strbuf* target = CLINE_CUR_VAL(&cline); + DEEP_COPY(strbuf)(target, &ctx.str); + strbuf_cap(target); + strbuf_soft_reset(&ctx.str); + + ++ctx.line; + ctx.column = 0; + handle_kv(&cline, &ctx); } @@ -142,8 +166,6 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { FREE(parse_ctx)(&ctx); - FREE(PAIR(strbuf,strbuf))(&kv); - return 0; } @@ -159,9 +181,12 @@ int handle_kv ( */ NEW(strbuf, s); - DEEP_COPY(strbuf)(s, cline->vals.cur->value); + strbuf* type = CLINE_CUR_VAL(cline); + DEEP_COPY(strbuf)(s, type); PUSH(LLIST(strbuf))(&ctx->key_stack, s); + RESET(LLIST(content_set))(&cline->val); + NEW(vcomponent, e, s->mem, ctx->filename); @@ -170,9 +195,12 @@ int handle_kv ( } else if (strbuf_c(&cline->key, "END")) { strbuf* s = POP(LLIST(strbuf))(&ctx->key_stack); - if (strbuf_cmp(s, cline->vals.cur->value) != 0) { + if (strbuf_cmp(s, CLINE_CUR_VAL(cline)) != 0) { ERR_F("Expected END:%s, got END:%s.\n%s line %i", - s->mem, cline->vals.cur->value->mem, PEEK(LLIST(vcomponent))(&ctx->comp_stack)->filename, ctx->line); + s->mem, + CLINE_CUR_VAL(cline)->mem, + PEEK(LLIST(vcomponent))(&ctx->comp_stack)->filename, + ctx->line); PUSH(LLIST(strbuf))(&ctx->key_stack, s); return -1; @@ -186,15 +214,13 @@ int handle_kv ( } } else { NEW(content_line, c); - content_line_copy(c, cline); + DEEP_COPY(content_line)(c, cline); PUSH(TRIE(content_line))( &PEEK(LLIST(vcomponent))(&ctx->comp_stack)->clines, c->key.mem, c); - if ( SIZE(LLIST(PAIR(strbuf,strbuf)))(&c->params) != 0 ) { - RESET(LLIST(PAIR(strbuf,strbuf)))(&cline->params); - } + RESET(LLIST(content_set))(&cline->val); } return 0; diff --git a/scheme.scm.c b/scheme.scm.c index d148a435..7d96f2d6 100644 --- a/scheme.scm.c +++ b/scheme.scm.c @@ -61,7 +61,7 @@ SCM_DEFINE (calendar_get_attr, "calendar-get-attr", 3, 0, 0, if (c == NULL) return SCM_BOOL_F; - return scm_from_llist(&c->vals); + return scm_from_llist(&c->val); } SCM scm_from_trie_node(TRIE_NODE(content_line)* node) { @@ -75,7 +75,7 @@ SCM scm_from_trie_node(TRIE_NODE(content_line)* node) { SCM lst; if (node->value != NULL) { lst = scm_cons(scm_from_char(node->c), - scm_from_llist(&node->value->vals)); + scm_from_llist(&node->value->val)); } else { lst = scm_list_1(scm_from_char(node->c)); } diff --git a/strbuf.c b/strbuf.c index 8cc624e5..7193b134 100644 --- a/strbuf.c +++ b/strbuf.c @@ -81,7 +81,7 @@ int strbuf_cmp(strbuf* a, strbuf* b) { b == NULL || b->alloc == 0) { ERR("a or b not alloced"); - return -1; + // return -1; } return strncmp(a->mem, b->mem, a->len); @@ -132,3 +132,7 @@ strbuf* RESOLVE(strbuf)(strbuf* dest, strbuf* new) { FMT_F(strbuf) { return sprintf(buf, "%s", this->mem); } + +int SIZE(strbuf)(strbuf* this) { + return this->len; +} diff --git a/strbuf.h b/strbuf.h index 35ff9543..fcbb7e74 100644 --- a/strbuf.h +++ b/strbuf.h @@ -100,4 +100,6 @@ strbuf* RESOLVE(strbuf)(strbuf*, strbuf*); FMT_F(strbuf); +int SIZE(strbuf)(strbuf*); + #endif /* STRBUF_H */ diff --git a/vcal.c b/vcal.c index a36e584d..7545ccf3 100644 --- a/vcal.c +++ b/vcal.c @@ -2,29 +2,38 @@ #include -#define TYPE content_line -// #include "hash.inc" -#include "trie.inc.h" +#define TYPE strbuf +#include "linked_list.inc.h" #undef TYPE -#define TYPE strbuf +#define TYPE param_set #include "linked_list.inc.h" #undef TYPE -#define TYPE vcomponent -#include "vector.inc.h" +#define TYPE content_set +#include "linked_list.inc.h" #undef TYPE #define T strbuf -#define V strbuf -#include "pair.inc.h" + #define V LLIST(strbuf) + #include "pair.inc.h" + #undef V + #define V LLIST(param_set) + #include "pair.inc.h" + #undef V + #define V LLIST(content_set) + #include "pair.inc.h" + #undef V #undef T -#undef V -#define TYPE PAIR(strbuf, strbuf) -#include "linked_list.inc.h" +#define TYPE content_line +// #include "hash.inc" +#include "trie.inc.h" #undef TYPE +#define TYPE vcomponent +#include "vector.inc.h" +#undef TYPE INIT_F(vcomponent) { (void) this; @@ -65,12 +74,8 @@ content_line* RESOLVE(content_line) return NULL; } - /* - * This destroys new. - */ - APPEND(LLIST(strbuf)) (&dest->vals, &new->vals); - - APPEND(LLIST(PAIR(strbuf,strbuf))) (&dest->params, &new->params); + /* This destroys new->val. */ + APPEND(LLIST(content_set)) (&dest->val, &new->val); FREE(strbuf)(&new->key); free(new); @@ -82,43 +87,6 @@ content_line* get_property (vcomponent* ev, char* key) { return GET(TRIE(content_line))(&ev->clines, key); } -INIT_F(content_line) { - INIT(strbuf, &this->key); - INIT( LLIST(strbuf), &this->vals ); - - INIT( LLIST(PAIR(strbuf,strbuf)), &this->params ); - - return 0; -} - -INIT_F(content_line, int keylen, int vallen) { - INIT(strbuf, &this->key, keylen); - INIT( LLIST(strbuf), &this->vals ); - NEW(strbuf, s, vallen); - PUSH(LLIST(strbuf))(&this->vals, s); - - INIT( LLIST(PAIR(strbuf,strbuf)), &this->params ); - - return 0; -} - -FREE_F(content_line) { - FREE(strbuf)(&this->key); - FREE(LLIST(strbuf))(&this->vals); - - FREE(LLIST(PAIR(strbuf,strbuf)))(&this->params); - - return 0; -} - -int content_line_copy (content_line* dest, content_line* src) { - DEEP_COPY(strbuf)(&dest->key, &src->key); - DEEP_COPY(LLIST(strbuf))(&dest->vals, &src->vals); - DEEP_COPY(LLIST(PAIR(strbuf,strbuf)))(&dest->params, &src->params); - - return 0; -} - FREE_F(vcomponent) { if (this->filename != NULL) free(this->filename); free(this->type); @@ -144,25 +112,19 @@ int DEEP_COPY(vcomponent)(vcomponent* a, vcomponent* b) { return -1; } -/* - * TODO this doesn't seem to work. - */ FMT_F(vcomponent) { int seek = 0; - FOR(int, i, 40) fmtf("-"); - seek += sprintf(buf, _YELLOW); - seek += sprintf(buf, "VComponet (Type := %s)\n", this->type); + FOR(int, i, 40) fmtf("_"); + + seek += sprintf(buf + seek, _YELLOW); + seek += sprintf(buf + seek, "\nVComponet (Type := %s)\n", this->type); + seek += sprintf(buf + seek, _RESET); seek += FMT(TRIE(content_line))(&this->clines, buf + seek); - seek += sprintf(buf, _RESET); + seek += sprintf(buf + seek, "\nComponents:\n"); + FOR(VECT(vcomponent), i, &this->components) { + seek += FMT(vcomponent)(GET(VECT(vcomponent))(&this->components, i), buf + seek); + } return seek; } - -FMT_F(content_line) { - char str_a[100], str_b[100], str_c[100];; - FMT(strbuf)(&this->key, str_a); - FMT(LLIST(PAIR(strbuf, 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 0451a037..21358cb6 100644 --- a/vcal.h +++ b/vcal.h @@ -11,28 +11,60 @@ #undef TYPE #define T strbuf -#define V strbuf -#include "pair.h" + #define V LLIST(strbuf) + #include "pair.h" + /* left := param_name | right := param_values */ + #define param_set PAIR(strbuf, LLIST(strbuf)) + #undef V #undef T -#undef V -#define TYPE PAIR(strbuf, strbuf) +#define TYPE param_set #include "linked_list.h" #undef TYPE -typedef struct { - strbuf key; +#define T strbuf + #define V LLIST(param_set) + #include "pair.h" + /* left := content | right := params */ + #define content_set PAIR(strbuf, LLIST(param_set)) + #undef V +#undef T + +#define TYPE content_set +#include "linked_list.h" +#undef TYPE + +#define T strbuf + #define V LLIST(content_set) + #include "pair.h" + /* left := content | right := params */ + #define content_line PAIR(strbuf, LLIST(content_set)) + #undef V +#undef T + +/* + * Helper macros for accessing fields in + * content_line, content_set, and param_set + * + * TODO find a better way to do this. + */ - LLIST(strbuf) vals; +/* ptr -> ptr */ +#define CLINE_KEY(c) (&(c)->key) +#define CLINE_CUR_CSET(c) (&((c)->val.cur->value)) - LLIST(PAIR(strbuf, strbuf)) params; -} content_line; +/* content_set */ +#define CLINE_CUR(c) ((c)->val.cur->value) +/* strbuf */ +#define CLINE_CUR_VAL(c) (& CLINE_CUR(c)->key) -INIT_F(content_line); -INIT_F(content_line, int keylen, int vallen); -FREE_F(content_line); +/* LLIST(param_set) */ +#define CLINE_CUR_PARAMS(c) (& CLINE_CUR(c)->val) -int content_line_copy (content_line* dest, content_line* src); +/* strbuf */ +#define CLINE_CUR_PARAM_KEY(c) (CLINE_CUR_PARAMS(c)->cur->value->key) +/* strbuf */ +#define CLINE_CUR_PARAM_VAL(c) (CLINE_CUR_PARAMS(c)->cur->value->val.cur->value) /* * Resolves a collision in some form of structure (probably a hash-map @@ -80,7 +112,6 @@ int PUSH(vcomponent)(vcomponent*, vcomponent*); int DEEP_COPY(vcomponent)(vcomponent*, vcomponent*); -FMT_F(content_line); FMT_F(vcomponent); #endif /* VCAL_H */ diff --git a/vector.h b/vector.h index f6acd66a..7dea4d58 100644 --- a/vector.h +++ b/vector.h @@ -24,4 +24,13 @@ TYPE* GET(VECT(TYPE))(VECT(TYPE)*, unsigned int idx); int EMPTY(VECT(TYPE))(VECT(TYPE)*); unsigned int SIZE(VECT(TYPE))(VECT(TYPE)*); +#define __BEG_VECT(i, set) unsigned int i = 0 +#define BEG_VECT(T) __BEG_VECT + +#define __END_VECT(i, set) (set) > i +#define END_VECT(T) SIZE(VECT(T)) __END_VECT + +#define __NXT_VECT(l, set) i++ +#define NXT_VECT(T) __NXT_VECT + #endif /* TYPE */ diff --git a/vector.inc.h b/vector.inc.h index 68f302cd..f6d1c796 100644 --- a/vector.inc.h +++ b/vector.inc.h @@ -7,7 +7,7 @@ INIT_F(VECT(TYPE)) { this->length = 0; - this->alloc = 0x10; + this->alloc = 1; this->items = calloc(sizeof(*this->items), this->alloc); return 0; } -- cgit v1.2.3