104 lines
3.0 KiB
C
Raw Normal View History

2024-12-13 22:56:16 +01:00
/* 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
*
*
*/
2024-12-10 17:42:45 +01:00
2024-12-13 22:56:16 +01:00
#include <stdio.h>
2024-12-13 22:56:16 +01:00
#include <stdlib.h>
2024-12-14 21:58:55 +01:00
#include <string.h>
2024-12-13 18:44:02 +01:00
#include "planner.h"
2024-12-14 21:58:55 +01:00
#include "llist.h"
2024-12-13 22:56:16 +01:00
/*
2024-12-14 20:21:52 +01:00
write a complete linkedlist to a task csv file.
2024-12-13 22:56:16 +01:00
*/
2024-12-14 21:58:55 +01:00
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);
2024-12-14 22:11:47 +01:00
fprintf(file, "%s, %llu, %llu, %d, %lu\n",
2024-12-14 21:58:55 +01:00
task->name,
task->created,
task->deadline,
task->priority,
task->spare);
current = current->next;
}
fclose(file); // Close the file
2024-12-14 16:42:54 +01:00
}
2024-12-14 21:58:55 +01:00
2024-12-14 16:42:54 +01:00
/*
2024-12-14 20:21:52 +01:00
read a task csv file and write the data in a linked list
2024-12-14 16:42:54 +01:00
*/
2024-12-14 21:58:55 +01:00
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
2024-12-14 16:42:54 +01:00
int count = 0; //task counter
fgets(line, sizeof(line), file); //read and ignore file head
while (fgets(line, sizeof(line), file)) {
2024-12-14 21:58:55 +01:00
count++;
//remove newline sign
2024-12-14 16:42:54 +01:00
line[strcspn(line, "\n")] = '\0';
// column slice
2024-12-14 21:58:55 +01:00
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, ",");
2024-12-14 20:21:52 +01:00
2024-12-14 16:42:54 +01:00
// convert char in integer and date
2024-12-14 22:11:47 +01:00
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);
2024-12-14 16:42:54 +01:00
//create Task
2024-12-14 21:58:55 +01:00
Task *task = newTask(task_name, task_creation_date, task_deadline_date, task_priority, task_spare);
2024-12-14 20:21:52 +01:00
2024-12-14 16:42:54 +01:00
// Insert task into the linked list
if (list == NULL) {
// If the list is empty, initialize it with the first task
2024-12-14 20:21:52 +01:00
list = llistNew(task, cmpTaskN);
2024-12-14 21:58:55 +01:00
} else {
2024-12-14 16:42:54 +01:00
// Append the task to the linked list
2024-12-14 20:21:52 +01:00
llistAppend(list, task);
2024-12-14 16:42:54 +01:00
}
2024-12-13 22:56:16 +01:00
}
2024-12-14 16:42:54 +01:00
fclose(file);
2024-12-14 21:58:55 +01:00
return list;
2024-12-14 16:42:54 +01:00
}