aboutsummaryrefslogtreecommitdiff
path: root/ttt.jai
diff options
context:
space:
mode:
authordam <dam@gudinoff>2023-03-29 23:48:23 +0100
committerdam <dam@gudinoff>2023-03-29 23:48:23 +0100
commit2e42845bda874cddfb1e0feed8448cad91dd0a24 (patch)
tree3971ea698f3d9f49a79ef3d12143b27deb759f24 /ttt.jai
parentbf8fb0e2a8bd35bfec3f3348d58367b9b6b54a54 (diff)
downloadtask-time-tracker-2e42845bda874cddfb1e0feed8448cad91dd0a24.tar.zst
task-time-tracker-2e42845bda874cddfb1e0feed8448cad91dd0a24.zip
Changed the way the tasks array is implemented on Database.
Diffstat (limited to 'ttt.jai')
-rw-r--r--ttt.jai63
1 files changed, 47 insertions, 16 deletions
diff --git a/ttt.jai b/ttt.jai
index 9e6af5f..1d7edfb 100644
--- a/ttt.jai
+++ b/ttt.jai
@@ -57,7 +57,8 @@ Database :: struct {
active_idx : s64;
selected_idx : s64;
total_times : [NUM_WEEK_DAYS] s64;
- tasks : [..] Task;
+ tasks : [] Task;
+ capacity : s64;
}
// const char DB_FILE_SIGN[] = DB_FILE_SIGN_STR;
@@ -316,14 +317,49 @@ task_st *get_selected_task(database_st *db) {
return task;
}
*/
-// Creates new task stored at location given by task pointer.
+// Adds a task to the database.
// If necessary, expands database capacity.
// Returns success.
-add_task :: (db: *Database, task: *Task) -> success: bool {
- idx := db.tasks.count;
- maybe_grow(*db.tasks); // TODO Check for errors?
+add_task :: (db: *Database, task: Task) -> success: bool {
+ assert(db != null, "Parameter 'db' is null.");
+
+// idx := db.tasks.count;
+// maybe_grow(*db.tasks); // TODO Check for errors?
+// db.tasks.count += 1;
+// db.tasks[idx] = task;
+
+ if (db.tasks.count == S64_MAX) {
+ print_error("Database reached maximum capacity.");
+ return false;
+ }
+
+ // If necessary, expand database capacity.
+ current_capacity := db.capacity;
+ if ((db.tasks.count + 1) > current_capacity) {
+ new_capacity :=
+ ifx current_capacity == 0 then 8 else
+ ifx current_capacity > (S64_MAX >> 1) then S64_MAX else
+ current_capacity << 1;
+
+ print("expanding from % to %\n", current_capacity, new_capacity);
+ new_tasks := realloc(db.tasks.data, new_capacity * size_of(Task), current_capacity * size_of(Task));
+ if (new_tasks == null) {
+ print_error("Failed to expand database.");
+ return false;
+ }
+
+ db.tasks.data = new_tasks;
+ db.capacity = new_capacity;
+ }
+
db.tasks.count += 1;
- db.tasks[idx] = task;
+ db.tasks[db.tasks.count-1] = task;
+
+ // Adjust selected task.
+ if (db.selected_idx < 0) {
+ db.selected_idx = db.tasks.count-1;
+ }
+
return true;
}
/*
@@ -637,7 +673,7 @@ store_database :: (db: *Database, path: string) -> success: bool {
file_write(*file, DB_FILE_SIGN_STR);
file_write(*file, db, size_of(Database));
- file_write(*file, db.tasks.data, size_of(Task)*db.tasks.count);
+ file_write(*file, db.tasks.data, size_of(Task) * db.tasks.count);
return true;
}
@@ -706,21 +742,16 @@ load_database :: (db: *Database, path: string) -> success: bool {
if read_success == false print_error("Failed to read file signature from '%'.", path);
if cast(string)file_signature != DB_FILE_SIGN_STR {
print_error("Invalid file signature.");
- file_close(*file);
return false;
}
// Read database structure.
- read_success = file_read(file, db, size_of(Database) - size_of([..] Task)); // HACK
+ read_success = file_read(file, db, size_of(Database));
//if read_success == false print_error("Failed to read database info from '%'.", path); TODO
- // Reserve database capacity for tasks. HACK
- tasks_count: s64;
- file_read(file, *tasks_count, size_of(s64));
- t: [2048]u8; file_read(file, *t, size_of([..] Task) - size_of(s64)); // HACK
- array_reserve(*db.tasks, tasks_count);
- file_read(file, db.tasks.data, size_of(Task)*tasks_count);
- db.tasks.count = tasks_count;
+ // Reserve database capacity for tasks.
+ db.tasks.data = alloc(db.tasks.count * size_of(Task));
+ db.capacity = db.tasks.count;
// Make sure we are reading all the file.
buffer: [1] u8;