From 6f3c772939463c97a4a8a8371db42b8f4b181c68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 19 Feb 2019 02:31:35 +0100 Subject: No idea, to tired. --- calendar.cpp | 4 +-- hash.c | 15 --------- linked_list.cpp | 2 -- linked_list.h | 3 +- main.c | 95 ------------------------------------------------------- main.cpp | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ parse.cpp | 33 ++++++++++---------- parse.h | 6 ---- strbuf.cpp | 6 ++++ strbuf.h | 20 +++++------- trie.cpp | 2 +- trie.h | 9 ++---- vcal.cpp | 7 +++++ vcal.h | 70 ++++++++++++++++++++--------------------- 14 files changed, 177 insertions(+), 192 deletions(-) delete mode 100644 hash.c delete mode 100644 main.c create mode 100644 main.cpp diff --git a/calendar.cpp b/calendar.cpp index d0476583..c13c033f 100644 --- a/calendar.cpp +++ b/calendar.cpp @@ -1,9 +1,9 @@ #include "calendar.h" +#include + #include #include -#include -#include #include #include "parse.h" diff --git a/hash.c b/hash.c deleted file mode 100644 index 47775b59..00000000 --- a/hash.c +++ /dev/null @@ -1,15 +0,0 @@ -#include "hash.h" - -/* - * http://www.cse.yorku.ca/~oz/hash.html - * djb2 from above url. - */ -unsigned long hash(char* str) { - unsigned long hash = 5381; - int c; - - while ( (c = *str++) ) - hash = ((hash << 5) + hash) + c; - - return hash; -} diff --git a/linked_list.cpp b/linked_list.cpp index d4bd5a91..b30458c9 100644 --- a/linked_list.cpp +++ b/linked_list.cpp @@ -66,8 +66,6 @@ void llist::operator+= (llist& 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; diff --git a/linked_list.h b/linked_list.h index 3eab5549..a27d82a1 100644 --- a/linked_list.h +++ b/linked_list.h @@ -21,7 +21,8 @@ template struct llist { llink* head; llink* tail; - llink* cur; + llink* __cur; + T* cur() { return __cur->value; } int length; llist (); diff --git a/main.c b/main.c deleted file mode 100644 index 830eb538..00000000 --- a/main.c +++ /dev/null @@ -1,95 +0,0 @@ -#include -#include -#include -#include - -#include "calendar.h" -#include "macro.h" -#include "vcal.h" -#include "graphs.h" -#include "err.h" - -typedef struct { - int argc; - char** argv; -} arg; - -int arg_shift (arg* a) { - if (a->argc == 0) return 0; - - ++a->argv; - return --a->argc; - -} - -int main (int argc, char** argv) { - arg args = { .argc = argc, .argv = argv }; - - if (arg_shift(&args) == 0) { - ERR("Please give vdir or a vcalendar file as first argument"); - exit (1); - } - - char* rootpath = args.argv[0]; - SNEW(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); - - 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); - assert(strcmp(cal->type, "VCALENDAR") == 0); - - 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); - - if (strcmp(ev->type, "VEVENT") != 0) continue; - - printf("%3lu : %3lu | %s | %s\n", - i + 1, j + 1, - filename, - get_property(ev, "SUMMARY")->val.cur->value->key.mem); - } - } - } else if (strcmp(args.argv[0], "-g") == 0) { - /* 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); - assert(strcmp(cal->type, "VCALENDAR") == 0); - - vcomponent* ev = FCHILD(cal); - - char target[0xFF]; - target[0] = '\0'; - strcat(target, "/tmp/dot/"); - strcat(target, ev->filename); - strcat(target, ".dot"); - // create_graph(ev, target); - } - } else { - // 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]); - } - } - - /* - char buf[0x20000]; - FMT(vcomponent)(&root, buf); - puts(buf); - */ - - FREE(vcomponent)(&root); -} diff --git a/main.cpp b/main.cpp new file mode 100644 index 00000000..1e2c960e --- /dev/null +++ b/main.cpp @@ -0,0 +1,97 @@ +#include + +#include +// #include +#include + +#include "calendar.h" +#include "vcal.h" +// #include "graphs.h" +#include "err.h" + +typedef struct { + int argc; + char** argv; +} arg; + +int arg_shift (arg* a) { + if (a->argc == 0) return 0; + + ++a->argv; + return --a->argc; + +} + +int main (int argc, char** argv) { + arg args = { .argc = argc, .argv = argv }; + + if (arg_shift(&args) == 0) { + ERR("Please give vdir or a vcalendar file as first argument"); + exit (1); + } + + char* rootpath = args.argv[0]; + vcomponent root("ROOT", rootpath); + // SNEW(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", + static_cast(root.components.size())); + + puts("CAL : OBJ | Filename | Description"); + puts("----------+----------+------------"); + + /* This loops over all VCALENDAR's in root */ + for (size_t i = 0; i < root.components.size(); i++) { + vcomponent* cal = &root.components[i]; + assert(cal->type == "VCALENDAR"); + + std::string filename = cal->filename; + /* This loop over all VEVENT's in the current VCALENDAR */ + for (size_t j = 0; j < cal->components.size(); j++) { + vcomponent* ev = &cal->components[j]; + + if (ev->type == "VEVENT") continue; + std::cout << i + 1 << " " << j + 1 << std::endl; + // printf("%3lu : %3lu | %s | %s\n", + // i + 1, j + 1, + // filename, + // ev["SUMMARY"]->val.cur->value->key.mem); + } + } + } +#if 0 + else if (strcmp(args.argv[0], "-g") == 0) { + /* 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); + assert(strcmp(cal->type, "VCALENDAR") == 0); + + vcomponent* ev = FCHILD(cal); + + char target[0xFF]; + target[0] = '\0'; + strcat(target, "/tmp/dot/"); + strcat(target, ev->filename); + strcat(target, ".dot"); + // create_graph(ev, target); + } + } else { + // 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]); + } + } + + /* + char buf[0x20000]; + FMT(vcomponent)(&root, buf); + puts(buf); + */ +#endif +} diff --git a/parse.cpp b/parse.cpp index 457f1663..f7e42d77 100644 --- a/parse.cpp +++ b/parse.cpp @@ -1,7 +1,8 @@ #include "parse.h" +#include + #include -#include #include #include "vcal.h" @@ -12,11 +13,11 @@ /* * name *(";" param) ":" value CRLF */ -int parse_file(char* filename, FILE* f, vcomponent& root) { +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; @@ -118,25 +119,22 @@ int parse_file(char* filename, FILE* f, vcomponent& root) { if (p_ctx == p_param_value) { /* push kv pair */ - auto s = new strbuf; - // TODO make sure this is a deep copy - *s = ctx.str; - + auto s = new strbuf(ctx.str);; s->cap(); ctx.str.soft_reset(); - llist* ls = & CLINE_CUR_PARAMS(&cline)->cur->value->second; - ls->push(s); + // llist* ls = & CLINE_CUR_PARAMS(&cline)->cur->value->second; + cline.push(s); } if (p_ctx == p_key) { - cline.first = ctx.str; - cline.first.cap(); + // cline.first = ctx.str; + // cline.first.cap(); ctx.str.soft_reset(); - content_set* p = new content_set; - cline.second.push(p); + // content_set* p = new content_set; + // cline.second.push(p); } if (c == ':') p_ctx = p_value; @@ -161,7 +159,8 @@ int parse_file(char* filename, FILE* f, vcomponent& root) { * the end here. */ - strbuf* target = CLINE_CUR_VAL(&cline); + //strbuf* target = CLINE_CUR_VAL(&cline); + strbuf* target = cline.second.cur(); *target = ctx.str; target->cap(); ctx.str.soft_reset(); @@ -192,21 +191,21 @@ int handle_kv ( */ strbuf* s = new strbuf; - strbuf* type = CLINE_CUR_VAL(cline); + strbuf* type = cline->second.cur(); *s = *type; ctx->key_stack.push(s); // TODO ompty cline->second here; // RESET(LLIST(content_set))(&cline->val); - auto e = new vcomponent(s->mem, ctx->filename); + auto e = new vcomponent(s->to_string(), ctx->filename); e->parent = ctx->comp_stack.top(); ctx->comp_stack.push(e); } 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 (s == cline->second.cur()) { #if 0 ERR_P(ctx, "Expected END:%s, got END:%s.\n%s line", s->mem, diff --git a/parse.h b/parse.h index d8cabfb3..03858a15 100644 --- a/parse.h +++ b/parse.h @@ -9,12 +9,6 @@ #include "strbuf.h" #include "vcal.h" -#if 0 - -#define TYPE vcomponent -#include "linked_list.h" -#undef TYPE -#endif /* * The standard says that no line should be longer than 75 octets. diff --git a/strbuf.cpp b/strbuf.cpp index 3864b271..7b63c58d 100644 --- a/strbuf.cpp +++ b/strbuf.cpp @@ -2,6 +2,12 @@ #include +strbuf::strbuf (const strbuf& other) { + this->alloc = other.len + 1; + this->mem = static_cast(malloc(this->alloc)); + strncpy(this->mem, other.mem, other.len); +} + void strbuf::realloc (size_t len) { this->mem = static_cast(std::realloc(this->mem, len)); this->alloc = len; diff --git a/strbuf.h b/strbuf.h index bf109daf..04457c0d 100644 --- a/strbuf.h +++ b/strbuf.h @@ -22,6 +22,9 @@ struct strbuf { size_t len = 0; strbuf () : strbuf (1) { }; + + strbuf (const strbuf& other); + strbuf (size_t alloc) : alloc(alloc) , mem(static_cast(malloc(alloc))) { }; @@ -61,10 +64,13 @@ struct strbuf { { return this->mem[this->len]; } /* Resets the seek for strbuf to 0. */ - void reset(); + void reset() + { this->ptr = 0; } /* Sets the length and seek ptr to 0, but doesn't touch the memmory. */ - void soft_reset(); + void soft_reset() { + this->ptr = 0; this->len = 0; + }; std::string to_string() { return std::string (this->mem); @@ -81,14 +87,4 @@ struct strbuf { * Copies contents from src to dest, also allocating dest in the * process. dest should not be initialized before self call. */ -#if 0 -int strbuf_init_copy(strbuf* dest, strbuf* src); - -strbuf* RESOLVE(strbuf)(strbuf*, strbuf*); - -FMT_F(strbuf); - -int SIZE(strbuf)(strbuf*); -#endif - #endif /* STRBUF_H */ diff --git a/trie.cpp b/trie.cpp index 729277bd..a3446a97 100644 --- a/trie.cpp +++ b/trie.cpp @@ -68,7 +68,7 @@ int trie::push_back (const char* key, const T& item) { } template -T& trie::operator[] (char* key) { +T& trie::operator[] (const char* key) { trie_node* n = this->root->child; char* subkey = key; diff --git a/trie.h b/trie.h index 880716e8..07c1bce3 100644 --- a/trie.h +++ b/trie.h @@ -7,13 +7,11 @@ template struct trie_node { char c; T* value = NULL; - trie_node* next = NULL; - trie_node* child = NULL; + trie_node* next = nullptr; + trie_node* child = nullptr; trie_node (char c); trie_node (char c, trie_node* next, trie_node* child); - - // ~trie_node (); }; template @@ -24,11 +22,10 @@ struct trie { trie_node* root; trie (); - // ~trie (); int push_back (const char* key, const T&); - T& operator[] ( char* key ); + T& operator[] ( const char* key ); bool empty () { return this->root->child == NULL; } }; diff --git a/vcal.cpp b/vcal.cpp index 8f49b7d8..8180cf80 100644 --- a/vcal.cpp +++ b/vcal.cpp @@ -3,6 +3,13 @@ #include +vcomponent::vcomponent( + const std::string& type, + const std::string& filename) + : type(type) + , filename(filename) +{ }; + std::ostream& operator<<(std::ostream& o, vcomponent* self) { for (int i = 0; i < 40; i++) o << '_'; diff --git a/vcal.h b/vcal.h index 48ee9e3f..a4c7252c 100644 --- a/vcal.h +++ b/vcal.h @@ -11,9 +11,41 @@ #include "strbuf.h" #include "linked_list.h" -typedef std::pair > param_set; -typedef std::pair > content_set; -typedef std::pair > content_line; +// typedef std::pair > param_set; +// typedef std::pair > content_set; +// typedef std::pair > content_line; +typedef llist content_line; + +struct vcomponent { + std::string filename; + std::string type; + vcomponent* parent = nullptr; + trie clines; + std::vector components; + + vcomponent(const std::string& type) : vcomponent(type, nullptr) { }; + + vcomponent(const std::string& type, const std::string& filename); + + + /* + * 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. + */ + vcomponent* operator= (vcomponent* other); + + content_line& operator[] (const char* key) { + return this->clines[key]; + } + + void push_back(const vcomponent& child) + { this->components.push_back(child); } +}; + +std::ostream& operator<<(std::ostream&, vcomponent*); + #if 1 /* @@ -41,36 +73,4 @@ typedef std::pair > content_line; #define CLINE_CUR_PARAM_VAL(c) (CLINE_CUR_PARAMS(c)->cur->value->second.cur->value) #endif - struct vcomponent { - std::string filename; - std::string type; - vcomponent* parent = nullptr; - trie clines; - std::vector components; - - vcomponent(const std::string& type) : vcomponent(type, nullptr) { }; - vcomponent(const std::string& type, const std::string& filename) - : type(type) , filename(filename) { }; - - ~vcomponent(); - - -/* - * 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. - */ - vcomponent* operator= (vcomponent* other); - - content_line& operator[] (char* key) { - return this->clines[key]; - } - - void push_back(const vcomponent& child) - { this->components.push_back(child); } -}; - -std::ostream& operator<<(std::ostream&, vcomponent*); - #endif /* VCAL_H */ -- cgit v1.2.3