diff --git a/src/llist.c b/src/llist.c index c406ab4..5c1a1da 100644 --- a/src/llist.c +++ b/src/llist.c @@ -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)); diff --git a/src/planner.c b/src/planner.c index 401040b..81046a5 100644 --- a/src/planner.c +++ b/src/planner.c @@ -7,11 +7,18 @@ */ #include "planner.h" // for subject and event structs -// #include "config.h" +#include "config.h" #include #include #include +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; diff --git a/src/planner.h b/src/planner.h index 2cf4dec..3ed2b87 100644 --- a/src/planner.h +++ b/src/planner.h @@ -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 */ diff --git a/src/test.c b/src/test.c index 676b64d..305c7ca 100644 --- a/src/test.c +++ b/src/test.c @@ -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; diff --git a/test.c b/test.c new file mode 100644 index 0000000..e69de29