commit 5dc782bee98477fd596b8b12dcb4bd08d5bf837b
parent 442dafe84d289c80deebc698422b73c411791064
Author: Andreas Gruhler <agruhl@gmx.ch>
Date: Sun, 31 Oct 2021 00:33:28 +0200
highlight imported entry, simplify unfold
Diffstat:
2 files changed, 32 insertions(+), 34 deletions(-)
diff --git a/src/import.c b/src/import.c
@@ -23,7 +23,7 @@ void ics_import(const char* ics_input, WINDOW* header, WINDOW* cal, WINDOW* asid
int conf_ch = 0;
char dstr[16];
- struct tm date;
+ struct tm date = {};
long search_pos = 0;
char* vevent;
@@ -45,7 +45,7 @@ void ics_import(const char* ics_input, WINDOW* header, WINDOW* cal, WINDOW* asid
// fprintf(stderr, "VEVENT DESCRIPTION: \n\n%s\n\n", vevent_desc);
// parse date
- strptime(vevent_date, "%Y%m%dT%H%M%SZ", &date);
+ strptime(vevent_date, "%Y%m%d", &date);
strftime(dstr, sizeof dstr, CONFIG.fmt, &date);
// get path of entry
@@ -96,12 +96,13 @@ void ics_import(const char* ics_input, WINDOW* header, WINDOW* cal, WINDOW* asid
}
fclose(cursordate_file);
- // add new entry highlight
- go_to(cal, aside, mktime(&date), pad_pos, curs_date, cal_start, cal_end);
- // update_date(header, curs_date);
- chtype atrs = winch(cal) & A_ATTRIBUTES;
- wchgat(cal, 2, atrs | A_BOLD, 0, NULL);
- prefresh(cal, *pad_pos, 0, 1, ASIDE_WIDTH, LINES - 1, ASIDE_WIDTH + CAL_WIDTH);
+ bool mv_valid = go_to(cal, aside, mktime(&date), pad_pos, curs_date, cal_start, cal_end);
+ if (mv_valid) {
+ // add new entry highlight
+ chtype atrs = winch(cal) & A_ATTRIBUTES;
+ wchgat(cal, 2, atrs | A_BOLD, 0, NULL);
+ prefresh(cal, *pad_pos, 0, 1, ASIDE_WIDTH, LINES - 1, ASIDE_WIDTH + CAL_WIDTH);
+ }
pthread_cancel(progress_tid);
}
}
@@ -110,8 +111,8 @@ void ics_import(const char* ics_input, WINDOW* header, WINDOW* cal, WINDOW* asid
// fprintf(stderr, "Import DESCRIPTION: %s\n", desc);
fprintf(stderr, "* * * * * * * * * * * * * \n");
}
- // free(vevent);
- // free(vevent_date);
- // free(vevent_desc);
+ free(vevent);
+ free(vevent_date);
+ free(vevent_desc);
free(ics);
}
\ No newline at end of file
diff --git a/src/utils.c b/src/utils.c
@@ -232,7 +232,7 @@ char* extract_ical_field(const char* ics, char* key, long* start_pos, bool multi
}
// work on a copy of the ical xml response
- char* icscp = (char *) malloc(strlen(ics) * sizeof(char) + 1);
+ char* icscp = (char *) malloc(strlen(ics) + 1 * sizeof(char));
if (icscp == NULL) {
perror("malloc failed");
return NULL;
@@ -240,10 +240,11 @@ char* extract_ical_field(const char* ics, char* key, long* start_pos, bool multi
strcpy(icscp, ics);
// tokenize ical by newlines
+ char* buf = NULL;
char* res = strtok(icscp, "\n");
-
while (res != NULL) {
if (regexec(&re, res, 1, pm, 0) == 0) {
+ // found the key in line 'res'
res = strstr(res, ":"); // value
res++; // strip the ":"
@@ -255,33 +256,26 @@ char* extract_ical_field(const char* ics, char* key, long* start_pos, bool multi
fprintf(stderr, "Res: %s\n", res);
*start_pos = res - icscp;
fprintf(stderr, "Start pos: %li\n", *start_pos);
- break;
- }
- // key not in this line, advance line
- res = strtok(NULL, "\n");
- }
- char* buf = NULL;
-
- if (res != NULL) {
- if (strlen(res) == 0) {
- // empty remote value
- buf = NULL;
- } else if (multiline) {
- buf = unfold(ics + *start_pos);
- } else {
- buf = malloc(strlen(res) + 1);
- if (buf == NULL) {
- perror("malloc failed");
- return NULL;
+ if (strlen(res) != 0) {
+ // non-empty remote value
+ if (multiline) {
+ buf = unfold(ics + *start_pos);
+ } else {
+ buf = malloc(strlen(res) + 1);
+ if (buf == NULL) {
+ perror("malloc failed");
+ return NULL;
+ }
+ strcpy(buf, res);
+ }
}
- strcpy(buf, res);
}
+ res = strtok(NULL, "\n");
}
regfree(&re);
free(icscp);
-
return buf;
}
@@ -347,8 +341,10 @@ void fpath(const char* dir, size_t dir_size, const struct tm* date, char** rpath
}
bool go_to(WINDOW* calendar, WINDOW* aside, time_t date, int* cur_pad_pos, struct tm* curs_date, struct tm* cal_start, struct tm* cal_end) {
- if (date < mktime(cal_start) || date > mktime(cal_end))
+ if (date < mktime(cal_start) || date > mktime(cal_end)) {
+ fprintf(stderr, "Invalid cursor move, return from go_to\n");
return false;
+ }
int diff_seconds = date - mktime(cal_start);
int diff_days = diff_seconds / 60 / 60 / 24;
@@ -402,6 +398,7 @@ void* show_progress(void* vargp){
}
config CONFIG = {
+ .dir = NULL,
.range = 1,
.weekday = 1,
.fmt = "%Y-%m-%d",