diff options
-rw-r--r-- | calendar.cpp | 4 | ||||
-rw-r--r-- | hash.c | 15 | ||||
-rw-r--r-- | linked_list.cpp | 2 | ||||
-rw-r--r-- | linked_list.h | 3 | ||||
-rw-r--r-- | main.cpp (renamed from main.c) | 46 | ||||
-rw-r--r-- | parse.cpp | 33 | ||||
-rw-r--r-- | parse.h | 6 | ||||
-rw-r--r-- | strbuf.cpp | 6 | ||||
-rw-r--r-- | strbuf.h | 20 | ||||
-rw-r--r-- | trie.cpp | 2 | ||||
-rw-r--r-- | trie.h | 9 | ||||
-rw-r--r-- | vcal.cpp | 7 | ||||
-rw-r--r-- | vcal.h | 70 |
13 files changed, 104 insertions, 119 deletions
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 <cstring> + #include <sys/stat.h> #include <dirent.h> -#include <string.h> -#include <stdio.h> #include <errno.h> #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<T>::operator+= (llist<T>& 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 <typename T> struct llist { llink<T>* head; llink<T>* tail; - llink<T>* cur; + llink<T>* __cur; + T* cur() { return __cur->value; } int length; llist (); @@ -1,12 +1,12 @@ +#include <cstring> + #include <errno.h> -#include <stdio.h> -#include <string.h> +// #include <stdio.h> #include <assert.h> #include "calendar.h" -#include "macro.h" #include "vcal.h" -#include "graphs.h" +// #include "graphs.h" #include "err.h" typedef struct { @@ -31,37 +31,40 @@ int main (int argc, char** argv) { } char* rootpath = args.argv[0]; - SNEW(vcomponent, root, "ROOT", rootpath); + 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", - root.components.length); + static_cast<int>(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.length; i++) { - vcomponent* cal = GET(VECT(vcomponent))(&root.components, i); - assert(strcmp(cal->type, "VCALENDAR") == 0); + for (size_t i = 0; i < root.components.size(); i++) { + vcomponent* cal = &root.components[i]; + assert(cal->type == "VCALENDAR"); - char* filename = cal->filename; + std::string 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); + 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); } } - } else if (strcmp(args.argv[0], "-g") == 0) { + } +#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++) { @@ -90,6 +93,5 @@ int main (int argc, char** argv) { FMT(vcomponent)(&root, buf); puts(buf); */ - - FREE(vcomponent)(&root); +#endif } @@ -1,7 +1,8 @@ #include "parse.h" +#include <cstring> + #include <errno.h> -#include <string.h> #include <assert.h> #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<strbuf>* ls = & CLINE_CUR_PARAMS(&cline)->cur->value->second; - ls->push(s); + // llist<strbuf>* 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, @@ -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. @@ -2,6 +2,12 @@ #include <cstdlib> +strbuf::strbuf (const strbuf& other) { + this->alloc = other.len + 1; + this->mem = static_cast<char*>(malloc(this->alloc)); + strncpy(this->mem, other.mem, other.len); +} + void strbuf::realloc (size_t len) { this->mem = static_cast<char*>(std::realloc(this->mem, len)); this->alloc = len; @@ -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<char*>(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 */ @@ -68,7 +68,7 @@ int trie<T>::push_back (const char* key, const T& item) { } template <class T> -T& trie<T>::operator[] (char* key) { +T& trie<T>::operator[] (const char* key) { trie_node<T>* n = this->root->child; char* subkey = key; @@ -7,13 +7,11 @@ template <typename T> struct trie_node { char c; T* value = NULL; - trie_node<T>* next = NULL; - trie_node<T>* child = NULL; + trie_node<T>* next = nullptr; + trie_node<T>* child = nullptr; trie_node (char c); trie_node (char c, trie_node<T>* next, trie_node<T>* child); - - // ~trie_node (); }; template <class T> @@ -24,11 +22,10 @@ struct trie { trie_node<T>* 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; } }; @@ -3,6 +3,13 @@ #include <iostream> +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 << '_'; @@ -11,9 +11,41 @@ #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, llist<strbuf> > param_set; +// typedef std::pair<strbuf, llist<param_set> > content_set; +// typedef std::pair<strbuf, llist<strbuf> > content_line; +typedef llist<strbuf> content_line; + +struct vcomponent { + std::string filename; + std::string type; + vcomponent* parent = nullptr; + trie<content_line> clines; + std::vector<vcomponent> 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<strbuf, llist<content_set> > 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<content_line> clines; - std::vector<vcomponent> 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 */ |