StudyPlanner/src/planner.c

109 lines
2.4 KiB
C
Raw Normal View History

/*
* 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)??
*
*/
#include "planner.h" // for subject and event structs
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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";
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;
}
// print Event to stdout
void printEvent(Event *s) {
printf(eventFormat, s->task->name, s->plannedStartTime, s->plannedEndTime,
s->spare);
}
/*
* creat new task with name n created c deadline d priority p spare sp
*/
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 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;
}
void printTask(Task *s) {
printf(taskFormat, s->name, s->created, s->deadline, s->priority, s->spare);
}
// 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;
}
// for llist
// compare task by name
int cmpTaskN(void *a, void *b) {
Task *aa = (Task *)a;
Task *bb = (Task *)b;
return strcmp(aa->name, bb->name);
}
// 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
int cmpEvent(void *a, void *b) {
Event *aa = (Event *)a;
Event *bb = (Event *)b;
return cmpTaskN(aa->task, bb->task);
}