~ubuntu-branches/ubuntu/karmic/remind/karmic

« back to all changes in this revision

Viewing changes to src/sort.c

  • Committer: Bazaar Package Importer
  • Author(s): Javier Fernandez-Sanguino Pen~a
  • Date: 1999-02-19 13:36:15 UTC
  • Revision ID: james.westby@ubuntu.com-19990219133615-ovob95sord67b0ks
Tags: 03.00.22-1
* NMU upload, maintainer seems to be missing.
* Changed to main (now GPL) (Closes: #42402)
* New upstream version (Closes: #59447)
* Moved to use debconf.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************/
 
2
/*                                                             */
 
3
/*  SORT.C                                                     */
 
4
/*                                                             */
 
5
/*  Routines for sorting reminders by trigger date             */
 
6
/*                                                             */
 
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.   */
 
10
/*                                                             */
 
11
/***************************************************************/
 
12
 
 
13
#include "config.h"
 
14
static char const RCSID[] = "$Id: sort.c,v 1.6 2000/02/18 03:46:09 dfs Exp $";
 
15
 
 
16
#include <stdio.h>
 
17
#include <string.h>
 
18
 
 
19
#ifdef HAVE_STDLIB_H
 
20
#include <stdlib.h>
 
21
#endif
 
22
 
 
23
#ifdef HAVE_MALLOC_H
 
24
#include <malloc.h>
 
25
#endif
 
26
 
 
27
#include "types.h"
 
28
#include "protos.h"
 
29
#include "expr.h"
 
30
#include "globals.h"
 
31
#include "err.h"
 
32
 
 
33
/* The structure of a sorted entry */
 
34
typedef struct sortrem {
 
35
    struct sortrem *next;
 
36
    char *text;
 
37
    int trigdate;
 
38
    int trigtime;
 
39
    int typ;
 
40
    int priority;
 
41
} Sortrem;
 
42
 
 
43
/* The sorted reminder queue */
 
44
static Sortrem *SortedQueue = (Sortrem *) NULL;
 
45
 
 
46
PRIVATE Sortrem *MakeSortRem ARGS ((int jul, int tim, char *body, int typ, int prio));
 
47
PRIVATE void IssueSortBanner ARGS ((int jul));
 
48
 
 
49
/***************************************************************/
 
50
/*                                                             */
 
51
/*  MakeSortRem                                                */
 
52
/*                                                             */
 
53
/*  Create a new Sortrem entry - return NULL on failure.       */
 
54
/*                                                             */
 
55
/***************************************************************/
 
56
#ifdef HAVE_PROTOS
 
57
PRIVATE Sortrem *MakeSortRem(int jul, int tim, char *body, int typ, int prio)
 
58
#else
 
59
static Sortrem *MakeSortRem(jul, tim, body, typ, prio)
 
60
int jul, tim;
 
61
char *body;
 
62
int typ, prio;
 
63
#endif
 
64
{
 
65
    Sortrem *new = NEW(Sortrem);
 
66
    if (!new) return NULL;
 
67
 
 
68
    new->text = StrDup(body);
 
69
    if (!new->text) {
 
70
        free(new);
 
71
        return NULL;
 
72
    }
 
73
  
 
74
    new->trigdate = jul;
 
75
    new->trigtime = tim;
 
76
    new->typ = typ;
 
77
    new->priority = prio;
 
78
    new->next = NULL;
 
79
    return new;
 
80
}
 
81
 
 
82
/***************************************************************/
 
83
/*                                                             */
 
84
/*  InsertIntoSortBuffer                                       */
 
85
/*                                                             */
 
86
/*  Insert a reminder into the sort buffer                     */
 
87
/*                                                             */
 
88
/***************************************************************/
 
89
#ifdef HAVE_PROTOS
 
90
PUBLIC int InsertIntoSortBuffer(int jul, int tim, char *body, int typ, int prio)
 
91
#else
 
92
int InsertIntoSortBuffer(jul, tim, body, typ, prio)
 
93
int jul;
 
94
int tim;
 
95
char *body;
 
96
int typ, prio;
 
97
#endif
 
98
{
 
99
    Sortrem *new = MakeSortRem(jul, tim, body, typ, prio);
 
100
    Sortrem *cur = SortedQueue, *prev = NULL;
 
101
    int ShouldGoAfter;
 
102
 
 
103
    if (!new) {
 
104
        Eprint("%s", ErrMsg[E_NO_MEM]);
 
105
        IssueSortedReminders();
 
106
        SortByDate = 0;
 
107
        SortByTime = 0;
 
108
        SortByPrio = 0;
 
109
        return E_NO_MEM;
 
110
    }
 
111
 
 
112
    /* Find the correct place in the sorted list */
 
113
    if (!SortedQueue) {
 
114
        SortedQueue = new;
 
115
        return OK;
 
116
    }
 
117
    while (cur) {
 
118
        ShouldGoAfter = CompareRems(new->trigdate, new->trigtime, new->priority,
 
119
                                    cur->trigdate, cur->trigtime, cur->priority,
 
120
                                    SortByDate, SortByTime, SortByPrio);
 
121
                      
 
122
        if (ShouldGoAfter <= 0) {
 
123
            prev = cur;
 
124
            cur = cur->next;
 
125
        } else {
 
126
            if (prev) {
 
127
                prev->next = new;
 
128
                new->next = cur;
 
129
            } else {
 
130
                SortedQueue = new;
 
131
                new->next = cur;
 
132
            }
 
133
            return OK;
 
134
        }
 
135
      
 
136
    }
 
137
    prev->next = new;
 
138
    new->next = cur;  /* For safety - actually redundant */
 
139
    return OK;
 
140
}
 
141
 
 
142
   
 
143
/***************************************************************/
 
144
/*                                                             */
 
145
/*  IssueSortedReminders                                       */
 
146
/*                                                             */
 
147
/*  Issue all of the sorted reminders and free memory.         */
 
148
/*                                                             */
 
149
/***************************************************************/
 
150
#ifdef HAVE_PROTOS
 
151
PUBLIC void IssueSortedReminders(void)
 
152
#else
 
153
void IssueSortedReminders()
 
154
#endif
 
155
{
 
156
    Sortrem *cur = SortedQueue;
 
157
    Sortrem *next;
 
158
    int olddate = NO_DATE;
 
159
 
 
160
    while (cur) {
 
161
        next = cur->next;
 
162
        switch(cur->typ) {
 
163
        case MSG_TYPE:
 
164
            if (MsgCommand) {
 
165
                DoMsgCommand(MsgCommand, cur->text);
 
166
            } else {
 
167
                if (cur->trigdate != olddate) {
 
168
                    IssueSortBanner(cur->trigdate);
 
169
                    olddate = cur->trigdate;
 
170
                }
 
171
                printf("%s", cur->text);
 
172
            }
 
173
            break;
 
174
 
 
175
        case MSF_TYPE:
 
176
#ifdef OS2_POPUP
 
177
            FillParagraph(cur->text, 0);
 
178
#else
 
179
            FillParagraph(cur->text);
 
180
#endif
 
181
            break;
 
182
 
 
183
        case RUN_TYPE:
 
184
            system(cur->text);
 
185
            break;
 
186
        }
 
187
 
 
188
        free(cur->text);
 
189
        free(cur);
 
190
        cur = next;
 
191
    }
 
192
    SortedQueue = NULL;
 
193
}
 
194
/***************************************************************/
 
195
/*                                                             */
 
196
/*  IssueSortBanner                                            */
 
197
/*                                                             */
 
198
/*  Issue a daily banner if the function sortbanner() is       */
 
199
/*  defined to take one argument.                              */
 
200
/*                                                             */
 
201
/***************************************************************/
 
202
#ifdef HAVE_PROTOS
 
203
PRIVATE void IssueSortBanner(int jul)
 
204
#else
 
205
static void IssueSortBanner(jul)
 
206
int jul;
 
207
#endif
 
208
{
 
209
    char BanExpr[64];
 
210
    int y, m, d;
 
211
    Value v;
 
212
    char *s = BanExpr;
 
213
    DynamicBuffer buf;
 
214
 
 
215
    if (UserFuncExists("sortbanner") != 1) return;
 
216
 
 
217
    FromJulian(jul, &y, &m, &d);
 
218
    sprintf(BanExpr, "sortbanner('%04d/%02d/%02d')", y, m+1, d);   
 
219
    y = EvalExpr(&s, &v);
 
220
    if (y) return;
 
221
    if (DoCoerce(STR_TYPE, &v)) return;
 
222
    DBufInit(&buf);
 
223
    if (!DoSubstFromString(v.v.str, &buf, jul, NO_TIME)) {
 
224
        if (*DBufValue(&buf)) printf("%s\n", DBufValue(&buf));
 
225
        DBufFree(&buf);
 
226
    }
 
227
    DestroyValue(v);
 
228
}
 
229
 
 
230
/***************************************************************/
 
231
/*                                                             */
 
232
/*  CompareRems                                                */
 
233
/*                                                             */
 
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.                  */
 
238
/*                                                             */
 
239
/***************************************************************/
 
240
#ifdef HAVE_PROTOS
 
241
PUBLIC int CompareRems(int dat1, int tim1, int prio1,
 
242
                       int dat2, int tim2, int prio2,
 
243
                       int bydate, int bytime, int byprio)
 
244
#else
 
245
int CompareRems(dat1, tim1, prio1, dat2, tim2, prio2, bydate, bytime, byprio)
 
246
int dat1, tim1, prio1, dat2, tim2, prio2, bydate, bytime, byprio;
 
247
#endif
 
248
{
 
249
    int dafter, tafter, pafter;
 
250
 
 
251
    dafter = (bydate != SORT_DESCEND) ? 1 : -1;
 
252
    tafter = (bytime != SORT_DESCEND) ? 1 : -1;
 
253
    pafter = (byprio != SORT_DESCEND) ? 1 : -1;
 
254
 
 
255
    if (dat1 < dat2) return dafter;
 
256
    if (dat1 > dat2) return -dafter;
 
257
 
 
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;
 
262
 
 
263
    if (prio1 < prio2) return pafter;
 
264
    if (prio1 > prio2) return -pafter;
 
265
 
 
266
    return 0;
 
267
}