From f4b3177bbd0e9b46eb078f199d57fcf120c2f88e Mon Sep 17 00:00:00 2001 From: Ariadna Vigo Date: Tue, 21 Jul 2020 14:29:47 +0200 Subject: New way to add tasks to an already existing task file. --- tasklst.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'tasklst.c') diff --git a/tasklst.c b/tasklst.c index 51d26ef..09c6b6d 100644 --- a/tasklst.c +++ b/tasklst.c @@ -65,7 +65,23 @@ tasklst_tasks_done(TaskLst tasks) { return tasklst_tasks_status(tasks, TASK_DONE); } - + +int +tasklst_add_task(TaskLst *tasks, int status, const char *str) +{ + int i; + + for (i = 0; i < TASK_LST_MAX_NUM; ++i) { + if (tasks->status[i] == TASK_VOID) { + strncpy(tasks->tdesc[i], str, TASK_LST_DESC_MAX_SIZE); + tasks->status[i] = status; + return i; + } + } + + return -1; /* We couldn't add any new task, because tasks is full */ +} + int tasklst_read_from_file(TaskLst *tasks, FILE *fp) { @@ -88,16 +104,11 @@ tasklst_read_from_file(TaskLst *tasks, FILE *fp) if (endptr[0] != '\0') return -1; - if (stat_buf == TASK_VOID) - break; - else - tasks->status[i] = stat_buf; - ptr = strtok(NULL, "\n"); if (ptr == NULL) return -1; - strncpy(tasks->tdesc[i], ptr, TASK_LST_DESC_MAX_SIZE); + tasklst_add_task(tasks, stat_buf, ptr); } return 0; -- cgit v1.2.3