generated from Riley/Conan-C
Refactor todoitem management: add todolist structure and related functions
This commit is contained in:
parent
d1c65e5010
commit
337714e540
2 changed files with 60 additions and 3 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue