aboutsummaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
authordam <dam@gudinoff>2022-11-02 01:03:16 +0000
committerdam <dam@gudinoff>2022-11-02 01:03:16 +0000
commit4b0f25ca86c6c5d14d414a7ac8ccf2fa52460f1b (patch)
treec0c3e49ba995f87a74f1e08bb8c7ef8f42cc18fe /misc.c
parenteb5a3b3535057a62728460cce562aa64312e0519 (diff)
downloadtask-time-tracker-4b0f25ca86c6c5d14d414a7ac8ccf2fa52460f1b.tar.zst
task-time-tracker-4b0f25ca86c6c5d14d414a7ac8ccf2fa52460f1b.zip
Implemented autosave feature. Fixed argument/options processing to allow repeating options, and using options to set behavior configurations.
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/misc.c b/misc.c
index 151e26f..f5a3ada 100644
--- a/misc.c
+++ b/misc.c
@@ -1,3 +1,32 @@
+
+// Writes only the database core structure and the provided task if not null.
+// Returns success.
+bool store_database_partial(const database_st *db, const task_st *task, const char *path) {
+ return store_database(db, path);
+ assert(db != NULL);
+ assert(path != NULL);
+
+ // Open file.
+ FILE *file = fopen(path, "r+b");
+ if (file == NULL) {
+ fprintf(stderr, "Failed to open file '%s' while partially storing database: %s.\n", path, strerror(errno));
+ return false;
+ }
+
+ fseek(file, DB_FILE_SIGN_LENGTH, SEEK_SET);
+ fwrite(db, SIZEOF_DATABASE_ST, 1, file);
+
+ if (task != NULL) {
+ assert(task >= db->tasks && task < &db->tasks[db->count]);
+ ptrdiff_t offset = task - db->tasks;
+ fseek(file, offset * SIZEOF_TASK_ST, SEEK_CUR);
+ fwrite(task, SIZEOF_TASK_ST, 1, file);
+ }
+
+ fclose(file);
+ return true;
+}
+
// Returns the number of characters in a string using UTF8 encoding.
size_t length_utf8(char *string) {
size_t size = 0;