planner makes plans now
parent
8629bb0420
commit
568c1dcf7d
4
Makefile
4
Makefile
|
@ -30,6 +30,10 @@ planner: $(PLF)
|
|||
db: $(DBF)
|
||||
gcc -c $(CFLAGS) $(DBF)
|
||||
|
||||
|
||||
edit:
|
||||
nvim $(PLF) Makefile $(LLST) src/test.c
|
||||
|
||||
clean:
|
||||
rm -rf *.o debugOut src/*.gch
|
||||
|
||||
|
|
|
@ -15,6 +15,9 @@
|
|||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
const uint intervalLen = 45; // min
|
||||
const uint pauseLen = 10; //
|
||||
|
||||
const char taskFormat[] =
|
||||
"Task { %.64s = {\n created={%lu},\n deadline={%lu},\n "
|
||||
"priority={%d},\n spare={%lu}\n}\n";
|
||||
|
@ -35,6 +38,7 @@ void printEvent(Event *s) {
|
|||
ctime_r(&s->plannedStartTime, st);
|
||||
ctime_r(&s->plannedEndTime, e);
|
||||
printf(eventFormat, s->task->name, st, e, s->spare);
|
||||
printTask(s->task);
|
||||
}
|
||||
void llistPrintE(llist *head) {
|
||||
llist *c = head;
|
||||
|
@ -152,7 +156,7 @@ int cmpEvent(const void *a, const void *b) {
|
|||
return cmpTaskN(aa->task, bb->task);
|
||||
}
|
||||
|
||||
llist *genPlan(llist *head) {
|
||||
llist *genPlan(llist *head, time_t timeAvail) {
|
||||
|
||||
// map llist to pointer arr & sort by priority
|
||||
// second arr sorted bby name
|
||||
|
@ -161,9 +165,9 @@ llist *genPlan(llist *head) {
|
|||
printf("len: %d\n", lLen);
|
||||
llistPrintT(head);
|
||||
|
||||
Task *sortedNames = calloc(lLen, sizeof(Task));
|
||||
// Task *sortedNames = calloc(lLen, sizeof(Task));
|
||||
Task *sortedPrio = calloc(lLen, sizeof(Task));
|
||||
if (sortedNames == NULL || sortedPrio == NULL) {
|
||||
if (/*sortedNames == NULL ||*/ sortedPrio == NULL) {
|
||||
planLog("gen plan : calloc failed!!\n", true);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -171,20 +175,22 @@ llist *genPlan(llist *head) {
|
|||
// add Tasks from llist to arr
|
||||
llist *c = head;
|
||||
for (int i = 0; c != NULL; i++) {
|
||||
sortedNames[i] = *(Task *)c->data;
|
||||
// sortedNames[i] = *copyTask(c->data);
|
||||
// sortedPrio[i] = *copyTask(c->data);
|
||||
// sortedNames[i] = *(Task *)c->data;
|
||||
sortedPrio[i] = *(Task *)c->data;
|
||||
c = c->next;
|
||||
}
|
||||
|
||||
// sort
|
||||
qsort(sortedNames, lLen, sizeof(Task), cmpTaskN);
|
||||
// qsort(sortedNames, lLen, sizeof(Task), cmpTaskN);
|
||||
qsort(sortedPrio, lLen, sizeof(Task), cmpTaskP);
|
||||
|
||||
// test print
|
||||
planLog("sortendName", 0);
|
||||
for (int i = 0; i < lLen; i++) {
|
||||
printTask(sortedNames + i);
|
||||
}
|
||||
// // test print
|
||||
// planLog("sortendName", 0);
|
||||
// for (int i = 0; i < lLen; i++) {
|
||||
// printTask(sortedNames + i);
|
||||
// }
|
||||
|
||||
planLog("sortendPrio", 0);
|
||||
for (int i = 0; i < lLen; i++) {
|
||||
|
@ -193,26 +199,60 @@ llist *genPlan(llist *head) {
|
|||
|
||||
planLog("creating eventList", false);
|
||||
|
||||
// genertate plan basen on priorities and available time
|
||||
time_t now = time(NULL);
|
||||
struct tm *lc = localtime(&now);
|
||||
llist *events_ll = llistNew(NULL, cmpEvent);
|
||||
for (int i = 0; i < lLen; i++) {
|
||||
time_t start = mktime(lc);
|
||||
// time_t avail = mktime(timeAvail);
|
||||
time_t start, end;
|
||||
// (mktime(lc) < mktime(timeAvail))
|
||||
do {
|
||||
start = mktime(lc); // start now
|
||||
lc->tm_min += intervalLen;
|
||||
end = mktime(lc); //
|
||||
|
||||
printf("start:: %s\n", ctime(&start));
|
||||
lc->tm_min += 45;
|
||||
time_t end = mktime(lc);
|
||||
printf("end:: %s\n", ctime(&end));
|
||||
|
||||
Event *c = newEvent(sortedPrio + i, start, end, 0);
|
||||
printEvent(c);
|
||||
lc->tm_min += 15;
|
||||
Event *c =
|
||||
newEvent(sortedPrio, start, end, 0); // use elem with wighest priority
|
||||
llistAppend(events_ll, c);
|
||||
}
|
||||
printEvent(c);
|
||||
|
||||
llistPrintE(events_ll->next);
|
||||
// decrement priority of first elem and resort list
|
||||
(*sortedPrio).priority -= 1;
|
||||
qsort(sortedPrio, lLen, sizeof(Task), cmpTaskP);
|
||||
|
||||
lc->tm_min += pauseLen; // add pause
|
||||
|
||||
} while (mktime(lc) < timeAvail);
|
||||
|
||||
// for (int i = 0; i < lLen; i++) {
|
||||
// time_t start = mktime(lc);
|
||||
//
|
||||
// printf("start:: %s\n", ctime(&start));
|
||||
// lc->tm_min += 45;
|
||||
// time_t end = mktime(lc);
|
||||
// printf("end:: %s\n", ctime(&end));
|
||||
//
|
||||
// Event *c = newEvent(sortedPrio + i, start, end, 0);
|
||||
// printEvent(c);
|
||||
// lc->tm_min += 15;
|
||||
// llistAppend(events_ll, c);
|
||||
// }
|
||||
//
|
||||
|
||||
// free empty head
|
||||
llist *tmp = events_ll;
|
||||
events_ll = events_ll->next;
|
||||
tmp->next = NULL;
|
||||
llistFreeE(tmp);
|
||||
llistPrintE(events_ll);
|
||||
|
||||
// for (int i = 0; i < lLen; i++) {
|
||||
// printTask(sortedPrio + i);
|
||||
// }
|
||||
|
||||
planLog("END GEN PLAN", 0);
|
||||
llistFreeT(head);
|
||||
// send updated tasks to db for storage
|
||||
|
||||
return events_ll;
|
||||
}
|
||||
|
|
|
@ -81,6 +81,6 @@ int cmpEvent(const void *a, const void *b);
|
|||
/*
|
||||
* takes llist of tasks and returns llist of events
|
||||
*/
|
||||
llist *genPlan(llist *head);
|
||||
llist *genPlan(llist *head, time_t timeAvail);
|
||||
|
||||
#endif // !PLANNER
|
||||
|
|
43
src/test.c
43
src/test.c
|
@ -7,6 +7,7 @@
|
|||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main() {
|
||||
|
||||
|
@ -33,29 +34,33 @@ int main() {
|
|||
llistAppend(list1, t3);
|
||||
llistAppend(list1, t4);
|
||||
|
||||
llist *l1 = genPlan(list1);
|
||||
// gnerate plan from task list in time
|
||||
struct tm *lc = localtime(&now);
|
||||
lc->tm_hour += 9; // add available time;
|
||||
time_t maxTime = mktime(lc); // create timestamp
|
||||
llist *l1 = genPlan(list1, maxTime); // return inked list of event;
|
||||
llistFreeE(l1);
|
||||
|
||||
// print test tasks
|
||||
printTask(t1);
|
||||
printTask(t2);
|
||||
// printTask(t1);
|
||||
|
||||
// find in list & modify
|
||||
Task search = {.name = "Phys"}; // key to look for. cmpTask only compares
|
||||
// names using strcmp(a.name, b.name)
|
||||
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);
|
||||
}
|
||||
|
||||
char *t1Str = taskToStr(t1);
|
||||
printf("%s\n", t1Str);
|
||||
llistFreeT(list1);
|
||||
free(t1Str);
|
||||
|
||||
// Task search = {.name = "Analysis"}; // key to look for. cmpTask only
|
||||
// compares
|
||||
// // names using strcmp(a.name, b.name)
|
||||
// llist *found = llistGet(list1, &search);
|
||||
// if (found != NULL) {
|
||||
// ((Task *)found->data)->deadline = time(NULL) + days(10);
|
||||
// // ((Task *)found->data)->priority Names= 9;
|
||||
// // printTask(found->data);
|
||||
// } else {
|
||||
// printf("%s not in List!\n", search.name);
|
||||
// }
|
||||
//
|
||||
// char *t1Str = taskToStr(t1);
|
||||
// // printf("%s\n", t1Str);
|
||||
// llistFreeT(list1);
|
||||
// free(t1Str);
|
||||
//
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue