diff options
| author | El-BG-1970 <elouan.gros.fr@gmail.com> | 2021-10-14 12:54:52 +0200 |
|---|---|---|
| committer | El-BG-1970 <elouan.gros.fr@gmail.com> | 2021-10-14 12:54:52 +0200 |
| commit | 330aa88ba3a8f3e9de0362146b6e2a4d54aa61a7 (patch) | |
| tree | 04cbd1c0731522db50d7058399cac737b5eb5f3e /main.c | |
| parent | 8190f10e4880b73cbd4c0d3694f38b6d5953bb4e (diff) | |
| download | org-to-conky-330aa88ba3a8f3e9de0362146b6e2a4d54aa61a7.tar.gz | |
better formatting loop
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 151 |
1 files changed, 65 insertions, 86 deletions
@@ -4,9 +4,9 @@ #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); + for (int i = 0; i < elements; i++) + destroy_entry(array[i]); + free(array); } char *read_file_to_buffer(char *filename) { @@ -30,93 +30,72 @@ char *read_file_to_buffer(char *filename) { // 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 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); - /*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; - }*/ + // 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 - // sort entries - sort_entry_array(agenda, idx); + // read entries into array + int idx = 0; + entry *agenda = read_entries_to_array(buf, &idx); + free(buf); - // 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); - } + // sort entries + sort_entry_array(agenda, idx); + + // format nicely + char *e; + Date td = today(); + Date nw = nextweek(td); + Date last_date = (struct Date){ -1, -1, -1 };//agenda[0].date; + 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; + // free up the memory + fclose(stdout); + destroy_entry_array(agenda, idx); + return 0; } |
