diary

Text-based journaling program
git clone https://git.in0rdr.ch/diary.git
Log | Files | Refs | README | LICENSE

commit 04ec397be3d57c9eea510b5f7b7f6af937056677
parent b574de537cb5f2bd3ae066ad8e637b465281a4e5
Author: Andreas Gruhler <agruhl@gmx.ch>
Date:   Sat, 22 May 2021 00:27:43 +0200

move fpath function to utils

Diffstat:
Mdiary.c | 36------------------------------------
Mdiary.h | 1-
Mutils.c | 36++++++++++++++++++++++++++++++++++++
Mutils.h | 2++
4 files changed, 38 insertions(+), 37 deletions(-)

diff --git a/diary.c b/diary.c @@ -203,42 +203,6 @@ bool date_has_entry(const char* dir, size_t dir_size, const struct tm* i) return (access(epath, F_OK) != -1); } -/* Writes file path for 'date' entry to 'rpath'. '*rpath' is NULL on error. */ -void fpath(const char* dir, size_t dir_size, const struct tm* date, char** rpath, size_t rpath_size) -{ - // check size of result path - if (dir_size + 1 > rpath_size) { - fprintf(stderr, "Directory path too long"); - *rpath = NULL; - return; - } - - // add path of the diary dir to result path - strcpy(*rpath, dir); - - // check for terminating '/' in path - if (dir[dir_size - 1] != '/') { - // check size again to accommodate '/' - if (dir_size + 1 > rpath_size) { - fprintf(stderr, "Directory path too long"); - *rpath = NULL; - return; - } - strcat(*rpath, "/"); - } - - char dstr[16]; - strftime(dstr, sizeof dstr, CONFIG.fmt, date); - - // append date to the result path - if (strlen(*rpath) + strlen(dstr) > rpath_size) { - fprintf(stderr, "File path too long"); - *rpath = NULL; - return; - } - strcat(*rpath, dstr); -} - /* * Finds the date with a diary entry closest to <current>. * Depending on <search_backwards>, it will find the most recent diff --git a/diary.h b/diary.h @@ -40,6 +40,5 @@ void display_entry(const char* dir, size_t dir_size, const struct tm* date, WIND void edit_cmd(const char* dir, size_t dir_size, const struct tm* date, char** rcmd, size_t rcmd_size); bool date_has_entry(const char* dir, size_t dir_size, const struct tm* i); -void fpath(const char* dir, size_t dir_size, const struct tm* date, char** rpath, size_t rpath_size); #endif diff --git a/utils.c b/utils.c @@ -48,6 +48,42 @@ char* strrstr(char *haystack, char *needle) { return NULL; } +/* Writes file path for 'date' entry to 'rpath'. '*rpath' is NULL on error. */ +void fpath(const char* dir, size_t dir_size, const struct tm* date, char** rpath, size_t rpath_size) +{ + // check size of result path + if (dir_size + 1 > rpath_size) { + fprintf(stderr, "Directory path too long"); + *rpath = NULL; + return; + } + + // add path of the diary dir to result path + strcpy(*rpath, dir); + + // check for terminating '/' in path + if (dir[dir_size - 1] != '/') { + // check size again to accommodate '/' + if (dir_size + 1 > rpath_size) { + fprintf(stderr, "Directory path too long"); + *rpath = NULL; + return; + } + strcat(*rpath, "/"); + } + + char dstr[16]; + strftime(dstr, sizeof dstr, CONFIG.fmt, date); + + // append date to the result path + if (strlen(*rpath) + strlen(dstr) > rpath_size) { + fprintf(stderr, "File path too long"); + *rpath = NULL; + return; + } + strcat(*rpath, dstr); +} + config CONFIG = { .range = 1, .weekday = 1, diff --git a/utils.h b/utils.h @@ -1,6 +1,7 @@ #ifndef DIARY_UTILS_H #define DIARY_UTILS_H +#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> @@ -18,6 +19,7 @@ char* extract_json_value(char* json, char* key, bool quoted); char* expand_path(char* str); char* strrstr(char *haystack, char *needle); +void fpath(const char* dir, size_t dir_size, const struct tm* date, char** rpath, size_t rpath_size); typedef struct {