1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#include <stdio.h>
#include <unistd.h>
#include "agenda_entry.h"
#define BS 100 //blocksize for buffers
void destroy_entry_array(entry *array, int elements) {
for (int i = 0; i < elements; i++)
destroy_entry(array[i]);
free(array);
}
char *read_file_to_buffer(char *filename) {
FILE *fd = fopen(filename, "r");
int bufsize = BS,
bufread = 0;
char *buf = (char *)malloc(bufsize);
char *newbuf;
while (fread(buf + bufread, 1, 100, fd) == BS) {
newbuf = (char *)malloc(bufsize + BS);
strncpy(newbuf, buf, bufsize);
free(buf);
buf = newbuf;
bufsize += BS;
bufread += BS;
}
fclose(fd);
return buf;
}
// this function does not work... to debug!
entry *read_entries_to_array(char *buffer, int *entries) {
int numentries = 5,
idx = 0;
entry *ret = (entry *)malloc(numentries * sizeof(entry));
char *cursor = buffer;
while (cursor) {
if (idx == numentries) {
entry *newret = (entry *)malloc((numentries + 5) * sizeof(entry));
memcpy(newret, ret, (idx)*sizeof(entry));
free(ret);
ret = newret;
numentries += 5;
}
ret[idx++] = read_agenda_entry(cursor);
cursor = next_entry(cursor);
}
*entries = idx;
return ret;
}
int main(int argc, char **argv) {
// unbuffering stdout in order to prevent leaks
// has the added benefit of reducing memory usage too
setvbuf(stdout, NULL, _IONBF, 0);
if (argc <= 1) {
printf("no agenda file supplied!\nUsage: otc <agendafile>\n");
return -1;
}
// read file into buf
char *buf = read_file_to_buffer(argv[1]);
if (strlen(buf) == 0) return -1; // don't do empty buffers
// read entries into array
int idx = 0;
entry *agenda = read_entries_to_array(buf, &idx);
free(buf);
// sort entries
sort_entry_array(agenda, idx);
// format nicely
char *e;
Date td = today();
Date tm = tomorrow(td);
Date nw = nextweek(td);
Date last_date = (struct Date){ -1, -1, -1 };
for (int i = 0; i < idx; i++) {
if (!eql(agenda[i].date, last_date)) {
last_date = agenda[i].date;
if (!zero(last_date)) {
e = print_date_to_string(last_date);
if (strictly_smaller(last_date, td))
printf("\n%s [OUTATIME]:\n", e);
else if (eql(last_date, td))
printf("\n%s [TODAY]:\n", e);
else if (eql(last_date, tm))
printf("\n%s [TOMORROW]:\n", e);
else if (smaller(last_date, nw))
printf("\n%s:\n", e);
free(e);
} else {
printf("\nUnscheduled:\n");
}
}
e = format_entry(agenda[i]);
if (zero(agenda[i].date) || smaller(agenda[i].date, nw)) puts(e);
free(e);
}
// free up the memory
fclose(stdout);
destroy_entry_array(agenda, idx);
return 0;
}
|