home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume36 / remind / patch04c / sort.c < prev   
Encoding:
C/C++ Source or Header  |  1993-03-07  |  6.1 KB  |  214 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. } Sortrem;
  37.  
  38. /* The sorted reminder queue */
  39. static Sortrem *SortedQueue = (Sortrem *) NULL;
  40.  
  41. PRIVATE Sortrem *MakeSortRem ARGS ((int jul, int tim, char *body, int typ));
  42. PRIVATE void IssueSortBanner ARGS ((int jul));
  43.  
  44. /***************************************************************/
  45. /*                                                             */
  46. /*  MakeSortRem                                                */
  47. /*                                                             */
  48. /*  Create a new Sortrem entry - return NULL on failure.       */
  49. /*                                                             */
  50. /***************************************************************/
  51. #ifdef HAVE_PROTOS
  52. PRIVATE Sortrem *MakeSortRem(int jul, int tim, char *body, int typ)
  53. #else
  54. static Sortrem *MakeSortRem(jul, tim, body, typ)
  55. int jul, tim;
  56. char *body;
  57. int typ;
  58. #endif
  59. {
  60.    Sortrem *new = NEW(Sortrem);
  61.    if (!new) return NULL;
  62.  
  63.    new->text = StrDup(body);
  64.    if (!new->text) {
  65.       free(new);
  66.       return NULL;
  67.    }
  68.   
  69.    new->trigdate = jul;
  70.    new->trigtime = tim;
  71.    new->typ = typ;
  72.    new->next = NULL;
  73.    return new;
  74. }
  75.  
  76. /***************************************************************/
  77. /*                                                             */
  78. /*  InsertIntoSortBuffer                                       */
  79. /*                                                             */
  80. /*  Insert a reminder into the sort buffer                     */
  81. /*                                                             */
  82. /***************************************************************/
  83. #ifdef HAVE_PROTOS
  84. PUBLIC int InsertIntoSortBuffer(int jul, int tim, char *body, int typ)
  85. #else
  86. int InsertIntoSortBuffer(jul, tim, body, typ)
  87. int jul;
  88. int tim;
  89. char *body;
  90. int typ;
  91. #endif
  92. {
  93.    Sortrem *new = MakeSortRem(jul, tim, body, typ);
  94.    Sortrem *cur = SortedQueue, *prev = NULL;
  95.    int ShouldGoAfter;
  96.  
  97.    if (!new) {
  98.       Eprint("Out of memory for sorting.");
  99.       IssueSortedReminders();
  100.       SortByDate = 0;
  101.       SortByTime = 0;
  102.       return E_NO_MEM;
  103.    }
  104.  
  105.    /* Find the correct place in the sorted list */
  106.    if (!SortedQueue) {
  107.       SortedQueue = new;
  108.       return OK;
  109.    }
  110.    while (cur) {
  111.       if (cur->trigdate == new->trigdate) {
  112.          ShouldGoAfter =
  113.         (SortByTime == SORT_ASCEND) ?
  114.            (new->trigtime >= cur->trigtime) :
  115.            ((new->trigtime == NO_TIME) || (new->trigtime <= cur->trigtime));
  116.       } else {
  117.          ShouldGoAfter =
  118.         (SortByDate == SORT_ASCEND) ?
  119.            (new->trigdate >= cur->trigdate) :
  120.            (new->trigdate <= cur->trigdate);
  121.       }
  122.  
  123.       if (ShouldGoAfter) {
  124.          prev = cur;
  125.      cur = cur->next;
  126.       } else {
  127.          if (prev) {
  128.         prev->next = new;
  129.         new->next = cur;
  130.      } else {
  131.         SortedQueue = new;
  132.         new->next = cur;
  133.      }
  134.      return OK;
  135.       }
  136.       
  137.    }
  138.    prev->next = new;
  139.    new->next = cur;  /* For safety - actually redundant */
  140.    return OK;
  141. }
  142.  
  143.    
  144. /***************************************************************/
  145. /*                                                             */
  146. /*  IssueSortedReminders                                       */
  147. /*                                                             */
  148. /*  Issue all of the sorted reminders and free memory.         */
  149. /*                                                             */
  150. /***************************************************************/
  151. #ifdef HAVE_PROTOS
  152. PUBLIC void IssueSortedReminders(void)
  153. #else
  154. void IssueSortedReminders()
  155. #endif
  156. {
  157.    Sortrem *cur = SortedQueue;
  158.    Sortrem *next;
  159.    int olddate = NO_DATE;
  160.  
  161.    while (cur) {
  162.       next = cur->next;
  163.       if (cur->typ == MSG_TYPE) {
  164.     if (!MsgCommand) {
  165.             if (cur->trigdate != olddate) {
  166.                IssueSortBanner(cur->trigdate);
  167.            olddate = cur->trigdate;
  168.             }
  169.             printf("%s\n", cur->text);
  170.          } else {
  171.             char buf[LINELEN+TOKSIZE];
  172.             sprintf(buf, MsgCommand, cur->text);
  173.         system(buf);
  174.          }
  175.       } else { /* Must be RUN_TYPE */
  176.          system(cur->text);
  177.       }
  178.       free(cur->text);
  179.       free(cur);
  180.       cur = next;
  181.    }
  182. }
  183. /***************************************************************/
  184. /*                                                             */
  185. /*  IssueSortBanner                                            */
  186. /*                                                             */
  187. /*  Issue a daily banner if the function sortbanner() is       */
  188. /*  defined to take one argument.                              */
  189. /*                                                             */
  190. /***************************************************************/
  191. #ifdef HAVE_PROTOS
  192. PRIVATE void IssueSortBanner(int jul)
  193. #else
  194. static void IssueSortBanner(jul)
  195. int jul;
  196. #endif
  197. {
  198.    char BanExpr[25];
  199.    int y, m, d;
  200.    Value v;
  201.    char *s = BanExpr;
  202.  
  203.    if (UserFuncExists("sortbanner") != 1) return;
  204.  
  205.    FromJulian(jul, &y, &m, &d);
  206.    sprintf(BanExpr, "sortbanner('%04d/%02d/%02d')", y, m+1, d);   
  207.    y = EvalExpr(&s, &v);
  208.    if (y) return;
  209.    if (DoCoerce(STR_TYPE, &v)) return;
  210.    if (!DoSubstFromString(v.v.str, SubstBuffer, jul, NO_TIME))
  211.       if (*SubstBuffer) printf("%s\n", SubstBuffer);
  212.    DestroyValue(&v);
  213. }
  214.