Compare commits
No commits in common. "6fe70236b608819cc0c0d92f3a5bb927ed8fef6b" and "8e233caf99b5645d0d5ce3c56e903844f6515781" have entirely different histories.
6fe70236b6
...
8e233caf99
12
src/db.c
12
src/db.c
@ -1,9 +1,9 @@
|
||||
/*
|
||||
* INPUT: query for previous state
|
||||
* linked list of tasks to store
|
||||
* linked list of subjects to store
|
||||
*
|
||||
* OUTPUT:
|
||||
* linked list of tasks from file
|
||||
* linked list of subjects from file
|
||||
* OK
|
||||
*
|
||||
*
|
||||
@ -11,3 +11,11 @@
|
||||
|
||||
#include "planner.h"
|
||||
#include <stdio.h>
|
||||
|
||||
// serialize struct test format
|
||||
const char format[] = "{%s = {%lu, %lu, %d, %lu}\n";
|
||||
|
||||
int serialize(Task *s) {
|
||||
printf(format, s->name, s->created, s->deadline, s->priority, s->spare);
|
||||
return 0;
|
||||
}
|
||||
|
@ -4,4 +4,4 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "planner.h" // for task and event structs
|
||||
#include "planner.h" // for subject and event structs
|
||||
|
@ -4,9 +4,6 @@
|
||||
/* 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));
|
||||
@ -21,8 +18,8 @@ llist *llistNew(void *data, int (*cmpFN)(void *a, void *b)) {
|
||||
|
||||
/*return last node of linked list;
|
||||
*/
|
||||
llist *llistTail(llist *head) {
|
||||
llist *tail = head;
|
||||
llist *llistTail(llist *l) {
|
||||
llist *tail = l;
|
||||
while (tail->next != NULL) {
|
||||
tail = tail->next;
|
||||
}
|
||||
|
@ -7,18 +7,10 @@
|
||||
*/
|
||||
|
||||
#include "planner.h" // for subject and event structs
|
||||
#include "config.h"
|
||||
#include <stdio.h>
|
||||
// #include "config.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) {
|
||||
@ -30,15 +22,6 @@ Event *newEvent(Task *t, time_t s, time_t e, uint64_t 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) {
|
||||
@ -52,22 +35,6 @@ Task *newTask(char *n, time_t c, time_t d, int p, uint64_t sp) {
|
||||
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
|
||||
int cmpTask(void *a, void *b) {
|
||||
Task *aa = (Task *)a;
|
||||
|
@ -18,8 +18,6 @@ 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;
|
||||
|
||||
/*
|
||||
@ -33,25 +31,12 @@ 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
|
||||
*/
|
||||
void printTask(Task *s);
|
||||
|
||||
/*
|
||||
* Event struct contains task and planned time frame
|
||||
*
|
||||
* used to create timetable
|
||||
*
|
||||
* eventNew(task, start, end, spare)
|
||||
* task: pointer to task (Alloced with newTask)
|
||||
* plannedStartTime: time_t
|
||||
* plannedEndTime: time_t
|
||||
* uint64_t spare var
|
||||
*
|
||||
*/
|
||||
typedef struct Event {
|
||||
|
21
src/test.c
21
src/test.c
@ -11,11 +11,8 @@ int main() {
|
||||
|
||||
time_t now = time(NULL);
|
||||
|
||||
// create new task named LinAlg with priority 3, created now with deadline in
|
||||
// 5 days sp is currently unused spare var
|
||||
Task *t1 = newTask("LinAlg", now, now + days(5), 3, 0);
|
||||
|
||||
// Stack Allocated vars only for local use!
|
||||
Task t2 = {
|
||||
.name = "Phys", .created = now, .deadline = now + days(2), .priority = 7};
|
||||
|
||||
@ -26,25 +23,17 @@ int main() {
|
||||
llistAppend(list1, &t2);
|
||||
|
||||
// print test tasks
|
||||
printTask(t1);
|
||||
printTask(&t2);
|
||||
serialize(t1);
|
||||
serialize(&t2);
|
||||
|
||||
// find in list & modify
|
||||
Task search = {.name = "Phys"}; // key to look for. cmpTask only compares
|
||||
// names using strcmp(a.name, b.name)
|
||||
Task search = {.name = "Phys"};
|
||||
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);
|
||||
serialize(found->data);
|
||||
}
|
||||
|
||||
char *t1Str = taskToStr(t1);
|
||||
printf("%s\n", t1Str);
|
||||
free(t1Str);
|
||||
|
||||
serialize(&t2);
|
||||
free(t1);
|
||||
|
||||
return 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user