From f3858d899c9b29e38521e4eab46dcd97dc3fb57f Mon Sep 17 00:00:00 2001
From: Lukas Fleischer <calcurse@cryptocrack.de>
Date: Mon, 25 Jun 2012 13:50:55 +0200
Subject: src/args.c: Revise app_arg()

This kills the hackish code we used to filter and display appointments
and events in non-interactive mode. From now on, the same algorithm that
is used in interactive mode (read day_store_items() for details) is
called, resulting in code that is much easier to maintain.

The resulting performance loss is rather small. Here are the run times
of `calcurse -s01/01/1902 -r18250 -D ../test/data >/dev/null` before and
after the patch was applied:

    0.30user 0.26system 0:01.22elapsed 46%CPU (0avgtext+0avgdata 1340maxresident)k
    0inputs+0outputs (0major+398minor)pagefaults 0swaps

    0.33user 0.21system 0:01.18elapsed 46%CPU (0avgtext+0avgdata 1360maxresident)k
    0inputs+0outputs (0major+395minor)pagefaults 0swaps

Note that this also fixes a few bugs that were encountered in
non-interactive mode, most notably BUG#2.

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
---
 src/args.c | 118 +++++--------------------------------------------------------
 1 file changed, 8 insertions(+), 110 deletions(-)

(limited to 'src')

diff --git a/src/args.c b/src/args.c
index 4284886..e375fd7 100644
--- a/src/args.c
+++ b/src/args.c
@@ -288,123 +288,21 @@ static void arg_print_date(long date)
 static int
 app_arg(int add_line, struct date *day, long date, const char *fmt_apt,
         const char *fmt_rapt, const char *fmt_ev, const char *fmt_rev,
-        regex_t * regex)
+        regex_t *regex)
 {
-  llist_item_t *i, *j;
-  long today;
-  unsigned print_date = 1;
-  int app_found = 0;
-
   if (date == 0)
-    today = get_sec_date(*day);
-  else
-    today = date;
-
-  /*
-   * Calculate and print the selected date if there is an event for
-   * that date and it is the first one, and then print all the events for
-   * that date.
-   */
-  LLIST_FIND_FOREACH(&recur_elist, today, recur_event_inday, i) {
-    struct recur_event *re = LLIST_GET_DATA(i);
-    if (regex && regexec(regex, re->mesg, 0, 0, 0) != 0)
-      continue;
-
-    app_found = 1;
-    if (add_line) {
-      fputs("\n", stdout);
-      add_line = 0;
-    }
-    if (print_date) {
-      arg_print_date(today);
-      print_date = 0;
-    }
-    print_recur_event(fmt_rev, today, re);
-  }
+    date = get_sec_date(*day);
 
-  LLIST_FIND_FOREACH_CONT(&eventlist, today, event_inday, i) {
-    struct event *ev = LLIST_TS_GET_DATA(i);
-    if (regex && regexec(regex, ev->mesg, 0, 0, 0) != 0)
-      continue;
+  int n = day_store_items(date, NULL, NULL, regex);
 
-    app_found = 1;
-    if (add_line) {
+  if (n > 1) {
+    if (add_line)
       fputs("\n", stdout);
-      add_line = 0;
-    }
-    if (print_date) {
-      arg_print_date(today);
-      print_date = 0;
-    }
-    print_event(fmt_ev, today, ev);
+    arg_print_date(date);
+    day_write_stdout(date, fmt_apt, fmt_rapt, fmt_ev, fmt_rev);
   }
 
-  /* Same process is performed but this time on the appointments. */
-  LLIST_TS_LOCK(&alist_p);
-  LLIST_TS_LOCK(&recur_alist_p);
-
-  /*
-   * Iterate over regular appointments and recurrent ones simultaneously (fixes
-   * http://lists.calcurse.org/bugs/msg00002.html).
-   */
-  i = LLIST_TS_FIND_FIRST(&alist_p, today, apoint_inday);
-  j = LLIST_TS_FIND_FIRST(&recur_alist_p, today, recur_apoint_inday);
-  while (i || j) {
-    struct apoint *apt = LLIST_TS_GET_DATA(i);
-    struct recur_apoint *ra = LLIST_TS_GET_DATA(j);
-    unsigned occurrence;
-
-    while (i && regex && regexec(regex, apt->mesg, 0, 0, 0) != 0) {
-      i = LLIST_TS_FIND_NEXT(i, today, apoint_inday);
-      apt = LLIST_TS_GET_DATA(i);
-    }
-
-    while (j && regex && regexec(regex, ra->mesg, 0, 0, 0) != 0) {
-      j = LLIST_TS_FIND_NEXT(j, today, recur_apoint_inday);
-      ra = LLIST_TS_GET_DATA(j);
-    }
-
-    if (apt && ra) {
-      if (recur_apoint_find_occurrence(ra, today, &occurrence) &&
-          apt->start <= occurrence)
-        ra = NULL;
-      else
-        apt = NULL;
-    }
-
-    if (apt) {
-      app_found = 1;
-      if (add_line) {
-        fputs("\n", stdout);
-        add_line = 0;
-      }
-      if (print_date) {
-        arg_print_date(today);
-        print_date = 0;
-      }
-      print_apoint(fmt_apt, today, apt);
-      i = LLIST_TS_FIND_NEXT(i, today, apoint_inday);
-    } else if (ra) {
-      app_found = 1;
-      if (add_line) {
-        fputs("\n", stdout);
-        add_line = 0;
-      }
-      if (print_date) {
-        arg_print_date(today);
-        print_date = 0;
-      }
-      recur_apoint_find_occurrence(ra, today, &occurrence);
-      print_recur_apoint(fmt_rapt, today, occurrence, ra);
-      apt = NULL;
-      j = LLIST_TS_FIND_NEXT(j, today, recur_apoint_inday);
-    }
-  }
-
-  LLIST_TS_UNLOCK(&recur_alist_p);
-  LLIST_TS_UNLOCK(&alist_p);
-
-  return app_found;
+  return n - 1;
 }
 
 /*
-- 
cgit v1.2.3-70-g09d2