2024-12-09 19:27:50 +01:00
|
|
|
/*
|
2024-12-10 22:06:49 +01:00
|
|
|
* INPUT: linked list of subjects
|
|
|
|
* OUTPUT: ll of events to iCal
|
|
|
|
* ll of updated subjects to db for next day
|
|
|
|
* return events_ll to caller(ui)??
|
2024-12-09 19:27:50 +01:00
|
|
|
*
|
|
|
|
*/
|
2024-12-10 22:06:49 +01:00
|
|
|
|
|
|
|
#include "planner.h" // for subject and event structs
|
2024-12-11 13:41:31 +01:00
|
|
|
#include "config.h"
|
2024-12-11 13:03:28 +01:00
|
|
|
#include <stdio.h>
|
2024-12-11 01:08:31 +01:00
|
|
|
#include <stdlib.h>
|
2024-12-11 03:09:54 +01:00
|
|
|
#include <string.h>
|
2024-12-11 01:08:31 +01:00
|
|
|
|
2024-12-11 13:41:31 +01:00
|
|
|
const char taskFormat[] =
|
|
|
|
"Task { %.64s = {\n created={%lu},\n deadline={%lu},\n "
|
|
|
|
"priority={%d},\n spare={%lu}\n}\n";
|
|
|
|
|
|
|
|
const char eventFormat[] =
|
|
|
|
"Event { %s = {\n start={%lu},\n end={%lu},\n spare={%lu}\n}\n";
|
|
|
|
|
2024-12-11 01:08:31 +01:00
|
|
|
Event *newEvent(Task *t, time_t s, time_t e, uint64_t sp) {
|
|
|
|
Event *r = (Event *)malloc(sizeof(Event));
|
|
|
|
if (r != NULL) {
|
|
|
|
r->task = t;
|
|
|
|
r->plannedEndTime = s;
|
|
|
|
r->plannedEndTime = e;
|
|
|
|
r->spare = sp;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
2024-12-11 13:41:31 +01:00
|
|
|
|
|
|
|
// print Event to stdout
|
2024-12-11 13:03:28 +01:00
|
|
|
void printEvent(Event *s) {
|
|
|
|
printf(eventFormat, s->task->name, s->plannedStartTime, s->plannedEndTime,
|
|
|
|
s->spare);
|
|
|
|
}
|
2024-12-11 01:08:31 +01:00
|
|
|
|
2024-12-11 13:41:31 +01:00
|
|
|
/*
|
|
|
|
* creat new task with name n created c deadline d priority p spare sp
|
|
|
|
*/
|
2024-12-11 01:08:31 +01:00
|
|
|
Task *newTask(char *n, time_t c, time_t d, int p, uint64_t sp) {
|
|
|
|
Task *r = (Task *)malloc(sizeof(Task));
|
|
|
|
if (r != NULL) {
|
|
|
|
r->created = c;
|
|
|
|
r->deadline = d;
|
|
|
|
r->priority = p;
|
|
|
|
r->spare = sp;
|
|
|
|
r->name = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
2024-12-11 03:09:54 +01:00
|
|
|
|
2024-12-11 14:06:29 +01:00
|
|
|
/*
|
|
|
|
* create deepCopy of task
|
|
|
|
* !! r->name is malloced
|
|
|
|
*/
|
|
|
|
Task *copyTask(Task *t) {
|
|
|
|
Task *r = malloc(sizeof(Task));
|
|
|
|
if (r != NULL) {
|
|
|
|
r->name = strdup(t->name);
|
|
|
|
r->created = t->created;
|
|
|
|
r->deadline = t->deadline;
|
|
|
|
r->priority = t->priority;
|
|
|
|
r->spare = t->spare;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2024-12-11 13:03:28 +01:00
|
|
|
void printTask(Task *s) {
|
|
|
|
printf(taskFormat, s->name, s->created, s->deadline, s->priority, s->spare);
|
|
|
|
}
|
|
|
|
|
2024-12-11 13:41:31 +01:00
|
|
|
// return string representation of task
|
|
|
|
// NULL on failure
|
|
|
|
// free string after use
|
|
|
|
char *taskToStr(Task *t) {
|
|
|
|
char *r = malloc(sizeof(char) * strlen(taskFormat) + 0x200);
|
|
|
|
if (r != NULL) {
|
|
|
|
sprintf(r, taskFormat, t->name, t->created, t->deadline, t->priority,
|
|
|
|
t->spare);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2024-12-11 03:09:54 +01:00
|
|
|
// for llist
|
2024-12-11 13:59:57 +01:00
|
|
|
|
|
|
|
// compare task by name
|
|
|
|
int cmpTaskN(void *a, void *b) {
|
2024-12-11 11:27:00 +01:00
|
|
|
Task *aa = (Task *)a;
|
|
|
|
Task *bb = (Task *)b;
|
|
|
|
return strcmp(aa->name, bb->name);
|
|
|
|
}
|
2024-12-11 13:59:57 +01:00
|
|
|
|
|
|
|
// compare task by priority
|
|
|
|
int cmpTaskP(void *a, void *b) {
|
|
|
|
Task *aa = (Task *)a;
|
|
|
|
Task *bb = (Task *)b;
|
|
|
|
return aa->priority - bb->priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cmp event by task name
|
2024-12-11 11:27:00 +01:00
|
|
|
int cmpEvent(void *a, void *b) {
|
|
|
|
Event *aa = (Event *)a;
|
|
|
|
Event *bb = (Event *)b;
|
2024-12-11 13:59:57 +01:00
|
|
|
return cmpTaskN(aa->task, bb->task);
|
2024-12-11 11:27:00 +01:00
|
|
|
}
|