104 lines
3.0 KiB
C
104 lines
3.0 KiB
C
/* Create a database and handel it
|
|
* Created by Jan on 13.12.2024.
|
|
* INPUT: query for previous state
|
|
* linked list of tasks to store
|
|
*
|
|
* OUTPUT:
|
|
* linked list of tasks from file
|
|
* OK
|
|
*
|
|
*
|
|
*/
|
|
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "planner.h"
|
|
#include "llist.h"
|
|
|
|
/*
|
|
write a complete linkedlist to a task csv file.
|
|
*/
|
|
void write_linkedlist_to_csv(llist *head, const char *filename) {
|
|
//Open file with write permission
|
|
FILE *file = fopen(filename, "w");
|
|
//if there is no file or a wrong file path you become an error
|
|
if (file == NULL) {
|
|
perror("Could not open file\n");
|
|
}
|
|
|
|
//write the file header
|
|
fprintf(file, "TaskName, Created, Deadline, Priority, Spare\n");
|
|
|
|
// Iterate through the linked list and write the data to the CSV
|
|
llist *current = head;
|
|
while (current != NULL) {
|
|
// Write the task data to the CSV file
|
|
Task *task = (Task *) (current->data);
|
|
fprintf(file, "%s, %llu, %llu, %d, %lu\n",
|
|
task->name,
|
|
task->created,
|
|
task->deadline,
|
|
task->priority,
|
|
task->spare);
|
|
|
|
|
|
current = current->next;
|
|
}
|
|
fclose(file); // Close the file
|
|
}
|
|
|
|
|
|
/*
|
|
read a task csv file and write the data in a linked list
|
|
*/
|
|
|
|
llist *write_csv_to_llist(const char *filename) {
|
|
//initialize list
|
|
llist *list = NULL;
|
|
//Open file with read permission
|
|
FILE *file = fopen(filename, "r");
|
|
if (file == NULL) {
|
|
perror("Could not open file\n");
|
|
}
|
|
char line[1024]; //Line Buffer
|
|
int count = 0; //task counter
|
|
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 *task_name = strtok(line, ",");
|
|
char *task_creation_dateSTR = strtok(NULL, ",");
|
|
char *task_deadline_dateSTR = strtok(NULL, ",");
|
|
char *task_prioritySTR = strtok(NULL, ",");
|
|
char *task_spareSTR = strtok(NULL, ",");
|
|
|
|
|
|
// convert char in integer and date
|
|
unsigned long long int task_creation_date = (unsigned long long int)strtoul(task_creation_dateSTR, NULL, 10);
|
|
unsigned long long int task_deadline_date = (unsigned long long int)strtoul(task_deadline_dateSTR,NULL, 10);
|
|
unsigned long int task_spare = (unsigned long int)strtoul(task_spareSTR,NULL, 10);
|
|
int task_priority = (int)strtol(task_prioritySTR, NULL, 10);
|
|
|
|
|
|
//create Task
|
|
Task *task = newTask(task_name, task_creation_date, task_deadline_date, task_priority, task_spare);
|
|
|
|
|
|
// Insert task into the linked list
|
|
if (list == NULL) {
|
|
// If the list is empty, initialize it with the first task
|
|
list = llistNew(task, cmpTaskN);
|
|
} else {
|
|
// Append the task to the linked list
|
|
llistAppend(list, task);
|
|
}
|
|
}
|
|
fclose(file);
|
|
return list;
|
|
}
|