diff options
author | Lukas Fleischer <calcurse@cryptocrack.de> | 2011-04-14 11:28:22 +0200 |
---|---|---|
committer | Lukas Fleischer <calcurse@cryptocrack.de> | 2011-04-19 11:42:34 +0200 |
commit | 6f883c0f3f4c08fe8e125f269da9b940519ccf44 (patch) | |
tree | b66a7de57c6e56004f4f96867884270bc635d27f /src | |
parent | 1de34587abc135edf9af8f9d8d6094ac10f0143c (diff) | |
download | calcurse-6f883c0f3f4c08fe8e125f269da9b940519ccf44.tar.gz calcurse-6f883c0f3f4c08fe8e125f269da9b940519ccf44.zip |
Use generic lists for events.
Use the new generic list implementation instead of those insane "next"
pointers in events. Includes some cleanups.
Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
Diffstat (limited to 'src')
-rw-r--r-- | src/args.c | 39 | ||||
-rw-r--r-- | src/calcurse.c | 3 | ||||
-rw-r--r-- | src/calcurse.h | 5 | ||||
-rw-r--r-- | src/day.c | 18 | ||||
-rw-r--r-- | src/dmon.c | 1 | ||||
-rw-r--r-- | src/event.c | 134 | ||||
-rw-r--r-- | src/io.c | 26 |
7 files changed, 105 insertions, 121 deletions
@@ -352,7 +352,6 @@ app_arg (int add_line, struct date *day, long date, int print_note, { llist_item_t *i; struct recur_event *re; - struct event *j; struct recur_apoint *ra; long today; unsigned print_date = 1; @@ -397,30 +396,28 @@ app_arg (int add_line, struct date *day, long date, int print_note, } } - for (j = eventlist; j != NULL; j = j->next) + LLIST_FIND_FOREACH (&eventlist, today, event_inday, i) { - if (event_inday (j, today)) - { - if (regex && regexec (regex, j->mesg, 0, 0, 0) != 0) - continue; + struct apoint *ev = LLIST_TS_GET_DATA (i); + if (regex && regexec (regex, ev->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, conf); - print_date = 0; - } - fputs (" * ", stdout); - fputs (j->mesg, stdout); + app_found = 1; + if (add_line) + { fputs ("\n", stdout); - if (print_note && j->note) - print_notefile (stdout, j->note, 2); + add_line = 0; } + if (print_date) + { + arg_print_date (today, conf); + print_date = 0; + } + fputs (" * ", stdout); + fputs (ev->mesg, stdout); + fputs ("\n", stdout); + if (print_note && ev->note) + print_notefile (stdout, ev->note, 2); } /* Same process is performed but this time on the appointments. */ diff --git a/src/calcurse.c b/src/calcurse.c index 6a90f57..95c9de8 100644 --- a/src/calcurse.c +++ b/src/calcurse.c @@ -75,6 +75,9 @@ main (int argc, char **argv) apoint_llist_init (); recur_apoint_llist_init (); + /* Initialize non-thread-safe data structures. */ + event_llist_init (); + /* * Begin by parsing and handling command line arguments. * The data path is also initialized here. diff --git a/src/calcurse.h b/src/calcurse.h index fec38b7..a4f33fa 100644 --- a/src/calcurse.h +++ b/src/calcurse.h @@ -270,7 +270,6 @@ struct apoint /* Event definition. */ struct event { - struct event *next; int id; /* event identifier */ long day; /* seconds since 1 jan 1970 */ char *mesg; @@ -558,7 +557,6 @@ enum save_display { /* apoint.c */ extern llist_ts_t alist_p; void apoint_free_bkp (enum eraseflg); -void apoint_free (struct apoint *); void apoint_llist_init (void); void apoint_llist_free (void); void apoint_hilt_set (int); @@ -647,8 +645,9 @@ void dmon_start (int); void dmon_stop (void); /* event.c */ -extern struct event *eventlist; +extern llist_t eventlist; void event_free_bkp (enum eraseflg); +void event_llist_init (void); void event_llist_free (void); struct event *event_new (char *, char *, long, int); unsigned event_inday (struct event *, long); @@ -150,16 +150,14 @@ day_add_apoint (int type, char *mesg, char *note, long start, long dur, static int day_store_events (long date) { - struct event *j; + llist_item_t *i; int e_nb = 0; - for (j = eventlist; j != NULL; j = j->next) + LLIST_FIND_FOREACH (&eventlist, date, event_inday, i) { - if (event_inday (j, date)) - { - e_nb++; - (void)day_add_event (EVNT, j->mesg, j->note, j->day, j->id); - } + struct event *ev = LLIST_TS_GET_DATA (i); + (void)day_add_event (EVNT, ev->mesg, ev->note, ev->day, ev->id); + e_nb++; } return e_nb; @@ -475,7 +473,6 @@ day_check_if_item (struct date day) { struct recur_event *re; struct recur_apoint *ra; - struct event *e; const long date = date2sec (day, 0, 0); for (re = recur_elist; re != NULL; re = re->next) @@ -493,9 +490,8 @@ day_check_if_item (struct date day) } pthread_mutex_unlock (&(recur_alist_p->mutex)); - for (e = eventlist; e != NULL; e = e->next) - if (event_inday (e, date)) - return (1); + if (LLIST_FIND_FIRST (&eventlist, date, event_inday)) + return (1); LLIST_TS_LOCK (&alist_p); if (LLIST_TS_FIND_FIRST (&alist_p, date, apoint_inday)) @@ -172,6 +172,7 @@ dmon_start (int parent_exit_status) path_apts, strerror (errno)); apoint_llist_init (); recur_apoint_llist_init (); + event_llist_init (); io_load_app (); data_loaded = 1; diff --git a/src/event.c b/src/event.c index a3c4750..9d00fc2 100644 --- a/src/event.c +++ b/src/event.c @@ -41,7 +41,7 @@ #include "calcurse.h" -struct event *eventlist; +llist_t eventlist; static struct event bkp_cut_event; void @@ -56,6 +56,14 @@ event_free_bkp (enum eraseflg flag) } static void +event_free (struct event *ev) +{ + mem_free (ev->mesg); + erase_note (&ev->note, ERASE_FORCE_KEEP_NOTE); + mem_free (ev); +} + +static void event_dup (struct event *in, struct event *bkp) { EXIT_IF (!in || !bkp, _("null pointer")); @@ -68,43 +76,39 @@ event_dup (struct event *in, struct event *bkp) } void +event_llist_init (void) +{ + LLIST_INIT (&eventlist); +} + +void event_llist_free (void) { - struct event *o, **i; + LLIST_FREE_INNER (&eventlist, event_free); + LLIST_FREE (&eventlist); +} - i = &eventlist; - while (*i) - { - o = *i; - *i = o->next; - mem_free (o->mesg); - erase_note (&o->note, ERASE_FORCE_KEEP_NOTE); - mem_free (o); - } +static int +event_cmp_day (struct event *a, struct event *b) +{ + return (a->day < b->day ? -1 : (a->day == b->day ? 0 : 1)); } /* Create a new event */ struct event * event_new (char *mesg, char *note, long day, int id) { - struct event *o, **i; - o = mem_malloc (sizeof (struct event)); - o->mesg = mem_strdup (mesg); - o->day = day; - o->id = id; - o->note = (note != NULL) ? mem_strdup (note) : NULL; - i = &eventlist; - for (;;) - { - if (*i == NULL || (*i)->day > day) - { - o->next = *i; - *i = o; - break; - } - i = &(*i)->next; - } - return (o); + struct event *ev; + + ev = mem_malloc (sizeof (struct event)); + ev->mesg = mem_strdup (mesg); + ev->day = day; + ev->id = id; + ev->note = (note != NULL) ? mem_strdup (note) : NULL; + + LLIST_ADD_SORTED (&eventlist, ev, event_cmp_day); + + return ev; } /* Check if the event belongs to the selected day */ @@ -168,21 +172,12 @@ event_scan (FILE *f, struct tm start, int id, char *note) struct event * event_get (long day, int pos) { - struct event *o; - int n; + llist_item_t *i = LLIST_FIND_NTH (&eventlist, pos, day, event_inday); + + if (i) + return LLIST_TS_GET_DATA (i); - n = 0; - for (o = eventlist; o; o = o->next) - { - if (event_inday (o, day)) - { - if (n == pos) - return o; - n++; - } - } EXIT (_("event not found")); - return 0; /* NOTREACHED */ } @@ -190,43 +185,30 @@ event_get (long day, int pos) void event_delete_bynum (long start, unsigned num, enum eraseflg flag) { - unsigned n; - struct event *i, **iptr; + llist_item_t *i = LLIST_FIND_NTH (&eventlist, num, start, event_inday); + + if (!i) + EXIT (_("no such appointment")); + struct event *ev = LLIST_TS_GET_DATA (i); - n = 0; - iptr = &eventlist; - for (i = eventlist; i != NULL; i = i->next) + switch (flag) { - if (event_inday (i, start)) - { - if (n == num) - { - switch (flag) - { - case ERASE_FORCE_ONLY_NOTE: - erase_note (&i->note, flag); - break; - case ERASE_CUT: - event_free_bkp (ERASE_FORCE); - event_dup (i, &bkp_cut_event); - erase_note (&i->note, ERASE_FORCE_KEEP_NOTE); - /* FALLTHROUGH */ - default: - *iptr = i->next; - mem_free (i->mesg); - if (flag != ERASE_FORCE_KEEP_NOTE && flag != ERASE_CUT) - erase_note (&i->note, flag); - mem_free (i); - break; - } - return; - } - n++; - } - iptr = &i->next; + case ERASE_FORCE_ONLY_NOTE: + erase_note (&ev->note, flag); + break; + case ERASE_CUT: + event_free_bkp (ERASE_FORCE); + event_dup (ev, &bkp_cut_event); + erase_note (&ev->note, ERASE_FORCE_KEEP_NOTE); + /* FALLTHROUGH */ + default: + LLIST_REMOVE (&eventlist, i); + mem_free (ev->mesg); + if (flag != ERASE_FORCE_KEEP_NOTE && flag != ERASE_CUT) + erase_note (&ev->note, flag); + mem_free (ev); + break; } - EXIT (_("event not found")); - /* NOTREACHED */ } void @@ -461,15 +461,16 @@ pcal_export_recur_events (FILE *stream) static void ical_export_events (FILE *stream) { - struct event *i; + llist_item_t *i; char ical_date[BUFSIZ]; - for (i = eventlist; i != NULL; i = i->next) + LLIST_FOREACH (&eventlist, i) { - date_sec2date_fmt (i->day, ICALDATEFMT, ical_date); + struct event *ev = LLIST_TS_GET_DATA (i); + date_sec2date_fmt (ev->day, ICALDATEFMT, ical_date); (void)fprintf (stream, "BEGIN:VEVENT\n"); (void)fprintf (stream, "DTSTART:%s\n", ical_date); - (void)fprintf (stream, "SUMMARY:%s\n", i->mesg); + (void)fprintf (stream, "SUMMARY:%s\n", ev->mesg); (void)fprintf (stream, "END:VEVENT\n"); } } @@ -477,11 +478,14 @@ ical_export_events (FILE *stream) static void pcal_export_events (FILE *stream) { - struct event *i; + llist_item_t *i; (void)fprintf (stream, "\n# ======\n# Events\n# ======\n"); - for (i = eventlist; i != NULL; i = i->next) - pcal_dump_event (stream, i->day, 0, i->mesg); + LLIST_FOREACH (&eventlist, i) + { + struct event *ev = LLIST_TS_GET_DATA (i); + pcal_dump_event (stream, ev->day, 0, ev->mesg); + } (void)fprintf (stream, "\n"); } @@ -964,7 +968,6 @@ unsigned io_save_apts (void) { llist_item_t *i; - struct event *e; FILE *fp; if ((fp = fopen (path_apts, "w")) == NULL) @@ -982,8 +985,11 @@ io_save_apts (void) if (ui_mode == UI_CURSES) LLIST_TS_UNLOCK (&alist_p); - for (e = eventlist; e != NULL; e = e->next) - event_write (e, fp); + LLIST_FOREACH (&eventlist, i) + { + struct event *ev = LLIST_TS_GET_DATA (i); + event_write (ev, fp); + } file_close (fp, __FILE_POS__); return 1; |