2024-12-13 22:56:16 +01:00
|
|
|
/* Create a database and handel it
|
|
|
|
* Created by Jan on 13.12.2024.
|
2024-12-10 22:12:25 +01:00
|
|
|
* INPUT: query for previous state
|
2024-12-11 13:03:28 +01:00
|
|
|
* linked list of tasks to store
|
2024-12-10 22:06:49 +01:00
|
|
|
*
|
|
|
|
* OUTPUT:
|
2024-12-11 13:03:28 +01:00
|
|
|
* linked list of tasks from file
|
2024-12-10 22:06:49 +01:00
|
|
|
* OK
|
|
|
|
*
|
|
|
|
*
|
2024-12-09 19:27:50 +01:00
|
|
|
*/
|
2024-12-10 17:42:45 +01:00
|
|
|
|
2024-12-13 22:56:16 +01:00
|
|
|
|
2024-12-10 22:06:49 +01:00
|
|
|
#include <stdio.h>
|
2024-12-13 22:56:16 +01:00
|
|
|
#include <stdlib.h>
|
2024-12-13 18:44:02 +01:00
|
|
|
#include <time.h>
|
|
|
|
#include "planner.h"
|
2024-12-13 22:56:16 +01:00
|
|
|
#include "list.h"
|
|
|
|
|
|
|
|
/*
|
2024-12-14 16:42:54 +01:00
|
|
|
write a complete linkedlist to a Event csv file.
|
2024-12-13 22:56:16 +01:00
|
|
|
*/
|
|
|
|
void write_linkedlist_to_csv(llist* head, const char* filename) {
|
2024-12-14 16:42:54 +01:00
|
|
|
//Open file with write permision
|
|
|
|
FILE* file = fopen(filename, "w");
|
|
|
|
//if there is no file or a wrong file path you becom an error
|
|
|
|
if (file == NULL) {
|
|
|
|
perror("Could not open file %s\n", filename);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
//write the file header
|
|
|
|
fprintf(file, "TaskName, Created, Deadline, Priority, Spare, EventStartTime, EventEndTime, EventSpare\n");
|
|
|
|
|
|
|
|
// Iterate through the linked list and write the data to the CSV
|
|
|
|
llist* current = head;
|
|
|
|
while (current != NULL) {
|
|
|
|
// Extract data from the current node
|
|
|
|
Event* event = (Event*)current->data; // Assumed that current->data holds an Event pointer
|
|
|
|
|
|
|
|
// Extract the associated task from the event
|
|
|
|
Task* task = event->task;
|
|
|
|
|
|
|
|
// Write the task and event data to the CSV file
|
|
|
|
fprintf(file, "%s, %lu, %lu, %d, %lu, %lu, %lu, %lu\n",
|
|
|
|
task->name,
|
|
|
|
task->created,
|
|
|
|
task->deadline,
|
|
|
|
task->priority,
|
|
|
|
task->spare,
|
|
|
|
event->plannedStartTime,
|
|
|
|
event->plannedEndTime,
|
|
|
|
event->spare);
|
|
|
|
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
fclose(file); // Close the file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
read an event csv file and write the data in a linked list
|
|
|
|
*/
|
|
|
|
|
|
|
|
void write_csv_to_llist(const char* filename) {
|
|
|
|
//Open file with read permision
|
|
|
|
FILE* file = fopen(filename, "r");
|
2024-12-13 22:56:16 +01:00
|
|
|
if (file == NULL) {
|
2024-12-14 16:42:54 +01:00
|
|
|
perror("Could not open file %s\n", filename);
|
2024-12-13 22:56:16 +01:00
|
|
|
}
|
|
|
|
else{
|
2024-12-14 16:42:54 +01:00
|
|
|
char line[1024]; //Line Buffer
|
|
|
|
int count = 0; //task counter
|
|
|
|
llist *list = NULL
|
|
|
|
fgets(line, sizeof(line), file); //read and ignore file head
|
|
|
|
while (fgets(line, sizeof(line), file)) {
|
|
|
|
count++;
|
|
|
|
//remove newline sign
|
|
|
|
line[strcspn(line, "\n")] = '\0';
|
|
|
|
|
|
|
|
// column slice
|
|
|
|
char *taskname = strtok(line, ",");
|
|
|
|
char *tsakcreation_dateSTR = strtok(NULL, ",");
|
|
|
|
char *tsakdeadline_dateSTR = strtok(NULL, ",");
|
|
|
|
char *tsakprioritySTR = strtok(NULL, ",");
|
|
|
|
char *tsakspareSTR = strtok(NULL, ",");
|
|
|
|
char *eventStartTimeSTR = strtok(NULL, ",");
|
|
|
|
char *eventEndTimeSTR = strtok(NULL, ",");
|
|
|
|
char *eventSpareSTR = strtok(NULL, ",");
|
|
|
|
|
|
|
|
// convert char in integer and date
|
|
|
|
unsigned long int taskcreation_date = strtoul(taskcreation_dateSTR,NULL,10);
|
|
|
|
unsigned long int taskdeadline_date = strtoul(taskdeadline_dateSTR,NULL,10);
|
|
|
|
unsigned long int taskspare = strtoul(taskspareSTR,NULL,10);
|
|
|
|
unsigned long int eventStartTime = strtoul(eventStartTimeSTR,NULL,10);
|
|
|
|
unsigned long int eventEndTime = strtoul(eventEndTimeSTR,NULL,10);
|
|
|
|
unsigned long int eventSpare = strtoul(eventSpareSTR,NULL,10);
|
|
|
|
|
|
|
|
int priority = atoi(prioritySTR);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//create Task
|
|
|
|
Task *task = newTask(taskname, taskcreation_date, taskdeadline_date, taskpriority, taskspare);
|
|
|
|
|
|
|
|
//create Event
|
|
|
|
Event *event = newEvent(task,eventStartTime, eventEndTime, eventSpare);
|
|
|
|
|
|
|
|
// Insert task into the linked list
|
|
|
|
if (list == NULL) {
|
|
|
|
// If the list is empty, initialize it with the first task
|
|
|
|
list = llistNew(event, NULL);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Append the task to the linked list
|
|
|
|
llistAppend(list, event);
|
|
|
|
}
|
2024-12-13 22:56:16 +01:00
|
|
|
}
|
2024-12-14 16:42:54 +01:00
|
|
|
|
|
|
|
fclose(file);
|
2024-12-13 22:56:16 +01:00
|
|
|
}
|
2024-12-14 16:42:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|