1 /**
2 * Dailytodo is an easy tool to review daily todo items
3 * Copyright (C) 2022 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 #define _XOPEN_SOURCE
19 #include <stdio.h>
20
21 #include "common.h"
22 #include "config.h"
23
24 int print_section(char* path, char* prefix, char* title) {
25 FILE* fd = NULL;
26 char buf[4096] = {'\0'};
27 char header[512] = {'\0'};
28 char hprefix[32] = {'\0'};
29 int headerlen = 0;
30 int hprefixlen = 0;
31 int output = 0;
32
33 snprintf(header, 512, "%s %s\n", prefix, title);
34 snprintf(hprefix, 32, "%s \n", prefix);
35 headerlen = strlen(header);
36 hprefixlen = strlen(hprefix);
37
38 fd = fopen(path, "r");
39 while(fgets(buf, 4096, fd) != NULL) {
40 if(strncmp(buf, header, headerlen) == 0 ) {
41 output = 1;
42 continue;
43 } else if(strncmp(buf, hprefix, hprefixlen) == 0 ) {
44 output = 0;
45 }
46 if(output == 1)
47 fputs(buf, stdout);
48 }
49
50 fclose(fd);
51 return 0;
52 }
53
54 int main(int argc, char* argv[]) {
55 char notepath[256] = {'\0'};
56 char date[128] = {'\0'};
57 char* prefix = "#";
58 char* title = "Todo";
59 int days = 0;
60 int i = 0;
61 struct config c;
62
63 memset(&c, sizeof(struct config), 1);
64 config_init(&c);
65
66 if(argc > 1)
67 days = strtol(argv[1], NULL, 10);
68
69 // If user requests negative days, we want to skip non-existant days. So `-2`
70 // is really "two entries ago" rather than two days ago, which could yield
71 // creating an empty historical note if one does not already exist (for
72 // example: on Monday, opening Friday's entry which is -3 days, but -1
73 // entry).
74 if(days < 0) {
75 while(days < 0) {
76 i--;
77 getpath(c.journaldir, i, notepath, c.extension);
78 if(fexists(notepath))
79 days++;
80 // Stop traversing after a year to avoid infinite loops when no notes
81 // exist (or exist very long ago)
82 if(i < -365) {
83 fprintf(stderr, "Refusing to search further back than one year\n");
84 return 1;
85 }
86 }
87 } else {
88 getpath(c.journaldir, days, notepath, c.extension);
89 }
90 getdate(i, date, 128);
91
92 printf("%s %s (%s)\n", prefix, title, date);
93 print_section(notepath, prefix, title);
94
95 return 0;
96 }
|