From 4fe953de7b5994a896094cf0d62192559a08967d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 2 May 2019 17:58:07 +0200 Subject: work on porting most internal datastructures to scheme equivalents. --- Makefile | 2 +- module/output/html.scm | 3 +- module/vcomponent.scm | 4 +- module/vcomponent/primitive.scm | 2 +- src/calendar.c | 36 +++++-- src/graphs.c | 144 ------------------------- src/graphs.h | 15 --- src/guile_interface.h | 1 + src/guile_interface.scm.c | 39 +++++-- src/linked_list.h | 93 ---------------- src/linked_list.inc.h | 179 ------------------------------- src/main.c | 120 --------------------- src/pair.h | 19 ---- src/pair.inc.h | 34 ------ src/parse.c | 152 +++++++++++++------------- src/parse.h | 9 +- src/trie.h | 54 ---------- src/trie.inc.h | 231 ---------------------------------------- src/vcal.c | 72 ++++++++++--- src/vcal.h | 74 ++++++------- test.scm | 16 +++ 21 files changed, 264 insertions(+), 1035 deletions(-) delete mode 100644 src/graphs.c delete mode 100644 src/graphs.h delete mode 100644 src/linked_list.h delete mode 100644 src/linked_list.inc.h delete mode 100644 src/main.c delete mode 100644 src/pair.h delete mode 100644 src/pair.inc.h delete mode 100644 src/trie.h delete mode 100644 src/trie.inc.h create mode 100644 test.scm diff --git a/Makefile b/Makefile index e7dae809..ac0cdf9d 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ X_FILES = $(SCM_C_FILES:.scm.c=.x) O_FILES = $(C_FILES:src/%.c=obj/%.o) -all: parse $(SO_FILES) guild-stuff guile-none +all: $(SO_FILES) guild-stuff guile-none guild-stuff: guild compile module/vcomponent/primitive.scm diff --git a/module/output/html.scm b/module/output/html.scm index 01736cac..23a7b9ac 100644 --- a/module/output/html.scm +++ b/module/output/html.scm @@ -198,7 +198,8 @@ (append (map (td '(class "prev")) (iota month-start (- prev-month-len month-start))) (map (td '(class "cur")) - (map (lambda (d) `(a (@ (href "#" ,(date->string date "~Y-~m-") ,d) + (map (lambda (d) `(a (@ (href "#" ,(date->string date "~Y-~m-") + ,(when (< d 10) 0) ,d) (class "hidelink")) ,d)) (iota month-len 1))) (map (td '(class "next")) diff --git a/module/vcomponent.scm b/module/vcomponent.scm index 4ef8f794..6df4d7b8 100644 --- a/module/vcomponent.scm +++ b/module/vcomponent.scm @@ -63,7 +63,7 @@ (define (get-attr component attr) (%vcomponent-get-attribute component - (as-string attr))) + (as-symb attr))) (define (set-attr! component attr value) (set! (car (get-attr component (as-string attr))) @@ -123,7 +123,7 @@ (define-public copy-vcomponent %vcomponent-shallow-copy) -(define-public filter-children! %vcomponent-filter-children!) +;; (define-public filter-children! %vcomponent-filter-children!) (define-public (extract field) (lambda (e) (attr e field))) diff --git a/module/vcomponent/primitive.scm b/module/vcomponent/primitive.scm index b583d454..06b6fd83 100644 --- a/module/vcomponent/primitive.scm +++ b/module/vcomponent/primitive.scm @@ -3,7 +3,7 @@ (define-module (vcomponent primitive) #:export (%vcomponent-children %vcomponent-push-child! - %vcomponent-filter-children! + ;; %vcomponent-filter-children! %vcomponent-parent diff --git a/src/calendar.c b/src/calendar.c index 500f0827..d7c2d7dd 100644 --- a/src/calendar.c +++ b/src/calendar.c @@ -5,6 +5,7 @@ #include #include #include +#include /* basename */ #include @@ -42,11 +43,19 @@ int handle_file(vcomponent* cal, char* path) { INFO("Parsing a single file"); /* NAME is the `fancy' name of the calendar. */ - vcomponent_push_val(cal, "NAME", basename(path)); - vcomponent_push_val(cal, "X-HNH-SOURCETYPE", "file"); + SNEW(strbuf, name); + strbuf_load (&name, "NAME"); + SNEW(strbuf, sourcetype); + strbuf_load (&sourcetype, "X-HNH-SOURCETYPE"); + + const char* bname = basename(path); + vcomponent_push_val(cal, &name, scm_from_utf8_stringn (bname, strlen(bname))); + vcomponent_push_val(cal, &sourcetype, scm_from_utf8_symbol("file")); char* resolved_path = realpath(path, NULL); open_ics (resolved_path, cal); free (resolved_path); + FREE(strbuf)(&name); + FREE(strbuf)(&sourcetype); return 0; } @@ -64,10 +73,19 @@ int handle_dir(vcomponent* cal, char* path) { /* Slash to guarantee we have at least one */ buf[path_len - 1] = '/'; + SNEW(strbuf, name); + strbuf_load (&name, "NAME"); + SNEW(strbuf, sourcetype); + strbuf_load (&sourcetype, "X-HNH-SOURCETYPE"); + + const char* bname = basename(path); /* NAME is the `fancy' name of the calendar. */ - vcomponent_push_val(cal, "NAME", basename(path)); - vcomponent_push_val(cal, "X-HNH-SOURCETYPE", "vdir"); + vcomponent_push_val(cal, &name, scm_from_utf8_stringn(bname, strlen(bname))); + vcomponent_push_val(cal, &sourcetype, scm_from_utf8_symbol("vdir")); + + FREE(strbuf)(&name); + FREE(strbuf)(&sourcetype); struct dirent* d; while ((d = readdir(dir)) != NULL) { @@ -90,7 +108,10 @@ int handle_dir(vcomponent* cal, char* path) { info_buf[read - 1] = '\0'; fclose(f); - vcomponent_push_val(cal, "COLOR", info_buf); + SNEW(strbuf, color); + strbuf_load (&color, "COLOR"); + vcomponent_push_val(cal, &color, scm_from_utf8_stringn(info_buf, strlen(info_buf))); + FREE(strbuf)(&color); } else if (strcmp (d->d_name, "displayname") == 0) { f = fopen(resolved_path, "r"); read = getline(&info_buf, &size, f); @@ -104,7 +125,10 @@ int handle_dir(vcomponent* cal, char* path) { * This works since *currently* values are returned in * reverse order */ - vcomponent_push_val(cal, "NAME", info_buf); + SNEW(strbuf, name); + strbuf_load (&name, "NAME"); + vcomponent_push_val(cal, &name, scm_from_utf8_stringn(info_buf, strlen(info_buf))); + FREE(strbuf)(&name); } else { open_ics (resolved_path, cal); } diff --git a/src/graphs.c b/src/graphs.c deleted file mode 100644 index 51a26117..00000000 --- a/src/graphs.c +++ /dev/null @@ -1,144 +0,0 @@ -#include "graphs.h" - -#include -#include -#include -#include "err.h" - -// #define TYPE strbuf -// #include "linked_list.h" -// #include "linked_list.inc.h" -// #undef TYPE - -int create_graph_trie (vcomponent* ev, char* filename) { - FILE* f = fopen(filename, "w"); - - fputs("digraph {\n rankdir=LR;", f); - trie_to_dot(&ev->clines, f); - fputs("}", f); - - fclose(f); - - INFO_F("Wrote '%s' to '%s'", vcomponent_get_val(ev, "X-HNH-FILENAME"), filename); - - return 0; -} - -int helper_vcomponent (vcomponent* root, FILE* f) { - fprintf(f, "subgraph \"cluster_root\" { label=File; \"%p\" [label=%s] }\n", root, root->type); - - TRIE(content_line)* trie = &root->clines; - TRIE_NODE(content_line)* n = trie->root->child; - - if (! EMPTY(TRIE(content_line))(trie)) { - fprintf(f, "subgraph \"cluster_%p\" {\n", root); - fprintf(f, "\"%p\" [label=trie fontcolor=gray, color=gray];", trie); - fprintf(f, "\"%p\" -> \"%p\" [color=red]\n", root, trie); - while (n != NULL) { - fprintf(f, "\"%p\" -> \"%p\" [color=gray]\n", - (void*) trie, - (void*) n); - fprintf(f, "subgraph \"cluster_%c_%p\" {\ncolor=red; \n", - n->c, root); - trie_to_dot_helper( n, f ); - - - fputs("}", f); - n = n->next; - } - fputs("}", f); - } - - FOR(LLIST, vcomponent, child, &root->components) { - fprintf(f, "\"%p\" -> \"%p\"\n", root, child); - helper_vcomponent(child, f); - } - return 0; -} - -int create_graph_vcomponent (vcomponent* root, char* outfile) { - FILE* f = fopen(outfile, "w"); - if (f == NULL) { - ERR_F("Error opening file %s, errno = %i", outfile, errno); - return 1; - } - vcomponent* c = root; - fputs("digraph {", f); - helper_vcomponent(c, f); - fputs("}", f); - fclose(f); - return 0; -} - -#define T content_line - -int trie_to_dot ( TRIE(T)* trie, FILE* f ) { - TRIE_NODE(T)* n = trie->root->child; - fprintf(f, "\"%p\" [label=root fontcolor=gray, color=gray];", trie); - while (n != NULL) { - fprintf(f, "\"%p\" -> \"%p\" [color=gray]\n", - (void*) trie, - (void*) n); - fprintf(f, "subgraph \"cluster_%c\" {\n", - n->c); - trie_to_dot_helper( n, f ); - fputs("}", f); - n = n->next; - } - return 0; -} - -int trie_to_dot_helper ( TRIE_NODE(T)* root, FILE* f ) { - if (L(root) == NULL) { - fprintf(f, "\"%p\"[label = \"%c\" style=filled fillcolor=white];\n", - (void*) root, root->c); - } else { - fprintf(f, "\"%p\"[label = \"%c [%i]\" style=filled fillcolor=green];\n", - (void*) root, root->c, - SIZE(LLIST(content_set))(L(root)) - ); - } - TRIE_NODE(T)* child = root->child; - - // ---------------------------------------- -#if 1 /* Toggle values */ - if (L(root) != NULL) { - - FOR(LLIST, content_set, v, L(root)) { - char buf[0x100]; - FMT(strbuf)(&v->key, buf); - fprintf(f, "\"%p\" [label=\"%s\" shape=rectangle color=darkgreen];\n", - v, buf); - /* Edge between TRIE char node and data node */ - fprintf(f, "\"%p\" -> \"%p\";\n", root, v); - - /* Parameters */ - LLIST(strbuf)* keys = KEYS(TRIE(param_set))(&v->val); - FOR(LLIST, strbuf, key, keys) { - param_set* p = GET(TRIE(param_set))(&v->val, key->mem); - - fprintf(f, "\"%p\" [label=\"%s\" color=blue];\n", - key, key->mem); - /* Edge between data node and param key node */ - fprintf(f, "\"%p\" -> \"%p\";", v, key); - - FOR(LLIST, strbuf, str, p) { - fprintf(f, "\"%p\" [label=\"%s\" color=orange];", - str, str->mem); - /* Edge between param key node and param value node */ - fprintf(f, "\"%p\" -> \"%p\";", key, str); - } - } - } - } -#endif - // ---------------------------------------- - - while (child != NULL) { - fprintf(f, "\"%p\" -> \"%p\";\n", - (void*) root, (void*) child); - trie_to_dot_helper(child, f); - child = child->next; - } - return 0; -} diff --git a/src/graphs.h b/src/graphs.h deleted file mode 100644 index fe521003..00000000 --- a/src/graphs.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef GRAPHS_H -#define GRAPHS_H - -#include "vcal.h" - -int create_graph_trie (vcomponent* ev, char* filename); - -int create_graph_vcomponent (vcomponent* root, char* outfile); - -int helper_vcomponent (vcomponent* root, FILE* f); - -int trie_to_dot ( TRIE(content_line)*, FILE* ); -int trie_to_dot_helper ( TRIE_NODE(content_line)*, FILE* ); - -#endif /* GRAPHS_H */ diff --git a/src/guile_interface.h b/src/guile_interface.h index 76ec24d3..f8b38ed5 100644 --- a/src/guile_interface.h +++ b/src/guile_interface.h @@ -24,5 +24,6 @@ SCM vcomponent_children (SCM); SCM vcomponent_typeof (SCM); SCM scm_from_vcomponent (vcomponent*); +vcomponent* scm_to_vcomponent (SCM); #endif /* GUILE_INTERFACE_H */ diff --git a/src/guile_interface.scm.c b/src/guile_interface.scm.c index 20c413df..7828389f 100644 --- a/src/guile_interface.scm.c +++ b/src/guile_interface.scm.c @@ -34,6 +34,16 @@ SCM_DEFINE (make_vcomponent, "%vcomponent-make", 0, 1, 0, return scm_from_vcomponent (cal); } +SCM_DEFINE_PUBLIC (scm_vcomponent_get_hash_table, "%vcomponent-get-hash-table", 1, 0, 0, + (SCM comp), + "") +{ + scm_assert_foreign_object_type (vcomponent_type, comp); + vcomponent* cal = scm_foreign_object_ref (comp, 0); + + return cal->clines; +} + /* * Returns a line from a component. */ @@ -44,6 +54,9 @@ SCM_DEFINE (vcomponent_get_attribute, "%vcomponent-get-attribute", 2, 0, 0, scm_assert_foreign_object_type (vcomponent_type, calendar); vcomponent* cal = scm_foreign_object_ref (calendar, 0); + return scm_hashq_ref (cal->clines, attr, SCM_BOOL_F); + +#if 0 const char* key = scm_i_string_chars (attr); content_line* c = get_attributes (cal, key); @@ -105,6 +118,7 @@ SCM_DEFINE (vcomponent_get_attribute, "%vcomponent-get-attribute", 2, 0, 0, scm_hashq_set_x (content_set_lists, ptr, attrlist); return attrlist; +#endif } SCM_DEFINE (vcomponent_child_count, "%vcomponent-child-count", 1, 0, 0, @@ -113,7 +127,8 @@ SCM_DEFINE (vcomponent_child_count, "%vcomponent-child-count", 1, 0, 0, { scm_assert_foreign_object_type (vcomponent_type, component); vcomponent* c = scm_foreign_object_ref (component, 0); - return scm_from_size_t (SIZE(LLIST(vcomponent))(&c->components)); + // return scm_from_size_t (SIZE(LLIST(vcomponent))(&c->components)); + return scm_length (c->components); } SCM_DEFINE(vcomponent_children, "%vcomponent-children", 1, 0, 0, @@ -123,13 +138,16 @@ SCM_DEFINE(vcomponent_children, "%vcomponent-children", 1, 0, 0, scm_assert_foreign_object_type (vcomponent_type, component); vcomponent* cal = scm_foreign_object_ref (component, 0); - SCM llist = SCM_EOL; - FOR (LLIST, vcomponent, v, &cal->components) { - llist = scm_cons(scm_from_vcomponent(v), llist); - } - return llist; + // SCM llist = SCM_EOL; + // FOR (LLIST, vcomponent, v, &cal->components) { + // llist = scm_cons(scm_from_vcomponent(v), llist); + // } + // return llist; + + return cal->components; } +#if 0 SCM_DEFINE(vcomponent_filter_children_x, "%vcomponent-filter-children!", 2, 0, 0, (SCM pred, SCM component), @@ -150,6 +168,7 @@ SCM_DEFINE(vcomponent_filter_children_x, "%vcomponent-filter-children!", return SCM_UNSPECIFIED; } +#endif SCM_DEFINE(vcomponent_push_child_x, "%vcomponent-push-child!", 2, 0, 0, (SCM component, SCM child), @@ -218,6 +237,13 @@ SCM scm_from_vcomponent(vcomponent* v) { return v->scm; } +vcomponent* scm_to_vcomponent (SCM s) { + scm_assert_foreign_object_type (vcomponent_type, s); + vcomponent* comp = scm_foreign_object_ref (s, 0); + return comp; +} + +#if 0 SCM_DEFINE(vcomponent_attr_list, "%vcomponent-attribute-list", 1, 0, 0, (SCM component), "Returns list of all keys in component.") @@ -235,6 +261,7 @@ SCM_DEFINE(vcomponent_attr_list, "%vcomponent-attribute-list", 1, 0, 0, return llist; } +#endif SCM_DEFINE(vcomponent_shallow_copy, "%vcomponent-shallow-copy", 1, 0, 0, (SCM component), diff --git a/src/linked_list.h b/src/linked_list.h deleted file mode 100644 index 0d32b988..00000000 --- a/src/linked_list.h +++ /dev/null @@ -1,93 +0,0 @@ -#ifndef LINKED_LIST_H -#define LINKED_LIST_H - -#include "macro.h" - -#define LLIST(T) TEMPL(llist, T) -#define LINK(T) TEMPL(llist_link, T) - -#define UNLINK(T) TEMPL(unlink, T) - -#endif /* LINKED_LIST_H */ -#ifdef TYPE - -typedef struct LINK(TYPE) { - struct LINK(TYPE)* before; - struct LINK(TYPE)* after; - TYPE* value; -} LINK(TYPE); - -#define L(link) (link)->value - -typedef struct { - LINK(TYPE)* head; - LINK(TYPE)* tail; - LINK(TYPE)* cur; - TYPE* cval; - int length; -} LLIST(TYPE); - -#define FIRST(lst) (lst)->head->after -#define FIRST_V(lst) (lst)->head->after->value -#define LAST(lst) (lst)->tail->before -#define LAST_V(lst) (lst)->tail->before->value - -INIT_F ( LLIST(TYPE) ); - -/* - * NOTE freeing a linked list alsa FFREE's all its contents. - * TODO some form of shared pointer to ensure nothing is free'd twice - * would be a good idea. - */ -FREE_F ( LLIST(TYPE) ); - -INIT_F ( LINK(TYPE) ); -INIT_F ( LINK(TYPE), TYPE* val ); -FREE_F ( LINK(TYPE) ); - -int UNLINK(LINK(TYPE)) ( LINK(TYPE)* ); - -int PUSH(LLIST(TYPE)) ( LLIST(TYPE)*, TYPE* ); -TYPE* PEEK(LLIST(TYPE)) ( LLIST(TYPE)* ); -TYPE* POP(LLIST(TYPE)) ( LLIST(TYPE)* ); - -int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ); - -int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new_ ); - -int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist ); -int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist ); - -/* - * Resets a linked list by removing all it's objects. - * FREE's all elements stored in the list. - */ -int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ); - -/* - * Takes to lists, and merges them into a single one. Destroys new_ in - * the process. - */ -LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new_); - -FMT_F(LLIST(TYPE)); - -/* Iterator */ - -#define __PRE_LLIST(T, l, set) \ - T* l; LINK(T)* __INTER(l); - -#define PRE_FOR_LLIST(T) __PRE_LLIST - -// #define __BEG_LLIST(v, set) v = (set)->head -#define __BEG_LLIST(T, l, set) __INTER(l) = FIRST(set), l = L(__INTER(l)) -#define BEG_LLIST(T) __BEG_LLIST - -#define __END_LLIST(T, l, set) __INTER(l) != (set)->tail -#define END_LLIST(T) __END_LLIST - -#define __NXT_LLIST(T, l, set) __INTER(l) = __INTER(l)->after, l = L(__INTER(l)) -// #define __NXT_LLIST(T, l, set) l = L(__INTER(l) = __INTER(l)->after) -#define NXT_LLIST(T) __NXT_LLIST - -#endif /* TYPE */ diff --git a/src/linked_list.inc.h b/src/linked_list.inc.h deleted file mode 100644 index 3984e485..00000000 --- a/src/linked_list.inc.h +++ /dev/null @@ -1,179 +0,0 @@ -#ifndef TYPE -#error "Set TYPE before including self file" -#else - -INIT_F ( LLIST(TYPE) ) { - self->length = 0; - NEW(LINK(TYPE), head); - NEW(LINK(TYPE), tail); - self->head = head; - self->tail = tail; - head->after = tail; - tail->before = head; - self->cur = head; - self->cval = head->value; - return 0; -} - -FREE_F (LINK(TYPE)) { - UNLINK(LINK(TYPE))(self); - - if (self->value != NULL) FFREE(TYPE, self->value); - return 0; -} - -FREE_F( LLIST(TYPE) ) { - LINK(TYPE) *n, *next; - n = self->head; - while ( n != NULL ) { - next = n->after; - FFREE(LINK(TYPE), n); - n = next; - } - - self->length = -1; - - return 0; -} - -INIT_F ( LINK(TYPE) ) { - self->before = NULL; - self->after = NULL; - self->value = NULL; - return 0; -} - -INIT_F ( LINK(TYPE), TYPE* val ) { - self->before = NULL; - self->after = NULL; - self->value = val; - return 0; -} - -int UNLINK(LINK(TYPE)) ( LINK(TYPE)* self ) { - if (self->before != NULL) self->before->after = self->after; - if (self->after != NULL) self->after->before = self->before; - return 0; -} - - -int PUSH(LLIST(TYPE)) ( LLIST(TYPE)* lst, TYPE* val) { - NEW(LINK(TYPE), link, val); - - link->after = FIRST(lst); - FIRST(lst) = link; - - link->after->before = link; - link->before = lst->head; - - ++lst->length; - - // TODO do I want to change that? - lst->cur = link; - lst->cval = link->value; - - return 0; -} - -TYPE* PEEK(LLIST(TYPE)) ( LLIST(TYPE)* lst ) { - if (EMPTY(LLIST(TYPE))(lst)) return NULL; - - return FIRST(lst)->value; -} - -TYPE* POP(LLIST(TYPE)) ( LLIST(TYPE)* lst) { - if (EMPTY(LLIST(TYPE))(lst)) return NULL; - - LINK(TYPE)* frst = FIRST(lst); - UNLINK(LINK(TYPE))(frst); - - TYPE* retval = frst->value; - --lst->length; - free (frst); - return retval; -} - -int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ) { - LINK(TYPE)* n = FIRST(src); - - while (n->after != NULL) { - NEW(TYPE, cpy); - DEEP_COPY(TYPE)(cpy, n->value); - PUSH(LLIST(TYPE)) ( dest, cpy ); - n = n->after; - } - - return 0; -} - -/* - * Adds two linked lists together. - * O(1) time. - * destroys new__ in the process, but keeps the elements. - * make sure to free(new__) after. - */ -int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new__ ) { - - /* Link end of dest onto start of new__. */ - LAST(dest)->after = FIRST(new__); - FIRST(new__)->before = LAST(dest); - - /* Free the two now not needed end links. */ - free(new__->head); - free(dest->tail); - - /* Update dest with new__ tail ptr. */ - dest->tail = new__->tail; - - dest->length += new__->length; - - return 0; -} - -int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist ) { - return llist->length; -} - -int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist ) { - return FIRST(llist) == llist->tail; -} - -LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new__) { - if (dest == NULL) return new__; - APPEND(LLIST(TYPE))(dest, new__); - free(new__); - return dest; -} - -int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ) { - - LINK(TYPE) *link = FIRST(llist), *next; - /* - * Manual looping rather than itterators since we destroyed the - * loop variable. - */ - while (link != llist->tail) { - next = link->after; - FFREE(LINK(TYPE), link); - link = next; - } - - llist->cur = llist->head; - llist->cval = llist->head->value; - - return 0; -} - -FMT_F(LLIST(TYPE)) { - int seek = 0; - fmtf("("); - FOR(LLIST, TYPE, v, self) { - seek += FMT(TYPE)(v, buf + seek); - fmtf(" "); - } - fmtf(")"); - - return seek; -} - -#endif /* TYPE */ diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 4d8da7d3..00000000 --- a/src/main.c +++ /dev/null @@ -1,120 +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; - -} - -#define GETSET(C, KEY) \ - vcomponent_push_val((C), (KEY), "DUMMY VALUE"); \ - INFO_F("cline = %p", get_attributes((C), (KEY))); - -/* - * Tests defined here instead of in own header to ensure that all the - * correct modules are loaded. - */ -int run_tests() { - NEW(vcomponent, c); - INFO(All the following should print a valid pointer != 0x0); - GETSET(c, "FILENAME"); - GETSET(c, "X-HNH-FILENAME"); - GETSET(c, "DATA"); - GETSET(c, "DAT"); - GETSET(c, "DA"); - GETSET(c, "D"); - GETSET(c, "A"); - GETSET(c, "F"); - FFREE(vcomponent, c); - return 0; -} - -int main (int argc, char** argv) { - arg args = { .argc = argc, .argv = argv }; - - - if (arg_shift(&args) == 0) { - ERR("Please give something to parse, or some other flags"); - exit (1); - } - - if (strcmp(args.argv[0], "--run-tests") == 0) { - run_tests(); - return 0; - } - - 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 (LLIST, vcomponent, cal, &root.components) { - assert(strcmp(cal->type, "VCALENDAR") == 0); - - char* filename = vcomponent_get_val(cal, "X-HNH-FILENAME"); - - /* This loop over all VEVENT's in the current VCALENDAR */ - FOR (LLIST, vcomponent, ev, &cal->components) { - if (strcmp(ev->type, "VEVENT") != 0) continue; - - printf("%s | %s\n", - filename, - get_attributes(ev, "SUMMARY")->cval->key.mem); - } - } - } else if (strcmp(args.argv[0], "-g") == 0) { - /* TODO self might be broken */ - if (arg_shift(&args) == 0) { - FOR (LLIST, vcomponent, cal, &root.components) { - assert(strcmp(cal->type, "VCALENDAR") == 0); - - vcomponent* ev = FCHILD(cal); - - char target[0xFF]; - target[0] = '\0'; - strcat(target, "/tmp/dot/"); - strcat(target, vcomponent_get_val(ev, "X-HNH-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/src/pair.h b/src/pair.h deleted file mode 100644 index e96cf180..00000000 --- a/src/pair.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef PAIR_H -#define PAIR_H - -#define PAIR(T, V) TEMPL2(pair, T, V) - -#endif /* PAIR_H */ -#if defined(T) && defined(V) - -typedef struct { - T key; - V val; -} PAIR(T, V); - -INIT_F(PAIR(T, V)); -FREE_F(PAIR(T, V)); -FMT_F(PAIR(T, V)); -int DEEP_COPY(PAIR(T, V)) (PAIR(T, V)* dest, PAIR(T, V)* src); - -#endif diff --git a/src/pair.inc.h b/src/pair.inc.h deleted file mode 100644 index c42b2dfd..00000000 --- a/src/pair.inc.h +++ /dev/null @@ -1,34 +0,0 @@ -#if ! (defined(T) && defined(V)) -#error "Both T and V must be defiend here" -#else - -INIT_F(PAIR(T, V)) { - INIT(T, &self->key); - INIT(V, &self->val); - - return 0; -} - -FREE_F(PAIR(T, V)) { - FREE(T)(&self->key); - FREE(V)(&self->val); - - return 0; -} - -FMT_F(PAIR(T, V)) { - char lbuf[0x100]; - char rbuf[0x1000]; - FMT(T)(&self->key, lbuf); - FMT(V)(&self->val, rbuf); - - return sprintf(buf, "<%s, %s>", lbuf, rbuf); -} - -int DEEP_COPY(PAIR(T, V)) (PAIR(T, V)* dest, PAIR(T, V)* src) { - DEEP_COPY(T)(&dest->key, &src->key); - DEEP_COPY(V)(&dest->val, &src->val); - return 0; -} - -#endif /* T & V */ diff --git a/src/parse.c b/src/parse.c index 565e1d6c..d9a75296 100644 --- a/src/parse.c +++ b/src/parse.c @@ -3,23 +3,14 @@ #include #include #include +#include +#include "guile_interface.h" #include "macro.h" #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,7 +18,7 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { part_context p_ctx = p_key; SNEW(parse_ctx, ctx, f, filename); - PUSH(LLIST(vcomponent))(&ctx.comp_stack, root); + SCM_PUSH_X(ctx.comp_stack, scm_from_vcomponent(root)); /* * Create a content_line which we use as storage while we are @@ -36,8 +27,13 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { * {cline,param}_key is also temporary register used during * parsing. */ - SNEW(content_line, cline); + // SNEW(content_line, cline); + SCM content_pair = scm_cons (SCM_BOOL_F, scm_make_hash_table (scm_from_ulong(32))); + scm_gc_protect_object (content_pair); + SNEW(strbuf, cline_key); + SNEW(strbuf, cline_val); + SNEW(strbuf, param_key); char c; @@ -47,9 +43,12 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { if (c == '\r' || c == '\n') { if (fold(&ctx, c) > 0) { + TRANSFER(&cline_val, &ctx.str); + /* Actuall end of line, handle value */ - TRANSFER(CLINE_CUR_VAL(&cline), &ctx.str); - handle_kv(&cline_key, &cline, &ctx); + INFO_F("cp: %p", content_pair); + handle_kv(&cline_key, &cline_val, &content_pair, &ctx); + INFO_F("cp: %p", content_pair); p_ctx = p_key; } /* Else continue on current line */ @@ -78,14 +77,13 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { if (p_ctx == p_param_value) { /* save current parameter value. */ - NEW(strbuf, s); - TRANSFER(s, &ctx.str); - - NEW(param_set, ps); - PUSH(param_set)(ps, s); + // TODO resolve + scm_hashq_set_x ( + scm_cdr (content_pair), + scm_string_to_symbol (scm_from_utf8_stringn (param_key.mem, param_key.len)), + scm_from_utf8_stringn (ctx.str.mem, ctx.str.len)); - PUSH(TRIE(param_set))(CLINE_CUR_PARAMS(&cline), param_key.mem, ps); - strbuf_soft_reset (¶m_key); + strbuf_soft_reset (&ctx.str); } /* @@ -95,11 +93,7 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { * parameters. */ if (p_ctx == p_key) { - TRANSFER(&cline_key, &ctx.str); - - NEW(content_set, p); - PUSH(LLIST(content_set))(&cline, p); } if (c == ':') p_ctx = p_value; @@ -128,11 +122,12 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { * the end here. */ - TRANSFER(CLINE_CUR_VAL(&cline), &ctx.str); - handle_kv(&cline_key, &cline, &ctx); + // TODO + handle_kv(&cline_key, &cline_val, &content_pair, &ctx); } + /* FREE(content_line)(&cline); FREE(strbuf)(&cline_key); FREE(strbuf)(¶m_key); @@ -140,6 +135,7 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { assert(POP(LLIST(vcomponent))(&ctx.comp_stack) == root); assert(EMPTY(LLIST(strbuf))(&ctx.key_stack)); assert(EMPTY(LLIST(vcomponent))(&ctx.comp_stack)); + */ FREE(parse_ctx)(&ctx); @@ -151,26 +147,29 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { */ int handle_kv ( strbuf* key, - content_line* cline, + strbuf* val, + SCM* content_pair, parse_ctx* ctx ) { + strbuf_cap (key); + strbuf_cap (val); + INFO_F("%s: %s", key->mem, val->mem); + /* * The key being BEGIN means that we decend into a new component. */ if (strbuf_c(key, "BEGIN")) { /* key \in { VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE, ... } */ - /* - * Take a copy of the name of the entered component, and store - * it on the stack of component names. - */ - NEW(strbuf, s); - DEEP_COPY(strbuf)(s, CLINE_CUR_VAL(cline)); - PUSH(LLIST(strbuf))(&ctx->key_stack, s); + NEW(vcomponent, e, + ctx->str.mem, + ctx->filename); + + SCM_PUSH_X(ctx->key_stack, scm_string_to_symbol(scm_from_utf8_stringn(ctx->str.mem, ctx->str.len))); /* Clear the value list in the parse content_line */ - RESET(LLIST(content_set))(cline); + // RESET(LLIST(content_set))(cline); /* * Create the new curent component, link it with the current @@ -178,36 +177,40 @@ int handle_kv ( * Finally push the new component on to the top of the * component stack. */ - NEW(vcomponent, e, - s->mem, - ctx->filename); - vcomponent* parent = PEEK(LLIST(vcomponent))(&ctx->comp_stack); + + // vcomponent* parent = PEEK(LLIST(vcomponent))(&ctx->comp_stack); + vcomponent* parent = scm_to_vcomponent(scm_car(ctx->comp_stack)); PUSH(vcomponent)(parent, e); - PUSH(LLIST(vcomponent))(&ctx->comp_stack, e); + // PUSH(LLIST(vcomponent))(&ctx->comp_stack, e); + SCM_PUSH_X(ctx->comp_stack, scm_from_vcomponent (e)); /* * The end of a component, go back along the stack to the previous * component. */ } else if (strbuf_c(key, "END")) { - strbuf* expected_key = POP(LLIST(strbuf))(&ctx->key_stack); + // strbuf* expected_key = POP(LLIST(strbuf))(&ctx->key_stack); + SCM expected_key = scm_car(ctx->key_stack); - if (strbuf_cmp(expected_key, CLINE_CUR_VAL(cline)) != 0) { + if (! scm_is_eq (expected_key, + scm_string_to_symbol (scm_from_utf8_stringn (ctx->str.mem, ctx->str.len)))) { - ERR_P(ctx, "Expected END:%s, got END:%s.\n%s line", - expected_key->mem, - CLINE_CUR_VAL(cline)->mem, - vcomponent_get_val( - PEEK(LLIST(vcomponent))(&ctx->comp_stack), - "X-HNH-FILENAME")); - PUSH(LLIST(strbuf))(&ctx->key_stack, expected_key); + // ERR_P(ctx, "Expected END:%s, got END:%s.\n%s line", + // expected_key->mem, + // CLINE_CUR_VAL(cline)->mem, + // vcomponent_get_val( + // PEEK(LLIST(vcomponent))(&ctx->comp_stack), + // "X-HNH-FILENAME")); + // PUSH(LLIST(strbuf))(&ctx->key_stack, expected_key); return -1; } else { - FFREE(strbuf, expected_key); - POP(LLIST(vcomponent))(&ctx->comp_stack); + // FFREE(strbuf, expected_key); + // POP(LLIST(vcomponent))(&ctx->comp_stack); + SCM_POP_X (ctx->key_stack); + SCM_POP_X (ctx->comp_stack); } /* @@ -216,29 +219,23 @@ int handle_kv ( */ } else { - /* - * cline is the value store used during parsing, meaning that - * its values WILL mutate at a later point. Therefore we take - * a copy of it here. - */ - NEW(content_line, c); - DEEP_COPY(content_line)(c, cline); + SNEW(strbuf, sbuf); + TRANSFER (&sbuf, val); - /* - * The PUSH(TRIE(T)) method handles collisions by calling - * RESOLVE(T). content_line resolves by merging the new value - * into the old value, and freeing the new value's container. - * - * This means that |c| declared above might be destroyed - * here. - */ - PUSH(TRIE(content_line))( - &PEEK(LLIST(vcomponent))(&ctx->comp_stack)->clines, - key->mem, c); + SCM str = scm_from_utf8_stringn (sbuf.mem, sbuf.len); + FREE(strbuf) (&sbuf); + scm_set_car_x (*content_pair, str); - RESET(LLIST(content_set))(cline); + vcomponent* e = scm_to_vcomponent (scm_car (ctx->comp_stack)); + vcomponent_push_val (e, key, *content_pair); + + *content_pair = scm_cons (SCM_BOOL_F, scm_make_hash_table (scm_from_ulong(32))); + scm_gc_protect_object (*content_pair); } + strbuf_reset(key); + strbuf_reset(&ctx->str); + return 0; } @@ -277,8 +274,9 @@ int fold(parse_ctx* ctx, char c) { INIT_F(parse_ctx, FILE* f, char* filename) { - INIT(LLIST(strbuf), &self->key_stack); - INIT(LLIST(vcomponent), &self->comp_stack); + self->key_stack = SCM_EOL; + self->comp_stack = SCM_EOL; + self->filename = (char*) calloc(sizeof(*filename), strlen(filename) + 1); strcpy(self->filename, filename); self->f = f; @@ -296,8 +294,10 @@ INIT_F(parse_ctx, FILE* f, char* filename) { FREE_F(parse_ctx) { - FREE(LLIST(strbuf))(&self->key_stack); - FREE(LLIST(vcomponent))(&self->comp_stack); + // FREE(LLIST(strbuf))(&self->key_stack); + // FREE(LLIST(vcomponent))(&self->comp_stack); + self->key_stack = SCM_UNDEFINED; + self->comp_stack = SCM_UNDEFINED; free(self->filename); self->line = 0; diff --git a/src/parse.h b/src/parse.h index 53263b4c..ed235e88 100644 --- a/src/parse.h +++ b/src/parse.h @@ -51,8 +51,10 @@ typedef struct { * context stacks used since ICS files form a tree. key_stack is * only for sequrity purposes. */ - LLIST(strbuf) key_stack; - LLIST(vcomponent) comp_stack; + // LLIST(strbuf) key_stack; + // LLIST(vcomponent) comp_stack; + SCM key_stack; + SCM comp_stack; /* Number for unfolded lines * TODO remove this @@ -91,7 +93,8 @@ int parse_file(char* filename, FILE* f, vcomponent* cal); */ int handle_kv( strbuf* key, - content_line* cline, + strbuf* val, + SCM* content_pair, parse_ctx* ctx ); diff --git a/src/trie.h b/src/trie.h deleted file mode 100644 index 9de38be3..00000000 --- a/src/trie.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef TRIE_H -#define TRIE_H - -#include - -#include "macro.h" - -#define TRIE(T) TEMPL(trie, T) -#define TRIE_NODE(T) TEMPL(trie_node, T) - -#endif /* TRIE_H */ -#ifdef TYPE - -#include "linked_list.h" -#include "strbuf.h" - -typedef struct TRIE_NODE(TYPE) { - char c; - TYPE* value; - struct TRIE_NODE(TYPE)* next; - struct TRIE_NODE(TYPE)* child; -} TRIE_NODE(TYPE); - -typedef struct { - TRIE_NODE(TYPE)* root; -} TRIE(TYPE); - - -INIT_F ( TRIE(TYPE) ); - -INIT_F (TRIE_NODE(TYPE), char c); - -INIT_F (TRIE_NODE(TYPE), - char c, TRIE_NODE(TYPE)* next, TRIE_NODE(TYPE)* child ); - -int PUSH(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key, TYPE* val ); - -TYPE* GET(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key ); - -FREE_F(TRIE_NODE(TYPE)); - -FREE_F(TRIE(TYPE)); - -int EMPTY(TRIE(TYPE))(TRIE(TYPE)*); - -FMT_F(TRIE_NODE(TYPE)); -FMT_F(TRIE(TYPE)); - -int DEEP_COPY(TRIE_NODE(TYPE)) (TRIE_NODE(TYPE)* dest, TRIE_NODE(TYPE)* src); -int DEEP_COPY(TRIE(TYPE)) (TRIE(TYPE)* dest, TRIE(TYPE)* src); - -LLIST(strbuf)* KEYS(TRIE(TYPE)) (TRIE(TYPE)*); - -#endif /* TYPE */ diff --git a/src/trie.inc.h b/src/trie.inc.h deleted file mode 100644 index 64e5239d..00000000 --- a/src/trie.inc.h +++ /dev/null @@ -1,231 +0,0 @@ -#ifndef TYPE -#error "Set TYPE before including self file" -#else - -#include - -#include "err.h" -#include "macro.h" -#include "linked_list.inc.h" -#include "strbuf.h" - -INIT_F ( TRIE(TYPE) ) { - NEW(TRIE_NODE(TYPE), t, '\0'); - self->root = t; - return 0; -} - -INIT_F (TRIE_NODE(TYPE), char c) { - self->c = c; - self->value = NULL; - self->next = NULL; - self->child = NULL; - return 0; -} - -INIT_F (TRIE_NODE(TYPE), - char c, - TRIE_NODE(TYPE)* next, - TRIE_NODE(TYPE)* child ) -{ - self->c = c; - self->next = next; - self->child = child; - return 0; -} - -int PUSH(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key, TYPE* val ) { - TRIE_NODE(TYPE) *cur, *last; - - last = trie->root; - cur = last->child; - - char* subkey = key; - - while (1) { - if (cur == NULL) { - /* Build direct LL for remaining subkey */ - for (char* c = subkey; c[0] != '\0'; c++) { - NEW(TRIE_NODE(TYPE), t, *c); - last->child = t; - last = t; - } - last->value = RESOLVE(TYPE)(last->value, val); - return 0; - } else if (cur->c == subkey[0]) { - /* This node belongs to the key, - * Decend further */ - last = cur; - cur = cur->child; - subkey++; - } else if (subkey[0] == '\0') { - /* Key finished */ - last->value = RESOLVE(TYPE)(last->value, val); - return 0; - } else if (cur->next != NULL) { - /* This node was not part of the set, but it's sibling might */ - cur = cur->next; - /* `last` not set since we aren't moving down */ - } else { - /* No node on self level was part of the set, create a new__ - * sibling and follow down that parse */ - NEW(TRIE_NODE(TYPE), t, *subkey); - cur->next = t; - last = cur; - cur = t; - } - } - - return 0; -} - -/* - * TODO what happens when I give an invalid key? - */ -TYPE* GET(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key ) { - TRIE_NODE(TYPE)* n = trie->root->child; - char* subkey = key; - - while (n != NULL) { - if (subkey[0] == n->c) { - if (subkey[1] == '\0') { - /* Wanted node found, - * value can however be NULL */ - return n->value; - } else { - n = n->child; - subkey++; - } - } else { - n = n->next; - } - - } - - /* Position not found */ - return 0; -} - -FREE_F(TRIE_NODE(TYPE)) { - if (self == NULL) return 0; - if (self->value != NULL) FFREE(TYPE, self->value); - if (self->next != NULL) FREE(TRIE_NODE(TYPE))(self->next); - if (self->child != NULL) FREE(TRIE_NODE(TYPE))(self->child); - free (self); - return 0; -} - -FREE_F(TRIE(TYPE)) { - if (self->root->c != '\0') { - // ERR("Invalid trie"); - return 1; - } - return FREE(TRIE_NODE(TYPE))(self->root); -} - -int EMPTY(TRIE(TYPE))(TRIE(TYPE)* self) { - return self->root->child == NULL; -} - -FMT_F(TRIE_NODE(TYPE)) { - - va_list ap; - va_start(ap, buf); - int argc = va_arg(ap, int); - int depth = argc >= 1 - ? va_arg(ap, int) - : 0; - va_end(ap); - - int seek = 0; - - TRIE_NODE(TYPE)* n = self; - - if (n == NULL) { fmtf("\n"); } - while (n != NULL) { - fmtf("|"); - // FOR(int, i, depth) fmtf(" "); - for (int i = 0; i < depth; i++) fmtf(" "); - fmtf("%c ", n->c == '\0' ? '0' : n->c); - if (n->value != NULL) { - seek += FMT(TYPE)(n->value, buf + seek); - fmtf("\n"); - } - - if (n->child != NULL) { - fmtf("\n"); - seek += FMT(TRIE_NODE(TYPE))(n->child, buf + seek, depth + 1); - } - n = n->next; - } - return seek; - -} - -FMT_F(TRIE(TYPE)) { - int seek = 0; - fmtf("Trie: %p: {", self); - if (EMPTY(TRIE(TYPE))(self)) { - fmtf(" [EMPTY] "); - } else { - fmtf("\n"); - seek += FMT(TRIE_NODE(TYPE))(self->root->child, buf + seek); - } - fmtf("}"); - return seek; -} - -int DEEP_COPY(TRIE_NODE(TYPE)) (TRIE_NODE(TYPE)* dest, TRIE_NODE(TYPE)* src) { - dest->c = src->c; - - if (src->value != NULL) { - RENEW(TYPE, dest->value); - DEEP_COPY(TYPE)(dest->value, src->value); - } - - if (src->next != NULL) { - RENEW(TRIE_NODE(TYPE), dest->next, '\0'); - DEEP_COPY(TRIE_NODE(TYPE))(dest->next, src->next); - } - - if (src->child != NULL) { - RENEW(TRIE_NODE(TYPE), dest->child, '\0'); - DEEP_COPY(TRIE_NODE(TYPE))(dest->child, src->child); - } - - return 0; -} - -int DEEP_COPY(TRIE(TYPE)) (TRIE(TYPE)* dest, TRIE(TYPE)* src) { - return DEEP_COPY(TRIE_NODE(TYPE))(dest->root, src->root); -} - -void KEYS(TRIE_NODE(TYPE)) (TRIE_NODE(TYPE)* node, LLIST(strbuf)* list, strbuf* path) { - if (node == NULL) return; - - - if (node->value != NULL) { - strbuf_append(path, node->c); - NEW(strbuf, c); - DEEP_COPY(strbuf)(c, path); - PUSH(LLIST(strbuf))(list, c); - strbuf_pop(path); - } - if (node->next != NULL) { - KEYS(TRIE_NODE(TYPE)) (node->next, list, path); - } - if (node->child != NULL) { - if (node->c != '\0') strbuf_append(path, node->c); - KEYS(TRIE_NODE(TYPE)) (node->child, list, path); - if (node->c != '\0') strbuf_pop(path); - } -} - -LLIST(strbuf)* KEYS(TRIE(TYPE)) (TRIE(TYPE)* trie) { - NEW(LLIST(strbuf), retlist); - SNEW(strbuf, key); - KEYS(TRIE_NODE(TYPE)) (trie->root, retlist, &key); - return retlist; -} - -#endif /* TYPE */ diff --git a/src/vcal.c b/src/vcal.c index 3c9885ba..f9f2cb75 100644 --- a/src/vcal.c +++ b/src/vcal.c @@ -1,11 +1,16 @@ #include "vcal.h" #include +#include -#define TYPE strbuf -#include "linked_list.inc.h" -#undef TYPE +#include "guile_interface.h" +#include "err.h" +// #define TYPE strbuf +// #include "linked_list.inc.h" +// #undef TYPE + +#if 0 #define TYPE param_set #include "trie.inc.h" #undef TYPE @@ -29,13 +34,21 @@ // #include "vector.inc.h" #include "linked_list.inc.h" #undef TYPE +#endif INIT_F(vcomponent) { - INIT(TRIE(content_line), &self->clines); - INIT(LLIST(vcomponent), &self->components); + // INIT(TRIE(content_line), &self->clines); + // INIT(LLIST(vcomponent), &self->components); + + self->clines = scm_make_hash_table(scm_from_ulong(32)); + scm_gc_protect_object (self->clines); + self->components = SCM_EOL; // vcomponent_push_val (self, "X-HNH-FILENAME", "VIRTUAL"); - vcomponent_push_val (self, "X-HNH-SOURCETYPE", "virtual"); + SNEW(strbuf, s); + strbuf_load(&s, "X-HNH-SOURCETYPE"); + vcomponent_push_val (self, &s, scm_from_utf8_symbol("virtual")); + FREE(strbuf)(&s); char* type = "VIRTUAL"; self->type = (char*) calloc(sizeof(*type), strlen(type) + 1); strcpy(self->type, type); @@ -54,8 +67,11 @@ INIT_F(vcomponent, const char* type) { INIT_F(vcomponent, const char* type, const char* filename) { - INIT(TRIE(content_line), &self->clines); - INIT(LLIST(vcomponent), &self->components); + // INIT(TRIE(content_line), &self->clines); + // INIT(LLIST(vcomponent), &self->components); + self->clines = scm_make_hash_table(scm_from_ulong(32)); + scm_gc_protect_object (self->clines); + self->components = SCM_EOL; if (filename != NULL) { /* @@ -67,7 +83,9 @@ INIT_F(vcomponent, const char* type, const char* filename) { * differently (but not where the original data can be fonud * agani). */ - vcomponent_push_val (self, "X-HNH-FILENAME", filename); + SNEW(strbuf, fname); + strbuf_load (&fname, "X-HNH-FILENAME"); + vcomponent_push_val (self, &fname, scm_from_utf8_stringn(filename, strlen(filename))); } self->type = (char*) calloc(sizeof(*type), strlen(type) + 1); @@ -80,6 +98,7 @@ INIT_F(vcomponent, const char* type, const char* filename) { return 0; } +#if 0 content_line* get_attributes (vcomponent* ev, const char* key) { size_t len = strlen(key) + 1; char* cpy = (char*) (calloc(sizeof(*cpy), len)); @@ -90,22 +109,28 @@ content_line* get_attributes (vcomponent* ev, const char* key) { free (cpy); return ret; } +#endif FREE_F(vcomponent) { free(self->type); + /* if (FREE(TRIE(content_line))(&self->clines) != 0) { ERR("Error freeing vcomponent"); } + */ - FREE(LLIST(vcomponent))(&self->components); + // FREE(LLIST(vcomponent))(&self->components); return 0; } int PUSH(vcomponent)(vcomponent* parent, vcomponent* child) { child->parent = parent; - return PUSH(LLIST(vcomponent))(&parent->components, child); + SCM_PUSH_X (parent->components, scm_from_vcomponent(child)); + scm_gc_protect_object (parent->components); + return 0; + // return PUSH(LLIST(vcomponent))(&parent->components, child); } int DEEP_COPY(vcomponent)(vcomponent* a, vcomponent* b) { @@ -115,8 +140,13 @@ int DEEP_COPY(vcomponent)(vcomponent* a, vcomponent* b) { return -1; } +// TODO int vcomponent_copy(vcomponent* dest, vcomponent* src) { + ERR("Deep copy not implemented for vcomponent"); + (void) dest; + (void) src; +#if 0 DEEP_COPY(TRIE(content_line))(&dest->clines, &src->clines); /* Children are the same objects */ @@ -125,12 +155,14 @@ int vcomponent_copy(vcomponent* dest, vcomponent* src) { } PUSH(vcomponent)(src->parent, dest); +#endif return 0; } FMT_F(vcomponent) { int seek = 0; +#if 0 for (int i = 0; i < 40; i++) fmtf("_"); @@ -142,11 +174,14 @@ FMT_F(vcomponent) { FOR(LLIST, vcomponent, comp, &self->components) { seek += FMT(vcomponent)(comp, buf + seek); } +#endif + seek += sprintf(buf + seek, "#", self); return seek; } -int vcomponent_push_val (vcomponent* comp, const char* key, const char* val) { +int vcomponent_push_val (vcomponent* comp, strbuf* key, SCM val) { + /* NEW(content_line, cl); NEW(content_set, cs); strbuf_load(&cs->key, val); @@ -156,11 +191,18 @@ int vcomponent_push_val (vcomponent* comp, const char* key, const char* val) { strcpy (key_cpy, key); PUSH(TRIE(content_line))(&comp->clines, key_cpy, cl); free (key_cpy); + */ + + SCM k = scm_string_to_symbol (scm_from_utf8_stringn (key->mem, key->len)); + // TODO this should cons + scm_hashq_set_x (comp->clines, k, val); return 0; } +#if 0 char* vcomponent_get_val (vcomponent* comp, const char* key) { + /* char* key_cpy = calloc(sizeof(*key_cpy), strlen(key) + 1); strcpy (key_cpy, key); content_line* cl = GET(TRIE(content_line))(&comp->clines, key_cpy); @@ -169,6 +211,10 @@ char* vcomponent_get_val (vcomponent* comp, const char* key) { if (cl != NULL && cl->cval != NULL) { return cl->cval->key.mem; } + */ + + return scm_i_string_chars (scm_hashq_ref (comp->clines, scm_from_utf8_symbol (key), NULL)); - return NULL; + // return NULL; } +#endif diff --git a/src/vcal.h b/src/vcal.h index 2a3ad294..ed811084 100644 --- a/src/vcal.h +++ b/src/vcal.h @@ -5,12 +5,10 @@ #include -#include "strbuf.h" +#define SCM_PUSH_X(parent, child) parent = scm_cons(child, parent) +#define SCM_POP_X(lst) lst = SCM_CDR(lst) -#define TYPE strbuf -#include "linked_list.h" -// #include "trie.h" -#undef TYPE +#include "strbuf.h" /* * content_line: @@ -22,25 +20,25 @@ * A parameter key, along with a list of all its values. */ -#define param_set LLIST(strbuf) - -#define TYPE param_set -#include "trie.h" -#undef TYPE - -#define T strbuf - #define V TRIE(param_set) - #include "pair.h" - /* left := content | right := params */ - #define content_set PAIR(strbuf, TRIE(param_set)) - #undef V -#undef T - -#define TYPE content_set -#include "linked_list.h" -#undef TYPE - -#define content_line LLIST(content_set) +// #define param_set LLIST(strbuf) +// +// #define TYPE param_set +// #include "trie.h" +// #undef TYPE +// +// #define T strbuf +// #define V TRIE(param_set) +// #include "pair.h" +// /* left := content | right := params */ +// #define content_set PAIR(strbuf, TRIE(param_set)) +// #undef V +// #undef T +// +// #define TYPE content_set +// #include "linked_list.h" +// #undef TYPE +// +// #define content_line LLIST(content_set) /* * Helper macros for accessing fields in @@ -56,23 +54,25 @@ /* TRIE(param_set) */ #define CLINE_CUR_PARAMS(c) (& CLINE_CUR(c)->val) -#define TYPE content_line -#include "trie.h" -#undef TYPE +// #define TYPE content_line +// #include "trie.h" +// #undef TYPE typedef struct s_vcomponent vcomponent; -#define TYPE vcomponent -// #include "vector.h" -#include "linked_list.h" -#undef TYPE +// #define TYPE vcomponent +// // #include "vector.h" +// #include "linked_list.h" +// #undef TYPE struct s_vcomponent { /* VCALENDAR, VEVENT, ... */ char* type; vcomponent* parent; - TRIE(content_line) clines; - LLIST(vcomponent) components; + // TRIE(content_line) clines; + // LLIST(vcomponent) components; + SCM clines; + SCM components; /* * Holds a Guile representation of this object. Used to always @@ -90,12 +90,12 @@ INIT_F(vcomponent, const char* type); INIT_F(vcomponent, const char* type, const char* filename); FREE_F(vcomponent); -content_line* get_attributes (vcomponent* ev, const char* key); +// content_line* get_attributes (vcomponent* ev, const char* key); -int add_content_line (vcomponent* ev, content_line* c); +// int add_content_line (vcomponent* ev, SCM c); -int vcomponent_push_val (vcomponent*, const char* key, const char* val); -char* vcomponent_get_val (vcomponent*, const char* key); +int vcomponent_push_val (vcomponent* comp, strbuf* key, SCM val); +// char* vcomponent_get_val (vcomponent*, const char* key); /* * Appends ev to cal. Doesn't copy ev. So make sure that it wont go diff --git a/test.scm b/test.scm new file mode 100644 index 00000000..a3ea80ef --- /dev/null +++ b/test.scm @@ -0,0 +1,16 @@ +(load "module/main.scm") +(use-modules (vcomponent primitive)) + +(define root (%vcomponent-make "/home/hugo/Möte grupp.ics")) +(define cal (car (children root))) + +(use-modules (ice-9 pretty-print)) + +(pretty-print (hash-map->list cons (%vcomponent-get-hash-table cal))) +(newline) + +(for e in (children cal) + (pretty-print (hash-map->list cons (%vcomponent-get-hash-table + e))) + (display (make-string 40 #\-)) + (newline)) -- cgit v1.2.3