From 9b3238978b4edacaf4edaee579317275a0d8db93 Mon Sep 17 00:00:00 2001 From: ketrptr Date: Sun, 15 Dec 2024 12:18:32 +0100 Subject: [PATCH] modified: src/db.c modified: src/db.h modified: src/test.c modified: src/ui.c --- src/db.c | 135 +++++++++++++++++++--------------------- src/db.h | 4 +- src/test.c | 2 +- src/ui.c | 178 ++++++++++++++++++++++++++--------------------------- 4 files changed, 155 insertions(+), 164 deletions(-) diff --git a/src/db.c b/src/db.c index c670b3f..a31bc6e 100644 --- a/src/db.c +++ b/src/db.c @@ -10,102 +10,95 @@ * */ - +#include "llist.h" +#include "planner.h" #include #include +#include #include -#include "planner.h" -#include "list.h" /* write a complete linkedlist to a task csv file. */ -void write_linkedlist_to_csv(llist* head, const char* filename) { - //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\n"); +void write_linkedlist_to_csv(llist *head, const char *filename) { + // 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) { + fprintf(stderr, "Could not open file %s\n", filename); + exit(1); - // Iterate through the linked list and write the data to the CSV - llist* current = head; - while (current != NULL) { + } else { + // write the file header + fprintf(file, "TaskName, Created, Deadline, Priority, Spare\n"); - // Write the task data to the CSV file - fprintf(file, "%s, %lu, %lu, %d, %lu\n", - task->name, - task->created, - task->deadline, - task->priority, - task->spare, + // Iterate through the linked list and write the data to the CSV + llist *current = head; + Task *task; + while (current != NULL) { + task = current->data; + // Write the task data to the CSV file + fprintf(file, "%s, %lu, %lu, %d, %lu\n", task->name, task->created, + task->deadline, task->priority, task->spare); - current = current->next; - } - fclose(file); // Close the file - } + current = current->next; + } + fclose(file); // Close the file + } } /* read a task 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"); +llist *write_csv_to_llist(const char *filename) { + llist *list = NULL; // list to return + // Open file with read permision + FILE *file = fopen(filename, "r"); if (file == NULL) { - perror("Could not open file %s\n", filename); - } - else{ - char line[1024]; //Line Buffer - int count = 0; //task counter - llist *list = NULL - fgets(line, sizeof(line), file); //read and ignore file head + fprintf(stderr, "Could not open file %s\n", filename); + + } else { + 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'; + 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, ","); + // column slice + char *taskname = strtok(line, ","); + char *taskcreation_dateSTR = strtok(NULL, ","); + char *taskdeadline_dateSTR = strtok(NULL, ","); + char *taskprioritySTR = strtok(NULL, ","); + char *taskspareSTR = 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); + int priority = atoi(taskprioritySTR); - // 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); - int priority = atoi(prioritySTR); + // create Task + Task *task = newTask(taskname, taskcreation_date, taskdeadline_date, + priority, taskspare); - - - //create Task - Task *task = newTask(taskname, taskcreation_date, taskdeadline_date, taskpriority, taskspare); - - - - // 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); - } + // 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; // null on error } - diff --git a/src/db.h b/src/db.h index 334e59b..1d740dc 100644 --- a/src/db.h +++ b/src/db.h @@ -5,5 +5,5 @@ #endif -extern void write_linkedlist_to_csv(llist* head, const char* filename) -extern void write_csv_to_llist(const char* filename) \ No newline at end of file +extern void write_linkedlist_to_csv(llist *head, const char *filename); +extern void write_csv_to_llist(const char *filename); diff --git a/src/test.c b/src/test.c index fa215e7..c5bf334 100644 --- a/src/test.c +++ b/src/test.c @@ -9,7 +9,7 @@ #include #include -int main() { +int test() { time_t now = time(NULL); diff --git a/src/ui.c b/src/ui.c index e6a63c0..27fa9fc 100644 --- a/src/ui.c +++ b/src/ui.c @@ -5,7 +5,7 @@ /* Created by Juergen Buechel, 13.12.2024/ */ #include "ui.h" -// #include "db.h" +#include "db.h" #include "llist.h" #include "planner.h" // for subject and event structs #include @@ -31,98 +31,96 @@ int main(void) { if (scanf("%d", &choice) != 1) { printf("Falsche Eingabe\n"); }; - choice = 0; + // choice = 0; - while ((choice < 1 || choice > 5)) { - switch (choice) { - case 1: { - printf(" Geben sie das gewünschte Fach ein: \n"); - if (scanf("%c", taskname) != 1) { - printf("Ungültige Eingabe für den Namen.\n"); - return -1; - } - - printf(" Wie viel Zeit bleibt ihnen:\n"); - if (scanf("%d", &taskdeadline_date) != 1) { - printf("Ungültige Eingabe.\n"); - return -1; - } - - printf(" Gib die Priorität des Faches an: \n"); - if (scanf("%d", &taskpriority) != 1) { - printf("Ungültige Eingabe.\n"); - return -1; - } - - printf(" Wie viel Zeit habe Sie für dieses Fach: \n"); - if (scanf("%d", &taskspare) != 1) { - printf("Ungültige Eingabe.\n"); - return -1; - } - break; - case 2: - printf( - "Geben Sie die zur verfuegung stehende Zeit für die Fächer an: \n"); - scanf("%c", &taskspare); - break; - - case 3: - // if (task == NULL) { - // printf("Die Liste ist leer"); - // } else { - // llist *iterator = list; - // while (iterator != NULL) { - // Task *currentTask = (Task *)(iterator->data); // Cast zu Task - // printf("Fach: %s, Deadline: %ld, Priorität: %d\n", - // currentTask->name, currentTask->deadline, - // currentTask->priority); - // iterator = iterator->next; // Gehe zum nächsten Listenelement - // } - // } - break; - // case 4: iCAl();break; - - printf(" Wie viel Zeit bleibt ihnen:\n"); - if (scanf("%d", &taskdeadline_date) != 1) { - printf("Ungültige Eingabe.\n"); - return -1; - } - - printf(" Gib die Priorität des Faches an: \n"); - if (scanf("%d", &taskpriority) != 1) { - printf("Ungültige Eingabe.\n"); - return -1; - } - - printf(" Wie viel Zeit habe Sie für dieses Fach: \n"); - if (scanf("%d", &taskspare) != 1) { - printf("Ungültige Eingabe.\n"); - return -1; - } - break; - case 4: - printf( - "Geben Sie die zur verfuegung stehende Zeit für die Fächer an: \n"); - scanf("%d", &taskspare); - break; - - case 5: - // if (task == NULL) {//task? - // printf("Die Liste ist leer"); - // } else { - // llist *iterator = list; - // while (iterator != NULL) { - // Task *currentTask = (Task *)(iterator->data); // Cast zu Task - // printf("Fach: %s, Deadline: %ld, Priorität: %d\n", - // currentTask->name, currentTask->deadline, - // currentTask->priority); - // iterator = iterator->next; // Gehe zum nächsten Listenelement - // } - // } - break; - // case 4: iCAl();break; + switch (choice) { + case 1: { + printf(" Geben sie das gewünschte Fach ein: \n"); + if (scanf("%c", taskname) != 1) { + printf("Ungültige Eingabe für den Namen.\n"); + return -1; } + + printf(" Wie viel Zeit bleibt ihnen:\n"); + if (scanf("%d", &taskdeadline_date) != 1) { + printf("Ungültige Eingabe.\n"); + return -1; } + + printf(" Gib die Priorität des Faches an: \n"); + if (scanf("%d", &taskpriority) != 1) { + printf("Ungültige Eingabe.\n"); + return -1; + } + + printf(" Wie viel Zeit habe Sie für dieses Fach: \n"); + if (scanf("%d", &taskspare) != 1) { + printf("Ungültige Eingabe.\n"); + return -1; + } + break; + case 2: + printf( + "Geben Sie die zur verfuegung stehende Zeit für die Fächer an: \n"); + scanf("%d", &taskspare); + break; + + case 3: + // if (task == NULL) { + // printf("Die Liste ist leer"); + // } else { + // llist *iterator = list; + // while (iterator != NULL) { + // Task *currentTask = (Task *)(iterator->data); // Cast zu Task + // printf("Fach: %s, Deadline: %ld, Priorität: %d\n", + // currentTask->name, currentTask->deadline, + // currentTask->priority); + // iterator = iterator->next; // Gehe zum nächsten Listenelement + // } + // } + break; + // case 4: iCAl();break; + + printf(" Wie viel Zeit bleibt ihnen:\n"); + if (scanf("%d", &taskdeadline_date) != 1) { + printf("Ungültige Eingabe.\n"); + return -1; + } + + printf(" Gib die Priorität des Faches an: \n"); + if (scanf("%d", &taskpriority) != 1) { + printf("Ungültige Eingabe.\n"); + return -1; + } + + printf(" Wie viel Zeit habe Sie für dieses Fach: \n"); + if (scanf("%d", &taskspare) != 1) { + printf("Ungültige Eingabe.\n"); + return -1; + } + break; + case 4: + printf( + "Geben Sie die zur verfuegung stehende Zeit für die Fächer an: \n"); + scanf("%d", &taskspare); + break; + + case 5: + // if (task == NULL) {//task? + // printf("Die Liste ist leer"); + // } else { + // llist *iterator = list; + // while (iterator != NULL) { + // Task *currentTask = (Task *)(iterator->data); // Cast zu Task + // printf("Fach: %s, Deadline: %ld, Priorität: %d\n", + // currentTask->name, currentTask->deadline, + // currentTask->priority); + // iterator = iterator->next; // Gehe zum nächsten Listenelement + // } + // } + break; + // case 4: iCAl();break; + } } } while (choice != 5); {