diff --git a/src/main.c b/src/main.c index 31dc144..ed5638c 100644 --- a/src/main.c +++ b/src/main.c @@ -3,15 +3,8 @@ #include "cli_arg_parsing.h" #include "date_time_handling.h" #include "string_handling.h" -#include "read_until_nl.h" -#include "read_until_string.h" #include "seek_string_a.h" #include -#include -#include -#include -#include -#include int main(int argc, char **argv) { char *ics_path = ""; @@ -19,15 +12,6 @@ int main(int argc, char **argv) { get_cli_args(argc, argv, &ics_path, &show_all_events); - char my_event[8192] = ""; - char unfolded_event[8192] = ""; - - int myfd = open(ics_path, O_RDONLY); - if (myfd == -1) { - perror ("Error opening file"); - return 1; - } - // initialize linked list struct event *head = NULL; @@ -37,17 +21,7 @@ int main(int argc, char **argv) { pretty_print_date_time(current_date); printf ("\n\n"); - while(read_until_nl(myfd, my_event)) { - if (strncmp(my_event, "BEGIN:VEVENT", 12) == 0) { - // include the BEGIN:EVENT to not loose the new line of first field - lseek(myfd, -1, SEEK_CUR); - memset(my_event, '\0', sizeof(my_event)); - read_until_string(myfd, my_event, "END:VEVENT"); - unfolding_string(my_event, unfolded_event); - parse_event(unfolded_event, &head); - } - memset(my_event, '\0', sizeof(my_event)); - } + parse_ics_file(ics_path, &head); print_upcoming(head, current_date, show_all_events); diff --git a/src/parse_ics.c b/src/parse_ics.c index 1822c70..5341be7 100644 --- a/src/parse_ics.c +++ b/src/parse_ics.c @@ -1,11 +1,15 @@ #include "list_handling.h" #include "parse_ics.h" +#include "read_until_nl.h" +#include "read_until_string.h" #include "string_handling.h" -#include -#include #include #include +#include #include +#include +#include +#include void parse_event(char event_string[], struct event **head) { char *start_date = strstr(event_string, "\nDTSTART"); @@ -67,3 +71,26 @@ void unfolding_string(char *folded_string, char *unfolded_string) { } } } + +void parse_ics_file(char *file_path, struct event **head) { + char my_event[8192] = ""; + char unfolded_event[8192] = ""; + + int myfd = open(file_path, O_RDONLY); + if (myfd == -1) { + perror ("Error opening file"); + exit(1); + } + + while(read_until_nl(myfd, my_event)) { + if (strncmp(my_event, "BEGIN:VEVENT", 12) == 0) { + // include the BEGIN:EVENT to not loose the new line of first field + lseek(myfd, -1, SEEK_CUR); + memset(my_event, '\0', sizeof(my_event)); + read_until_string(myfd, my_event, "END:VEVENT"); + unfolding_string(my_event, unfolded_event); + parse_event(unfolded_event, head); + } + memset(my_event, '\0', sizeof(my_event)); + } +} diff --git a/src/parse_ics.h b/src/parse_ics.h index 8e02cec..e114a14 100644 --- a/src/parse_ics.h +++ b/src/parse_ics.h @@ -2,3 +2,4 @@ void parse_event(char event_string[], struct event **head); void unfolding_string(char* folded_string, char* unfolded_string); +void parse_ics_file(char *file_path, struct event **head);