From 337714e540e9101fdc0d945e7ea1673af84a3c97 Mon Sep 17 00:00:00 2001 From: Riley Date: Tue, 13 May 2025 19:34:39 -0500 Subject: [PATCH] Refactor todoitem management: add todolist structure and related functions --- src/main.c | 2 +- src/todoitem.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 659dc12..5d13a5f 100644 --- a/src/main.c +++ b/src/main.c @@ -13,7 +13,7 @@ int main(void) { puts("Hello, World!"); setup_appdata(); printf_s("Appdata path: %s\n", appdata_path()); - const char *cupath = custom_appdata_path("CustomFolder"); + const char *cupath = custom_appdata_path("ToDoLists"); printf_s("Custom appdata path: %s\n", cupath); return 0; } \ No newline at end of file diff --git a/src/todoitem.c b/src/todoitem.c index 52cd484..0af18b3 100644 --- a/src/todoitem.c +++ b/src/todoitem.c @@ -8,7 +8,40 @@ typedef struct { int completed; } todoitem; - todoitem *create_todoitem(char *name, char *description, int priority, int completed) { + +typedef struct { + todoitem *items; + int count; +} todolist; + +todolist *create_todolist() { + todolist *list = malloc(sizeof(todolist)); + list->items = NULL; + list->count = 0; + return list; +} + +void tdl_free(todolist *list) { + if (list != NULL) { + for (int i = 0; i < list->count; i++) { + free(list->items[i].name); + free(list->items[i].description); + } + free(list->items); + free(list); + } +} + + +void tdl_add_item(todolist *list, char *name, char *description, int priority, int completed) { + list->items = realloc(list->items, sizeof(todoitem) * (list->count + 1)); + list->items[list->count].name = name; + list->items[list->count].description = description; + list->items[list->count].priority = priority; + list->items[list->count].completed = completed; + list->count++; +} + todoitem *tdi_new(char *name, char *description, int priority, int completed) { todoitem *item = malloc(sizeof(todoitem)); item->name = name; item->description = description; @@ -17,7 +50,31 @@ typedef struct { return item; } -void free_todoitem(todoitem *item) { +void tdl_free(todolist *list) { + if (list != NULL) { + for (int i = 0; i < list->count; i++) { + free(list->items[i].name); + free(list->items[i].description); + } + free(list->items); + free(list); + } +} + +void tdl_remove_item(todolist *list, int index) { + if (index < 0 || index >= list->count) { + return; // Invalid index + } + free(list->items[index].name); + free(list->items[index].description); + for (int i = index; i < list->count - 1; i++) { + list->items[i] = list->items[i + 1]; + } + list->count--; + list->items = realloc(list->items, sizeof(todoitem) * list->count); +} + +void tdi_free(todoitem *item) { if (item != NULL) { free(item->name); free(item->description);