aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@hornquist.se>2019-02-19 01:25:45 +0100
committerHugo Hörnquist <hugo@hornquist.se>2019-02-19 01:25:45 +0100
commit3362e2fb538c9426635e2fda8d9737662b48fc06 (patch)
treeb7fe6dc049bb7a86e5ece3500bafaf8992984f9c
parentStarted full rewrite in C++. (diff)
downloadcalp-3362e2fb538c9426635e2fda8d9737662b48fc06.tar.gz
calp-3362e2fb538c9426635e2fda8d9737662b48fc06.tar.xz
I somehow got stuff to bulid, now it just doesn't link.
-rw-r--r--Makefile2
-rw-r--r--linked_list.cpp61
-rw-r--r--linked_list.h82
-rw-r--r--linked_list.inc.h152
-rw-r--r--parse.cpp108
-rw-r--r--parse.h4
-rw-r--r--strbuf.h11
-rw-r--r--trie.h21
-rw-r--r--vcal.cpp11
-rw-r--r--vcal.h11
10 files changed, 176 insertions, 287 deletions
diff --git a/Makefile b/Makefile
index e72ba47c..7d9337d8 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ CC := g++
OBJDIR = obj
-CFLAGS = -std=gnu++11 -Wall -Wextra \
+CFLAGS = -std=gnu++11 -Wall -Wextra -Wno-reorder \
-ggdb -fPIC \
$(shell guile-config compile)
LDFLAGS = -fPIC $(shell guile-config link)
diff --git a/linked_list.cpp b/linked_list.cpp
index bd49510e..d4bd5a91 100644
--- a/linked_list.cpp
+++ b/linked_list.cpp
@@ -3,27 +3,27 @@
template <typename T>
llist<T>::llist () {
this->length = 0;
- this->cur = this->head = new link<T>;
- this->tail = new link<T>;
+ this->cur = this->head = new llink<T>;
+ this->tail = new llink<T>;
head->after = tail;
tail->before = head;
}
template <typename T>
-link<T>::~link () {
+llink<T>::~llink () {
this.unlink();
}
template <typename T>
-void link<T>::unlink () {
+void llink<T>::unlink () {
if (this->before != nullptr) this->before->after = this->after;
if (this->after != nullptr) this->after->before = this->before;
}
template <typename T>
-void llist<T>::push(T& val) {
- auto l = new link<T>(val);
+void llist<T>::push(T* val) {
+ auto l = new llink<T>(val);
l->after = FIRST(this);
FIRST(this) = l;
@@ -36,3 +36,52 @@ void llist<T>::push(T& val) {
// TODO do I want to change that?
this->cur = l;
}
+
+template <typename T>
+T& llist<T>::peek() {
+ if (this->empty()) return nullptr;
+
+ return FIRST(this)->value;
+}
+
+template <typename T>
+T& llist<T>::pop() {
+ if (this->empty()) return nullptr;
+
+ llink<T>* frst = FIRST(this);
+ frst.unlinke();
+
+ T& retval = frst->value;
+ --this->length;
+ delete frst;
+
+ return retval;
+}
+
+template <typename T>
+void llist<T>::operator+= (llist<T>& other) {
+
+ /* Link end of dest onto start of new__. */
+ LAST(this)->after = FIRST(other);
+ FIRST(other)->before = LAST(this);
+
+ /* Free the two now not needed end links. */
+ // free(other->head);
+ // free(other->tail);
+ delete other->head;
+ delete other->tail;
+
+ /* Update dest with new__ tail ptr. */
+ this->tail = other->tail;
+
+ this->length += other->length;
+}
+
+// template <typename T>
+// std::ostream& std::operator<<(std::ostream&, llist<T>) {
+// o << '(';
+// for (T t : list) {
+// o << t;
+// }
+// o << ')';
+// }
diff --git a/linked_list.h b/linked_list.h
index 7d11ca81..3eab5549 100644
--- a/linked_list.h
+++ b/linked_list.h
@@ -3,14 +3,14 @@
template <typename T>
-struct link {
- link<T>* before = nullptr;
- link<T>* after = nullptr;
+struct llink {
+ llink<T>* before = nullptr;
+ llink<T>* after = nullptr;
T* value;
- link ();
- link (T* val) : value(val) { }
- ~link ();
+ llink ();
+ llink (T* val) : value(val) { }
+ ~llink ();
void unlink ();
};
@@ -19,64 +19,62 @@ struct link {
template <typename T>
struct llist {
- link<T>* head;
- link<T>* tail;
- link<T>* cur;
+ llink<T>* head;
+ llink<T>* tail;
+ llink<T>* cur;
int length;
llist ();
- void push ( T& );
+ void push ( T* );
T& peek ();
T& pop ();
- llist& operator += (llist& other);
+ /*
+ * Adds two linked lists together.
+ * O(1) time.
+ * destroys new__ in the process, but keeps the elements.
+ * make sure to free(new__) after.
+ */
+ void operator += (llist<T>& other);
+
+ int size () { return length; }
+ bool empty () { return length == 0; }
};
+// template <typename T>
+// std::ostream& std::operator<<(std::ostream&, llist<T>);
+
#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
-/*
- * 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.
- */
-
-int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src );
+#if 0
+DEEP_COPY
+ LINK(TYPE)* n = FIRST(src);
-int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new_ );
+ while (n->after != NULL) {
+ NEW(TYPE, cpy);
+ DEEP_COPY(TYPE)(cpy, n->value);
+ PUSH(LLIST(TYPE)) ( dest, cpy );
+ n = n->after;
+ }
-int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist );
-int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist );
+ return 0;
+#endif
/*
* 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 );
+// int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist );
+#if 0
LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new_);
-
-FMT_F(LLIST(TYPE));
-
-/* Iterator */
-
-#define __PRE_LLIST(T, l, set) \
- T* l; LINK(T)* __INTER(l);
-
-#define PRE_FOR_LLIST(T) __PRE_LLIST
-
-// #define __BEG_LLIST(v, set) v = (set)->head
-#define __BEG_LLIST(T, l, set) __INTER(l) = FIRST(set), l = L(__INTER(l))
-#define BEG_LLIST(T) __BEG_LLIST
-
-#define __END_LLIST(T, l, set) __INTER(l) != (set)->tail
-#define END_LLIST(T) __END_LLIST
-
-#define __NXT_LLIST(T, l, set) __INTER(l) = __INTER(l)->after, l = L(__INTER(l))
-// #define __NXT_LLIST(T, l, set) l = L(__INTER(l) = __INTER(l)->after)
-#define NXT_LLIST(T) __NXT_LLIST
+ if (dest == NULL) return new__;
+ APPEND(LLIST(TYPE))(dest, new__);
+ return dest;
+#endif
#endif /* LINKED_LIST_H */
diff --git a/linked_list.inc.h b/linked_list.inc.h
deleted file mode 100644
index 410fddba..00000000
--- a/linked_list.inc.h
+++ /dev/null
@@ -1,152 +0,0 @@
-FREE_F( LLIST(TYPE) ) {
- LINK(TYPE) *n, *next;
- n = self->head;
- while ( n != NULL ) {
- next = n->after;
- FFREE(LINK(TYPE), n);
- n = next;
- }
-
- self->length = -1;
-
- return 0;
-}
-
-INIT_F ( LINK(TYPE) ) {
- self->before = NULL;
- self->after = NULL;
- self->value = NULL;
- return 0;
-}
-
-INIT_F ( LINK(TYPE), TYPE* val ) {
- self->before = NULL;
- self->after = NULL;
- self->value = val;
- return 0;
-}
-
-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;
- return 0;
-}
-
-
-int PUSH(LLIST(TYPE)) ( LLIST(TYPE)* lst, TYPE* val) {
- NEW(LINK(TYPE), link, val);
-
- link->after = FIRST(lst);
- FIRST(lst) = link;
-
- link->after->before = link;
- link->before = lst->head;
-
- ++lst->length;
-
- // TODO do I want to change that?
- lst->cur = link;
-
- return 0;
-}
-
-TYPE* PEEK(LLIST(TYPE)) ( LLIST(TYPE)* lst ) {
- if (EMPTY(LLIST(TYPE))(lst)) return NULL;
-
- return FIRST(lst)->value;
-}
-
-TYPE* POP(LLIST(TYPE)) ( LLIST(TYPE)* lst) {
- if (EMPTY(LLIST(TYPE))(lst)) return NULL;
-
- LINK(TYPE)* frst = FIRST(lst);
- UNLINK(LINK(TYPE))(frst);
-
- TYPE* retval = frst->value;
- --lst->length;
- free (frst);
- return retval;
-}
-
-int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ) {
- LINK(TYPE)* n = FIRST(src);
-
- while (n->after != NULL) {
- NEW(TYPE, cpy);
- DEEP_COPY(TYPE)(cpy, n->value);
- PUSH(LLIST(TYPE)) ( dest, cpy );
- n = n->after;
- }
-
- return 0;
-}
-
-/*
- * Adds two linked lists together.
- * O(1) time.
- * 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__ ) {
-
- /* Link end of dest onto start of new__. */
- LAST(dest)->after = FIRST(new__);
- FIRST(new__)->before = LAST(dest);
-
- /* Free the two now not needed end links. */
- free(new__->head);
- free(dest->tail);
-
- /* Update dest with new__ tail ptr. */
- dest->tail = new__->tail;
-
- dest->length += new__->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__) {
- if (dest == NULL) return new__;
- APPEND(LLIST(TYPE))(dest, new__);
- return dest;
-}
-
-int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ) {
-
- LINK(TYPE) *link = FIRST(llist), *next;
- /*
- * Manual looping rather than itterators since we destroyed the
- * loop variable.
- */
- while (link != llist->tail) {
- next = link->after;
- FFREE(LINK(TYPE), link);
- link = next;
- }
-
- llist->cur = llist->head;
-
- return 0;
-}
-
-FMT_F(LLIST(TYPE)) {
- int seek = 0;
- fmtf("(");
- FOR(LLIST, TYPE, v, self) {
- seek += FMT(TYPE)(v, buf + seek);
- fmtf(" ");
- }
- fmtf(")");
-
- return seek;
-}
-
-#endif /* TYPE */
diff --git a/parse.cpp b/parse.cpp
index 310ab7fe..457f1663 100644
--- a/parse.cpp
+++ b/parse.cpp
@@ -16,7 +16,7 @@ int parse_file(char* filename, FILE* f, vcomponent& root) {
part_context p_ctx = p_key;
parse_ctx ctx(filename);
- ctx.comp_stack.push(root);
+ ctx.comp_stack.push(&root);
content_line cline;
@@ -90,7 +90,8 @@ 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);
- std::list<param_set>* params = cline.second.second;
+ /*
+ std::list<param_set>* params = cline.val.val;
// NEW(param_set, ps);
auto ps = new param_set;
@@ -98,7 +99,10 @@ int parse_file(char* filename, FILE* f, vcomponent& root) {
ps->first = ctx.str;
ps->first.cap();
ctx.str.soft_reset();
- PUSH(LLIST(param_set))(params, ps);
+ params += ps;
+ // PUSH(LLIST(param_set))(params, ps);
+ *
+ */
p_ctx = p_param_value;
@@ -114,31 +118,32 @@ int parse_file(char* filename, FILE* f, vcomponent& root) {
if (p_ctx == p_param_value) {
/* push kv pair */
- NEW(strbuf, s);
+ auto s = new strbuf;
+ // TODO make sure this is a deep copy
+ *s = ctx.str;
- DEEP_COPY(strbuf)(s, &ctx.str);
- strbuf_cap(s);
- strbuf_soft_reset(&ctx.str);
+ s->cap();
+ ctx.str.soft_reset();
- LLIST(strbuf)* ls = & CLINE_CUR_PARAMS(&cline)->cur->value->val;
- PUSH(LLIST(strbuf))(ls, s);
+ llist<strbuf>* ls = & CLINE_CUR_PARAMS(&cline)->cur->value->second;
+ ls->push(s);
}
if (p_ctx == p_key) {
- DEEP_COPY(strbuf)(&cline.key, &ctx.str);
- strbuf_cap(&cline.key);
- strbuf_soft_reset(&ctx.str);
+ cline.first = ctx.str;
+ cline.first.cap();
+ ctx.str.soft_reset();
- NEW(content_set, p);
- PUSH(LLIST(content_set))(&cline.val, p);
+ content_set* p = new content_set;
+ cline.second.push(p);
}
if (c == ':') p_ctx = p_value;
else if (c == ';') p_ctx = p_param_name;
} else {
- strbuf_append(&ctx.str, c);
+ ctx.str += c;
++ctx.column;
++ctx.pcolumn;
@@ -157,9 +162,9 @@ int parse_file(char* filename, FILE* f, vcomponent& root) {
*/
strbuf* target = CLINE_CUR_VAL(&cline);
- DEEP_COPY(strbuf)(target, &ctx.str);
- strbuf_cap(target);
- strbuf_soft_reset(&ctx.str);
+ *target = ctx.str;
+ target->cap();
+ ctx.str.soft_reset();
++ctx.line;
ctx.column = 0;
@@ -168,13 +173,9 @@ int parse_file(char* filename, FILE* f, vcomponent& root) {
}
- 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));
-
- FREE(parse_ctx)(&ctx);
+ // assert(POP(LLIST(vcomponent))(&ctx.comp_stack) == root);
+ // assert(EMPTY(LLIST(strbuf))(&ctx.key_stack));
+ // assert(EMPTY(LLIST(vcomponent))(&ctx.comp_stack));
return 0;
}
@@ -184,52 +185,59 @@ int handle_kv (
parse_ctx* ctx
) {
- if (strbuf_c(&cline->key, "BEGIN")) {
+ if (cline->first == "BEGIN") {
/* should be one of:
* VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE,
* and possibly some others I forget.
*/
- NEW(strbuf, s);
+ strbuf* s = new strbuf;
strbuf* type = CLINE_CUR_VAL(cline);
- DEEP_COPY(strbuf)(s, type);
- PUSH(LLIST(strbuf))(&ctx->key_stack, s);
+ *s = *type;
+ ctx->key_stack.push(s);
- RESET(LLIST(content_set))(&cline->val);
+ // TODO ompty cline->second here;
+ // RESET(LLIST(content_set))(&cline->val);
- NEW(vcomponent, e,
- s->mem,
- ctx->filename);
- e->parent = PEEK(LLIST(vcomponent))(&ctx->comp_stack);
- PUSH(LLIST(vcomponent))(&ctx->comp_stack, e);
+ auto e = new vcomponent(s->mem, ctx->filename);
+ e->parent = ctx->comp_stack.top();
+ ctx->comp_stack.push(e);
- } else if (strbuf_c(&cline->key, "END")) {
- strbuf* s = POP(LLIST(strbuf))(&ctx->key_stack);
- if (strbuf_cmp(s, CLINE_CUR_VAL(cline)) != 0) {
+ } else if (cline->first == "END") {
+ // strbuf* s = POP(LLIST(strbuf))(&ctx->key_stack);
+ strbuf* s = ctx->key_stack.top(); ctx->key_stack.pop();
+ if (s == CLINE_CUR_VAL(cline)) {
+#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);
- PUSH(LLIST(strbuf))(&ctx->key_stack, s);
+#endif
+ ctx->key_stack.push(s);
return -1;
} else {
- FFREE(strbuf, s);
+ delete s;
+
/* Received propper end, push cur into parent */
- vcomponent* cur = POP(LLIST(vcomponent))(&ctx->comp_stack);
+ vcomponent* cur = ctx->comp_stack.top(); ctx->comp_stack.pop();
// TODO should self instead be done at creation time?
- PUSH(vcomponent)(PEEK(LLIST(vcomponent))(&ctx->comp_stack), cur);
+ ctx->comp_stack.push(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);
+ content_line* c = new content_line;
+ // TODO make sure deep-copy
+ *c = *cline;
+
+ // 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/parse.h b/parse.h
index 6ab98bfd..d8cabfb3 100644
--- a/parse.h
+++ b/parse.h
@@ -33,8 +33,8 @@ enum part_context {
*/
struct parse_ctx {
std::string filename;
- std::stack<std::string> key_stack;
- std::stack<vcomponent> comp_stack;
+ std::stack<strbuf*> key_stack;
+ std::stack<vcomponent*> comp_stack;
/* Number for unfolded lines */
int line = 0;
diff --git a/strbuf.h b/strbuf.h
index 07580f63..bf109daf 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -4,6 +4,7 @@
#include <unistd.h>
#include <cstdlib>
#include <cstring>
+#include <string>
/*
* A high level string type which holds it's own length, how much
@@ -32,11 +33,11 @@ struct strbuf {
*/
void realloc (size_t len);
- bool operator==(strbuf& other) {
- return strncmp(this->mem, other.mem, this->len) == 0;
- }
- bool operator==(char* other)
- { strncmp(this->mem, other, this->len) == 0 ; }
+ bool operator==(strbuf& other)
+ { return strncmp(this->mem, other.mem, this->len) == 0; }
+
+ bool operator==(const char* other)
+ { return strncmp(this->mem, other, this->len) == 0 ; }
strbuf& operator=(strbuf* other);
diff --git a/trie.h b/trie.h
index d672125b..880716e8 100644
--- a/trie.h
+++ b/trie.h
@@ -36,25 +36,4 @@ struct trie {
template <class T>
std::ostream& operator<<(std::ostream&, trie<T>* trie);
-
-// INIT_F ( TRIE(TYPE) );
-
-// INIT_F (TRIE_NODE(TYPE), char c);
-
-// INIT_F (TRIE_NODE(TYPE),
-// char c, TRIE_NODE(TYPE)* next, TRIE_NODE(TYPE)* child );
-
-// int PUSH(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key, TYPE* val );
-
-// TYPE* GET(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key );
-
-// FREE_F(TRIE_NODE(TYPE));
-
-// FREE_F(TRIE(TYPE));
-
-// int EMPTY(TRIE(TYPE))(TRIE(TYPE)*);
-
-// FMT_F(TRIE_NODE(TYPE));
-// FMT_F(TRIE(TYPE));
-
#endif /* TRIE_H */
diff --git a/vcal.cpp b/vcal.cpp
index a104664d..8f49b7d8 100644
--- a/vcal.cpp
+++ b/vcal.cpp
@@ -1,14 +1,19 @@
#include "vcal.h"
#include "err.h"
+#include <iostream>
+
std::ostream& operator<<(std::ostream& o, vcomponent* self) {
for (int i = 0; i < 40; i++) o << '_';
o << _YELLOW << std::endl
<< "VComponet (Type := " << self->type << _RESET
- << self->clines
- << std::endl << "Components:" << std::endl
- << self->components;
+ // << self->clines
+ << std::endl << "Components:" << std::endl;
+ for (auto v : self->components) {
+ o << &v;
+ }
+ //<< self->components;
return o;
}
diff --git a/vcal.h b/vcal.h
index bb30e482..48ee9e3f 100644
--- a/vcal.h
+++ b/vcal.h
@@ -8,13 +8,14 @@
#include <iostream>
#include "trie.h"
+#include "strbuf.h"
+#include "linked_list.h"
+typedef std::pair<strbuf, llist<strbuf> > param_set;
+typedef std::pair<strbuf, llist<param_set> > content_set;
+typedef std::pair<strbuf, llist<content_set> > content_line;
-typedef std::pair<strbuf, std::list<strbuf> > param_set;
-typedef std::pair<strbuf, std::list<param_set> > content_set;
-typedef std::pair<strbuf, std::list<content_set> > content_line;
-
-#if 0
+#if 1
/*
* Helper macros for accessing fields in
* content_line, content_set, and param_set