From 329e3092542067f3a99fda328a8118e8d024e054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 21 Feb 2019 19:57:31 +0100 Subject: Build, but doesn't link. --- err.h | 2 + graphs.c | 144 ----------------------------------------- graphs.h | 15 ----- linked_list.h | 104 ++++++++++++++++++------------ linked_list.inc.h | 187 +++++++++++++++++++++++++++++------------------------ macro.h | 2 + main.c | 6 +- pair.h | 23 ++++--- pair.inc.h | 24 ++++--- parse.c | 149 +++++++++++++++++++++++++------------------ parse.h | 4 +- strbuf.c | 65 +++++++++++-------- strbuf.h | 20 ++++-- trie.c.inc | 188 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ trie.h | 50 +++++++-------- trie.inc.h | 166 ----------------------------------------------- vcal.c | 62 ++++++++++-------- vcal.h | 38 ++++++++--- 18 files changed, 625 insertions(+), 624 deletions(-) delete mode 100644 graphs.c delete mode 100644 graphs.h create mode 100644 trie.c.inc delete mode 100644 trie.inc.h diff --git a/err.h b/err.h index d9d19ec7..b7448d47 100644 --- a/err.h +++ b/err.h @@ -33,10 +33,12 @@ printf("\n"); \ } while (0) +#if 0 #define PRINT(T, v) do { \ char buf[0x1000]; \ FMT(T)(v, buf); \ INFO_F("%s", buf); \ } while (0) +#endif #endif /* ERR_H */ diff --git a/graphs.c b/graphs.c deleted file mode 100644 index 2ed7009e..00000000 --- a/graphs.c +++ /dev/null @@ -1,144 +0,0 @@ -#include "graphs.h" - -#include -#include -#include -#include "err.h" - -// #define TYPE strbuf -// #include "linked_list.h" -// #include "linked_list.inc.h" -// #undef TYPE - -int create_graph_trie (vcomponent* ev, char* filename) { - FILE* f = fopen(filename, "w"); - - fputs("digraph {\n rankdir=LR;", f); - trie_to_dot(&ev->clines, f); - fputs("}", f); - - fclose(f); - - INFO_F("Wrote '%s' to '%s'", ev->filename, filename); - - return 0; -} - -int helper_vcomponent (vcomponent* root, FILE* f) { - fprintf(f, "subgraph \"cluster_root\" { label=File; \"%p\" [label=%s] }\n", root, root->type); - - TRIE(content_line)* trie = &root->clines; - TRIE_NODE(content_line)* n = trie->root->child; - - if (! EMPTY(TRIE(content_line))(trie)) { - fprintf(f, "subgraph \"cluster_%p\" {\n", root); - fprintf(f, "\"%p\" [label=trie fontcolor=gray, color=gray];", trie); - fprintf(f, "\"%p\" -> \"%p\" [color=red]\n", root, trie); - while (n != NULL) { - fprintf(f, "\"%p\" -> \"%p\" [color=gray]\n", - (void*) trie, - (void*) n); - fprintf(f, "subgraph \"cluster_%c_%p\" {\ncolor=red; \n", - n->c, root); - trie_to_dot_helper( n, f ); - - - fputs("}", f); - n = n->next; - } - fputs("}", f); - } - - vcomponent* child; - for (size_t i = 0; i < root->components.length; i++) { - child = GET(VECT(vcomponent))(&root->components, i); - if (child == NULL) continue; - fprintf(f, "\"%p\" -> \"%p\"\n", root, child); - helper_vcomponent(child, f); - } - return 0; -} - -int create_graph_vcomponent (vcomponent* root, char* outfile) { - FILE* f = fopen(outfile, "w"); - if (f == NULL) { - ERR_F("Error opening file %s, errno = %i", outfile, errno); - return 1; - } - vcomponent* c = root; - fputs("digraph {", f); - helper_vcomponent(c, f); - fputs("}", f); - fclose(f); - return 0; -} - -#define T content_line - -int trie_to_dot ( TRIE(T)* trie, FILE* f ) { - TRIE_NODE(T)* n = trie->root->child; - fprintf(f, "\"%p\" [label=root fontcolor=gray, color=gray];", trie); - while (n != NULL) { - fprintf(f, "\"%p\" -> \"%p\" [color=gray]\n", - (void*) trie, - (void*) n); - fprintf(f, "subgraph \"cluster_%c\" {\n", - n->c); - trie_to_dot_helper( n, f ); - fputs("}", f); - n = n->next; - } - return 0; -} - -int trie_to_dot_helper ( TRIE_NODE(T)* root, FILE* f ) { - 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(content_set))(&L(root)->val) - ); - } - TRIE_NODE(T)* child = root->child; - - // ---------------------------------------- -#if 1 /* Toggle values */ - if (L(root) != NULL) { - - FOR(LLIST, content_set, v, &L(root)->val) { - char buf[0x100]; - FMT(strbuf)(&v->key, buf); - fprintf(f, "\"%p\" [label=\"%s\" shape=rectangle color=darkgreen];\n", - v, buf); - fprintf(f, "\"%p\" -> \"%p\";\n", root, v); - - /* Parameters */ - FOR(LLIST, param_set, p, &v->val) { - strbuf* param_key = &p->key; - - fprintf(f, "\"%p\" [label=\"%s\" color=blue];\n", - param_key, param_key->mem); - fprintf(f, "\"%p\" -> \"%p\";", p, param_key); - - FOR(LLIST, strbuf, str, &p->val) { - fprintf(f, "\"%p\" [label=\"%s\" color=orange];", - str, str->mem); - fprintf(f, "\"%p\" -> \"%p\";", param_key, str); - } - } - } - } -#endif - - // ---------------------------------------- - - while (child != NULL) { - fprintf(f, "\"%p\" -> \"%p\";\n", - (void*) root, (void*) child); - trie_to_dot_helper(child, f); - child = child->next; - } - return 0; -} diff --git a/graphs.h b/graphs.h deleted file mode 100644 index fe521003..00000000 --- a/graphs.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef GRAPHS_H -#define GRAPHS_H - -#include "vcal.h" - -int create_graph_trie (vcomponent* ev, char* filename); - -int create_graph_vcomponent (vcomponent* root, char* outfile); - -int helper_vcomponent (vcomponent* root, FILE* f); - -int trie_to_dot ( TRIE(content_line)*, FILE* ); -int trie_to_dot_helper ( TRIE_NODE(content_line)*, FILE* ); - -#endif /* GRAPHS_H */ diff --git a/linked_list.h b/linked_list.h index a17bd797..d87aa8b0 100644 --- a/linked_list.h +++ b/linked_list.h @@ -3,69 +3,87 @@ #include "macro.h" -#define LLIST(T) TEMPL(llist, T) -#define LINK(T) TEMPL(llist_link, T) +// #define LLIST(T) TEMPL(llist, T) +// #define LINK(T) TEMPL(llist_link, T) +// +// #define UNLINK(T) TEMPL(unlink, T) -#define UNLINK(T) TEMPL(unlink, T) +// #endif /* LINKED_LIST_H */ +// #ifdef TYPE -#endif /* LINKED_LIST_H */ -#ifdef TYPE +#define FIRST(lst) (lst)->head->after +#define FIRST_V(lst) (lst)->head->after->value +#define LAST(lst) (lst)->tail->before +#define LAST_V(lst) (lst)->tail->before->value + +template struct llink { + struct llink* before; + struct llink* after; + T* value; + + llink (T* val); + llink () : llink (NULL) { }; + ~llink (); -typedef struct LINK(TYPE) { - struct LINK(TYPE)* before; - struct LINK(TYPE)* after; - TYPE* value; -} LINK(TYPE); + int unlink (); +}; #define L(link) (link)->value -typedef struct { - LINK(TYPE)* head; - LINK(TYPE)* tail; - LINK(TYPE)* cur; +template struct llist { + llink* head; + llink* tail; + llink* cur; int length; -} LLIST(TYPE); -#define FIRST(lst) (lst)->head->after -#define FIRST_V(lst) (lst)->head->after->value -#define LAST(lst) (lst)->tail->before -#define LAST_V(lst) (lst)->tail->before->value + llist (); + llist (llist* other); + ~llist (); + + int push (T*); + T* peek (); + T* pop (); -INIT_F ( LLIST(TYPE) ); + int size () { return this->length; } + int empty () { return FIRST(this) == this->tail; } + int append (llist* other); + + /* + * Resets a linked list by removing all it's objects. + * FREE's all elements stored in the list. + */ + int reset (); + +}; + + +// INIT_F ( LLIST(TYPE) ); /* * NOTE freeing a linked list alsa FFREE's all its contents. * TODO some form of shared pointer to ensure nothing is free'd twice * would be a good idea. */ -FREE_F ( LLIST(TYPE) ); - -INIT_F ( LINK(TYPE) ); -INIT_F ( LINK(TYPE), TYPE* val ); -FREE_F ( LINK(TYPE) ); +// FREE_F ( LLIST(TYPE) ); +// +// INIT_F ( LINK(TYPE) ); +// INIT_F ( LINK(TYPE), TYPE* val ); +// FREE_F ( LINK(TYPE) ); -int UNLINK(LINK(TYPE)) ( LINK(TYPE)* ); +// int UNLINK(LINK(TYPE)) ( LINK(TYPE)* ); -int PUSH(LLIST(TYPE)) ( LLIST(TYPE)*, TYPE* ); -TYPE* PEEK(LLIST(TYPE)) ( LLIST(TYPE)* ); -TYPE* POP(LLIST(TYPE)) ( LLIST(TYPE)* ); +// int PUSH(LLIST(TYPE)) ( LLIST(TYPE)*, TYPE* ); +// TYPE* PEEK(LLIST(TYPE)) ( LLIST(TYPE)* ); +// TYPE* POP(LLIST(TYPE)) ( LLIST(TYPE)* ); -int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ); +// // int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ); -int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new_ ); -int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist ); -int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist ); +// LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new_); +template +llist* resolve (llist* dest, llist* new__); -/* - * Resets a linked list by removing all it's objects. - * FREE's all elements stored in the list. - */ -int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ); - -LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new_); - -FMT_F(LLIST(TYPE)); +// FMT_F(LLIST(TYPE)); /* Iterator */ @@ -85,4 +103,6 @@ FMT_F(LLIST(TYPE)); // #define __NXT_LLIST(T, l, set) l = L(__INTER(l) = __INTER(l)->after) #define NXT_LLIST(T) __NXT_LLIST +#include "linked_list.inc.h" + #endif /* TYPE */ diff --git a/linked_list.inc.h b/linked_list.inc.h index 8ae720ba..e20c6339 100644 --- a/linked_list.inc.h +++ b/linked_list.inc.h @@ -1,107 +1,129 @@ -#ifndef TYPE -#error "Set TYPE before including self file" -#else - -INIT_F ( LLIST(TYPE) ) { - self->length = 0; - NEW(LINK(TYPE), head); - NEW(LINK(TYPE), tail); - self->head = head; - self->tail = tail; +// #ifndef TYPE +// #error "Set TYPE before including this file" +// #else + +// INIT_F ( LLIST(TYPE) ) { +template +llist::llist () { + this->length = 0; + head = new llink; + tail = new llink; + this->head = head; + this->tail = tail; head->after = tail; tail->before = head; - self->cur = head; - return 0; + this->cur = head; + // return 0; } -FREE_F (LINK(TYPE)) { - UNLINK(LINK(TYPE))(self); +// FREE_F (LINK(TYPE)) { +template +llink::~llink () { + this->unlink(); - if (self->value != NULL) FFREE(TYPE, self->value); - return 0; + // if (this->value != NULL) delete this->value; } -FREE_F( LLIST(TYPE) ) { - LINK(TYPE) *n, *next; - n = self->head; +// FREE_F( LLIST(TYPE) ) { +template +llist::~llist () { + llink *n, *next; + n = this->head; while ( n != NULL ) { next = n->after; - FFREE(LINK(TYPE), n); + delete n; + // FFREE(LINK(TYPE), n); n = next; } - self->length = -1; - - return 0; + this->length = -1; } +/* INIT_F ( LINK(TYPE) ) { - self->before = NULL; - self->after = NULL; - self->value = NULL; + this->before = NULL; + this->after = NULL; + this->value = NULL; return 0; } +*/ -INIT_F ( LINK(TYPE), TYPE* val ) { - self->before = NULL; - self->after = NULL; - self->value = val; - return 0; +// INIT_F ( LINK(TYPE), TYPE* val ) { +template +llink::llink (T* val) { + this->before = NULL; + this->after = NULL; + this->value = val; } -int UNLINK(LINK(TYPE)) ( LINK(TYPE)* self ) { - if (self->before != NULL) self->before->after = self->after; - if (self->after != NULL) self->after->before = self->before; +// int UNLINK(LINK(TYPE)) ( LINK(TYPE)* this ) { +template +int llink::unlink () { + if (this->before != NULL) this->before->after = this->after; + if (this->after != NULL) this->after->before = this->before; return 0; } -int PUSH(LLIST(TYPE)) ( LLIST(TYPE)* lst, TYPE* val) { - NEW(LINK(TYPE), link, val); +// int PUSH(LLIST(TYPE)) ( LLIST(TYPE)* lst, TYPE* val) { +template +int llist::push (T* val) { + // NEW(LINK(TYPE), link, val); + auto link = new llink(val); - link->after = FIRST(lst); - FIRST(lst) = link; + link->after = FIRST(this); + FIRST(this) = link; link->after->before = link; - link->before = lst->head; + link->before = this->head; - ++lst->length; + ++this->length; // TODO do I want to change that? - lst->cur = link; + this->cur = link; return 0; } -TYPE* PEEK(LLIST(TYPE)) ( LLIST(TYPE)* lst ) { - if (EMPTY(LLIST(TYPE))(lst)) return NULL; +// TYPE* PEEK(LLIST(TYPE)) ( LLIST(TYPE)* lst ) { +template +T* llist::peek () { + if (this->empty()) return NULL; + // if (EMPTY(LLIST(TYPE))(lst)) return NULL; - return FIRST(lst)->value; + return FIRST(this)->value; } -TYPE* POP(LLIST(TYPE)) ( LLIST(TYPE)* lst) { - if (EMPTY(LLIST(TYPE))(lst)) return NULL; +// TYPE* POP(LLIST(TYPE)) ( LLIST(TYPE)* lst) { +template +T* llist::pop () { + // if (EMPTY(LLIST(TYPE))(lst)) return NULL; + if (this->empty()) return NULL; - LINK(TYPE)* frst = FIRST(lst); - UNLINK(LINK(TYPE))(frst); + // LINK(TYPE)* frst = FIRST(lst); + //UNLINK(LINK(TYPE))(frst); + auto frst = FIRST(this); + frst->unlink(); - TYPE* retval = frst->value; - --lst->length; - free (frst); + T* retval = frst->value; + --this->length; + delete frst; return retval; } -int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ) { - LINK(TYPE)* n = FIRST(src); +// int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ) { +template +llist::llist (llist* other) { + llink* n = FIRST(other); while (n->after != NULL) { - NEW(TYPE, cpy); - DEEP_COPY(TYPE)(cpy, n->value); - PUSH(LLIST(TYPE)) ( dest, cpy ); + // NEW(TYPE, cpy); + T* cpy = new T(n->value); + // DEEP_COPY(TYPE)(cpy, n->value); + //PUSH(LLIST(TYPE)) ( dest, cpy ); + this->push (cpy); n = n->after; } - - return 0; } /* @@ -110,60 +132,60 @@ int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ) { * destroys new__ in the process, but keeps the elements. * make sure to free(new__) after. */ -int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new__ ) { +template +int llist::append ( llist* other ) { /* Link end of dest onto start of new__. */ - LAST(dest)->after = FIRST(new__); - FIRST(new__)->before = LAST(dest); + LAST(this)->after = FIRST(other); + FIRST(other)->before = LAST(this); /* Free the two now not needed end links. */ - free(new__->head); - free(dest->tail); + // free(new__->head); + // free(dest->tail); + delete this->tail; + delete other->head; /* Update dest with new__ tail ptr. */ - dest->tail = new__->tail; + this->tail = other->tail; - dest->length += new__->length; + this->length += other->length; return 0; } -int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist ) { - return llist->length; -} - -int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist ) { - return FIRST(llist) == llist->tail; -} - -LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new__) { +template +llist* resolve (llist* dest, llist* new__) { if (dest == NULL) return new__; - APPEND(LLIST(TYPE))(dest, new__); + dest.append(new__); return dest; } -int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ) { +// int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ) { +template +int llist::reset () { - LINK(TYPE) *link = FIRST(llist), *next; + llink *link = FIRST(this), *next; /* * Manual looping rather than itterators since we destroyed the * loop variable. */ - while (link != llist->tail) { + while (link != this->tail) { next = link->after; - FFREE(LINK(TYPE), link); + // FFREE(LINK(TYPE), link); + delete link; link = next; } - llist->cur = llist->head; + this->cur = this->head; return 0; } +#if 0 FMT_F(LLIST(TYPE)) { int seek = 0; fmtf("("); - FOR(LLIST, TYPE, v, self) { + FOR(LLIST, TYPE, v, this) { seek += FMT(TYPE)(v, buf + seek); fmtf(" "); } @@ -171,5 +193,6 @@ FMT_F(LLIST(TYPE)) { return seek; } +#endif -#endif /* TYPE */ +// #endif /* TYPE */ diff --git a/macro.h b/macro.h index 6421b2fd..ea687bbf 100644 --- a/macro.h +++ b/macro.h @@ -121,11 +121,13 @@ * Should return the number of bytes written (like sprintf). */ +#if 0 #define FMT_T(T) TEMPL(format , T) #define FMT_F(T) int FMT_T(T)(T* self, 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 #endif /* MACRO_H */ diff --git a/main.c b/main.c index 830eb538..48235bd0 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ #include "calendar.h" #include "macro.h" #include "vcal.h" -#include "graphs.h" +// #include "graphs.h" #include "err.h" typedef struct { @@ -58,7 +58,7 @@ int main (int argc, char** argv) { printf("%3lu : %3lu | %s | %s\n", i + 1, j + 1, filename, - get_property(ev, "SUMMARY")->val.cur->value->key.mem); + get_property(ev, "SUMMARY")->val->cur->value->key->mem); } } } else if (strcmp(args.argv[0], "-g") == 0) { @@ -81,7 +81,7 @@ int main (int argc, char** argv) { // create_graph(FCHILD(FCHILD(&root)), args.argv[0]); INFO("Creating graph for single file"); INFO_F("output = %s\n", args.argv[0]); - create_graph_vcomponent(&root, args.argv[0]); + // create_graph_vcomponent(&root, args.argv[0]); } } diff --git a/pair.h b/pair.h index e96cf180..1862a72d 100644 --- a/pair.h +++ b/pair.h @@ -1,19 +1,26 @@ #ifndef PAIR_H #define PAIR_H -#define PAIR(T, V) TEMPL2(pair, T, V) +// #define PAIR(T, V) TEMPL2(pair, T, V) -#endif /* PAIR_H */ -#if defined(T) && defined(V) +// #endif /* PAIR_H */ +// #if defined(T) && defined(V) -typedef struct { - T key; - V val; -} PAIR(T, V); +template struct pair { + T* key; + V* val; + pair () { } + pair (pair& other); +}; + +#if 0 INIT_F(PAIR(T, V)); FREE_F(PAIR(T, V)); FMT_F(PAIR(T, V)); -int DEEP_COPY(PAIR(T, V)) (PAIR(T, V)* dest, PAIR(T, V)* src); +#endif +// int DEEP_COPY(PAIR(T, V)) (PAIR(T, V)* dest, PAIR(T, V)* src); + +#include "pair.inc.h" #endif diff --git a/pair.inc.h b/pair.inc.h index c42b2dfd..bfc90756 100644 --- a/pair.inc.h +++ b/pair.inc.h @@ -1,7 +1,8 @@ -#if ! (defined(T) && defined(V)) -#error "Both T and V must be defiend here" -#else +// #if ! (defined(T) && defined(V)) +// #error "Both T and V must be defiend here" +// #else +#if 0 INIT_F(PAIR(T, V)) { INIT(T, &self->key); INIT(V, &self->val); @@ -24,11 +25,16 @@ FMT_F(PAIR(T, V)) { 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->key, &src->key); - DEEP_COPY(V)(&dest->val, &src->val); - return 0; +#endif + +// int DEEP_COPY(PAIR(T, V)) (PAIR(T, V)* dest, PAIR(T, V)* src) { +template +pair::pair (pair& other) { + this->key = new T(*other.key); + this->val = new V(*other.val); + // DEEP_COPY(T)(&dest->key, &src->key); + // DEEP_COPY(V)(&dest->val, &src->val); + // return 0; } -#endif /* T & V */ +// #endif /* T & V */ diff --git a/parse.c b/parse.c index 8f7a9102..31d80797 100644 --- a/parse.c +++ b/parse.c @@ -9,16 +9,16 @@ #include "err.h" -#define TYPE vcomponent -#include "linked_list.inc.h" -#undef TYPE +// #define TYPE vcomponent +// #include "linked_list.inc.h" +// #undef TYPE -#define T strbuf -#define V strbuf +// #define T strbuf +// #define V strbuf #include "pair.h" -#include "pair.inc.h" -#undef T -#undef V +// #include "pair.inc.h" +// #undef T +// #undef V /* * name *(";" param) ":" value CRLF @@ -27,9 +27,11 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { part_context p_ctx = p_key; SNEW(parse_ctx, ctx, filename); - PUSH(LLIST(vcomponent))(&ctx.comp_stack, root); + // PUSH(LLIST(vcomponent))(&ctx.comp_stack, root); + ctx.comp_stack.push(root); - SNEW(content_line, cline); + content_line cline; + // SNEW(content_line, cline); char c; while ( (c = fgetc(f)) != EOF) { @@ -94,13 +96,16 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { /* Border between param {key, value} */ } else if (p_ctx == p_param_name && c == '=') { - LLIST(param_set)* params = CLINE_CUR_PARAMS(&cline); + auto params = CLINE_CUR_PARAMS(&cline); - NEW(param_set, ps); - DEEP_COPY(strbuf)(&ps->key, &ctx.str); - strbuf_cap(&ps->key); + // NEW(param_set, ps); + auto ps = new param_set; + // DEEP_COPY(strbuf)(&ps->key, &ctx.str); + *ps->key = ctx.str; + strbuf_cap(ps->key); strbuf_soft_reset(&ctx.str); - PUSH(LLIST(param_set))(params, ps); + // PUSH(LLIST(param_set))(params, ps); + params->push(ps); p_ctx = p_param_value; @@ -116,24 +121,29 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { if (p_ctx == p_param_value) { /* push kv pair */ - NEW(strbuf, s); + // NEW(strbuf, s); + auto s = new strbuf; + *s = ctx.str; - DEEP_COPY(strbuf)(s, &ctx.str); + // 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); + CLINE_CUR_PARAMS(&cline)->cur->value->val->push(s); + // PUSH(LLIST(strbuf))(ls, s); } if (p_ctx == p_key) { - DEEP_COPY(strbuf)(&cline.key, &ctx.str); - strbuf_cap(&cline.key); + // DEEP_COPY(strbuf)(&cline.key, &ctx.str); + *cline.key = ctx.str; + strbuf_cap(cline.key); strbuf_soft_reset(&ctx.str); - NEW(content_set, p); - PUSH(LLIST(content_set))(&cline.val, p); + // NEW(content_set, p); + auto p = new content_set; + // PUSH(LLIST(content_set))(&cline.val, p); + cline.val->push(p); } if (c == ':') p_ctx = p_value; @@ -170,13 +180,16 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { } - FREE(content_line)(&cline); + // FREE(content_line)(&cline); - assert(POP(LLIST(vcomponent))(&ctx.comp_stack) == root); - assert(EMPTY(LLIST(strbuf))(&ctx.key_stack)); - assert(EMPTY(LLIST(vcomponent))(&ctx.comp_stack)); + // assert(POP(LLIST(vcomponent))(&ctx.comp_stack) == root); + assert(ctx.comp_stack.pop() == root); + // assert(EMPTY(LLIST(strbuf))(&ctx.key_stack)); + assert(ctx.key_stack.empty()); + // assert(EMPTY(LLIST(vcomponent))(&ctx.comp_stack)); + assert(ctx.comp_stack.empty()); - FREE(parse_ctx)(&ctx); + // FREE(parse_ctx)(&ctx); return 0; } @@ -186,52 +199,66 @@ int handle_kv ( parse_ctx* ctx ) { - if (strbuf_c(&cline->key, "BEGIN")) { + if (strbuf_c(cline->key, "BEGIN")) { /* should be one of: * VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE, * and possibly some others I forget. */ - NEW(strbuf, s); - strbuf* type = CLINE_CUR_VAL(cline); - DEEP_COPY(strbuf)(s, type); - PUSH(LLIST(strbuf))(&ctx->key_stack, s); + // NEW(strbuf, s); + auto s = new strbuf; + // strbuf* type = CLINE_CUR_VAL(cline); + // DEEP_COPY(strbuf)(s, type); + *s = *CLINE_CUR_VAL(cline); + // PUSH(LLIST(strbuf))(&ctx->key_stack, s); + ctx->key_stack.push(s); - RESET(LLIST(content_set))(&cline->val); + // RESET(LLIST(content_set))(&cline->val); + cline->val->reset(); NEW(vcomponent, e, s->mem, ctx->filename); - e->parent = PEEK(LLIST(vcomponent))(&ctx->comp_stack); - PUSH(LLIST(vcomponent))(&ctx->comp_stack, e); - - } else if (strbuf_c(&cline->key, "END")) { - strbuf* s = POP(LLIST(strbuf))(&ctx->key_stack); + // e->parent = PEEK(LLIST(vcomponent))(&ctx->comp_stack); + e->parent = ctx->comp_stack.peek(); + //PUSH(LLIST(vcomponent))(&ctx->comp_stack, e); + ctx->comp_stack.push(e); + + } else if (strbuf_c(cline->key, "END")) { + //strbuf* s = POP(LLIST(strbuf))(&ctx->key_stack); + strbuf* s = ctx->key_stack.pop(); if (strbuf_cmp(s, CLINE_CUR_VAL(cline)) != 0) { - ERR_P(ctx, "Expected END:%s, got END:%s.\n%s line", - s->mem, - CLINE_CUR_VAL(cline)->mem, - PEEK(LLIST(vcomponent))(&ctx->comp_stack)->filename); - PUSH(LLIST(strbuf))(&ctx->key_stack, s); + // ERR_P(ctx, "Expected END:%s, got END:%s.\n%s line", + // s->mem, + // CLINE_CUR_VAL(cline)->mem, + // PEEK(LLIST(vcomponent))(&ctx->comp_stack)->filename); + // PUSH(LLIST(strbuf))(&ctx->key_stack, s); + ctx->key_stack.push(s); return -1; } else { - FFREE(strbuf, s); + delete s; + // FFREE(strbuf, s); /* Received propper end, push cur into parent */ - vcomponent* cur = POP(LLIST(vcomponent))(&ctx->comp_stack); + // vcomponent* cur = POP(LLIST(vcomponent))(&ctx->comp_stack); + vcomponent* cur = ctx->comp_stack.pop(); // TODO should self instead be done at creation time? - PUSH(vcomponent)(PEEK(LLIST(vcomponent))(&ctx->comp_stack), cur); + PUSH(vcomponent)(ctx->comp_stack.peek(), cur); } } else { - NEW(content_line, c); - DEEP_COPY(content_line)(c, cline); - - PUSH(TRIE(content_line))( - &PEEK(LLIST(vcomponent))(&ctx->comp_stack)->clines, - c->key.mem, c); - - RESET(LLIST(content_set))(&cline->val); + //NEW(content_line, c); + content_line* c = new content_line(*cline); + // DEEP_COPY(content_line)(c, cline); + + // PUSH(TRIE(content_line))( + // &PEEK(LLIST(vcomponent))(&ctx->comp_stack)->clines, + // c->key.mem, c); + // (PEEK(LLIST(vcomponent))(&ctx->comp_stack)->clines).push(c->key.mem, c); + ctx->comp_stack.peek()->clines.push(c->key->mem, c); + + //RESET(LLIST(content_set))(&cline->val); + cline->val->reset(); } return 0; @@ -272,8 +299,8 @@ int fold(FILE* f, parse_ctx* ctx, char c) { INIT_F(parse_ctx, char* filename) { - INIT(LLIST(strbuf), &self->key_stack); - INIT(LLIST(vcomponent), &self->comp_stack); + // INIT(LLIST(strbuf), &self->key_stack); + // INIT(LLIST(vcomponent), &self->comp_stack); self->filename = (char*) calloc(sizeof(*filename), strlen(filename) + 1); strcpy(self->filename, filename); @@ -283,20 +310,20 @@ INIT_F(parse_ctx, char* filename) { self->pline = 1; self->pcolumn = 1; - INIT(strbuf, &self->str); + // INIT(strbuf, &self->str); return 0; } FREE_F(parse_ctx) { - FREE(LLIST(strbuf))(&self->key_stack); - FREE(LLIST(vcomponent))(&self->comp_stack); + // FREE(LLIST(strbuf))(&self->key_stack); + // FREE(LLIST(vcomponent))(&self->comp_stack); free(self->filename); self->line = 0; self->column = 0; - FREE(strbuf)(&self->str); + // FREE(strbuf)(&self->str); return 0; } diff --git a/parse.h b/parse.h index b69bcfa5..f166d502 100644 --- a/parse.h +++ b/parse.h @@ -28,8 +28,8 @@ typedef enum { */ typedef struct { char* filename; - LLIST(strbuf) key_stack; - LLIST(vcomponent) comp_stack; + llist key_stack; + llist comp_stack; /* Number for unfolded lines */ int line; diff --git a/strbuf.c b/strbuf.c index 54960a54..c2b09a10 100644 --- a/strbuf.c +++ b/strbuf.c @@ -5,20 +5,20 @@ #include "err.h" -INIT_F(strbuf) { - INIT(strbuf, self, 1); - return 0; -} +// INIT_F(strbuf) { +// INIT(strbuf, self, 1); +// return 0; +// } /* * Giving len < 1 is an error. */ -INIT_F(strbuf, size_t len) { - self->mem = (char*) calloc(sizeof(*self->mem), len); - self->alloc = len; - self->ptr = 0; - self->len = 0; - return 0; +//INIT_F(strbuf, size_t len) { +strbuf::strbuf (size_t len) { + this->mem = (char*) calloc(sizeof(*this->mem), len); + this->alloc = len; + this->ptr = 0; + this->len = 0; } int strbuf_realloc(strbuf* str, size_t len) { @@ -27,15 +27,15 @@ int strbuf_realloc(strbuf* str, size_t len) { return 0; } -FREE_F(strbuf) { +// FREE_F(strbuf) { +strbuf::~strbuf () { /* has already been freed */ - if (self->mem == NULL) return 1; + if (this->mem == NULL) return; // error - free (self->mem); - self->mem = NULL; - self->alloc = 0; - self->len = 0; - return 0; + free (this->mem); + this->mem = NULL; + this->alloc = 0; + this->len = 0; } /* @@ -62,18 +62,31 @@ int strbuf_cap(strbuf* s) { return strbuf_append(s, 0); } -int DEEP_COPY(strbuf)(strbuf* dest, strbuf* src) { - int retval = 0; +// int DEEP_COPY(strbuf)(strbuf* dest, strbuf* src) { +strbuf::strbuf(strbuf& src) { + // int retval = 0; - if (dest->alloc < src->len) { + if (this->alloc < src.len) { /* +1 in length is to have room for '\0'. */ - strbuf_realloc(dest, src->len + 1); - retval = 1; + strbuf_realloc(this, src.len + 1); + // retval = 1; } - dest->len = src->len; - memcpy(dest->mem, src->mem, src->len); - return retval; + this->len = src.len; + memcpy(this->mem, src.mem, src.len); + // return retval; +} + +strbuf& strbuf::operator=(strbuf& other) { + if (this->alloc < other.len) { + /* +1 in length is to have room for '\0'. */ + strbuf_realloc(this, other.len + 1); + // retval = 1; + } + + this->len = other.len; + memcpy(this->mem, other.mem, other.len); + return *this; } int strbuf_cmp(strbuf* a, strbuf* b) { @@ -129,9 +142,11 @@ strbuf* RESOLVE(strbuf)(strbuf* dest, strbuf* new_) { else return dest; } +#if 0 FMT_F(strbuf) { return sprintf(buf, "%s", self->mem); } +#endif int SIZE(strbuf)(strbuf* self) { return self->len; diff --git a/strbuf.h b/strbuf.h index de4cc29b..71fe4e88 100644 --- a/strbuf.h +++ b/strbuf.h @@ -12,22 +12,30 @@ * Also comes with a number of functions which allow for safe(er) * access to the memmory. */ -typedef struct { +struct strbuf { char* mem; /* TODO add support for negative ptr */ int ptr; unsigned int alloc; unsigned int len; -} strbuf; + + strbuf (size_t len); + strbuf (strbuf& other); + strbuf () : strbuf (1) { }; + + ~strbuf (); + + strbuf& operator=(strbuf& other); +}; /* * Init strbuf to size of 0 * Doesnt't call malloc. */ -INIT_F(strbuf); +// INIT_F(strbuf); /* Constructor */ -INIT_F(strbuf, size_t len); +// INIT_F(strbuf, size_t len); /* * Like realloc, but for strbuf @@ -37,7 +45,7 @@ int strbuf_realloc(strbuf* str, size_t len); /* * Free's contents of str, but keeps str. */ -FREE_F(strbuf); +// FREE_F(strbuf); int strbuf_cmp(strbuf* a, strbuf* b); int strbuf_c(strbuf* a, const char* b); @@ -98,7 +106,7 @@ int strbuf_init_copy(strbuf* dest, strbuf* src); strbuf* RESOLVE(strbuf)(strbuf*, strbuf*); -FMT_F(strbuf); +// FMT_F(strbuf); int SIZE(strbuf)(strbuf*); diff --git a/trie.c.inc b/trie.c.inc new file mode 100644 index 00000000..3c98c6cd --- /dev/null +++ b/trie.c.inc @@ -0,0 +1,188 @@ +// #include "trie.h" +// #ifndef TYPE +// #error "Set TYPE before including this file" +// #else + +#include + +#include "err.h" +#include "macro.h" + +// INIT_F ( TRIE(TYPE) ) { +template +trie::trie () { + // NEW(TRIE_NODE(TYPE), t, '\0'); + auto t = new trie_node('\0'); + this->root = t; +} + +// INIT_F (TRIE_NODE(TYPE), char c) { +template +trie_node::trie_node (char c) { + this->c = c; + this->value = NULL; + this->next = NULL; + this->child = NULL; +} + +// INIT_F (TRIE_NODE(TYPE), +// char c, +// TRIE_NODE(TYPE)* next, +// TRIE_NODE(TYPE)* child ) +template +trie_node::trie_node ( + char c, + trie_node* next, + trie_node* child ) +{ + this->c = c; + this->next = next; + this->child = child; +} + +// int PUSH(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key, TYPE* val ) { +template +int trie::push (char* key, T* val) { + trie_node *cur, *last; + + last = this->root; + cur = last->child; + + char* subkey = key; + + while (1) { + if (cur == NULL) { + /* Build direct LL for remaining subkey */ + for (char* c = subkey; c[0] != '\0'; c++) { + // NEW(TRIE_NODE(TYPE), t, *c); + auto t = new trie_node(*c); + last->child = t; + last = t; + } + last->value = RESOLVE(TYPE)(last->value, val); + return 0; + } else if (cur->c == subkey[0]) { + /* This node belongs to the key, + * Decend further */ + last = cur; + cur = cur->child; + subkey++; + } else if (subkey[0] == '\0') { + /* Key finished */ + last->value = RESOLVE(TYPE)(last->value, val); + return 0; + } else if (cur->next != NULL) { + /* This node was not part of the set, but it's sibling might */ + cur = cur->next; + /* `last` not set since we aren't moving down */ + } else { + /* No node on this level was part of the set, create a new__ + * sibling and follow down that parse */ + // NEW(TRIE_NODE(TYPE), t, *subkey); + auto t = new trie_node(*subkey); + cur->next = t; + last = cur; + cur = t; + } + } + + return 0; +} + +/* + * TODO what happens when I give an invalid key? + */ +template +T* trie::get (char* key) { +// TYPE* GET(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key ) { + // TRIE_NODE(TYPE)* n = trie->root->child; + trie_node* n = this->root->child; + char* subkey = key; + + while (n != NULL) { + if (subkey[1] == '\0') { + return n->value; + } else if (subkey[0] == n->c) { + n = n->child; + subkey++; + } else { + n = n->next; + } + } + + ERR("Position not found"); + return 0; +} + +// FREE_F(TRIE_NODE(TYPE)) { +template +trie_node::~trie_node () { + // if (this == NULL) return 0; + if (this->value != NULL) free (this->value); // FFREE(TYPE, this->value); + if (this->next != NULL) free (this->next); + if (this->child != NULL) free (this->child); + // free (this); +} + +// FREE_F(TRIE(TYPE)) { +template +trie::~trie () { + if (this->root->c != '\0') { + // ERR("Invalid trie"); + // return 1; + return; // error + } + free (this->root); +} + +template +int trie::empty () { +//int EMPTY(TRIE(TYPE))(TRIE(TYPE)* this) { + return this->root->child == NULL; +} + +#if 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(" "); + for (int i = 0; i < depth; i++) 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))(self->root->child, buf + seek); + fmtf("}"); + return seek; +} + +#endif /* TYPE */ diff --git a/trie.h b/trie.h index 404864d3..0ce21c06 100644 --- a/trie.h +++ b/trie.h @@ -5,42 +5,42 @@ #include "macro.h" -#define TRIE(T) TEMPL(trie, T) -#define TRIE_NODE(T) TEMPL(trie_node, T) +// #define TRIE(T) TEMPL(trie, T) +// #define TRIE_NODE(T) TEMPL(trie_node, T) -#endif /* TRIE_H */ -#ifdef TYPE +// #endif /* TRIE_H */ +// #ifdef TYPE -typedef struct TRIE_NODE(TYPE) { +template struct trie_node { char c; - TYPE* value; - struct TRIE_NODE(TYPE)* next; - struct TRIE_NODE(TYPE)* child; -} TRIE_NODE(TYPE); + T* value; + trie_node* next; + trie_node* child; -typedef struct { - TRIE_NODE(TYPE)* root; -} TRIE(TYPE); + trie_node (char c); + trie_node (char c, trie_node* next, trie_node* child); + ~trie_node (); +}; -INIT_F ( TRIE(TYPE) ); +template struct trie { + trie_node* root; -INIT_F (TRIE_NODE(TYPE), char c); + trie (); + ~trie(); -INIT_F (TRIE_NODE(TYPE), - char c, TRIE_NODE(TYPE)* next, TRIE_NODE(TYPE)* child ); + int push (char* key, T* val); + T* get (char* key); -int PUSH(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key, TYPE* val ); + int empty (); +}; -TYPE* GET(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key ); +// FMT_F(TRIE_NODE(TYPE)); +// FMT_F(TRIE(TYPE)); -FREE_F(TRIE_NODE(TYPE)); +extern template struct trie; +extern template struct trie_node; -FREE_F(TRIE(TYPE)); - -int EMPTY(TRIE(TYPE))(TRIE(TYPE)*); - -FMT_F(TRIE_NODE(TYPE)); -FMT_F(TRIE(TYPE)); +#include "trie.c.inc" #endif /* TYPE */ diff --git a/trie.inc.h b/trie.inc.h deleted file mode 100644 index 5517939e..00000000 --- a/trie.inc.h +++ /dev/null @@ -1,166 +0,0 @@ -#ifndef TYPE -#error "Set TYPE before including self file" -#else - -#include - -#include "err.h" -#include "macro.h" - -INIT_F ( TRIE(TYPE) ) { - NEW(TRIE_NODE(TYPE), t, '\0'); - self->root = t; - return 0; -} - -INIT_F (TRIE_NODE(TYPE), char c) { - self->c = c; - self->value = NULL; - self->next = NULL; - self->child = NULL; - return 0; -} - -INIT_F (TRIE_NODE(TYPE), - char c, - TRIE_NODE(TYPE)* next, - TRIE_NODE(TYPE)* child ) -{ - self->c = c; - self->next = next; - self->child = child; - return 0; -} - -int PUSH(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key, TYPE* val ) { - TRIE_NODE(TYPE) *cur, *last; - - last = trie->root; - cur = last->child; - - char* subkey = key; - - while (1) { - if (cur == NULL) { - /* Build direct LL for remaining subkey */ - for (char* c = subkey; c[0] != '\0'; c++) { - NEW(TRIE_NODE(TYPE), t, *c); - last->child = t; - last = t; - } - last->value = RESOLVE(TYPE)(last->value, val); - return 0; - } else if (cur->c == subkey[0]) { - /* This node belongs to the key, - * Decend further */ - last = cur; - cur = cur->child; - subkey++; - } else if (subkey[0] == '\0') { - /* Key finished */ - last->value = RESOLVE(TYPE)(last->value, val); - return 0; - } else if (cur->next != NULL) { - /* This node was not part of the set, but it's sibling might */ - cur = cur->next; - /* `last` not set since we aren't moving down */ - } else { - /* No node on self level was part of the set, create a new__ - * sibling and follow down that parse */ - NEW(TRIE_NODE(TYPE), t, *subkey); - cur->next = t; - last = cur; - cur = t; - } - } - - return 0; -} - -/* - * TODO what happens when I give an invalid key? - */ -TYPE* GET(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key ) { - TRIE_NODE(TYPE)* n = trie->root->child; - char* subkey = key; - - while (n != NULL) { - if (subkey[1] == '\0') { - return n->value; - } else if (subkey[0] == n->c) { - n = n->child; - subkey++; - } else { - n = n->next; - } - } - - ERR("Position not found"); - return 0; -} - -FREE_F(TRIE_NODE(TYPE)) { - if (self == NULL) return 0; - if (self->value != NULL) FFREE(TYPE, self->value); - if (self->next != NULL) FREE(TRIE_NODE(TYPE))(self->next); - if (self->child != NULL) FREE(TRIE_NODE(TYPE))(self->child); - free (self); - return 0; -} - -FREE_F(TRIE(TYPE)) { - if (self->root->c != '\0') { - // ERR("Invalid trie"); - return 1; - } - return FREE(TRIE_NODE(TYPE))(self->root); -} - -int EMPTY(TRIE(TYPE))(TRIE(TYPE)* self) { - return self->root->child == NULL; -} - -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 = self; - - if (n == NULL) { fmtf("\n"); } - while (n != NULL) { - fmtf("|"); - // FOR(int, i, depth) fmtf(" "); - for (int i = 0; i < depth; i++) 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", self); - seek += FMT(TRIE_NODE(TYPE))(self->root->child, buf + seek); - fmtf("}"); - return seek; -} - -#endif /* TYPE */ diff --git a/vcal.c b/vcal.c index 2f736892..d0bc6626 100644 --- a/vcal.c +++ b/vcal.c @@ -2,18 +2,19 @@ #include -#define TYPE strbuf -#include "linked_list.inc.h" -#undef TYPE - -#define TYPE param_set -#include "linked_list.inc.h" -#undef TYPE - -#define TYPE content_set -#include "linked_list.inc.h" -#undef TYPE - +// #define TYPE strbuf +// #include "linked_list.inc.h" +// #undef TYPE +// +// #define TYPE param_set +// #include "linked_list.inc.h" +// #undef TYPE +// +// #define TYPE content_set +// #include "linked_list.inc.h" +// #undef TYPE + +#if 0 #define T strbuf #define V LLIST(strbuf) #include "pair.inc.h" @@ -25,11 +26,12 @@ #include "pair.inc.h" #undef V #undef T +#endif -#define TYPE content_line -// #include "hash.inc" -#include "trie.inc.h" -#undef TYPE +// #define TYPE content_line +// // #include "hash.inc" +// #include "trie.inc.h" +// #undef TYPE #define TYPE vcomponent #include "vector.inc.h" @@ -47,7 +49,7 @@ INIT_F(vcomponent, const char* type) { INIT_F(vcomponent, const char* type, const char* filename) { - INIT(TRIE(content_line), &self->clines); + // INIT(TRIE(content_line), &self->clines); INIT(VECT(vcomponent), &self->components); self->filename = NULL; @@ -69,16 +71,19 @@ content_line* RESOLVE(content_line) { if (dest == NULL) return new_; - if (strbuf_cmp(&dest->key, &new_->key) != 0) { + if (strbuf_cmp(dest->key, new_->key) != 0) { ERR("Can't resolve between these two types"); return NULL; } /* This destroys new_->val. */ - APPEND(LLIST(content_set)) (&dest->val, &new_->val); + // APPEND(LLIST(content_set)) (&dest->val, &new_->val); + dest->val->append(new_->val); - FREE(strbuf)(&new_->key); - free(new_); + // FREE(strbuf)(&new_->key); + // delete new_->key; + delete new_; + // free(new_); return dest; } @@ -88,7 +93,8 @@ content_line* get_property (vcomponent* ev, const char* key) { char* cpy = (char*) (calloc(sizeof(*cpy), len)); strncpy (cpy, key, len); - content_line* ret = GET(TRIE(content_line))(&ev->clines, cpy); + // content_line* ret = GET(TRIE(content_line))(&ev->clines, cpy); + content_line* ret = ev->clines.get(cpy); free (cpy); return ret; @@ -98,10 +104,10 @@ FREE_F(vcomponent) { if (self->filename != NULL) free(self->filename); free(self->type); - if (FREE(TRIE(content_line))(&self->clines) != 0) { - fprintf(stderr, "Error freeing vcomponent belonging to file \n %s \n", - self->filename); - } + // if (FREE(TRIE(content_line))(&self->clines) != 0) { + // fprintf(stderr, "Error freeing vcomponent belonging to file \n %s \n", + // self->filename); + // } FREE(VECT(vcomponent))(&self->components); @@ -119,6 +125,7 @@ int DEEP_COPY(vcomponent)(vcomponent* a, vcomponent* b) { return -1; } +#if 0 FMT_F(vcomponent) { int seek = 0; @@ -127,7 +134,7 @@ FMT_F(vcomponent) { seek += sprintf(buf + seek, _YELLOW); seek += sprintf(buf + seek, "\nVComponet (Type := %s)\n", self->type); seek += sprintf(buf + seek, _RESET); - seek += FMT(TRIE(content_line))(&self->clines, buf + seek); + // seek += FMT(TRIE(content_line))(&self->clines, buf + seek); seek += sprintf(buf + seek, "\nComponents:\n"); FOR(VECT, vcomponent, comp, &self->components) { seek += FMT(vcomponent)(comp, buf + seek); @@ -135,3 +142,4 @@ FMT_F(vcomponent) { return seek; } +#endif diff --git a/vcal.h b/vcal.h index 95811a7b..2bbc11d1 100644 --- a/vcal.h +++ b/vcal.h @@ -5,11 +5,14 @@ #include "strbuf.h" -#define TYPE strbuf +//#define TYPE strbuf #include "linked_list.h" // #include "trie.h" -#undef TYPE +//#undef TYPE + +// #include +#if 0 #define T strbuf #define V LLIST(strbuf) #include "pair.h" @@ -41,6 +44,22 @@ #define content_line PAIR(strbuf, LLIST(content_set)) #undef V #undef T +#endif + +#include "pair.h" +typedef pair > param_set; + +#define TYPE param_set +#include "linked_list.h" +#undef TYPE + +typedef pair > content_set; + +#define TYPE content_set +#include "linked_list.h" +#undef TYPE + +typedef pair > content_line; /* * Helper macros for accessing fields in @@ -54,12 +73,12 @@ #define CLINE_CUR_CSET(c) (&((c)->val.cur->value)) /* content_set */ -#define CLINE_CUR(c) ((c)->val.cur->value) +#define CLINE_CUR(c) ((c)->val->cur->value) /* strbuf */ -#define CLINE_CUR_VAL(c) (& CLINE_CUR(c)->key) +#define CLINE_CUR_VAL(c) (CLINE_CUR(c)->key) /* LLIST(param_set) */ -#define CLINE_CUR_PARAMS(c) (& CLINE_CUR(c)->val) +#define CLINE_CUR_PARAMS(c) (CLINE_CUR(c)->val) /* strbuf */ #define CLINE_CUR_PARAM_KEY(c) (CLINE_CUR_PARAMS(c)->cur->value->key) @@ -75,9 +94,9 @@ content_line* RESOLVE(content_line) (content_line* dest, content_line* new_); -#define TYPE content_line +// #define TYPE content_line #include "trie.h" -#undef TYPE +// #undef TYPE typedef struct s_vcomponent vcomponent; @@ -89,7 +108,8 @@ struct s_vcomponent { char* filename; char* type; vcomponent* parent; - TRIE(content_line) clines; + // TRIE(content_line) clines; + trie clines; VECT(vcomponent) components; }; @@ -112,6 +132,6 @@ int PUSH(vcomponent)(vcomponent*, vcomponent*); int DEEP_COPY(vcomponent)(vcomponent*, vcomponent*); -FMT_F(vcomponent); +// FMT_F(vcomponent); #endif /* VCAL_H */ -- cgit v1.2.3