aboutsummaryrefslogtreecommitdiff
path: root/scheme.scm.c
blob: d148a43538ac11cb7827ca362d2a6866d358320c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "scheme.h"

#include "macro.h"
#include "calendar.h"
#include "strbuf.h"

static SCM calendar_type;

void init_calendar_type (void) {
	SCM name = scm_from_utf8_symbol("calendar");
	SCM slots = scm_list_1(scm_from_utf8_symbol("data"));

	calendar_type = scm_make_foreign_object_type(name, slots, NULL);
}

SCM_DEFINE (make_calendar, "make-calendar", 1, 0, 0,
		(SCM path),
		"Loads a vdir iCalendar from the given path.")
{
	vcomponent* cal =
		(vcomponent*) scm_gc_malloc (
				sizeof(*cal), "calendar");
	INIT(vcomponent, cal, "ROOT");

	char* p = scm_to_utf8_stringn(path, NULL);
	read_vcalendar(cal, p);
	free(p);

	return scm_make_foreign_object_1
		(calendar_type, cal);

}

SCM scm_from_strbuf(strbuf* s) {
	return scm_from_utf8_stringn (s->mem, s->len - 2);
}

SCM scm_from_llist(LLIST(strbuf)* lst) {

	SCM llist = SCM_EOL;

	for ( LINK(strbuf)* n = FIRST(lst);
			n->after != NULL;
			n = n->after) {
		llist = scm_cons(scm_from_strbuf(n->value), llist);
	}
	return llist;
}

SCM_DEFINE (calendar_get_attr, "calendar-get-attr", 3, 0, 0,
		(SCM calendar, SCM id, SCM attr),
		"Retuns the given attribute from the vevent object at index in calendar.")
{
	scm_assert_foreign_object_type (calendar_type, calendar);
	vcomponent* cal = scm_foreign_object_ref (calendar, 0);

	vcomponent* v = cal->components.items[scm_to_int(id)];
	char* key = scm_to_utf8_stringn(scm_string_upcase(attr), NULL);
	content_line* c = get_property (v, key);
	free(key);

	if (c == NULL) return SCM_BOOL_F;

	return scm_from_llist(&c->vals);
}

SCM scm_from_trie_node(TRIE_NODE(content_line)* node) {

	TRIE_NODE(content_line)* n = node->child;
	SCM childs = SCM_EOL;
	while (n != NULL) {
		childs = scm_cons(scm_from_trie_node(n), childs);
		n = n->next;
	}
	SCM lst;
	if (node->value != NULL) {
		lst = scm_cons(scm_from_char(node->c),
				scm_from_llist(&node->value->vals));
	} else {
		lst = scm_list_1(scm_from_char(node->c));
	}
	return scm_cons (lst, childs);
}

SCM scm_from_trie(TRIE(content_line)* trie) {
	return scm_from_trie_node (trie->root);
}

SCM scm_from_vector(VECT(vcomponent)* vect) {
	SCM l = SCM_EOL;
	for (size_t i = 0; i < vect->length; i++) {
		l = scm_cons(
				scm_make_foreign_object_1 (calendar_type, GET(VECT(vcomponent))(vect, i)),
				l);
	}
	return scm_reverse(l);
}

SCM_DEFINE (calendar_size, "calendar-size", 1, 0, 0,
		(SCM calendar),
		"Returns number of events in a vcalendar.")
{
	scm_assert_foreign_object_type (calendar_type, calendar);
	vcomponent* cal = scm_foreign_object_ref (calendar, 0);
	return scm_from_size_t (cal->components.length);
}

SCM_DEFINE (calendar_properties, "primitive-get-properties", 1, 0, 0,
		(SCM calendar),
		"Returns the TRIE from a calendar object")
{
	scm_assert_foreign_object_type (calendar_type, calendar);
	vcomponent* cal = scm_foreign_object_ref (calendar, 0);
	return scm_from_trie(&cal->clines);
}

SCM_DEFINE(calendar_components, "get-components", 1, 0, 0,
		(SCM component),
		"")
{
	scm_assert_foreign_object_type (calendar_type, component);
	vcomponent* cal = scm_foreign_object_ref (component, 0);
	return scm_from_vector(&cal->components);
}

SCM_DEFINE(component_type, "component-type", 1, 0, 0,
		(SCM component),
		"Returns type of vcomponent#")
{
	scm_assert_foreign_object_type (calendar_type, component);
	vcomponent* comp = scm_foreign_object_ref (component, 0);
	return scm_from_utf8_symbol(comp->type);
}

void init_calendar () {
	init_calendar_type();

#ifndef SCM_MAGIC_SNARFER
#include "scheme.x"
#endif
}