diff options
author | Hugo Hörnquist <hugo@hornquist.se> | 2019-02-22 14:39:34 +0100 |
---|---|---|
committer | Hugo Hörnquist <hugo@hornquist.se> | 2019-02-22 14:39:34 +0100 |
commit | a608b90f8c146430803871e20d043b60a278248b (patch) | |
tree | 194e6e80cd5403cc64d1e4915f9be45a462d3918 | |
parent | strbuf (diff) | |
download | calp-a608b90f8c146430803871e20d043b60a278248b.tar.gz calp-a608b90f8c146430803871e20d043b60a278248b.tar.xz |
rest
-rw-r--r-- | macro.h | 7 | ||||
-rw-r--r-- | main.c | 19 | ||||
-rw-r--r-- | pair.h | 46 | ||||
-rw-r--r-- | parse.c | 51 | ||||
-rw-r--r-- | parse.h | 13 | ||||
-rw-r--r-- | trie.c.inc | 9 | ||||
-rw-r--r-- | trie.h | 4 | ||||
-rw-r--r-- | vcal.c | 102 | ||||
-rw-r--r-- | vcal.h | 92 | ||||
-rw-r--r-- | vector.h | 45 | ||||
-rw-r--r-- | vector.inc.h | 68 |
11 files changed, 208 insertions, 248 deletions
@@ -9,6 +9,7 @@ #define TP4(a, b, c, d) a ## b ## c ## d #define TP5(a, b, c, d, e) a ## b ## c ## d ## e #define TP6(a, b, c, d, e, f) a ## b ## c ## d ## e ## f +#define TP7(a, b, c, d, e, f, g) a ## b ## c ## d ## e ## f ## g /* * Get length of __VA_ARGS__ @@ -31,10 +32,11 @@ * * nameᐸTᐳ */ -#define TEMPL(name, T) TP4(name, \U00001438 , T, \U00001433 ) -#define TEMPL2(name, T, V) TP6(name, \U00001438\U00001438 , T , \U00001433_\U00001438 , V, \U00001433\U00001433) +#define TEMPL(name, T) TP5(DEP_, name, \U00001438 , T, \U00001433 ) +#define TEMPL2(name, T, V) TP7(DEP_, name, \U00001438\U00001438 , T , \U00001433_\U00001438 , V, \U00001433\U00001433) #define TEMPL_N(name, T, argcount) TP6(name, \U00001438 , T, _, argcount, \U00001433 ) +#if 1 /* Constructor type name */ #define __INIT_T(T, C) TEMPL_N(init, T, C) @@ -77,6 +79,7 @@ /* Declare destructor */ #define FREE_F(T) int FREE(T) (T* self) +#endif /* generate reusable internal symbol */ #define __INTER(s) TP3(__, s, __internal) @@ -31,28 +31,33 @@ int main (int argc, char** argv) { } char* rootpath = args.argv[0]; - SNEW(vcomponent, root, "ROOT", rootpath); + // SNEW(vcomponent, root, "ROOT", rootpath); + vcomponent root("ROOT", rootpath); read_vcalendar(&root, rootpath); arg_shift(&args); if (args.argc == 0 || strcmp(args.argv[0], "-p") == 0) { - INFO_F("Parsed calendar file containing [%u] events", - root.components.length); + // INFO_F("Parsed calendar file containing [%u] events", + // root.components.length); + printf("component %s, [%u] children\n", root.type, root.components.length); puts("CAL : OBJ | Filename | Description"); puts("----------+----------+------------"); /* This loops over all VCALENDAR's in root */ for (size_t i = 0; i < root.components.length; i++) { - vcomponent* cal = GET(VECT(vcomponent))(&root.components, i); + // vcomponent* cal = GET(VECT(vcomponent))(&root.components, i); + vcomponent* cal = root.components[i]; assert(strcmp(cal->type, "VCALENDAR") == 0); + printf("component %s, [%u] children\n", cal->type, cal->components.length); char* filename = cal->filename; /* This loop over all VEVENT's in the current VCALENDAR */ for (size_t j = 0; j < cal->components.length; j++) { - vcomponent* ev = GET(VECT(vcomponent))(&cal->components, j); + vcomponent* ev = cal->components[j]; + printf("component %s, [%u] children\n", ev->type, ev->components.length); if (strcmp(ev->type, "VEVENT") != 0) continue; printf("%3lu : %3lu | %s | %s\n", @@ -65,7 +70,7 @@ int main (int argc, char** argv) { /* TODO self might be broken */ if (arg_shift(&args) == 0) { for (size_t i = 0; i < root.components.length; i++) { - vcomponent* cal = GET(VECT(vcomponent))(&root.components, i); + vcomponent* cal = root.components[i]; assert(strcmp(cal->type, "VCALENDAR") == 0); vcomponent* ev = FCHILD(cal); @@ -91,5 +96,5 @@ int main (int argc, char** argv) { puts(buf); */ - FREE(vcomponent)(&root); + // FREE(vcomponent)(&root); } @@ -10,8 +10,52 @@ template<class T, class V> struct pair { T* key; V* val; - pair () { } + pair () { + key = new T; + val = new V; + } pair (pair<T,V>& other); + + T& operator= (const T& other) { + delete this->key; + delete this->val; + + *this->key = *other.key; + *this->val = *other.val; + + return this; + } + + pair* resolve (pair* other) { + if (this == NULL) return other; + return this; +#if 0 +/* + * Resolves a collision in some form of structure (probably a hash-map + * or a trie). If dest is NULL just return new_. Otherwise mutates dest + * to have the correct form, and returns it. Destroying new_ in the + * process. + */ + if (dest == NULL) return new_; + + 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); + dest->val->append(new_->val); + + // FREE(strbuf)(&new_->key); + // delete new_->key; + delete new_; + // free(new_); + + return dest; +#endif + } + }; #if 0 @@ -26,7 +26,7 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { part_context p_ctx = p_key; - SNEW(parse_ctx, ctx, filename); + parse_ctx ctx(filename); // PUSH(LLIST(vcomponent))(&ctx.comp_stack, root); ctx.comp_stack.push(root); @@ -44,7 +44,8 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { strbuf* target = CLINE_CUR_VAL(&cline); - DEEP_COPY(strbuf)(target, &ctx.str); + // DEEP_COPY(strbuf)(target, &ctx.str); + *target = ctx.str; strbuf_cap(target); strbuf_soft_reset(&ctx.str); @@ -169,7 +170,7 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { */ strbuf* target = CLINE_CUR_VAL(&cline); - DEEP_COPY(strbuf)(target, &ctx.str); + *target = ctx.str; strbuf_cap(target); strbuf_soft_reset(&ctx.str); @@ -216,9 +217,7 @@ int handle_kv ( // RESET(LLIST(content_set))(&cline->val); cline->val->reset(); - NEW(vcomponent, e, - s->mem, - ctx->filename); + auto e = new vcomponent(s->mem, ctx->filename); // e->parent = PEEK(LLIST(vcomponent))(&ctx->comp_stack); e->parent = ctx->comp_stack.peek(); //PUSH(LLIST(vcomponent))(&ctx->comp_stack, e); @@ -298,32 +297,30 @@ 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); - self->filename = (char*) calloc(sizeof(*filename), strlen(filename) + 1); - strcpy(self->filename, filename); +//INIT_F(parse_ctx, char* filename) { +parse_ctx::parse_ctx (const char* filename) { + // INIT(LLIST(strbuf), &this->key_stack); + // INIT(LLIST(vcomponent), &this->comp_stack); + this->filename = (char*) calloc(sizeof(*filename), strlen(filename) + 1); + strcpy(this->filename, filename); - self->line = 0; - self->column = 0; + this->line = 0; + this->column = 0; - self->pline = 1; - self->pcolumn = 1; + this->pline = 1; + this->pcolumn = 1; - // INIT(strbuf, &self->str); - - return 0; + // INIT(strbuf, &this->str); } -FREE_F(parse_ctx) { +// FREE_F(parse_ctx) { +parse_ctx::~parse_ctx () { - // FREE(LLIST(strbuf))(&self->key_stack); - // FREE(LLIST(vcomponent))(&self->comp_stack); - free(self->filename); + // FREE(LLIST(strbuf))(&this->key_stack); + // FREE(LLIST(vcomponent))(&this->comp_stack); + free(this->filename); - self->line = 0; - self->column = 0; - // FREE(strbuf)(&self->str); - - return 0; + this->line = 0; + this->column = 0; + // FREE(strbuf)(&this->str); } @@ -7,9 +7,9 @@ #include "strbuf.h" #include "vcal.h" -#define TYPE vcomponent +// #define TYPE vcomponent #include "linked_list.h" -#undef TYPE +// #undef TYPE /* * The standard says that no line should be longer than 75 octets. @@ -26,7 +26,7 @@ typedef enum { * Struct holding most state information needed while parsing. * Kept together for simplicity. */ -typedef struct { +struct parse_ctx { char* filename; llist<strbuf> key_stack; llist<vcomponent> comp_stack; @@ -40,10 +40,11 @@ typedef struct { int pcolumn; strbuf str; -} parse_ctx; -INIT_F(parse_ctx, char* filename); -FREE_F(parse_ctx); + parse_ctx (const char* filename); + + ~parse_ctx (); +}; int handle_kv( content_line* cline, @@ -59,7 +59,8 @@ int trie<T>::push (char* key, T* val) { last->child = t; last = t; } - last->value = RESOLVE(TYPE)(last->value, val); + // last->value = RESOLVE(TYPE)(last->value, val); + last->value = last->value->resolve(val); return 0; } else if (cur->c == subkey[0]) { /* This node belongs to the key, @@ -69,7 +70,8 @@ int trie<T>::push (char* key, T* val) { subkey++; } else if (subkey[0] == '\0') { /* Key finished */ - last->value = RESOLVE(TYPE)(last->value, val); + // last->value = RESOLVE(TYPE)(last->value, val); + last->value->resolve(val); return 0; } else if (cur->next != NULL) { /* This node was not part of the set, but it's sibling might */ @@ -132,7 +134,8 @@ trie<T>::~trie () { // return 1; return; // error } - free (this->root); + // free (this->root); + delete this->root; } template <class T> @@ -38,8 +38,8 @@ template<class T> struct trie { // FMT_F(TRIE_NODE(TYPE)); // FMT_F(TRIE(TYPE)); -extern template struct trie<content_line>; -extern template struct trie_node<content_line>; +// extern template struct trie<content_line>; +// extern template struct trie_node<content_line>; #include "trie.c.inc" @@ -2,90 +2,22 @@ #include <string.h> -// #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 +// INIT_F(vcomponent, const char* type, const char* filename) { +vcomponent::vcomponent (const char* type, const char* filename) { -#if 0 -#define T strbuf - #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 -#endif - -// #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) self; - ERR("Do not use"); - return 0; -} - -INIT_F(vcomponent, const char* type) { - return INIT(vcomponent, self, type, NULL); -} - -INIT_F(vcomponent, const char* type, const char* filename) { - - // INIT(TRIE(content_line), &self->clines); - INIT(VECT(vcomponent), &self->components); + // INIT(TRIE(content_line), &this->clines); + // INIT(VECT(vcomponent), &this->components); - self->filename = NULL; + this->filename = NULL; if (filename != NULL) { - self->filename = (char*) calloc(sizeof(*filename), strlen(filename) + 1); - strcpy(self->filename, filename); + this->filename = (char*) calloc(sizeof(*filename), strlen(filename) + 1); + strcpy(this->filename, filename); } - self->type = (char*) calloc(sizeof(*type), strlen(type) + 1); - strcpy(self->type, type); + this->type = (char*) calloc(sizeof(*type), strlen(type) + 1); + strcpy(this->type, type); - self->parent = NULL; - - return 0; -} - -content_line* RESOLVE(content_line) - (content_line* dest, content_line* new_) -{ - if (dest == NULL) return new_; - - 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); - dest->val->append(new_->val); - - // FREE(strbuf)(&new_->key); - // delete new_->key; - delete new_; - // free(new_); - - return dest; + this->parent = NULL; } content_line* get_property (vcomponent* ev, const char* key) { @@ -100,22 +32,22 @@ content_line* get_property (vcomponent* ev, const char* key) { return ret; } -FREE_F(vcomponent) { - if (self->filename != NULL) free(self->filename); - free(self->type); +// FREE_F(vcomponent) { +vcomponent::~vcomponent () { + if (this->filename != NULL) free(this->filename); + free(this->type); // 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); - - return 0; + // FREE(VECT(vcomponent))(&self->components); } int PUSH(vcomponent)(vcomponent* parent, vcomponent* child) { - return PUSH(VECT(vcomponent))(&parent->components, child); + // return PUSH(VECT(vcomponent))(&parent->components, child); + return parent->components.push(child); } int DEEP_COPY(vcomponent)(vcomponent* a, vcomponent* b) { @@ -4,61 +4,14 @@ #include <stdlib.h> #include "strbuf.h" - -//#define TYPE strbuf -#include "linked_list.h" -// #include "trie.h" -//#undef TYPE - -// #include <utility> - -#if 0 -#define T strbuf - #define V LLIST(strbuf) - #include "pair.h" - /* left := param_name | right := param_values */ - #define param_set PAIR(strbuf, LLIST(strbuf)) - #undef V -#undef T - -#define TYPE param_set #include "linked_list.h" -#undef TYPE - -#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 +#include "trie.h" -#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 -#endif - #include "pair.h" -typedef pair<strbuf, llist<strbuf> > param_set; - -#define TYPE param_set -#include "linked_list.h" -#undef TYPE - -typedef pair<strbuf, llist<param_set> > content_set; - -#define TYPE content_set -#include "linked_list.h" -#undef TYPE +typedef pair<strbuf, llist<strbuf> > param_set; +typedef pair<strbuf, llist<param_set> > content_set; typedef pair<strbuf, llist<content_set> > content_line; /* @@ -85,40 +38,33 @@ typedef pair<strbuf, llist<content_set> > content_line; /* 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 - * or a trie). If dest is NULL just return new_. Otherwise mutates dest - * to have the correct form, and returns it. Destroying new_ in the - * process. - */ -content_line* RESOLVE(content_line) - (content_line* dest, content_line* new_); +// typedef struct s_vcomponent vcomponent; -// #define TYPE content_line -#include "trie.h" -// #undef TYPE - -typedef struct s_vcomponent vcomponent; - -#define TYPE vcomponent +// #define TYPE vcomponent #include "vector.h" -#undef TYPE +// #undef TYPE -struct s_vcomponent { +struct vcomponent { char* filename; char* type; vcomponent* parent; // TRIE(content_line) clines; trie<content_line> clines; - VECT(vcomponent) components; + vect<vcomponent> components; + + vcomponent (const char* type) : vcomponent (type, NULL) { }; + vcomponent (const char* type, const char* filename); + + ~vcomponent (); }; -#define FCHILD(v) GET(VECT(vcomponent))(&(v)->components, 0) +// #define FCHILD(v) GET(VECT(vcomponent))(&(v)->components, 0) +#define FCHILD(v) ((v)->components[0]); -INIT_F(vcomponent); -INIT_F(vcomponent, const char* type); -INIT_F(vcomponent, const char* type, const char* filename); -FREE_F(vcomponent); +// INIT_F(vcomponent); +// INIT_F(vcomponent, const char* type); +// INIT_F(vcomponent, const char* type, const char* filename); +// FREE_F(vcomponent); content_line* get_property (vcomponent* ev, const char* key); @@ -1,28 +1,39 @@ -#ifndef VECTOR_H -#define VECTOR_H +#ifndef M_VECTOR_H +#define M_VECTOR_H #include <stdlib.h> #include "macro.h" -#define VECT(T) TEMPL(vect, T) +// #define VECT(T) TEMPL(vect, T) -#endif /* VECTOR_H */ +// #endif /* VECTOR_H */ -#ifdef TYPE +// #ifdef TYPE -typedef struct { +template <class T> +struct vect { unsigned int length; unsigned int alloc; - TYPE** items; -} VECT(TYPE); + T** items; -INIT_F(VECT(TYPE)); -FREE_F(VECT(TYPE)); + vect (); + ~vect (); -int PUSH(VECT(TYPE))(VECT(TYPE)*, TYPE*); -TYPE* GET(VECT(TYPE))(VECT(TYPE)*, unsigned int idx); -int EMPTY(VECT(TYPE))(VECT(TYPE)*); -unsigned int SIZE(VECT(TYPE))(VECT(TYPE)*); + int push (T* t); + + T* operator[] (unsigned int idx); + + int empty (); + unsigned int size (); +}; + +// INIT_F(VECT(TYPE)); +// FREE_F(VECT(TYPE)); +// +// int PUSH(VECT(TYPE))(VECT(TYPE)*, TYPE*); +// TYPE* GET(VECT(TYPE))(VECT(TYPE)*, unsigned int idx); +// int EMPTY(VECT(TYPE))(VECT(TYPE)*); +// unsigned int SIZE(VECT(TYPE))(VECT(TYPE)*); #define __PRE_VECT(T, i, set) \ unsigned int __INTER(i) = 0; T* i; @@ -37,4 +48,8 @@ unsigned int SIZE(VECT(TYPE))(VECT(TYPE)*); #define __NXT_VECT(T, i, set) i = GET(VECT(T))(set, ++__INTER(i)) #define NXT_VECT(T) __NXT_VECT -#endif /* TYPE */ +#define HEADER_ONLY +#include "vector.inc.h" +#undef HEADER_ONLY + +#endif /* M_VECTOR_H */ diff --git a/vector.inc.h b/vector.inc.h index 2b23eadc..76dd7ee4 100644 --- a/vector.inc.h +++ b/vector.inc.h @@ -1,51 +1,65 @@ -#ifndef TYPE -#error "Set TYPE before including self file" -#else +#ifndef HEADER_ONLY +#error "Only include this file from the appropriate header." +#endif +// #ifndef TYPE +// #error "Set TYPE before including this file" +// #else #include "macro.h" #include "err.h" -INIT_F(VECT(TYPE)) { - self->length = 0; - self->alloc = 1; - self->items = (TYPE**) calloc(sizeof(*self->items), self->alloc); - return 0; +// INIT_F(VECT(TYPE)) { +template <class T> +vect<T>::vect () { + this->length = 0; + this->alloc = 1; + this->items = (T**) calloc(sizeof(*this->items), this->alloc); } -FREE_F(VECT(TYPE)) { - for (unsigned int i = 0; i < self->length; i++) { - FFREE(TYPE, self->items[i]); +template <class T> +vect<T>::~vect() { +// FREE_F(VECT(TYPE)) { + for (unsigned int i = 0; i < this->length; i++) { + delete this->items[i]; + // FFREE(TYPE, this->items[i]); } - free(self->items); - return 0; + free(this->items); } -int PUSH(VECT(TYPE))(VECT(TYPE)* self, TYPE* t) { - if (self->length + 1 > self->alloc) { - self->alloc <<= 1; - self->items = (TYPE**) realloc(self->items, sizeof(*self->items) * self->alloc); +template <class T> +int vect<T>::push (T* t) { +// int PUSH(VECT(TYPE))(VECT(TYPE)* this, TYPE* t) { + if (this->length + 1 > this->alloc) { + this->alloc <<= 1; + this->items = (T**) realloc(this->items, sizeof(*this->items) * this->alloc); } - self->items[self->length] = t; - ++self->length; + this->items[this->length] = t; + ++this->length; return 0; } -TYPE* GET(VECT(TYPE))(VECT(TYPE)* self, unsigned int idx) { - if (idx >= self->length) { +template <class T> +T* vect<T>::operator[] (unsigned int idx) { +// TYPE* GET(VECT(TYPE))(VECT(TYPE)* this, unsigned int idx) { + if (idx >= this->length) { ERR("Index out of range"); return NULL; } - return self->items[idx]; + return this->items[idx]; } -int EMPTY(VECT(TYPE))(VECT(TYPE)* self) { - return self->length == 0; +template <class T> +int vect<T>::empty () { +//int EMPTY(VECT(TYPE))(VECT(TYPE)* this) { + return this->length == 0; } -unsigned int SIZE(VECT(TYPE))(VECT(TYPE)* self) { - return self->length; +template <class T> +unsigned int vect<T>::size () { +//unsigned int SIZE(VECT(TYPE))(VECT(TYPE)* this) { + return this->length; } -#endif /* TYPE */ +// #endif /* TYPE */ |