1 /**
2 * Journal-tools provides simple tools for keeping journal entries
3 * Copyright (C) 2023 Aaron Ball, nullspoon@oper.io
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "path.h"
19
20 void path_date_fmt_filename(char* fmt, time_t offset, char* outbuf, int size) {
21 time_t epoch;
22 struct tm* tm;
23
24 epoch = time(NULL) + offset;
25 tm = localtime(&epoch);
26
27 strftime(outbuf, size, fmt, tm);
28 }
29
30 void path_get_ext(char* envvar, char* buf, int size) {
31 if(getenv(envvar) != NULL)
32 strncpy(buf, getenv(envvar), size);
33 else
34 strcpy(buf, "md");
35 }
36
37 /**
38 * Searches paths for existing files, stopping upon finding a certain number
39 * behind. This is mostly for searching predictably named files where gaps in
40 * the naming convention may exist but should be ignored.
41 *
42 * For example, in a directory of date stampped filenames, some days may be
43 * missing. Instead of "-5" being "5 days ago", this function makes it "5 files
44 * ago", generating filenames as provided by the callback function.
45 *
46 * @int num Number of offset
47 * @int max Maximum number of files to search for, existing or not
48 * (prevents infinite loops)
49 * @void (*namecb)() Callback function of call signature (struct path*)
50 * @void* data Path structure to pass to the callback function
51 */
52 int path_find_timepast(int num, int max, void (*namecb)(), void* data) {
53 int found = 0;
54 int loops = 0;
55 struct path* pathdata = (struct path*) data;
56 char pathbuf[1024] = {0};
57
58 // Only loop up to 1 possible year
59 // Note this operates backwards, as offset is negative
60 while(found > num && loops < max) {
61 (*namecb)(pathdata, pathbuf, 1024);
62
63 // Get possible note path
64 sprintf(pathbuf, "%s/%s.%s", pathdata->base, pathdata->name, pathdata->ext);
65
66 // Check if it exists, and increment find counter if true
67 if(access(pathbuf, R_OK) == 0)
68 found--;
69
70 // Decrement offset seconds by one more day
71 pathdata->offset -= 24 * 60 * 60;
72 loops++;
73 }
74
75 if(loops >= max)
76 return -1;
77 return loops;
78 }
79
80
81 // TODO: Rewrite these. They are trash.
82 void path_get_daily_journaldir(char* envvar, char* buf, int size) {
83 if(getenv(envvar) != NULL)
84 strncpy(buf, getenv(envvar), size);
85 else
86 snprintf(buf, size, "%s/Documents/Journal", getenv("HOME"));
87
88 // Abort if journaldir is not accessible
89 if(access(buf, W_OK) != 0) {
90 fprintf(stderr, "Cannot access journal dir [%s]\n", buf);
91 fputs("Does it exist and do you have write access?\n", stderr);
92 exit(1);
93 }
94 }
95
96 void path_get_weekly_journaldir(char* envvar, char* buf, int size) {
97 if(getenv(envvar) != NULL)
98 strncpy(buf, getenv(envvar), size);
99 else
100 snprintf(buf, size, "%s/Documents/Journal/Weekly", getenv("HOME"));
101
102 // Abort if journaldir is not accessible
103 if(access(buf, W_OK) != 0) {
104 fprintf(stderr, "Cannot access journal dir [%s]\n", buf);
105 fputs("Does it exist and do you have write access?\n", stderr);
106 exit(1);
107 }
108 }
|