home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / remind / src / sort.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-31  |  7.6 KB  |  258 lines

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