aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2019-02-02 19:44:39 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-02-02 19:44:39 +0100
commit382a40466a9339b8153180303ae799ed9fd7c868 (patch)
treef50514548417f291e2f2c9051971d8c8ce9d1316
parentBroke read_vcalendar of into own file. (diff)
downloadcalp-382a40466a9339b8153180303ae799ed9fd7c868.tar.gz
calp-382a40466a9339b8153180303ae799ed9fd7c868.tar.xz
Start work on a scheme interface.
-rw-r--r--Makefile17
-rw-r--r--code.scm6
-rw-r--r--scheme.c55
-rw-r--r--scheme.h12
4 files changed, 85 insertions, 5 deletions
diff --git a/Makefile b/Makefile
index e522316e..12045831 100644
--- a/Makefile
+++ b/Makefile
@@ -5,9 +5,12 @@ LEX := flex
DIRS := obj
-CFLAGS = -std=gnu99 -Wall -Wextra -pedantic -DSAFE_STR -DSAFE_HASH -ggdb
-#LFLAGS =
-#LDFLAGS =
+CFLAGS = -std=gnu99 -Wall -Wextra -pedantic \
+ -DSAFE_STR -DSAFE_HASH -ggdb \
+ -fPIC \
+ $(shell guile-config compile)
+# LFLAGS =
+LDFLAGS = -fPIC $(shell guile-config link)
C_FILES = $(wildcard *.c)
INC_FILES = $(wildcard *.inc)
@@ -16,14 +19,18 @@ H_FILES = $(wildcard *.h)
$(shell mkdir -p $(DIRS))
-all: parse
+all: parse libguile-calendar.so
obj/%.o : %.c $(H_FILES) $(INC_FILES)
$(CC) -c -o $@ $< ${CFLAGS}
+libguile-calendar.so: $(O_FILES)
+ $(CC) -shared -o $@ $^ $(LDFLAGS)
+
parse: $(O_FILES)
- $(CC) -o $@ $^ ${LDFLAGS}
+ $(CC) -o $@ $^ $(LDFLAGS)
clean:
-rm parse
-rm obj/*.o
+ -rm *.so
diff --git a/code.scm b/code.scm
new file mode 100644
index 00000000..43914e76
--- /dev/null
+++ b/code.scm
@@ -0,0 +1,6 @@
+(begin
+ (setenv "LD_LIBRARY_PATH" (getcwd))
+ (load-extension "libguile-calendar" "init_calendar")
+ (define v (make-calendar "cal")))
+
+(get-attr v 0 "description")
diff --git a/scheme.c b/scheme.c
new file mode 100644
index 00000000..05bc4b0d
--- /dev/null
+++ b/scheme.c
@@ -0,0 +1,55 @@
+#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 make_calendar (SCM path) {
+
+ vcalendar* cal =
+ (vcalendar*) scm_gc_malloc (
+ sizeof(*cal), "calendar");
+ CONSTRUCT(vcalendar, cal);
+
+ char* p = scm_to_utf8_stringn(path, NULL);
+ read_vcalendar(cal, p);
+ free(p);
+
+ return scm_make_foreign_object_1
+ (calendar_type, cal);
+
+}
+
+static SCM scm_from_strbuf(strbuf* s) {
+ return scm_from_utf8_stringn (s->mem, s->len);
+}
+
+SCM calendar_get_attr(SCM calendar, SCM id, SCM attr) {
+ scm_assert_foreign_object_type (calendar_type, calendar);
+ vcalendar* cal = scm_foreign_object_ref (calendar, 0);
+
+ vevent* v = cal->events[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_strbuf(&c->val);
+}
+
+void init_calendar () {
+ init_calendar_type();
+
+ scm_c_define_gsubr ("make-calendar", 1, 0, 0, make_calendar);
+ scm_c_define_gsubr ("get-attr", 3, 0, 0, calendar_get_attr);
+}
diff --git a/scheme.h b/scheme.h
new file mode 100644
index 00000000..c06ee1a7
--- /dev/null
+++ b/scheme.h
@@ -0,0 +1,12 @@
+#ifndef SCHEME_H
+#define SCHEME_H
+
+#include <libguile.h>
+
+SCM make_calendar(SCM path);
+
+SCM calendar_get_attr(SCM calendar, SCM id, SCM attr);
+
+void init_calendar ();
+
+#endif /* SCHEME_H */