From 42abbf53463cbbdcd319063fcbfb91cf6ed5bdc5 Mon Sep 17 00:00:00 2001 From: Lars Henriksen Date: Thu, 6 Dec 2018 09:56:45 +0100 Subject: Filter option: invert New filter option: --filter-invert. When present it inverts (negates) the other filter options combined. This is mostly useful with the -G option (with -Q the output is limited by the query range (day range)). The ouput from "calcurse -G " is the (set) complement of "calcurse -G --filter-invert". Here may be any combination of filter options. Signed-off-by: Lars Henriksen Signed-off-by: Lukas Fleischer --- src/recur.c | 101 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 53 insertions(+), 48 deletions(-) (limited to 'src/recur.c') diff --git a/src/recur.c b/src/recur.c index ca99a21..e481c08 100644 --- a/src/recur.c +++ b/src/recur.c @@ -349,7 +349,8 @@ struct recur_apoint *recur_apoint_scan(FILE * f, struct tm start, { char buf[BUFSIZ], *nl; time_t tstart, tend, tuntil; - struct recur_apoint *rapt; + struct recur_apoint *rapt = NULL; + int cond; EXIT_IF(!check_date(start.tm_year, start.tm_mon, start.tm_mday) || !check_date(end.tm_year, end.tm_mon, end.tm_mday) || @@ -393,32 +394,34 @@ struct recur_apoint *recur_apoint_scan(FILE * f, struct tm start, /* Filter item. */ if (filter) { - if (!(filter->type_mask & TYPE_MASK_RECUR_APPT)) - return NULL; - if (filter->regex && regexec(filter->regex, buf, 0, 0, 0)) - return NULL; - if (filter->start_from != -1 && tstart < filter->start_from) - return NULL; - if (filter->start_to != -1 && tstart > filter->start_to) - return NULL; - if (filter->end_from != -1 && tend < filter->end_from) - return NULL; - if (filter->end_to != -1 && tend > filter->end_to) - return NULL; - } - - rapt = recur_apoint_new(buf, note, tstart, tend - tstart, state, - recur_char2def(type), freq, tuntil, exc); + cond = ( + !(filter->type_mask & TYPE_MASK_RECUR_APPT) || + (filter->regex && regexec(filter->regex, buf, 0, 0, 0)) || + (filter->start_from != -1 && tstart < filter->start_from) || + (filter->start_to != -1 && tstart > filter->start_to) || + (filter->end_from != -1 && tend < filter->end_from) || + (filter->end_to != -1 && tend > filter->end_to) + ); + if (filter->hash) { + rapt = recur_apoint_new(buf, note, tstart, + tend - tstart, state, + recur_char2def(type), + freq, tuntil, exc); + char *hash = recur_apoint_hash(rapt); + cond = cond || !hash_matches(filter->hash, hash); + mem_free(hash); + } - /* Filter by hash. */ - if (filter && filter->hash) { - char *hash = recur_apoint_hash(rapt); - if (!hash_matches(filter->hash, hash)) { - recur_apoint_erase(rapt); - rapt = NULL; + if ((!filter->invert && cond) || (filter->invert && !cond)) { + if (filter->hash) + recur_apoint_erase(rapt); + return NULL; } - mem_free(hash); } + if (!rapt) + rapt = recur_apoint_new(buf, note, tstart, tend - tstart, + state, recur_char2def(type), freq, + tuntil, exc); return rapt; } @@ -431,7 +434,8 @@ struct recur_event *recur_event_scan(FILE * f, struct tm start, int id, { char buf[BUFSIZ], *nl; time_t tstart, tend, tuntil; - struct recur_event *rev; + struct recur_event *rev = NULL; + int cond; EXIT_IF(!check_date(start.tm_year, start.tm_mon, start.tm_mday) || !check_time(start.tm_hour, start.tm_min) || @@ -466,32 +470,33 @@ struct recur_event *recur_event_scan(FILE * f, struct tm start, int id, /* Filter item. */ if (filter) { - if (!(filter->type_mask & TYPE_MASK_RECUR_EVNT)) - return NULL; - if (filter->regex && regexec(filter->regex, buf, 0, 0, 0)) - return NULL; - if (filter->start_from != -1 && tstart < filter->start_from) - return NULL; - if (filter->start_to != -1 && tstart > filter->start_to) - return NULL; - if (filter->end_from != -1 && tend < filter->end_from) - return NULL; - if (filter->end_to != -1 && tend > filter->end_to) - return NULL; - } - - rev = recur_event_new(buf, note, tstart, id, recur_char2def(type), - freq, tuntil, exc); + cond = ( + !(filter->type_mask & TYPE_MASK_RECUR_EVNT) || + (filter->regex && regexec(filter->regex, buf, 0, 0, 0)) || + (filter->start_from != -1 && tstart < filter->start_from) || + (filter->start_to != -1 && tstart > filter->start_to) || + (filter->end_from != -1 && tend < filter->end_from) || + (filter->end_to != -1 && tend > filter->end_to) + ); + if (filter->hash) { + rev = recur_event_new(buf, note, tstart, id, + recur_char2def(type), + freq, tuntil, exc); + char *hash = recur_event_hash(rev); + cond = cond || !hash_matches(filter->hash, hash); + mem_free(hash); + } - /* Filter by hash. */ - if (filter && filter->hash) { - char *hash = recur_event_hash(rev); - if (!hash_matches(filter->hash, hash)) { - recur_event_erase(rev); - rev = NULL; + if ((!filter->invert && cond) || (filter->invert && !cond)) { + if (filter->hash) + recur_event_erase(rev); + return NULL; } - mem_free(hash); } + if (!rev) + rev = recur_event_new(buf, note, tstart, id, + recur_char2def(type), + freq, tuntil, exc); return rev; } -- cgit v1.2.3-54-g00ecf