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 /* create new linked list
* use llistFree after use * use llistFree after use
* retturns NULL on failure * 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 *llistNew(void *data, int (*cmpFN)(void *a, void *b)) {
llist *r = (llist *)malloc(sizeof(llist)); llist *r = (llist *)malloc(sizeof(llist));

View File

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

View File

@ -18,6 +18,8 @@ typedef struct Task {
time_t deadline; time_t deadline;
int priority; int priority;
uint64_t spare; uint64_t spare;
char *(*toStr)(struct Task *t); // return human readable string of struct.
// Free after use!
} Task; } 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); int cmpTask(void *a, void *b);
/*
* return allocated string representation of Task
*/
char *taskToStr(Task *t);
/* /*
* prints human readable str of Task to stdout * prints human readable str of Task to stdout
*/ */

View File

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

0
test.c Normal file
View File