aboutsummaryrefslogtreecommitdiff
path: root/src/guile_interface.scm.c
blob: 1a843f4a2d65c5b5f6415f3609e345ce8de1f681 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "guile_interface.h"

#include "calendar.h"
#include "guile_type_helpers.h"

static SCM vcomponent_type;

void init_vcomponent_type (void) {
	SCM name = scm_from_utf8_symbol("vcomponent");
	SCM slots = scm_list_1(scm_from_utf8_symbol("data"));

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

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

	if (SCM_UNBNDP(path)) {
		INIT(vcomponent, cal);
	} else {
		INIT(vcomponent, cal, "ROOT");

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

	return scm_from_vcomponent (cal);
}

/*
 * Returns a line from a component.
 */
SCM_DEFINE (vcomponent_get_attribute, "%vcomponent-get-attribute", 2, 0, 0,
		(SCM calendar, SCM attr),
		"Retuns the given attribute from the vevent object at index in calendar.")
{
	scm_assert_foreign_object_type (vcomponent_type, calendar);
	vcomponent* cal = scm_foreign_object_ref (calendar, 0);

	char* key = scm_to_utf8_stringn(scm_string_upcase(attr), NULL);
	content_line* c = get_attributes (cal, key);
	free(key);

	if (c == NULL) return SCM_BOOL_F;

	SCM llist = SCM_EOL;
	FOR (LLIST, content_set, v, c) {
		llist = scm_cons(scm_from_strbuf(&v->key), llist);
	}

	/* returns the car of list if list is one long. */
	if (scm_to_int(scm_length(llist)) == 1) {
		return SCM_CAR(llist);
	} else {
		return llist;
	}
}

SCM_DEFINE (vcomponent_get_property, "%vcomponent-get-property", 3, 0, 0,
            (SCM component, SCM attr, SCM prop),
            "")
{
	scm_assert_foreign_object_type (vcomponent_type, component);
	vcomponent* comp = scm_foreign_object_ref (component, 0);

	char* key      = scm_to_utf8_stringn(scm_string_upcase(attr), NULL);
	char* prop_key = scm_to_utf8_stringn(scm_string_upcase(prop), NULL);
	content_line* cl = get_attributes (comp, key);
	free(key);

	if (cl == NULL) return SCM_BOOL_F;

	SCM llist = SCM_EOL;
	FOR (LLIST, content_set, cs, cl) {
		LLIST(strbuf)* strs = GET(TRIE(param_set))(&cs->val, prop_key);
		if (strs == NULL) continue;
		SCM subl = SCM_EOL;
		FOR (LLIST, strbuf, s, strs) {
			subl = scm_cons(scm_from_strbuf(s), subl);
		}
		llist = scm_cons(subl, llist);
	}

	return llist;
}

SCM_DEFINE (vcomponent_set_attr_x, "%vcomponent-set-attribute!", 3, 0, 0,
		(SCM component, SCM attr, SCM new_value),
		"")
{
	scm_assert_foreign_object_type (vcomponent_type, component);
	vcomponent* com = scm_foreign_object_ref (component, 0);

	char* key = scm_to_utf8_stringn(scm_string_upcase(attr), NULL);
	content_line* c = get_attributes (com, key);

	/* Create the position in the TRIE if it doesn't already exist */
	if (c == NULL) {
		/* Insert empty key since this allows me to use the helper
		 * function */
		vcomponent_push_val(com, key, "");
		c = get_attributes (com, key);
	} else {
		/*
		 * The SCM representation of an object is usually initialized
		 * when an attribute is first accessed from guile. If we
		 * however try to set it before accessing it then that field
		 * will still be NULL.
		 *
		 * If the object, and its SCM instance exist, unprotect it.
		 * Otherwise we know that it's NULL and safe to write a new
		 * SCM value there.
		 */
		if (c->cval->key.scm != NULL) {
			scm_gc_unprotect_object(c->cval->key.scm);
		}
	}

	free(key);

	strbuf* target = &c->cval->key;
	target->scm = new_value;
	scm_gc_protect_object(target->scm);

	return SCM_UNSPECIFIED;
}

SCM_DEFINE (vcomponent_child_count, "%vcomponent-child-count", 1, 0, 0,
		(SCM component),
		"Returns number of child components.")
{
	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));
}

SCM_DEFINE(vcomponent_children, "%vcomponent-children", 1, 0, 0,
		(SCM component),
		"")
{
	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_DEFINE(vcomponent_filter_children_x, "%vcomponent-filter-children!",
		2, 0, 0,
		(SCM pred, SCM component),
	   "Remove all children from component who DOESN'T satisfy `pred`")
{
	scm_assert_foreign_object_type (vcomponent_type, component);
	vcomponent* cal = scm_foreign_object_ref (component, 0);

	for (LINK(vcomponent)* l = FIRST(&cal->components);
			l->after != NULL;
			l = l->after)
	{
		if (scm_is_false(scm_call_1 (pred, scm_from_vcomponent(l->value)))) {
			FFREE(vcomponent, l->value);
			UNLINK(LINK(vcomponent))(l);
		}
	}

	return SCM_UNSPECIFIED;
}

SCM_DEFINE(vcomponent_push_child_x, "%vcomponent-push-child!", 2, 0, 0,
               (SCM component, SCM child),
               "")
{
	scm_assert_foreign_object_type (vcomponent_type, component);
	scm_assert_foreign_object_type (vcomponent_type, child);
	vcomponent* comp = scm_foreign_object_ref (component, 0);
	vcomponent* chil = scm_foreign_object_ref (child, 0);

	PUSH(vcomponent)(comp, chil);

	return SCM_UNSPECIFIED;
}

SCM_DEFINE (vcomponent_parent, "%vcomponent-parent", 1, 0, 0,
		(SCM component),
		"")
{
	scm_assert_foreign_object_type (vcomponent_type, component);
	vcomponent* comp = scm_foreign_object_ref (component, 0);

	vcomponent* parent = comp->parent;
	if (strcmp(parent->type, "ROOT") == 0) {
		return SCM_BOOL_F;
	} else {
		return scm_from_vcomponent(parent);
	}
}

SCM_DEFINE(vcomponent_typeof, "%vcomponent-get-type", 1, 0, 0,
		(SCM component),
		"Returns type of vcomponent")
{
	scm_assert_foreign_object_type (vcomponent_type, component);
	vcomponent* comp = scm_foreign_object_ref (component, 0);
	return scm_from_utf8_symbol(comp->type);
}

SCM_DEFINE(vcomponent_set_type_x, "%vcomponent-set-type!", 2, 0, 0,
		(SCM component, SCM type),
		"Replace current type of vcomponent")
{
	scm_assert_foreign_object_type (vcomponent_type, component);
	vcomponent* comp = scm_foreign_object_ref (component, 0);

	if (comp->type) free (comp->type);

	char* ntype = scm_to_utf8_stringn (type, NULL);
	comp->type = calloc(sizeof(*ntype), strlen(ntype) + 1);
	strcpy(comp->type, ntype);

	return SCM_UNSPECIFIED;
}

SCM scm_from_vcomponent(vcomponent* v) {
	if (v->scm == NULL) {
		v->scm = scm_make_foreign_object_1 (vcomponent_type, v);
		scm_gc_protect_object(v->scm);
	}
	return v->scm;
}

SCM_DEFINE(vcomponent_attr_list, "%vcomponent-attribute-list", 1, 0, 0,
		(SCM component),
		"Returns list of all keys in component.")
{
	scm_assert_foreign_object_type (vcomponent_type, component);
	vcomponent* comp = scm_foreign_object_ref (component, 0);
	LLIST(strbuf)* keys = KEYS(TRIE(content_line))(&comp->clines);

	SCM llist = SCM_EOL;
	FOR (LLIST, strbuf, s, keys) {
		llist = scm_cons(scm_from_strbuf(s), llist);
	}

	FFREE(LLIST(strbuf), keys);

	return llist;
}

SCM_DEFINE(vcomponent_prop_list, "%vcomponent-property-list", 2, 0, 0,
           (SCM component, SCM attr),
           "")
{
	scm_assert_foreign_object_type (vcomponent_type, component);
	vcomponent* comp = scm_foreign_object_ref (component, 0);

	char* key      = scm_to_utf8_stringn(scm_string_upcase(attr), NULL);
	content_line* cl = get_attributes (comp, key);
	free(key);

	if (cl == NULL) return SCM_BOOL_F;

	SCM llist = SCM_EOL;
	FOR (LLIST, content_set, cs, cl) {
		LLIST(strbuf)* keys = KEYS(TRIE(param_set))(&cs->val);
		if (keys == NULL) continue;

		FOR (LLIST, strbuf, s, keys) {
			SCM symb = scm_string_to_symbol(scm_from_strbuf(s));
			if ( scm_is_false(scm_memv (symb, llist)) ) {
				llist = scm_cons(symb, llist);
			}
		}

		FFREE(LLIST(strbuf), keys);
	}

	return llist;
}

SCM_DEFINE(vcomponent_shallow_copy, "%vcomponent-shallow-copy", 1, 0, 0,
           (SCM component),
           "Creates a shallow copy of the given component.")
{
	scm_assert_foreign_object_type (vcomponent_type, component);
	vcomponent* src = scm_foreign_object_ref (component, 0);

	vcomponent* dest =
		(vcomponent*) scm_gc_malloc (
				sizeof(*dest), "vcomponent");
	INIT(vcomponent, dest, src->type, NULL);
	vcomponent_copy (dest, src);
	return scm_from_vcomponent (dest);
}

void init_lib (void) {
	init_vcomponent_type();

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