added end_date and function to print it accordingly

This commit is contained in:
bjt-user 2023-08-24 01:55:50 +02:00
parent 3a53493e74
commit d8cb527f61
5 changed files with 47 additions and 11 deletions

View File

@ -1,5 +1,6 @@
#include "date_time_handling.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
@ -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]);
}

View File

@ -3,3 +3,4 @@
void get_date(char buffer[]);
void pretty_print_date_time(char date_time[]);
void print_end_date(char end_date[]);

View File

@ -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;

View File

@ -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[]);

View File

@ -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));