diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2019-02-23 12:23:39 +0100 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2019-02-23 12:23:39 +0100 |
commit | a56dedbe9807286928fc56fbb489fa637b4b7e80 (patch) | |
tree | 02fcc6104cea8e71114eb00d5c36ada77d281194 | |
parent | Something. (diff) | |
download | calp-a56dedbe9807286928fc56fbb489fa637b4b7e80.tar.gz calp-a56dedbe9807286928fc56fbb489fa637b4b7e80.tar.xz |
Even more work.
-rw-r--r-- | main.c | 3 | ||||
-rw-r--r-- | parse.c | 129 | ||||
-rw-r--r-- | parse.h | 3 | ||||
-rw-r--r-- | strbuf.c | 8 | ||||
-rw-r--r-- | strbuf.h | 7 | ||||
-rw-r--r-- | vcal.c | 10 | ||||
-rw-r--r-- | vcal.h | 46 |
7 files changed, 114 insertions, 92 deletions
@@ -61,10 +61,11 @@ int main (int argc, char** argv) { if (strcmp(ev->type, "VEVENT") != 0) continue; content_line* cl = get_property(ev, "SUMMARY"); + strbuf* s = cl->data.cur->value->key; printf("%3lu : %3lu | %s | %s\n", i + 1, j + 1, filename, - cl->val->cur->value->key->mem); + s->mem); } } } else if (strcmp(args.argv[0], "-g") == 0) { @@ -8,17 +8,7 @@ #include "vcal.h" #include "err.h" - -// #define TYPE vcomponent -// #include "linked_list.inc.h" -// #undef TYPE - -// #define T strbuf -// #define V strbuf #include "pair.h" -// #include "pair.inc.h" -// #undef T -// #undef V /* * name *(";" param) ":" value CRLF @@ -27,11 +17,10 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { part_context p_ctx = p_key; parse_ctx ctx(filename); - // PUSH(LLIST(vcomponent))(&ctx.comp_stack, root); ctx.comp_stack.push(root); - content_line cline; - // SNEW(content_line, cline); + strbuf key; + param_set* ps; char c; while ( (c = fgetc(f)) != EOF) { @@ -42,15 +31,10 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { if (fold(f, &ctx, c) > 0) { /* Actuall end of line, handle value */ - strbuf* target = CLINE_CUR_VAL(&cline); - - // DEEP_COPY(strbuf)(target, &ctx.str); - *target = ctx.str; - strbuf_cap(target); + strbuf_cap(&ctx.str); + handle_kv(&key, &ctx); strbuf_soft_reset(&ctx.str); - handle_kv(&cline, &ctx); - p_ctx = p_key; } /* Else continue on current line */ @@ -97,16 +81,11 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { /* Border between param {key, value} */ } else if (p_ctx == p_param_name && c == '=') { - auto params = CLINE_CUR_PARAMS(&cline); + strbuf_cap(&ctx.str); - // NEW(param_set, ps); - auto ps = new param_set; - // DEEP_COPY(strbuf)(&ps->key, &ctx.str); + ps = new param_set; *ps->key = ctx.str; - strbuf_cap(ps->key); strbuf_soft_reset(&ctx.str); - // PUSH(LLIST(param_set))(params, ps); - params->push(ps); p_ctx = p_param_value; @@ -118,39 +97,29 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { * 4) ,, param, ,, value */ } else if ((p_ctx == p_key || p_ctx == p_param_value) && (c == ':' || c == ';')) { + /* We have the end of the initial key, or the end of a + * parameter value */ + strbuf_cap(&ctx.str); if (p_ctx == p_param_value) { /* push kv pair */ - - // NEW(strbuf, s); - auto s = new strbuf; - *s = ctx.str; - - // DEEP_COPY(strbuf)(s, &ctx.str); - strbuf_cap(s); + ps->val->push(new strbuf(ctx.str)); strbuf_soft_reset(&ctx.str); - CLINE_CUR_PARAMS(&cline)->cur->value->val->push(s); - // PUSH(LLIST(strbuf))(ls, s); - } if (p_ctx == p_key) { - // DEEP_COPY(strbuf)(&cline.key, &ctx.str); - *cline.key = ctx.str; - strbuf_cap(cline.key); + strbuf_cap(&ctx.str); + key = ctx.str; strbuf_soft_reset(&ctx.str); - - // NEW(content_set, p); - auto p = new content_set; - // PUSH(LLIST(content_set))(&cline.val, p); - cline.val->push(p); } if (c == ':') p_ctx = p_value; else if (c == ';') p_ctx = p_param_name; } else { + /* Regular character, append it to the current read + * buffer */ strbuf_append(&ctx.str, c); ++ctx.column; @@ -169,15 +138,18 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { * the end here. */ + /* strbuf* target = CLINE_CUR_VAL(&cline); *target = ctx.str; - strbuf_cap(target); + */ + + strbuf_cap(&ctx.str); + handle_kv(&key, &ctx); strbuf_soft_reset(&ctx.str); ++ctx.line; ctx.column = 0; - handle_kv(&cline, &ctx); } @@ -196,68 +168,57 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { } int handle_kv ( - content_line* cline, + strbuf* key, + // content_line* cline, parse_ctx* ctx ) { - if (strbuf_c(cline->key, "BEGIN")) { + if (*key == "BEGIN") { /* should be one of: * VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE, * and possibly some others I forget. */ - // NEW(strbuf, s); - auto s = new strbuf; - // strbuf* type = CLINE_CUR_VAL(cline); - // DEEP_COPY(strbuf)(s, type); - *s = *CLINE_CUR_VAL(cline); - // PUSH(LLIST(strbuf))(&ctx->key_stack, s); - ctx->key_stack.push(s); + ctx->key_stack.push(key); - // RESET(LLIST(content_set))(&cline->val); - cline->val->reset(); - - auto e = new vcomponent(s->mem, ctx->filename); - // e->parent = PEEK(LLIST(vcomponent))(&ctx->comp_stack); + auto e = new vcomponent(ctx->str.mem, ctx->filename); e->parent = ctx->comp_stack.peek(); - //PUSH(LLIST(vcomponent))(&ctx->comp_stack, e); ctx->comp_stack.push(e); - } else if (strbuf_c(cline->key, "END")) { + /* A block is ending */ + } else if (*key == "END") { //strbuf* s = POP(LLIST(strbuf))(&ctx->key_stack); + /* Fetch what we are supposed to end */ strbuf* s = ctx->key_stack.pop(); - if (strbuf_cmp(s, CLINE_CUR_VAL(cline)) != 0) { + /* Error if we got something else */ + if (ctx->str != *s) { // 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); + /* put value back on stack */ ctx->key_stack.push(s); return -1; + /* We got a propper end, make the current vcomponent a child + * to the vcomponent under it on the stack.*/ } else { - delete s; - // FFREE(strbuf, s); + /* free the key string */ + // delete s; + /* Received propper end, push cur into parent */ - // vcomponent* cur = POP(LLIST(vcomponent))(&ctx->comp_stack); vcomponent* cur = ctx->comp_stack.pop(); // TODO should self instead be done at creation time? PUSH(vcomponent)(ctx->comp_stack.peek(), cur); } + + /* Neither BEGIN nor END, so a regular KV pair */ } else { - //NEW(content_line, c); - content_line* c = new content_line(*cline); - // DEEP_COPY(content_line)(c, cline); - - // PUSH(TRIE(content_line))( - // &PEEK(LLIST(vcomponent))(&ctx->comp_stack)->clines, - // c->key.mem, c); - // (PEEK(LLIST(vcomponent))(&ctx->comp_stack)->clines).push(c->key.mem, c); - ctx->comp_stack.peek()->clines.push(c->key->mem, c); - - //RESET(LLIST(content_set))(&cline->val); - cline->val->reset(); + strbuf* val = &ctx->str; + vcomponent* cur_comp = ctx->comp_stack.peek(); + cur_comp->push_kv (key, val); } return 0; @@ -297,10 +258,7 @@ int fold(FILE* f, parse_ctx* ctx, char c) { } -//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); @@ -309,18 +267,11 @@ parse_ctx::parse_ctx (const char* filename) { this->pline = 1; this->pcolumn = 1; - - // INIT(strbuf, &this->str); } -// FREE_F(parse_ctx) { parse_ctx::~parse_ctx () { - - // FREE(LLIST(strbuf))(&this->key_stack); - // FREE(LLIST(vcomponent))(&this->comp_stack); free(this->filename); this->line = 0; this->column = 0; - // FREE(strbuf)(&this->str); } @@ -47,7 +47,8 @@ struct parse_ctx { }; int handle_kv( - content_line* cline, + strbuf* key, + // content_line* cline, parse_ctx* ctx ); @@ -94,6 +94,14 @@ strbuf& strbuf::operator=(strbuf& other) { return *this; } +bool strbuf::operator==(strbuf& other) { + return strbuf_cmp(this, &other); +} + +bool strbuf::operator==(const char* other) { + return strbuf_c(this, other); +} + int strbuf_cmp(strbuf* a, strbuf* b) { if (a == NULL || a->alloc == 0 || b == NULL || b->alloc == 0) @@ -26,6 +26,13 @@ struct strbuf { ~strbuf (); strbuf& operator=(strbuf& other); + + bool operator==(strbuf& other); + bool operator==(const char* other); + bool operator!=(strbuf& other) + { return ! (*this == other); } + bool operator!=(const char* other) + { return ! (*this == other); } }; /* @@ -57,6 +57,16 @@ int DEEP_COPY(vcomponent)(vcomponent* a, vcomponent* b) { return -1; } +content_line::content_line (strbuf* key, strbuf* val) { + this->key = key; + this->push_value(val); +} + +void vcomponent:: push_kv (strbuf* key, strbuf* val) { + auto cl = new content_line (key, val); + this->clines.push(key->mem, cl); +} + #if 0 FMT_F(vcomponent) { int seek = 0; @@ -12,7 +12,47 @@ typedef pair<strbuf, llist<strbuf> > param_set; typedef pair<strbuf, llist<param_set> > content_set; -typedef pair<strbuf, llist<content_set> > content_line; +// typedef pair<strbuf, llist<content_set> > content_line; +// typedef llist<content_set> content_line; +struct content_line { + llist<content_set> data; + + content_line (strbuf* key, strbuf* val); + + void push_value (strbuf* s) { + auto cs = new content_set(); + cs->key = s; + this->data.push(cs); + } + + strbuf* cur_val () { + return this->data.peek()->key; + } + + content_line* resolve (content_line* other) { + if (this == NULL) return other; + + /* + * 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 ( ! (this->key == other->key)) { + ERR("Can't resolve between these two types"); + return NULL; + } + + /* This destroys new_->val. */ + this->data.append(&other->data); + delete other; + return this; + } + + private: + strbuf* key; +}; /* * Helper macros for accessing fields in @@ -21,6 +61,7 @@ typedef pair<strbuf, llist<content_set> > content_line; * TODO find a better way to do self. */ +#if 0 /* ptr -> ptr */ #define CLINE_KEY(c) (&(c)->key) #define CLINE_CUR_CSET(c) (&((c)->val.cur->value)) @@ -37,6 +78,7 @@ typedef pair<strbuf, llist<content_set> > content_line; #define CLINE_CUR_PARAM_KEY(c) (CLINE_CUR_PARAMS(c)->cur->value->key) /* strbuf */ #define CLINE_CUR_PARAM_VAL(c) (CLINE_CUR_PARAMS(c)->cur->value->val.cur->value) +#endif // typedef struct s_vcomponent vcomponent; @@ -56,6 +98,8 @@ struct vcomponent { vcomponent (const char* type, const char* filename); ~vcomponent (); + + void push_kv (strbuf* key, strbuf* val); }; // #define FCHILD(v) GET(VECT(vcomponent))(&(v)->components, 0) |