diff --git a/src/date_time_handling.c b/src/date_time_handling.c index 69aa8ea..e7e6a17 100644 --- a/src/date_time_handling.c +++ b/src/date_time_handling.c @@ -1,5 +1,6 @@ #include "date_time_handling.h" #include +#include #include #include @@ -15,12 +16,31 @@ void get_date(char buffer[]) { void pretty_print_date_time(char date_time[]) { char *date = strtok(date_time, "T"); char *time = strtok(NULL, "T"); + if (date == NULL) { + printf ("\nError: date points to NULL!\n"); + exit(1); + } printf ("%c%c%c%c-", date[0], date[1], date[2], date[3]); printf ("%c%c-", date[4], date[5]); printf ("%c%c", date[6], date[7]); + if (time != NULL) { printf (" %c%c:", time[0], time[1]); printf ("%c%c:", time[2], time[3]); printf ("%c%c", time[4], time[5]); } } + +void print_end_date(char end_date[]) { + // end_date is all day event + if (strlen(end_date) == 8) + return; + + strtok(end_date, "T"); + char *time = strtok(NULL, "T"); + + printf (" - %c%c:", time[0], time[1]); + printf ("%c%c:", time[2], time[3]); + printf ("%c%c", time[4], time[5]); +} + diff --git a/src/date_time_handling.h b/src/date_time_handling.h index e4e815f..9a9415b 100644 --- a/src/date_time_handling.h +++ b/src/date_time_handling.h @@ -3,3 +3,4 @@ void get_date(char buffer[]); void pretty_print_date_time(char date_time[]); +void print_end_date(char end_date[]); diff --git a/src/list_handling.c b/src/list_handling.c index 612c406..b91f5d5 100644 --- a/src/list_handling.c +++ b/src/list_handling.c @@ -7,14 +7,16 @@ void print_list(struct event *head) { while (head != NULL) { printf("%s\n", head->start_date); + printf("%s\n", head->end_date); printf("%s\n", head->summary); head = head->next; } } -void sorted_insert(struct event** head, char start_date[], char summary[]) { +void sorted_insert(struct event** head, char start_date[], char end_date[], char summary[]) { struct event *new_node = malloc(sizeof(struct event)); strcpy((*new_node).start_date, start_date); + strcpy((*new_node).end_date, end_date); strcpy((*new_node).summary, summary); if (*head == NULL || strcmp((*head)->start_date, new_node->start_date) >= 0) { @@ -24,9 +26,9 @@ void sorted_insert(struct event** head, char start_date[], char summary[]) { else { // Locate the node before the point of insertion struct event* current = *head; - while (current->next!=NULL && strcmp(current->next->start_date, new_node->start_date) < 0) { - current = current->next; - } + while (current->next!=NULL && strcmp(current->next->start_date, new_node->start_date) < 0) + current = current->next; + new_node->next = current->next; current->next = new_node; } @@ -48,6 +50,7 @@ void print_upcoming(struct event *head, char current_start_date[]) { while (head != NULL) { if (strcmp(head->start_date, current_start_date) >= 0) { pretty_print_date_time(head->start_date); + print_end_date(head->end_date); printf("\n%s\n", head->summary); } head = head->next; diff --git a/src/list_handling.h b/src/list_handling.h index 6a083ad..fde08bd 100644 --- a/src/list_handling.h +++ b/src/list_handling.h @@ -3,10 +3,11 @@ struct event { char summary[256]; char start_date[256]; + char end_date[256]; struct event *next; }; void print_list(struct event *head); -void sorted_insert(struct event **head, char start_date[], char summary[]); +void sorted_insert(struct event **head, char start_date[], char end_date[], char summary[]); void free_list(struct event *head); void print_upcoming(struct event *head, char current_date[]); diff --git a/src/main.c b/src/main.c index 6e21055..74902f8 100644 --- a/src/main.c +++ b/src/main.c @@ -32,31 +32,42 @@ int main(int argc, char **argv) { get_date(current_date); printf ("Current date and time: "); pretty_print_date_time(current_date); - printf ("\n"); + printf ("\n\n"); - char date[256] = ""; + 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)); - // go to DTSTART, but dont write to a variable + // 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(date, my_line); + 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, date, summary); - memset(date, '\0', sizeof(date)); + 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)); } memset(my_line, '\0', sizeof(my_line));