From 0955921918926a9c51c572f2629ad0a9ca498d9e Mon Sep 17 00:00:00 2001 From: bjoernf Date: Sat, 16 Sep 2023 12:02:22 +0200 Subject: [PATCH] checked out changes from fix-location-field-bug branch --- src/main.c | 47 +++++++++------------------------------------- src/parse_ics.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ src/parse_ics.h | 3 +++ 3 files changed, 62 insertions(+), 38 deletions(-) create mode 100644 src/parse_ics.c create mode 100644 src/parse_ics.h diff --git a/src/main.c b/src/main.c index f3d5d8f..dcf759a 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,7 @@ +#include "list_handling.h" +#include "parse_ics.h" #include "cli_arg_parsing.h" #include "date_time_handling.h" -#include "list_handling.h" #include "string_handling.h" #include "move_lines.h" #include "read_until_nl.h" @@ -16,7 +17,7 @@ int main(int argc, char **argv) { char *ics_path = ""; get_cli_args(argc, argv, &ics_path); - char my_line[4096] = ""; + char my_event[8192] = ""; int myfd = open(ics_path, O_RDONLY); if (myfd == -1) { @@ -33,43 +34,13 @@ int main(int argc, char **argv) { pretty_print_date_time(current_date); printf ("\n\n"); - char start_date[256] = ""; - char end_date[256] = ""; - char summary[256] = ""; - - while(read_until_nl(myfd, my_line)) { - if (strncmp(my_line, "BEGIN:VEVENT", 12) == 0) { - memset(my_line, '\0', sizeof(my_line)); - // put DTSTART into variable - seek_string_a(myfd, "DTSTART"); - read_until_string(myfd, my_line, "DTEND"); - remove_whitespace(my_line); - cut_string(my_line, ':', 1); - strcpy(start_date, my_line); - - memset(my_line, '\0', sizeof(my_line)); - - // put DTEND into variable - read_until_string(myfd, my_line, "SEQUENCE"); - remove_whitespace(my_line); - cut_string(my_line, ':', 1); - strcpy(end_date, my_line); - - memset(my_line, '\0', sizeof(my_line)); - - // put summary into variable - seek_string_a(myfd, "SUMMARY:"); - read_until_string(myfd, my_line, "TRANSP:"); - remove_nl_and_cr(my_line); - strcpy(summary, my_line); - memset(my_line, '\0', sizeof(my_line)); - - sorted_insert(&head, start_date, end_date, summary); - memset(start_date, '\0', sizeof(start_date)); - memset(end_date, '\0', sizeof(end_date)); - memset(summary, '\0', sizeof(summary)); + while(read_until_nl(myfd, my_event)) { + if (strncmp(my_event, "BEGIN:VEVENT", 12) == 0) { + memset(my_event, '\0', sizeof(my_event)); + read_until_string(myfd, my_event, "END:VEVENT"); + parse_event(my_event, &head); } - memset(my_line, '\0', sizeof(my_line)); + memset(my_event, '\0', sizeof(my_event)); } print_upcoming(head, current_date); diff --git a/src/parse_ics.c b/src/parse_ics.c new file mode 100644 index 0000000..13a4bba --- /dev/null +++ b/src/parse_ics.c @@ -0,0 +1,50 @@ +#include "list_handling.h" +#include "parse_ics.h" +#include "string_handling.h" +#include +#include + +void parse_event(char event_string[], struct event **head) { + char *start_date = strstr(event_string, "\r\nDTSTART;"); + char *end_date = strstr(event_string, "\r\nDTEND;"); + char *sequence = strstr(event_string, "\r\nSEQUENCE:"); + char *summary = strstr(event_string, "\r\nSUMMARY:"); + char *location = strstr(event_string, "\r\nLOCATION:"); + char *transp = strstr(event_string, "\r\nTRANSP:"); + + char *start_date_str = malloc(256); + char *end_date_str = malloc(256); + char *summary_str = malloc(512); + + memset(start_date_str, '\0', 256); + memset(end_date_str, '\0', 256); + memset(summary_str, '\0', 512); + + strncpy(start_date_str, start_date, end_date - start_date); + + strncpy(end_date_str, end_date, sequence - end_date); + + if (location != NULL) { + strncpy(summary_str,\ + summary + strlen("\r\nSUMMARY:"),\ + location - summary - strlen("\r\nSUMMARY:")); + } else { + strncpy(summary_str,\ + summary + strlen("\r\nSUMMARY:"),\ + transp - summary - strlen("\r\nSUMMARY:")); + } + + // parse dates + remove_whitespace(start_date_str); + cut_string(start_date_str, ':', 1); + + remove_whitespace(end_date_str); + cut_string(end_date_str, ':', 1); + + sorted_insert(head, start_date_str, end_date_str, summary_str); + + free(start_date_str); + free(end_date_str); + free(summary_str); +} + diff --git a/src/parse_ics.h b/src/parse_ics.h new file mode 100644 index 0000000..d7cf4f9 --- /dev/null +++ b/src/parse_ics.h @@ -0,0 +1,3 @@ +#pragma once + +void parse_event(char event_string[], struct event **head);