aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordam <dam@gudinoff>2023-03-31 00:38:26 +0100
committerdam <dam@gudinoff>2023-03-31 00:38:26 +0100
commitf82ce565436dc04d58c85d55fb78038acf0191b2 (patch)
treec89104f386e20be6362798aabac0c67cf434b439
parenta7d4d3038a11fd8b6f8a1439840c2034ebb3ae01 (diff)
downloadtask-time-tracker-f82ce565436dc04d58c85d55fb78038acf0191b2.tar.zst
task-time-tracker-f82ce565436dc04d58c85d55fb78038acf0191b2.zip
Fix load_database and free_memory.
-rw-r--r--ttt.jai172
1 files changed, 52 insertions, 120 deletions
diff --git a/ttt.jai b/ttt.jai
index 4c79f86..daa826b 100644
--- a/ttt.jai
+++ b/ttt.jai
@@ -70,9 +70,9 @@ Database :: struct {
//
//
//
-// database_st database = { .tasks = NULL };
-// database_st archive = { .tasks = NULL };
-// database_st *db = NULL;
+
+database : Database;
+archive : Database;
is_autosave_enabled := true;
// int countdown_to_autosave = -1;
app_directory : string;
@@ -631,34 +631,12 @@ void add_task_time(database_st *db, task_st *task, int day, int64_t time) {
// Resets database to the initial state and deallocates all memory taken by tasks.
reset_database :: (db: *Database) {
assert(db != null);
-
free(db.tasks.data);
<<db = .{};
}
// Stores data from database into binary file.
// Returns success.
-// bool store_database(const database_st *db, const char *path) {
-// assert(db != NULL);
-// assert(path != NULL);
-//
-// Open file.
-// FILE *file = fopen(path, "wb");
-// if (file == NULL) {
-// print_error("Failed to open file '%s' while storing database: %s.", path, strerror(errno));
-// return false;
-// }
-//
-// fwrite(DB_FILE_SIGN, SIZEOF_CHAR, DB_FILE_SIGN_LENGTH, file);
-// fwrite(db, SIZEOF_DATABASE_ST, 1, file);
-// fwrite(db->tasks, SIZEOF_TASK_ST, db->count, file);
-//
-// fclose(file);
-// return true;
-// }
-
-// Stores data from database into binary file.
-// Returns success.
store_database :: (db: *Database, path: string) -> success: bool {
assert(db != null, "Parameter 'db' is null.");
assert(xx path, "Parameter 'path' is empty.");
@@ -680,50 +658,6 @@ store_database :: (db: *Database, path: string) -> success: bool {
// Loads data from binary file into database.
// Returns success.
-/*
-bool load_database(database_st *db, const char *path) {
- assert(db != NULL);
- assert(path != NULL);
-
- Open file.
- FILE *file = fopen(path, "rb");
- if (file == NULL) {
- print_error("Failed to open file '%s' while loading database: %s.", path, strerror(errno));
- return false;
- }
-
- Validate file signature.
- char file_signature[DB_FILE_SIGN_LENGTH];
- fread(&file_signature, SIZEOF_CHAR, DB_FILE_SIGN_LENGTH, file);
- if (strncmp(file_signature, DB_FILE_SIGN, DB_FILE_SIGN_LENGTH) != 0) {
- print_error("Invalid file signature.");
- fclose(file);
- return false;
- }
-
- Read database structure.
- fread(db, SIZEOF_DATABASE_ST, 1, file);
-
- Reserve database capacity for tasks.
- size_t capacity_bytes = db->capacity * SIZEOF_TASK_ST;
- db->tasks = malloc(capacity_bytes);
- if (db->tasks == NULL && capacity_bytes > 0) {
- print_error("Failed to allocate memory while loading database: %s.", strerror(errno));
- return false;
- }
-
- Read database tasks.
- fread(db->tasks, SIZEOF_TASK_ST, db->count, file);
-
- Make sure we are reading all the file.
- assert(fgetc(file) == EOF);
-
- fclose(file);
- return true;
-}*/
-
-// Loads data from binary file into database.
-// Returns success.
load_database :: (db: *Database, path: string) -> success: bool {
assert(db != null, "Parameter 'db' is null.");
assert(xx path, "Parameter 'path' is empty.");
@@ -747,24 +681,25 @@ load_database :: (db: *Database, path: string) -> success: bool {
// Read database structure.
read_success = file_read(file, db, size_of(Database));
- //if read_success == false print_error("Failed to read database info from '%'.", path); TODO
+ // TODO Use print_error or assert?
+ if read_success == false {
+ print_error("Failed to read database info from '%'.", path);
+ return false;
+ }
+ assert(read_success == true, "Failed to read database info from '%'.", path);
// Reserve database capacity for tasks.
db.tasks.data = alloc(db.tasks.count * size_of(Task));
db.capacity = db.tasks.count;
// Read database tasks.
- file_read(file, db.tasks.data, size_of(Task) * db.tasks.count); // TODO WIP WIP WIP Does this generate stable memory or temporary?
+ file_read(file, db.tasks.data, size_of(Task) * db.tasks.count);
// Make sure we are reading all the file.
- // TODO Clean this. WIP WIP WIP
- buffer: [1] u8;
+ buffer: u8;
success, bytes := file_read(file, *buffer, 1);
- print("'%'\n", buffer);
- assert(bytes == 0);
+ assert(bytes == 0, "Unexpected content found at the end of file '%'.", path);
- print("done importing CSV with a bunch of hacks\n");
-
return true;
}
@@ -1298,11 +1233,8 @@ void *mem_alloc(size_t mem_size, const char *error_tag) {
*/
free_memory :: () {
- print(">> FREE <<\n");
-
- // TODO
- //reset_database(&database);
- //reset_database(&archive);
+ reset_database(*database);
+ reset_database(*archive);
//free(string_buffer); string_buffer = NULL;
free(app_directory);
@@ -1378,12 +1310,9 @@ bool read_enter_confirmation(int row, int style, const char *message) {
return getch() == '\n';
}
*/
-
+
main :: () {
- database: Database;
- archive: Database;
-
defer report_memory_leaks(); // TODO DEBUG
defer free_memory();
@@ -1422,17 +1351,6 @@ main :: () {
exit(1);
}
}
-
-
-// import_from_csv(*db, ar_file_path);
-//
-// for db.tasks print_task(it);
-//
-// print_task :: (task: Task) {
-// for task.times print("% |", it);
-// print(" %\n", cast(string)task.name);
-// }
-// return;
}
args := get_command_line_arguments();
@@ -1490,32 +1408,31 @@ main :: () {
"- During intensive tasks such as saving to file or recalculating totals times,\n",
" a diamond symbol is shown on the top left corner.\n"
);
- return;
+ exit(0);
}
if is_equal_to_any(args[it], "--version", "-v") {
print("Task Time Tracker version % \nCopyright % Daniel Martins\nLicense GPL-3.0-or-later\n", VERSION, YEAR);
- return;
+ exit(0);
}
if is_equal_to_any(args[it], "--import-csv", "-i") {
it += 1;
if it >= args.count {
print_error("Missing CSV file path to import.");
- return;
+ exit(1);
}
- // TODO Implement
if (load_database(*database, db_file_path) == false) {
print_error("Failed to load database.");
- return;
+ exit(1);
}
if (import_from_csv(*database, args[it]) == false) {
print_error("Failed to import CSV file.");
- return;
+ exit(1);
}
if (store_database(*database, db_file_path) == false) {
print_error("Failed to store database.");
- return;
+ exit(1);
}
reset_database(*database);
is_exit_requested = true;
@@ -1526,16 +1443,15 @@ main :: () {
it += 1;
if it >= args.count {
print_error("Missing CSV file path to export.");
- return;
+ exit(1);
}
- // TODO Implement
if (load_database(*database, db_file_path) == false) {
print_error("Failed to load database.");
- return;
+ exit(1);
}
if (export_to_csv(*database, args[it]) == false) {
print_error("Failed to export CSV file.");
- return;
+ exit(1);
}
reset_database(*database);
is_exit_requested = true;
@@ -1548,26 +1464,27 @@ main :: () {
}
print_error("%: invalid option '%'.\nTry '% --help' for more information.", args[0], args[it], args[0]);
- return;
+ exit(1);
}
if is_exit_requested {
- return;
+ exit(0);
}
}
- /*
- if (load_database(&database, db_file_path) == false) {
+
+ if (load_database(*database, db_file_path) == false) {
print_error("Failed to load database.");
- return;
+ exit(1);
}
- initialize_tui();
-
- signal(SIGTERM, exit_gracefully);
- signal(SIGINT, exit_gracefully);
- signal(SIGQUIT, exit_gracefully);
- signal(SIGHUP, exit_gracefully);
+ //initialize_tui();
+ /*
+ // TODO Remove this?!
+ //signal(SIGTERM, exit_gracefully);
+ //signal(SIGINT, exit_gracefully);
+ //signal(SIGQUIT, exit_gracefully);
+ //signal(SIGHUP, exit_gracefully);
flushinp();
ungetch(KEY_RESIZE);
@@ -2014,7 +1931,7 @@ main :: () {
}
*/
-
+// TODO DEBUG
print_owner_allocator :: (tag: string, memory: *void) {
owner := "unkown";
@@ -2023,3 +1940,18 @@ print_owner_allocator :: (tag: string, memory: *void) {
print("'%' belongs to '%'\n", tag, owner);
}
+
+// TODO DEBUG
+print_database :: (db: Database) {
+ for db.tasks {
+ print("% | % : % : % : % : % : % : %\n", cast(string)it.name,
+ it.times[0],
+ it.times[1],
+ it.times[2],
+ it.times[3],
+ it.times[4],
+ it.times[5],
+ it.times[6]
+ );
+ }
+}