From 151571c4540e02b130c808d58d3cc169dd87627a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Wed, 20 Feb 2019 22:18:23 +0100 Subject: new segfaults, I give up. --- Makefile | 2 +- parse.cpp | 46 +++++++++++++++++++++++++++------------------- strbuf.cpp | 24 ++++++++++++++++++++++++ strbuf.h | 28 +++++++++++++++------------- vcal.cpp | 5 +++++ vcal.h | 28 +++++++++++++++------------- 6 files changed, 87 insertions(+), 46 deletions(-) diff --git a/Makefile b/Makefile index e5a0ae1a..ef2e8949 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CC := g++ OBJDIR = obj -CFLAGS = -std=gnu++11 -pedantic -Wall -Wextra -Wno-reorder \ +CFLAGS = -std=gnu++11 -pedantic -Wall -Wextra \ -ggdb -fPIC # \ # $(shell guile-config compile) LDFLAGS = -fPIC # $(shell guile-config link) diff --git a/parse.cpp b/parse.cpp index d6da0bb4..0b6c175f 100644 --- a/parse.cpp +++ b/parse.cpp @@ -30,10 +30,13 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { if (fold(f, &ctx, c) > 0) { /* Actuall end of line, handle value */ - cline.push_value(ctx.str); - ctx.str.soft_reset(); + cline.push_value(new strbuf(ctx.str)); + std::cout << _RED << ctx.str.len << ' ' << ctx.str << _RESET << std::endl; + strbuf* n = cline.values.back()->value; + std::cout << _BLUE << n->len << ' ' << *n << _RESET << std::endl; handle_kv(&cline, &ctx); + ctx.str.soft_reset(); p_ctx = p_key; } /* Else continue on current line */ @@ -84,7 +87,7 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { // ctx.str holds param_key; // cline.values.back()->params.push_back(new __param_set(ctx.str)); - cline.values.back()->push_param_key(ctx.str); + cline.values.back()->push_param_key(new strbuf(ctx.str)); ctx.str.soft_reset(); @@ -102,14 +105,14 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { if (p_ctx == p_param_value) { /* push kv pair */ - cline.values.back()->push_param_value(ctx.str); + cline.values.back()->push_param_value(new strbuf(ctx.str)); ctx.str.soft_reset(); } if (p_ctx == p_key) { /* set key for content line */ - cline.key = ctx.str; - cline.key.cap(); + *cline.key = ctx.str; + cline.key->cap(); ctx.str.soft_reset(); @@ -140,11 +143,11 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { ++ctx.line; ctx.column = 0; - cline.values.back()->value = ctx.str; - cline.values.back()->value.cap(); - ctx.str.soft_reset(); + cline.push_value(new strbuf(ctx.str)); + std::cout << ctx.str; handle_kv(&cline, &ctx); + ctx.str.soft_reset(); } @@ -160,7 +163,7 @@ int handle_kv ( parse_ctx* ctx ) { - if (cline->key == "BEGIN") { + if (*cline->key == "BEGIN") { /* should be one of: * VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE, * and possibly some others I forget. @@ -176,16 +179,15 @@ int handle_kv ( e->parent = ctx->comp_stack.top(); ctx->comp_stack.push(e); - } else if (cline->key == "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 == ctx->str) { -#if 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); -#endif + s->c_str(), + cline->values.back()->value->c_str(), + ctx->comp_stack.top()->filename.c_str()); + ctx->key_stack.push(s); return -1; @@ -195,22 +197,28 @@ int handle_kv ( /* Received propper end, push cur into parent */ vcomponent* cur = ctx->comp_stack.top(); ctx->comp_stack.pop(); + // TODO we never get here + INFO(here); + // TODO should self instead be done at creation time? - ctx->comp_stack.push(cur); + ctx->comp_stack.top()->push(*cur); } } else { content_line* c = new content_line; + // TODO make sure deep-copy *c = *cline; + // TODO + // RESET(LLIST(content_set))(&cline->val); + + ctx->comp_stack.top()->clines.push(c->key->c_str(), c); // PUSH(TRIE(content_line))( // &PEEK(LLIST(vcomponent))(&ctx->comp_stack)->clines, // c->key.mem, c); // TODO? // ctx->comp_stack.top()->clines.push_back(c->first, c); - // TODO - // RESET(LLIST(content_set))(&cline->val); } return 0; diff --git a/strbuf.cpp b/strbuf.cpp index 7b63c58d..45c63beb 100644 --- a/strbuf.cpp +++ b/strbuf.cpp @@ -1,6 +1,7 @@ #include "strbuf.h" #include +#include strbuf::strbuf (const strbuf& other) { this->alloc = other.len + 1; @@ -28,5 +29,28 @@ strbuf& strbuf::operator+=(char c) { this->mem[this->len] = c; this->ptr = ++this->len; + return *this; } + +bool strbuf::operator==(strbuf& other) { + return strncmp(this->mem, other.mem, this->len) == 0; +} + +bool strbuf::operator==(const char* other) { + std::cerr << __FILE__ << ':' << __LINE__ << ' ' << this->c_str() << "==" << other << std::endl; + return strncmp(this->mem, other, this->len) == 0 ; +} + +std::ostream& operator << (std::ostream& out, strbuf& str) { + out << (str.to_string()); + return out; +} + +// TODO this leaks memmory +char* strbuf::c_str() { + char* buf = static_cast(malloc(this->len + 1)); + memcpy(buf, this->mem, this->len); + buf[this->len] = '\0'; + return buf; +} diff --git a/strbuf.h b/strbuf.h index 38a45b09..9bf603a9 100644 --- a/strbuf.h +++ b/strbuf.h @@ -3,9 +3,11 @@ #include #include -#include +// #include #include +#include + /* * A high level string type which holds it's own length, how much * memmory it has allocated for itself, and a seek pointer into the @@ -15,12 +17,13 @@ * access to the memmory. */ struct strbuf { - char* mem; /* TODO add support for negative ptr */ - int ptr = 0; - size_t alloc; + int ptr = 0; + size_t alloc = 0; size_t len = 0; + char* mem; + strbuf () : strbuf (1) { }; strbuf (const strbuf& other); @@ -36,11 +39,9 @@ struct strbuf { */ void realloc (size_t len); - bool operator==(strbuf& other) - { return strncmp(this->mem, other.mem, this->len) == 0; } + bool operator==(strbuf& other); - bool operator==(const char* other) - { return strncmp(this->mem, other, this->len) == 0 ; } + bool operator==(const char* other); strbuf& operator=(strbuf* other); @@ -72,15 +73,16 @@ struct strbuf { this->ptr = 0; this->len = 0; }; - std::string to_string() { - return std::string (this->mem); - } - char* c_str() { - return this->mem; + char* c_str(); + + std::string to_string() { + return std::string (this->c_str()); } }; +std::ostream& operator << (std::ostream& out, strbuf& str); + /* * Reallocs dest to be the same size as src, and copies the contents * of src into dest. diff --git a/vcal.cpp b/vcal.cpp index b0ce2c82..8120674c 100644 --- a/vcal.cpp +++ b/vcal.cpp @@ -3,6 +3,11 @@ #include +__content_set::__content_set (strbuf* value) : value(value) { + std::cout << _GREEN << value->len << ' ' << value << _RESET << std::endl; + this->value->cap(); +} + vcomponent::vcomponent( const std::string& type, const std::string& filename) diff --git a/vcal.h b/vcal.h index 21f66063..f92cf420 100644 --- a/vcal.h +++ b/vcal.h @@ -10,16 +10,17 @@ #include "trie.h" #include "strbuf.h" #include "linked_list.h" +#include "err.h" struct __param_set { - strbuf key; - std::list values; + strbuf* key; + std::list values; - __param_set (strbuf key) : key (key) { this->key.cap(); } + __param_set (strbuf* key) : key (key) { this->key->cap(); } - void push_value (strbuf val) { + void push_value (strbuf* val) { this->values.push_back(val); - this->values.back().cap(); + this->values.back()->cap(); } }; @@ -28,16 +29,16 @@ struct __param_set { * specific value and it's own (possible) set of parameters. */ struct __content_set { - strbuf value; + strbuf* value; std::list<__param_set*> params; - __content_set (strbuf value) : value(value) { this->value.cap(); } + __content_set (strbuf* value); - void push_param_key (strbuf key) { + void push_param_key (strbuf* key) { this->params.push_back (new __param_set(key)); } - void push_param_value (strbuf val) { + void push_param_value (strbuf* val) { this->params.back()->push_value(val); } }; @@ -47,18 +48,18 @@ struct __content_set { * key. */ struct content_line { - strbuf key; + strbuf* key; // llist<__content_set> values; std::list<__content_set*> values; - void push_value (strbuf str) { + void push_value (strbuf* str) { this->values.push_back(new __content_set(str)); } }; struct vcomponent { - std::string filename; std::string type; + std::string filename; vcomponent* parent = nullptr; trie clines; std::vector components; @@ -67,6 +68,7 @@ struct vcomponent { vcomponent(const std::string& type, const std::string& filename); + void add_content_line (content_line* c) { clines.push(c->key.c_str(), c); } @@ -82,7 +84,7 @@ struct vcomponent { content_line* operator[] (const char* key) { return this->clines[key]; } - void push_back(const vcomponent& child) + void push(const vcomponent& child) { this->components.push_back(child); } }; -- cgit v1.2.3