diff options
Diffstat (limited to 'misc.c')
| -rw-r--r-- | misc.c | 29 |
1 files changed, 29 insertions, 0 deletions
@@ -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; |
