aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2019-02-23 21:09:30 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-02-23 21:09:30 +0100
commit1b71eb8175de976045406035420f62c79a2fc91e (patch)
treea3c5bcd98b50628fbb7ca735bf810f5c30900e3a
parentEven more work. (diff)
downloadcalp-1b71eb8175de976045406035420f62c79a2fc91e.tar.gz
calp-1b71eb8175de976045406035420f62c79a2fc91e.tar.xz
Vill inte.
-rw-r--r--main.c6
-rw-r--r--parse.c45
-rw-r--r--strbuf.c10
-rw-r--r--strbuf.h5
4 files changed, 36 insertions, 30 deletions
diff --git a/main.c b/main.c
index 80e01817..78e631e8 100644
--- a/main.c
+++ b/main.c
@@ -57,15 +57,15 @@ int main (int argc, char** argv) {
for (size_t j = 0; j < cal->components.length; j++) {
vcomponent* ev = cal->components[j];
- printf("component %s, [%u] children\n", ev->type, ev->components.length);
+ // printf("component %s, [%u] children\n", ev->type, ev->components.length);
if (strcmp(ev->type, "VEVENT") != 0) continue;
content_line* cl = get_property(ev, "SUMMARY");
strbuf* s = cl->data.cur->value->key;
+ char* m = s->mem;
printf("%3lu : %3lu | %s | %s\n",
i + 1, j + 1,
- filename,
- s->mem);
+ filename, m);
}
}
} else if (strcmp(args.argv[0], "-g") == 0) {
diff --git a/parse.c b/parse.c
index 4378685a..1f2150a2 100644
--- a/parse.c
+++ b/parse.c
@@ -1,5 +1,7 @@
#include "parse.h"
+#include <iostream>
+
#include <errno.h>
#include <string.h>
#include <assert.h>
@@ -138,32 +140,21 @@ int parse_file(char* filename, FILE* f, vcomponent* root) {
* the end here.
*/
- /*
- strbuf* target = CLINE_CUR_VAL(&cline);
- *target = ctx.str;
- */
-
strbuf_cap(&ctx.str);
handle_kv(&key, &ctx);
strbuf_soft_reset(&ctx.str);
++ctx.line;
+ ++ctx.pline;
ctx.column = 0;
}
- // FREE(content_line)(&cline);
-
- // assert(POP(LLIST(vcomponent))(&ctx.comp_stack) == root);
assert(ctx.comp_stack.pop() == root);
- // assert(EMPTY(LLIST(strbuf))(&ctx.key_stack));
assert(ctx.key_stack.empty());
- // assert(EMPTY(LLIST(vcomponent))(&ctx.comp_stack));
assert(ctx.comp_stack.empty());
- // FREE(parse_ctx)(&ctx);
-
return 0;
}
@@ -173,30 +164,33 @@ int handle_kv (
parse_ctx* ctx
) {
+ strbuf* val = &ctx->str;
+
+ // std::cout << *key << ':' << *val << std::endl;
if (*key == "BEGIN") {
- /* should be one of:
- * VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE,
- * and possibly some others I forget.
- */
+ /* val \in { VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE } */
- ctx->key_stack.push(key);
+ ctx->key_stack.push(val);
- auto e = new vcomponent(ctx->str.mem, ctx->filename);
+ auto e = new vcomponent(val->mem, ctx->filename);
e->parent = ctx->comp_stack.peek();
ctx->comp_stack.push(e);
/* 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();
+
/* 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);
+ if (*val != *s) {
+ ERR_P(ctx, "Expected END:%s, got %s:%s.\n%s",
+ s->mem,
+ key->mem,
+ val->mem,
+ ctx->comp_stack.peek()->filename
+ );
+
/* put value back on stack */
ctx->key_stack.push(s);
return -1;
@@ -216,7 +210,6 @@ int handle_kv (
/* Neither BEGIN nor END, so a regular KV pair */
} else {
- strbuf* val = &ctx->str;
vcomponent* cur_comp = ctx->comp_stack.peek();
cur_comp->push_kv (key, val);
}
diff --git a/strbuf.c b/strbuf.c
index 6ed62c51..c2b9a2bf 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,5 +1,7 @@
#include "strbuf.h"
+#include <iostream>
+
#include <string.h>
#include <stdio.h>
@@ -60,7 +62,8 @@ int strbuf_cap(strbuf* s) {
/*
* TODO check memmory usage
*/
- return strbuf_append(s, 0);
+ int retval = strbuf_append(s, 0);
+ return retval;
}
// int DEEP_COPY(strbuf)(strbuf* dest, strbuf* src) {
@@ -164,3 +167,8 @@ FMT_F(strbuf) {
int SIZE(strbuf)(strbuf* self) {
return self->len;
}
+
+std::ostream& operator<<(std::ostream& os, strbuf& str) {
+ os << str.mem;
+ return os;
+}
diff --git a/strbuf.h b/strbuf.h
index 24dd8328..73f5f1dd 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -1,6 +1,8 @@
#ifndef STRBUF_H
#define STRBUF_H
+#include <iostream>
+
#include <stdlib.h>
#include "macro.h"
@@ -33,8 +35,11 @@ struct strbuf {
{ return ! (*this == other); }
bool operator!=(const char* other)
{ return ! (*this == other); }
+
};
+std::ostream& operator<<(std::ostream&, strbuf&);
+
/*
* Init strbuf to size of 0
* Doesnt't call malloc.