Compare commits
No commits in common. "d8cb527f6182be780e8b59103d962707bd49549c" and "36ede8c52da5e81040f1b993c31010dfbd8d686c" have entirely different histories.
d8cb527f61
...
36ede8c52d
@ -1,6 +1,5 @@
|
||||
#include "date_time_handling.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
|
||||
@ -16,31 +15,12 @@ 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]);
|
||||
}
|
||||
|
||||
|
@ -3,4 +3,3 @@
|
||||
|
||||
void get_date(char buffer[]);
|
||||
void pretty_print_date_time(char date_time[]);
|
||||
void print_end_date(char end_date[]);
|
||||
|
@ -6,29 +6,27 @@
|
||||
|
||||
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->date);
|
||||
printf("%s\n", head->summary);
|
||||
head = head->next;
|
||||
}
|
||||
}
|
||||
|
||||
void sorted_insert(struct event** head, char start_date[], char end_date[], char summary[]) {
|
||||
void sorted_insert(struct event** head, char 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).date, date);
|
||||
strcpy((*new_node).summary, summary);
|
||||
|
||||
if (*head == NULL || strcmp((*head)->start_date, new_node->start_date) >= 0) {
|
||||
if (*head == NULL || strcmp((*head)->date, new_node->date) >= 0) {
|
||||
new_node->next = *head;
|
||||
*head = new_node;
|
||||
}
|
||||
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)
|
||||
while (current->next!=NULL && strcmp(current->next->date, new_node->date) < 0) {
|
||||
current = current->next;
|
||||
|
||||
}
|
||||
new_node->next = current->next;
|
||||
current->next = new_node;
|
||||
}
|
||||
@ -46,11 +44,10 @@ void free_list(struct event *head)
|
||||
}
|
||||
}
|
||||
|
||||
void print_upcoming(struct event *head, char current_start_date[]) {
|
||||
void print_upcoming(struct event *head, char current_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);
|
||||
if (strcmp(head->date, current_date) >= 0) {
|
||||
pretty_print_date_time(head->date);
|
||||
printf("\n%s\n", head->summary);
|
||||
}
|
||||
head = head->next;
|
||||
|
@ -2,12 +2,11 @@
|
||||
|
||||
struct event {
|
||||
char summary[256];
|
||||
char start_date[256];
|
||||
char end_date[256];
|
||||
char date[256];
|
||||
struct event *next;
|
||||
};
|
||||
|
||||
void print_list(struct event *head);
|
||||
void sorted_insert(struct event **head, char start_date[], char end_date[], char summary[]);
|
||||
void sorted_insert(struct event **head, char date[], char summary[]);
|
||||
void free_list(struct event *head);
|
||||
void print_upcoming(struct event *head, char current_date[]);
|
||||
|
23
src/main.c
23
src/main.c
@ -32,42 +32,31 @@ int main(int argc, char **argv) {
|
||||
get_date(current_date);
|
||||
printf ("Current date and time: ");
|
||||
pretty_print_date_time(current_date);
|
||||
printf ("\n\n");
|
||||
printf ("\n");
|
||||
|
||||
char start_date[256] = "";
|
||||
char end_date[256] = "";
|
||||
char 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
|
||||
// go to DTSTART, but dont write to a 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);
|
||||
strcpy(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));
|
||||
sorted_insert(&head, date, summary);
|
||||
memset(date, '\0', sizeof(date));
|
||||
memset(summary, '\0', sizeof(summary));
|
||||
}
|
||||
memset(my_line, '\0', sizeof(my_line));
|
||||
|
Loading…
Reference in New Issue
Block a user