aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c46
1 files changed, 43 insertions, 3 deletions
diff --git a/main.c b/main.c
index 5e3a1e5..946eca8 100644
--- a/main.c
+++ b/main.c
@@ -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;
}