diff options
Diffstat (limited to 'src/path.c')
-rw-r--r-- | src/path.c | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/path.c b/src/path.c new file mode 100644 index 0000000..2294276 --- /dev/null +++ b/src/path.c @@ -0,0 +1,108 @@ + /** + * Journal-tools provides simple tools for keeping journal entries + * Copyright (C) 2023 Aaron Ball, nullspoon@oper.io + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "path.h" + +void path_date_fmt_filename(char* fmt, time_t offset, char* outbuf, int size) { + time_t epoch; + struct tm* tm; + + epoch = time(NULL) + offset; + tm = localtime(&epoch); + + strftime(outbuf, size, fmt, tm); +} + +void path_get_ext(char* envvar, char* buf, int size) { + if(getenv(envvar) != NULL) + strncpy(buf, getenv(envvar), size); + else + strcpy(buf, "md"); +} + +/** + * Searches paths for existing files, stopping upon finding a certain number + * behind. This is mostly for searching predictably named files where gaps in + * the naming convention may exist but should be ignored. + * + * For example, in a directory of date stampped filenames, some days may be + * missing. Instead of "-5" being "5 days ago", this function makes it "5 files + * ago", generating filenames as provided by the callback function. + * + * @int num Number of offset + * @int max Maximum number of files to search for, existing or not + * (prevents infinite loops) + * @void (*namecb)() Callback function of call signature (struct path*) + * @void* data Path structure to pass to the callback function + */ +int path_find_timepast(int num, int max, void (*namecb)(), void* data) { + int found = 0; + int loops = 0; + struct path* pathdata = (struct path*) data; + char pathbuf[1024] = {0}; + + // Only loop up to 1 possible year + // Note this operates backwards, as offset is negative + while(found > num && loops < max) { + (*namecb)(pathdata, pathbuf, 1024); + + // Get possible note path + sprintf(pathbuf, "%s/%s.%s", pathdata->base, pathdata->name, pathdata->ext); + + // Check if it exists, and increment find counter if true + if(access(pathbuf, R_OK) == 0) + found--; + + // Decrement offset seconds by one more day + pathdata->offset -= 24 * 60 * 60; + loops++; + } + + if(loops >= max) + return -1; + return loops; +} + + +// TODO: Rewrite these. They are trash. +void path_get_daily_journaldir(char* envvar, char* buf, int size) { + if(getenv(envvar) != NULL) + strncpy(buf, getenv(envvar), size); + else + snprintf(buf, size, "%s/Documents/Journal", getenv("HOME")); + + // Abort if journaldir is not accessible + if(access(buf, W_OK) != 0) { + fprintf(stderr, "Cannot access journal dir [%s]\n", buf); + fputs("Does it exist and do you have write access?\n", stderr); + exit(1); + } +} + +void path_get_weekly_journaldir(char* envvar, char* buf, int size) { + if(getenv(envvar) != NULL) + strncpy(buf, getenv(envvar), size); + else + snprintf(buf, size, "%s/Documents/Journal/Weekly", getenv("HOME")); + + // Abort if journaldir is not accessible + if(access(buf, W_OK) != 0) { + fprintf(stderr, "Cannot access journal dir [%s]\n", buf); + fputs("Does it exist and do you have write access?\n", stderr); + exit(1); + } +} |