modified: src/llist.c

modified:   src/planner.c
	modified:   src/planner.h
	modified:   src/test.c
	new file:   test.c
sql
simon 2024-12-11 13:41:31 +01:00
parent f559d2a8bf
commit a504f3cb03
5 changed files with 40 additions and 8 deletions

View File

@ -4,6 +4,9 @@
/* create new linked list
* use llistFree after use
* retturns NULL on failure
* otherwise newNode with data as content and cmpFN to compare data
* only use to initialize
* To insert into list use llistAppend
*/
llist *llistNew(void *data, int (*cmpFN)(void *a, void *b)) {
llist *r = (llist *)malloc(sizeof(llist));

View File

@ -7,11 +7,18 @@
*/
#include "planner.h" // for subject and event structs
// #include "config.h"
#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) {
@ -22,14 +29,16 @@ Event *newEvent(Task *t, time_t s, time_t e, uint64_t sp) {
}
return r;
}
// pretty print Event
const char eventFormat[] =
"Event { %s = {\n start={%lu},\n end={%lu},\n spare={%lu}\n}\n";
// 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) {
@ -43,13 +52,22 @@ Task *newTask(char *n, time_t c, time_t d, int p, uint64_t sp) {
return r;
}
// pretty print task
const char taskFormat[] = "Task { %s = {\n created={%lu},\n deadline={%lu},\n "
"priority={%d},\n spare={%lu}\n}\n";
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
int cmpTask(void *a, void *b) {
Task *aa = (Task *)a;

View File

@ -18,6 +18,8 @@ typedef struct Task {
time_t deadline;
int priority;
uint64_t spare;
char *(*toStr)(struct Task *t); // return human readable string of struct.
// Free after use!
} Task;
/*
@ -31,6 +33,10 @@ Task *newTask(char *n, time_t c, time_t d, int p, uint64_t sp);
*/
int cmpTask(void *a, void *b);
/*
* return allocated string representation of Task
*/
char *taskToStr(Task *t);
/*
* prints human readable str of Task to stdout
*/

View File

@ -35,11 +35,16 @@ int main() {
llist *found = llistGet(list1, &search);
if (found != NULL) {
((Task *)found->data)->deadline = time(NULL) + days(10);
((Task *)found->data)->priority = 9;
printTask(found->data);
} else {
printf("%s not in List!\n", search.name);
}
printTask(&t2);
char *t1Str = taskToStr(t1);
printf("%s\n", t1Str);
free(t1Str);
free(t1);
return 0;

0
test.c Normal file
View File