summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Stensgård <mastensg@mastensg.net>2025-07-03 22:47:55 +0200
committerMartin Stensgård <mastensg@mastensg.net>2025-07-03 22:47:55 +0200
commit3a1cf6359332ff1f24850582d3f6925c8871bfe5 (patch)
tree1a47b2edbc937c4bf19d0ff043e576622ec83f3b
parent805bd12436e491a2d0c66d21eddd81c3159d4ebe (diff)
import_booking: ical to sqlite
-rw-r--r--Makefile6
-rw-r--r--README3
-rw-r--r--import_booking.c91
3 files changed, 99 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 95ca7e1..d260340 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ LIBS = -lraylib -lm -lpthread -lGLESv2 -lEGL \
-lical -licalss -licalvcal \
$$([ `uname -m` = aarch64 ] && echo '-lvcos -lvchiq_arm -lgbm -ldrm')
-all: check_ical opplysning
+all: check_ical import_booking opplysning
check: all
./check_ical < check_ical_in.ical > check_ical_out.txt
@@ -13,6 +13,7 @@ check: all
clean:
rm -f check_ical check_ical_out.txt
+ rm -f import_booking
rm -f opplysning
rm -f sqlite.o
rm -f sqlite3
@@ -22,6 +23,9 @@ clean:
check_ical: check_ical.c
$(CC) $(CFLAGS) -o $@ check_ical.c $(LIBS)
+import_booking: import_booking.c sqlite.o
+ $(CC) $(CFLAGS) -o $@ import_booking.c sqlite.o -lical -licalss -licalvcal
+
opplysning: opplysning.c
$(CC) $(CFLAGS) -o $@ opplysning.c $(LIBS)
diff --git a/README b/README
index 6e18110..30ec13c 100644
--- a/README
+++ b/README
@@ -25,6 +25,9 @@ Meetup
Google
https://calendar.google.com/calendar/ical/u3054u2f4kpkl7edub90faijvo@group.calendar.google.com/public/basic.ics
+ select start, end, (end-start)/60/60, summary from event where unixepoch()-6*3600 < start order by start;
+ select datetime(start, 'unixepoch'), datetime(end, 'unixepoch'), (end-start)/60/60, summary from event where unixepoch()-6*3600 < start order by start;
+
Raylib
https://github.com/raysan5/raylib/wiki/Working-on-Raspberry-Pi
diff --git a/import_booking.c b/import_booking.c
new file mode 100644
index 0000000..bd20ba3
--- /dev/null
+++ b/import_booking.c
@@ -0,0 +1,91 @@
+#include <assert.h>
+#include <err.h>
+#include <stdio.h>
+
+#include <libical/ical.h>
+
+#include "sqlite/sqlite3.h"
+
+static const char *DATABASE = "booking.db";
+
+static char *SCHEMA = "CREATE TABLE event ("
+ " start TIMESTAMP,"
+ " end TIMESTAMP,"
+ " summary TEXT"
+ ")";
+
+static char *
+read_stream(char *s, size_t size, void *d)
+{
+ return fgets(s, (int)size, (FILE *)d);
+}
+
+static void
+component(sqlite3 *db, icalcomponent *c)
+{
+ struct icaltimetype dts = icalcomponent_get_dtstart(c);
+ struct icaltimetype dte = icalcomponent_get_dtend(c);
+
+ time_t start = icaltime_as_timet(dts);
+ time_t end = icaltime_as_timet(dte);
+ const char *summary = icalcomponent_get_summary(c);
+
+ sqlite3_stmt *stmt = NULL;
+ const char *sql = "INSERT INTO event (start, end, summary) "
+ "VALUES (?, ?, ?)";
+ if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL))
+ errx(1, "sqlite3_prepare_v2");
+
+ if (sqlite3_bind_int64(stmt, 1, start))
+ errx(1, "sqlite3_bind_int64");
+ if (sqlite3_bind_int64(stmt, 2, end))
+ errx(1, "sqlite3_bind_int64");
+ if (sqlite3_bind_text(stmt, 3, summary, -1, SQLITE_STATIC))
+ errx(1, "sqlite3_bind_text");
+ if (SQLITE_DONE != sqlite3_step(stmt))
+ errx(1, "sqlite3_step");
+}
+
+int
+main(void)
+{
+ sqlite3 *db;
+ char *errmsg = NULL;
+ if (sqlite3_open(DATABASE, &db))
+ errx(1, "sqlite3_open: %s", errmsg);
+
+ if (sqlite3_exec(db, SCHEMA, NULL, NULL, &errmsg))
+ errx(1, "sqlite3_exec: %s", errmsg);
+
+ icalparser *parser = icalparser_new();
+ assert(parser);
+
+ FILE *stream = stdin;
+ assert(stream);
+
+ if (sqlite3_exec(db, "BEGIN", NULL, NULL, &errmsg))
+ errx(1, "sqlite3_exec: %s", errmsg);
+ icalparser_set_gen_data(parser, stream);
+ for (;;) {
+ char *line = icalparser_get_line(parser, read_stream);
+ if (!line) {
+ break;
+ }
+ icalcomponent *c = icalparser_add_line(parser, line);
+ if (!c) {
+ continue;
+ }
+ for (icalcompiter i = icalcomponent_begin_component(
+ c, ICAL_VEVENT_COMPONENT);
+ icalcompiter_deref(&i); icalcompiter_next(&i)) {
+ icalcomponent *ic = icalcompiter_deref(&i);
+ component(db, ic);
+ }
+ icalcomponent_free(c);
+ }
+ if (sqlite3_exec(db, "COMMIT", NULL, NULL, &errmsg))
+ errx(1, "sqlite3_exec: %s", errmsg);
+
+ icalparser_free(parser);
+ sqlite3_close(db);
+}