aboutsummaryrefslogtreecommitdiff
path: root/unused.jai
diff options
context:
space:
mode:
Diffstat (limited to 'unused.jai')
-rw-r--r--unused.jai68
1 files changed, 68 insertions, 0 deletions
diff --git a/unused.jai b/unused.jai
index 1f71b37..da8b41d 100644
--- a/unused.jai
+++ b/unused.jai
@@ -48,6 +48,74 @@ print_database :: (db: Database) {
// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- //
+DB_FILE_SIGN_STR :: "TTT:B:02";
+
+// Stores data from database into binary file.
+// Returns success.
+store_database :: (db: Database, path: string) -> success: bool #must {
+ assert(xx path, ASSERT_NOT_EMPTY, "path");
+
+ // Open file.
+ file, open_success := file_open(path, for_writing = true);
+ if open_success == false return false;
+ defer file_close(*file);
+
+ 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);
+
+ return true;
+}
+
+// Loads data from binary file into database.
+// Returns success.
+load_database :: (db: *Database, path: string) -> success: bool #must {
+ assert(db != null, ASSERT_NOT_NULL, "db");
+ assert(xx path, ASSERT_NOT_EMPTY, "path");
+
+ // Open file.
+ file, open_success := file_open(path);
+ if open_success == false then return false;
+ defer file_close(*file);
+
+ // Validate file signature.
+ file_signature: [DB_FILE_SIGN_STR.count] u8;
+ read_success := file_read(file, *file_signature, DB_FILE_SIGN_STR.count);
+ if read_success == false log_error("Failed to read file signature.");
+ if cast(string)file_signature != DB_FILE_SIGN_STR {
+ log_error("Invalid file signature while loading database.");
+ return false;
+ }
+
+ // Read database structure.
+ read_success = file_read(file, db, size_of(Database));
+ if read_success == false {
+ log_error("Failed to read database info.");
+ return false;
+ }
+
+ // Reserve database capacity for tasks.
+ tasks_count := db.tasks.count;
+ Initialize(*db.tasks); // Cleanup whatever was read from file.
+ array_reserve(*db.tasks, tasks_count);
+
+ // Read database tasks.
+ file_read(file, db.tasks.data, size_of(Task) * tasks_count);
+ db.tasks.count = tasks_count;
+
+ // Make sure we are reading all the file.
+ buffer: u8;
+ success, bytes := file_read(file, *buffer, 1);
+ if bytes > 0 {
+ log_error("Unexpected content found at the end of file '%'.", path);
+ return false;
+ }
+
+ return true;
+}
+
+// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- //
+
// Average cumulative calculation.
average: float64 = 0;