ArchiSys/TD/TD1/td1_lucas/ex_6/ex_6.c

79 lines
1.7 KiB
C
Raw Normal View History

2024-02-02 11:08:48 +01:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "include/ex_6.h"
#include "include/colors.h"
event_t* new_event(time_t time, char* desc) {
event_t* new = malloc(sizeof(event_t));
new->desc = malloc((strlen(desc)+1)*sizeof(char));
new->time = time;
new->next = NULL;
strcpy(new->desc, desc);
return new;
}
event_t* insert_event(time_t time, char* desc, event_t* events) {
if (events && time > events->time) {
events->next = insert_event(time, desc, events->next);
return events;
}
event_t* new = new_event(time, desc);
new->next = events;
return new;
}
void show_events(event_t* event) {
if (!event)
return;
printf("Event: " BOLD "%s" RESET "\n", event->desc);
printf("Date: " BOLD "%s" RESET "\n", ctime(&event->time));
show_events(event->next);
}
void free_events(event_t* event) {
if (!event)
return;
free_events(event->next);
free(event->desc);
free(event);
}
time_t time_of_string(char* string) {
struct tm date = {0};
if (sscanf(string, "%d-%d-%d %dh%d", &date.tm_mday, &date.tm_mon, &date.tm_year, &date.tm_hour, &date.tm_min) == 5) {
date.tm_mon--;
date.tm_year -= 1900;
return mktime(&date);
}
printf(BOLD RED "Failed to parse date !\n" RESET);
return (-1);
}
int main() {
event_t* events = NULL;
events = insert_event(time_of_string("19-03-2024 10h15"), "TP ArchiSys", events);
events = insert_event(time_of_string("21-03-2024 8h05"), "Cours ArchiSys", events);
events = insert_event(time_of_string("12-03-2024 10h15"), "TP ArchiSys", events);
show_events(events);
free_events(events);
return 0;
}