From ecd15e3f6731a8298e9cd3ebc45e1875e8c19063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 19 Feb 2019 10:05:45 +0100 Subject: Now it links, but segfaults. --- Makefile | 6 +++--- linked_list.cpp | 26 -------------------------- linked_list.h | 36 ++++++++++++++++++++++++++++++------ parse.cpp | 17 ++++++----------- trie.cpp | 10 ---------- trie.h | 8 ++++---- vcal.h | 44 +++++++++++++++++++++++++++++++++++++------- 7 files changed, 80 insertions(+), 67 deletions(-) diff --git a/Makefile b/Makefile index 7d9337d8..e9c3140b 100644 --- a/Makefile +++ b/Makefile @@ -5,9 +5,9 @@ CC := g++ OBJDIR = obj CFLAGS = -std=gnu++11 -Wall -Wextra -Wno-reorder \ - -ggdb -fPIC \ - $(shell guile-config compile) -LDFLAGS = -fPIC $(shell guile-config link) + -ggdb -fPIC # \ + # $(shell guile-config compile) +LDFLAGS = -fPIC # $(shell guile-config link) H_FILES = $(wildcard *.h) C_FILES = $(wildcard *.cpp) diff --git a/linked_list.cpp b/linked_list.cpp index b30458c9..ed37eb23 100644 --- a/linked_list.cpp +++ b/linked_list.cpp @@ -1,15 +1,5 @@ #include "linked_list.h" -template -llist::llist () { - this->length = 0; - this->cur = this->head = new llink; - this->tail = new llink; - - head->after = tail; - tail->before = head; -} - template llink::~llink () { this.unlink(); @@ -21,22 +11,6 @@ void llink::unlink () { if (this->after != nullptr) this->after->before = this->before; } -template -void llist::push(T* val) { - auto l = new llink(val); - - l->after = FIRST(this); - FIRST(this) = l; - - l->after->before = l; - l->before = this->head; - - ++this->length; - - // TODO do I want to change that? - this->cur = l; -} - template T& llist::peek() { if (this->empty()) return nullptr; diff --git a/linked_list.h b/linked_list.h index a27d82a1..7ed4c6c4 100644 --- a/linked_list.h +++ b/linked_list.h @@ -8,22 +8,20 @@ struct llink { llink* after = nullptr; T* value; - llink (); + llink () { }; llink (T* val) : value(val) { } ~llink (); void unlink (); }; -// #define L(link) (link)->value - template struct llist { llink* head; llink* tail; llink* __cur; T* cur() { return __cur->value; } - int length; + int length = 0; llist (); @@ -43,14 +41,40 @@ struct llist { bool empty () { return length == 0; } }; -// template -// std::ostream& std::operator<<(std::ostream&, llist); +template +llist::llist () { + this->__cur = this->head = new llink; + this->tail = new llink; + + head->after = tail; + tail->before = head; +} + #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 +void llist::push(T* val) { + auto l = new llink(val); + + l->after = FIRST(this); + FIRST(this) = l; + + l->after->before = l; + l->before = this->head; + + ++this->length; + + // TODO do I want to change that? + this->__cur = l; +} + +// template +// std::ostream& std::operator<<(std::ostream&, llist); + #if 0 DEEP_COPY LINK(TYPE)* n = FIRST(src); diff --git a/parse.cpp b/parse.cpp index f7e42d77..8d47ee72 100644 --- a/parse.cpp +++ b/parse.cpp @@ -35,11 +35,8 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { // std::string& target = cline strbuf target = ctx.str; - // DEEP_COPY(strbuf)(target, &ctx.str); target.cap(); - // strbuf_cap(target); ctx.str.soft_reset(); - // strbuf_soft_reset(&ctx.str); handle_kv(&cline, &ctx); @@ -123,9 +120,7 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { s->cap(); ctx.str.soft_reset(); - // llist* ls = & CLINE_CUR_PARAMS(&cline)->cur->value->second; - cline.push(s); - + cline.push_param_value (s); } if (p_ctx == p_key) { @@ -160,7 +155,7 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { */ //strbuf* target = CLINE_CUR_VAL(&cline); - strbuf* target = cline.second.cur(); + strbuf* target = cline.value(); *target = ctx.str; target->cap(); ctx.str.soft_reset(); @@ -184,14 +179,14 @@ int handle_kv ( parse_ctx* ctx ) { - if (cline->first == "BEGIN") { + if (cline->key == "BEGIN") { /* should be one of: * VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE, * and possibly some others I forget. */ strbuf* s = new strbuf; - strbuf* type = cline->second.cur(); + strbuf* type = cline->value(); *s = *type; ctx->key_stack.push(s); @@ -202,10 +197,10 @@ int handle_kv ( e->parent = ctx->comp_stack.top(); ctx->comp_stack.push(e); - } else if (cline->first == "END") { + } else if (cline->key == "END") { // strbuf* s = POP(LLIST(strbuf))(&ctx->key_stack); strbuf* s = ctx->key_stack.top(); ctx->key_stack.pop(); - if (s == cline->second.cur()) { + if (s == cline->value()) { #if 0 ERR_P(ctx, "Expected END:%s, got END:%s.\n%s line", s->mem, diff --git a/trie.cpp b/trie.cpp index a3446a97..e3158a2b 100644 --- a/trie.cpp +++ b/trie.cpp @@ -1,15 +1,5 @@ #include "trie.h" -template -trie::trie () { - this->root = new trie_node ('\0'); -} - -template -trie_node::trie_node (char c) { - this->c = c; -} - template trie_node::trie_node (char c, trie_node* next, trie_node* child) { this->c = c; diff --git a/trie.h b/trie.h index 07c1bce3..4613694e 100644 --- a/trie.h +++ b/trie.h @@ -3,25 +3,25 @@ #include -template +template struct trie_node { char c; T* value = NULL; trie_node* next = nullptr; trie_node* child = nullptr; - trie_node (char c); + trie_node (char c) : c(c) { }; trie_node (char c, trie_node* next, trie_node* child); }; template std::ostream& operator<<(std::ostream&, trie_node* node); -template +template struct trie { trie_node* root; - trie (); + trie () : root (new trie_node ('\0')) { } int push_back (const char* key, const T&); diff --git a/vcal.h b/vcal.h index a4c7252c..fb767820 100644 --- a/vcal.h +++ b/vcal.h @@ -11,10 +11,40 @@ #include "strbuf.h" #include "linked_list.h" -// typedef std::pair > param_set; -// typedef std::pair > content_set; -// typedef std::pair > content_line; -typedef llist content_line; +struct __param_set { + strbuf key; + llist values; + + __param_set (strbuf key) : key (key) { } +}; + +/* + * A content set is a single instance of a content line, having a + * specific value and it's own (possible) set of parameters. + */ +struct __content_set { + strbuf value; + llist<__param_set> params; +}; + +/* + * A content line is the collection of all lines which share the same + * key. + */ +struct content_line { + strbuf key; + llist<__content_set> values; + + inline void push_param_key (strbuf key) { + auto p = new __param_set(key); + this->values.cur()->params.push(p); + } + + inline void push_param_value (strbuf* value) { this->values.cur()->params.cur()->values.push(value); } + + inline strbuf* value() { return &this->values.cur()->value; } + +}; struct vcomponent { std::string filename; @@ -60,16 +90,16 @@ std::ostream& operator<<(std::ostream&, vcomponent*); #define CLINE_CUR_CSET(c) (&((c)->second.cur->value)) /* content_set */ -#define CLINE_CUR(c) ((c)->second.cur->value) +#define CLINE_CUR(c) ((c)->second.cur()) /* strbuf */ #define CLINE_CUR_VAL(c) (& CLINE_CUR(c)->first) /* LLIST(param_set) */ #define CLINE_CUR_PARAMS(c) (& CLINE_CUR(c)->second) - /* strbuf */ +/* strbuf */ #define CLINE_CUR_PARAM_KEY(c) (CLINE_CUR_PARAMS(c)->cur->value->first) - /* strbuf */ +/* strbuf */ #define CLINE_CUR_PARAM_VAL(c) (CLINE_CUR_PARAMS(c)->cur->value->second.cur->value) #endif -- cgit v1.2.3