1
/***************************************************************/
5
/* Routines for sorting reminders by trigger date */
7
/* This file is part of REMIND. */
8
/* Copyright (C) 1992-1998 by David F. Skoll */
9
/* Copyright (C) 1999-2000 by Roaring Penguin Software Inc. */
11
/***************************************************************/
14
static char const RCSID[] = "$Id: sort.c,v 1.6 2000/02/18 03:46:09 dfs Exp $";
33
/* The structure of a sorted entry */
34
typedef struct sortrem {
43
/* The sorted reminder queue */
44
static Sortrem *SortedQueue = (Sortrem *) NULL;
46
PRIVATE Sortrem *MakeSortRem ARGS ((int jul, int tim, char *body, int typ, int prio));
47
PRIVATE void IssueSortBanner ARGS ((int jul));
49
/***************************************************************/
53
/* Create a new Sortrem entry - return NULL on failure. */
55
/***************************************************************/
57
PRIVATE Sortrem *MakeSortRem(int jul, int tim, char *body, int typ, int prio)
59
static Sortrem *MakeSortRem(jul, tim, body, typ, prio)
65
Sortrem *new = NEW(Sortrem);
66
if (!new) return NULL;
68
new->text = StrDup(body);
82
/***************************************************************/
84
/* InsertIntoSortBuffer */
86
/* Insert a reminder into the sort buffer */
88
/***************************************************************/
90
PUBLIC int InsertIntoSortBuffer(int jul, int tim, char *body, int typ, int prio)
92
int InsertIntoSortBuffer(jul, tim, body, typ, prio)
99
Sortrem *new = MakeSortRem(jul, tim, body, typ, prio);
100
Sortrem *cur = SortedQueue, *prev = NULL;
104
Eprint("%s", ErrMsg[E_NO_MEM]);
105
IssueSortedReminders();
112
/* Find the correct place in the sorted list */
118
ShouldGoAfter = CompareRems(new->trigdate, new->trigtime, new->priority,
119
cur->trigdate, cur->trigtime, cur->priority,
120
SortByDate, SortByTime, SortByPrio);
122
if (ShouldGoAfter <= 0) {
138
new->next = cur; /* For safety - actually redundant */
143
/***************************************************************/
145
/* IssueSortedReminders */
147
/* Issue all of the sorted reminders and free memory. */
149
/***************************************************************/
151
PUBLIC void IssueSortedReminders(void)
153
void IssueSortedReminders()
156
Sortrem *cur = SortedQueue;
158
int olddate = NO_DATE;
165
DoMsgCommand(MsgCommand, cur->text);
167
if (cur->trigdate != olddate) {
168
IssueSortBanner(cur->trigdate);
169
olddate = cur->trigdate;
171
printf("%s", cur->text);
177
FillParagraph(cur->text, 0);
179
FillParagraph(cur->text);
194
/***************************************************************/
196
/* IssueSortBanner */
198
/* Issue a daily banner if the function sortbanner() is */
199
/* defined to take one argument. */
201
/***************************************************************/
203
PRIVATE void IssueSortBanner(int jul)
205
static void IssueSortBanner(jul)
215
if (UserFuncExists("sortbanner") != 1) return;
217
FromJulian(jul, &y, &m, &d);
218
sprintf(BanExpr, "sortbanner('%04d/%02d/%02d')", y, m+1, d);
219
y = EvalExpr(&s, &v);
221
if (DoCoerce(STR_TYPE, &v)) return;
223
if (!DoSubstFromString(v.v.str, &buf, jul, NO_TIME)) {
224
if (*DBufValue(&buf)) printf("%s\n", DBufValue(&buf));
230
/***************************************************************/
234
/* Compare two reminders for sorting. Return 0 if they */
235
/* compare equal; 1 if rem2 should come after rem1, -1 if */
236
/* rem1 should come after rem2. bydate and bytime control */
237
/* sorting direction by date and time, resp. */
239
/***************************************************************/
241
PUBLIC int CompareRems(int dat1, int tim1, int prio1,
242
int dat2, int tim2, int prio2,
243
int bydate, int bytime, int byprio)
245
int CompareRems(dat1, tim1, prio1, dat2, tim2, prio2, bydate, bytime, byprio)
246
int dat1, tim1, prio1, dat2, tim2, prio2, bydate, bytime, byprio;
249
int dafter, tafter, pafter;
251
dafter = (bydate != SORT_DESCEND) ? 1 : -1;
252
tafter = (bytime != SORT_DESCEND) ? 1 : -1;
253
pafter = (byprio != SORT_DESCEND) ? 1 : -1;
255
if (dat1 < dat2) return dafter;
256
if (dat1 > dat2) return -dafter;
258
if (tim1 == NO_TIME && tim2 != NO_TIME) return -1;
259
if (tim1 != NO_TIME && tim2 == NO_TIME) return 1;
260
if (tim1 < tim2) return tafter;
261
if (tim1 > tim2) return -tafter;
263
if (prio1 < prio2) return pafter;
264
if (prio1 > prio2) return -pafter;