From 8140d75aeb50e45aaa2d4ce34bfc84509fd2e91d Mon Sep 17 00:00:00 2001 From: El-BG-1970 Date: Wed, 13 Oct 2021 11:58:17 +0200 Subject: first commit --- main.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 main.c (limited to 'main.c') diff --git a/main.c b/main.c new file mode 100644 index 0000000..c11ce29 --- /dev/null +++ b/main.c @@ -0,0 +1,122 @@ +#include +#include +#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 \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); + /*char *tmp = buf; + entry *agenda; + int numentries = 10, + idx = 0, + buflen = strlen(buf); + agenda = (entry *)malloc(numentries * sizeof(entry)); + while (buf) { + agenda[idx++] = read_agenda_entry(buf); + tmp = next_entry(buf); + if (tmp == NULL) buflen = 0; + else buflen -= (tmp - buf); + buf = tmp; + }*/ + + // sort entries + sort_entry_array(agenda, idx); + + // format nicely + char *e; + Date td = today(); + Date nw = nextweek(td); + Date last_date = agenda[0].date; + if (!zero(last_date)) { + e = print_date_to_string(last_date); + if (smaller(last_date, td)) + printf("\n%s [OUTATIME]:\n", e); + else if (smaller(last_date, nw)) + printf("\n%s:\n", e); + free(e); + } + 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 (smaller(last_date, td)) + printf("\n%s [OUTATIME]:\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; +} -- cgit v1.2.3