commit 20afd95306d3ccf4abdc1ec2a02f95ed700ed815
parent 73653992eaa307d59685d00ce742f46c4fd737af
Author: Andreas Gruhler <agruhl@gmx.ch>
Date: Sun, 3 Oct 2021 09:49:27 +0200
import all VEVENTS
Diffstat:
2 files changed, 52 insertions(+), 19 deletions(-)
diff --git a/src/import.c b/src/import.c
@@ -15,12 +15,30 @@ void ics_import(const char* ics_input) {
ics[ics_bytes] = 0;
// fprintf(stderr, "Import ICS file: %s\n", ics);
+ // find all VEVENTs
+
long search_pos = 0;
- char* desc = extract_ical_field(ics, "BEGIN", &search_pos, false);
- if (desc != NULL) {
- fprintf(stderr, "Import DESCRIPTION: %s\n", desc);
- fprintf(stderr, "Search pos: %li\n", search_pos);
+ char *i = ics;
+ char* vevent;
+ char* date;
+ char* desc;
+
+ for (;;) {
+ vevent = extract_ical_field(i, "BEGIN:VEVENT", &search_pos, false);
+ if (vevent == NULL) {
+ break;
+ }
+ // date = extract_ical_field((ics+search_pos), "DTSTART", &search_pos, false);
+ desc = extract_ical_field(i, "DESCRIPTION", &search_pos, true);
+ // fprintf(stderr, "Import DTSTART: %s\n", desc);
+ // fprintf(stderr, "Import DESCRIPTION: %s\n", desc);
+ fprintf(stderr, "* * * * * * * * * * * * * \n");
+
+ free(vevent);
+ // free(date);
free(desc);
+
+ i += search_pos;
}
free(ics);
}
\ No newline at end of file
diff --git a/src/utils.c b/src/utils.c
@@ -169,6 +169,12 @@ char* unfold(const char* str) {
char* res = strtok(strcp, "\n");
+ if (res == NULL) {
+ fprintf(stderr, "No more lines in multiline string, stop unfolding.\n");
+ free(strcp);
+ return NULL;
+ }
+
char* buf = malloc(strlen(res) + 1);
if (buf == NULL) {
perror("malloc failed");
@@ -188,6 +194,11 @@ char* unfold(const char* str) {
while (res != NULL) {
res = strtok(NULL, "\n");
+ if (res == NULL) {
+ fprintf(stderr, "No more lines in multiline string, stop unfolding.\n");
+ break;
+ }
+
if (regexec(&re, res, 1, pm, 0) == 0) {
// Stop unfolding if line does not start with white space/tab:
// https://datatracker.ietf.org/doc/html/rfc2445#section-4.1
@@ -195,10 +206,9 @@ char* unfold(const char* str) {
}
newbuf = realloc(buf, strlen(buf) + strlen(res) + 1);
- if (buf == NULL) {
+ if (newbuf == NULL) {
perror("realloc failed");
- free(buf);
- return NULL;
+ break;
} else {
buf = newbuf;
strcat(buf, res + 1);
@@ -249,28 +259,33 @@ char* extract_ical_field(const char* ics, char* key, long* start_pos, bool multi
fprintf(stderr, "Res: %s\n", res);
*start_pos = res - icscp;
fprintf(stderr, "Start pos: %li\n", *start_pos);
-
- if (strlen(res) == 0) {
- // empty remote value
- res = NULL;
- } else if (multiline) {
- res = unfold(icscp + *start_pos);
- }
break;
}
// key not in this line, advance line
res = strtok(NULL, "\n");
}
- char* buf = malloc(strlen(res) + 1);
- if (buf == NULL) {
- perror("malloc failed");
- return NULL;
+ char* buf = NULL;
+
+ if (res != NULL) {
+ if (strlen(res) == 0) {
+ // empty remote value
+ buf = NULL;
+ } else if (multiline) {
+ buf = unfold(icscp + *start_pos);
+ } else {
+ buf = malloc(strlen(res) + 1);
+ if (buf == NULL) {
+ perror("malloc failed");
+ return NULL;
+ }
+ strcpy(buf, res);
+ }
}
- strcpy(buf, res);
regfree(&re);
free(icscp);
+
return buf;
}