From 4b0f25ca86c6c5d14d414a7ac8ccf2fa52460f1b Mon Sep 17 00:00:00 2001 From: dam Date: Wed, 2 Nov 2022 01:03:16 +0000 Subject: Implemented autosave feature. Fixed argument/options processing to allow repeating options, and using options to set behavior configurations. --- misc.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'misc.c') 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; -- cgit v1.2.3