diff options
| author | dam <dam@gudinoff> | 2022-08-30 17:29:47 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2022-08-30 17:29:47 +0000 |
| commit | a3edef01b999aaa09fdd3c8995b867b258533ce4 (patch) | |
| tree | b4c16372975a3b9b194e8cce0e6ffafcaf7c34c6 | |
| parent | 6804d3035ba9ca6f6b28dedf8002e81d3ae4cd43 (diff) | |
| download | task-time-tracker-a3edef01b999aaa09fdd3c8995b867b258533ce4.tar.zst task-time-tracker-a3edef01b999aaa09fdd3c8995b867b258533ce4.zip | |
Completed add_task and remove_task functions. All screen is now refreshed, always. Added F1 and F2 to add and remove tasks as prototype.
| -rw-r--r-- | main.c | 46 |
1 files changed, 43 insertions, 3 deletions
@@ -124,6 +124,39 @@ bool add_task(database_t* db, const task_t* task) { return true; } +// Creates new database entry and points task to it. If necessary, expands database capacity. +// Returns success. +bool create_task(database_t* db, const task_t** task) { + assert(db != NULL); + + if (db->count == UINT32_MAX) { + fprintf(stderr, "Database reached maximum capacity.\n"); + return false; + } + + // If necessary, expand database capacity. + uint32_t current_capacity = db->capacity; + if((db->count + 1) > current_capacity) { + uint32_t new_capacity = current_capacity == 0 ? 2 : + current_capacity >= UINT32_MAX >> 2 ? UINT32_MAX : + current_capacity << 1; + + task_t* new_tasks = realloc(db->tasks, new_capacity * SIZEOF_TASK_T); + if (new_tasks == NULL) { + fprintf(stderr, "Failed to expand database.\n"); + return false; + } + db->capacity = new_capacity; + db->tasks = new_tasks; + } + + // Set new task pointer. + *task = db->tasks[db->count]; + db->count++; + + return true; +} + // Removes task by index from database. If possible, shrinks database capacity. // Returns success. bool remove_task(database_t* db, uint32_t index) { @@ -831,12 +864,19 @@ int main(int argc, char *argv[]) { { case KEY_F(1): { - task_t new_task; - add_task(&database, &new_task); + task_t* new_task; + create_task(&database, &new_task); move(database.count, 2); curs_set(1); - getnstr(database.tasks[database.count-1].name, MAX_TASK_NAME-1); + getnstr(new_task->name, MAX_TASK_NAME-1); curs_set(0); + +// task_t new_task; +// add_task(&database, &new_task); +// move(database.count, 2); +// curs_set(1); +// getnstr(database.tasks[database.count-1].name, MAX_TASK_NAME-1); +// curs_set(0); break; } |
