Compare commits

...

4 Commits

Author SHA1 Message Date
simon ca5065d569 modified: src/llist.c
modified:   src/llist.h
2024-12-11 11:01:37 +01:00
simon 9b98291c1e modified: src/planner.h 2024-12-11 03:51:32 +01:00
simon 8db61cec44 new file: src/llist.c
new file:   src/llist.h
2024-12-11 03:10:11 +01:00
simon 4f867b29cb modified: Makefile
modified:   src/planner.c
	modified:   src/planner.h
2024-12-11 03:09:54 +01:00
5 changed files with 109 additions and 3 deletions

View File

@ -8,15 +8,17 @@ UIF=src/ui.c src/ui.h #UI files
PLF=src/planner.c src/planner.h #planner files
DBF=src/db.c src/db.h #db files
CALF=src/iCal.c src/iCal.h
LLST=src/llist.c src/llist.h
CONFIG=src/config.h #config file
#targets
debug: test ui planner db iCal config
gcc test.o ui.o planner.o db.o iCal.o -o debugOut
debug: test ui planner db iCal config llist
gcc test.o ui.o planner.o db.o iCal.o llist.o -o debugOut
config: $(CONFIG)
llist: $(LLST)
gcc -c $(CFLAGS) $(LLST)
test: src/test.c
gcc -c $(CFLAGS) src/test.c
iCal: $(CALF)

67
src/llist.c Normal file
View File

@ -0,0 +1,67 @@
#include "llist.h"
#include <stdlib.h>
/* create new linked list
* use llistFree after use
* retturns NULL on failure
*/
llist *llistNew(void *data, int (*cmpFN)(void *a, void *b)) {
llist *r = (llist *)malloc(sizeof(llist));
if (r != NULL) {
r->cmpFn = cmpFN;
r->data = data;
r->prev = NULL;
r->next = NULL;
}
return r;
}
/*return last node of linked list;
*/
llist *llistTail(llist *l) {
llist *tail = l;
while (tail->next != NULL) {
tail = tail->next;
}
return tail;
}
/* creates new node from data and appends it to tail
*
*/
llist *llistAppend(llist *head, void *data) {
llist *new = llistNew(data, head->cmpFn);
if (new != NULL) {
llist *tail = llistTail(head);
new->data = data;
new->next = NULL;
new->prev = tail;
tail->next = new;
}
return new;
}
/*
* return Node containing key or NULL
*/
llist *llistGet(llist *head, void *key) {
llist *c = head;
while (c != NULL) {
if (key == c) {
return c;
}
c = c->next;
}
return NULL;
}
bool llistContains(llist *head, void *key) {
return llistGet(head, key) != NULL;
}
void llistFree(llist *head) {
while (head != NULL) {
llist *tmp = head->next;
free(head);
head = tmp;
}
}

23
src/llist.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef LLIST
#define LLIST
#include <stdbool.h>
typedef struct llist {
void *data;
struct llist *next;
struct llist *prev;
int (*cmpFn)(void *a, void *b); // function that compares data
} llist;
// create new linked list with content(void* data) and cmpFN
llist *llistNew(void *data, int (*cmpFN)(void *a, void *b));
// create new node with data and append to tail of head
llist *llistAppend(llist *head, void *data);
// return llistNode that contains key or NULL
llist *llistGet(llist *head, void *key);
// TURE if llist contains key
bool llistContains(llist *head, void *key);
// free llist node (Does Not free data it contains!!)
void llistFree(llist *head);
#endif

View File

@ -9,6 +9,7 @@
#include "planner.h" // for subject and event structs
// #include "config.h"
#include <stdlib.h>
#include <string.h>
Event *newEvent(Task *t, time_t s, time_t e, uint64_t sp) {
Event *r = (Event *)malloc(sizeof(Event));
@ -33,3 +34,7 @@ Task *newTask(char *n, time_t c, time_t d, int p, uint64_t sp) {
return r;
}
// for llist
int cmpTask(Task *a, Task *b) { return strcmp(a->name, b->name); }
int cmpEvent(Event *a, Event *b) { return cmpTask(a->task, b->task); }

View File

@ -26,6 +26,11 @@ typedef struct Task {
*/
Task *newTask(char *n, time_t c, time_t d, int p, uint64_t sp);
/*
* compare function for Task
*/
int cmpTask(Task *a, Task *b);
/*
* Event struct contains task and planned time frame
*
@ -45,6 +50,10 @@ typedef struct Event {
* returns NULL on failure
*/
Event *newEvent(Task *t, time_t s, time_t e, uint64_t sp);
/*
* compare function for Event
*/
int cmpEvent(Event *a, Event *b);
int genPlan(Task *head);