Compare commits
2 Commits
36ede8c52d
...
d8cb527f61
Author | SHA1 | Date | |
---|---|---|---|
d8cb527f61 | |||
3a53493e74 |
@ -1,5 +1,6 @@
|
|||||||
#include "date_time_handling.h"
|
#include "date_time_handling.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@ -15,12 +16,31 @@ void get_date(char buffer[]) {
|
|||||||
void pretty_print_date_time(char date_time[]) {
|
void pretty_print_date_time(char date_time[]) {
|
||||||
char *date = strtok(date_time, "T");
|
char *date = strtok(date_time, "T");
|
||||||
char *time = strtok(NULL, "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%c%c-", date[0], date[1], date[2], date[3]);
|
||||||
printf ("%c%c-", date[4], date[5]);
|
printf ("%c%c-", date[4], date[5]);
|
||||||
printf ("%c%c", date[6], date[7]);
|
printf ("%c%c", date[6], date[7]);
|
||||||
|
|
||||||
if (time != NULL) {
|
if (time != NULL) {
|
||||||
printf (" %c%c:", time[0], time[1]);
|
printf (" %c%c:", time[0], time[1]);
|
||||||
printf ("%c%c:", time[2], time[3]);
|
printf ("%c%c:", time[2], time[3]);
|
||||||
printf ("%c%c", time[4], time[5]);
|
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,3 +3,4 @@
|
|||||||
|
|
||||||
void get_date(char buffer[]);
|
void get_date(char buffer[]);
|
||||||
void pretty_print_date_time(char date_time[]);
|
void pretty_print_date_time(char date_time[]);
|
||||||
|
void print_end_date(char end_date[]);
|
||||||
|
@ -6,27 +6,29 @@
|
|||||||
|
|
||||||
void print_list(struct event *head) {
|
void print_list(struct event *head) {
|
||||||
while (head != NULL) {
|
while (head != NULL) {
|
||||||
printf("%s\n", head->date);
|
printf("%s\n", head->start_date);
|
||||||
|
printf("%s\n", head->end_date);
|
||||||
printf("%s\n", head->summary);
|
printf("%s\n", head->summary);
|
||||||
head = head->next;
|
head = head->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sorted_insert(struct event** head, char 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));
|
struct event *new_node = malloc(sizeof(struct event));
|
||||||
strcpy((*new_node).date, date);
|
strcpy((*new_node).start_date, start_date);
|
||||||
|
strcpy((*new_node).end_date, end_date);
|
||||||
strcpy((*new_node).summary, summary);
|
strcpy((*new_node).summary, summary);
|
||||||
|
|
||||||
if (*head == NULL || strcmp((*head)->date, new_node->date) >= 0) {
|
if (*head == NULL || strcmp((*head)->start_date, new_node->start_date) >= 0) {
|
||||||
new_node->next = *head;
|
new_node->next = *head;
|
||||||
*head = new_node;
|
*head = new_node;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Locate the node before the point of insertion
|
// Locate the node before the point of insertion
|
||||||
struct event* current = *head;
|
struct event* current = *head;
|
||||||
while (current->next!=NULL && strcmp(current->next->date, new_node->date) < 0) {
|
while (current->next!=NULL && strcmp(current->next->start_date, new_node->start_date) < 0)
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
|
||||||
new_node->next = current->next;
|
new_node->next = current->next;
|
||||||
current->next = new_node;
|
current->next = new_node;
|
||||||
}
|
}
|
||||||
@ -44,10 +46,11 @@ void free_list(struct event *head)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_upcoming(struct event *head, char current_date[]) {
|
void print_upcoming(struct event *head, char current_start_date[]) {
|
||||||
while (head != NULL) {
|
while (head != NULL) {
|
||||||
if (strcmp(head->date, current_date) >= 0) {
|
if (strcmp(head->start_date, current_start_date) >= 0) {
|
||||||
pretty_print_date_time(head->date);
|
pretty_print_date_time(head->start_date);
|
||||||
|
print_end_date(head->end_date);
|
||||||
printf("\n%s\n", head->summary);
|
printf("\n%s\n", head->summary);
|
||||||
}
|
}
|
||||||
head = head->next;
|
head = head->next;
|
||||||
|
@ -2,11 +2,12 @@
|
|||||||
|
|
||||||
struct event {
|
struct event {
|
||||||
char summary[256];
|
char summary[256];
|
||||||
char date[256];
|
char start_date[256];
|
||||||
|
char end_date[256];
|
||||||
struct event *next;
|
struct event *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
void print_list(struct event *head);
|
void print_list(struct event *head);
|
||||||
void sorted_insert(struct event **head, char date[], char summary[]);
|
void sorted_insert(struct event **head, char start_date[], char end_date[], char summary[]);
|
||||||
void free_list(struct event *head);
|
void free_list(struct event *head);
|
||||||
void print_upcoming(struct event *head, char current_date[]);
|
void print_upcoming(struct event *head, char current_date[]);
|
||||||
|
23
src/main.c
23
src/main.c
@ -32,31 +32,42 @@ int main(int argc, char **argv) {
|
|||||||
get_date(current_date);
|
get_date(current_date);
|
||||||
printf ("Current date and time: ");
|
printf ("Current date and time: ");
|
||||||
pretty_print_date_time(current_date);
|
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] = "";
|
char summary[256] = "";
|
||||||
|
|
||||||
while(read_until_nl(myfd, my_line)) {
|
while(read_until_nl(myfd, my_line)) {
|
||||||
if (strncmp(my_line, "BEGIN:VEVENT", 12) == 0) {
|
if (strncmp(my_line, "BEGIN:VEVENT", 12) == 0) {
|
||||||
memset(my_line, '\0', sizeof(my_line));
|
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");
|
seek_string_a(myfd, "DTSTART");
|
||||||
read_until_string(myfd, my_line, "DTEND");
|
read_until_string(myfd, my_line, "DTEND");
|
||||||
remove_whitespace(my_line);
|
remove_whitespace(my_line);
|
||||||
cut_string(my_line, ':', 1);
|
cut_string(my_line, ':', 1);
|
||||||
strcpy(date, my_line);
|
strcpy(start_date, my_line);
|
||||||
|
|
||||||
memset(my_line, '\0', sizeof(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:");
|
seek_string_a(myfd, "SUMMARY:");
|
||||||
read_until_string(myfd, my_line, "TRANSP:");
|
read_until_string(myfd, my_line, "TRANSP:");
|
||||||
remove_nl_and_cr(my_line);
|
remove_nl_and_cr(my_line);
|
||||||
strcpy(summary, my_line);
|
strcpy(summary, my_line);
|
||||||
memset(my_line, '\0', sizeof(my_line));
|
memset(my_line, '\0', sizeof(my_line));
|
||||||
|
|
||||||
sorted_insert(&head, date, summary);
|
sorted_insert(&head, start_date, end_date, summary);
|
||||||
memset(date, '\0', sizeof(date));
|
memset(start_date, '\0', sizeof(start_date));
|
||||||
|
memset(end_date, '\0', sizeof(end_date));
|
||||||
memset(summary, '\0', sizeof(summary));
|
memset(summary, '\0', sizeof(summary));
|
||||||
}
|
}
|
||||||
memset(my_line, '\0', sizeof(my_line));
|
memset(my_line, '\0', sizeof(my_line));
|
||||||
|
Loading…
Reference in New Issue
Block a user