home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / remind23.lzh / remind.3 / cache.c next >
C/C++ Source or Header  |  1991-02-24  |  5KB  |  164 lines

  1. /***************************************************************/
  2. /*                                                             */
  3. /* CACHE.C                                                     */
  4. /*                                                             */
  5. /* Contains routines for caching reminder file to improve      */
  6. /* calendar performance.                                       */
  7. /*                                                             */
  8. /* By David Skoll - 15 November 1990                           */
  9. /***************************************************************/
  10.  
  11. #include <stdio.h>
  12. #ifndef NO_MALLOC_H
  13. #include <malloc.h>
  14. #endif
  15. #include <string.h>
  16. #include "defines.h"
  17. #include "globals.h"
  18. #include "protos.h"
  19. #include "cache.h"
  20.  
  21. /* Define a cached line */
  22. typedef struct cached_line {
  23.    char *text;
  24.    struct cached_line *next;
  25. } Centry;
  26.  
  27. Centry Cache, *Current;
  28.  
  29. static int CacheDone, CacheFailed;
  30.  
  31. /***************************************************************/
  32. /*                                                             */
  33. /*  InitCache                                                  */
  34. /*                                                             */
  35. /*  Initializes the caching system.                            */
  36. /*                                                             */
  37. /***************************************************************/
  38. #ifdef __STDC__
  39. void InitCache(void)
  40. #else
  41. void InitCache()
  42. #endif
  43. {
  44.    CacheDone   = 0;
  45.    CacheFailed = 0;
  46.    Cache.next  = NULL;
  47.    Current     = &Cache;
  48. }
  49.  
  50. /***************************************************************/
  51. /*                                                             */
  52. /* GetLine                                                     */
  53. /*                                                             */
  54. /* This function either reads a line from the file, or gets    */
  55. /* it from memory if it is cached.                             */
  56. /*                                                             */
  57. /* Returns 0 if more data to be read; otherwise, non-zero.     */
  58. /*                                                             */
  59. /*                                                             */
  60. /***************************************************************/
  61. #ifdef __STDC__
  62. int GetLine(void)
  63. #else
  64. int GetLine()
  65. #endif
  66. {
  67.    int ret;
  68.    Token tok;
  69.    char *s;
  70.    Centry *c;
  71.  
  72.    if (CacheFailed) return ReadLine();
  73.  
  74.    if (!CacheDone) {
  75.       ret = ReadLine();
  76.       if (ret) {
  77.          CacheDone = 1;
  78.          strcpy(FileName, "* cache *");
  79.      CurLine = 0;
  80.          return ret;
  81.       }
  82.       /* Check if we should cache this line */
  83.  
  84.       s = Line;
  85.       tok = ParseToken(&s);
  86.       if (tok.type == Clear_t || tok.type == Push_t ||
  87.           tok.type == Pop_t || tok.type == Rem_t || tok.type == Omit_t) { 
  88.          c = (Centry *) malloc(sizeof(Centry));
  89.          if (c == NULL) {
  90.             CacheFailed = 1;
  91.             DestroyCache();
  92.             return 0;
  93.          }
  94.          c->text = (char *) malloc(strlen(Line)+1);
  95.          if (c->text == NULL) {
  96.             CacheFailed = 1;
  97.         DestroyCache();
  98.             free(c);
  99.         return 0;
  100.          }
  101.          /* Insert the cache entry */
  102.          c->next = NULL;
  103.          strcpy(c->text, Line);
  104.          Current->next = c;
  105.          Current = c;
  106.       }
  107.       return ret;
  108.    } else { /* Over here, we've finished caching, so just return the line */
  109.       if (Current == NULL) return 1;
  110.       else {
  111.          strcpy(Line, Current->text);
  112.          Current = Current->next;
  113.          return 0;
  114.       }
  115.    }
  116. }
  117. /***************************************************************/
  118. /*                                                             */
  119. /* ResetCache                                                  */
  120. /* Reset the cache to beginning, or reopen file if caching     */
  121. /* failed.                                                     */
  122. /*                                                             */
  123. /***************************************************************/
  124. #ifdef __STDC__
  125. void ResetCache(void)
  126. #else
  127. void ResetCache()
  128. #endif
  129. {
  130.    /* Reset the OMIT context */
  131.    ClearOmitContext();
  132.  
  133.    /* Get rid of any spurious stacked OMIT contexts */
  134.    FreeStackedOmits();
  135.  
  136.    if (CacheFailed) OpenFile(FileName);
  137.    else Current = Cache.next;
  138. }
  139.  
  140. /***************************************************************/
  141. /*                                                             */
  142. /* DestroyCache                                                */
  143. /* Frees all memory used by the cache.                         */
  144. /*                                                             */
  145. /***************************************************************/
  146. #ifdef __STDC__
  147. void DestroyCache(void)
  148. #else
  149. void DestroyCache()
  150. #endif
  151. {
  152.    Centry *p = &Cache;
  153.    Centry *c = p->next;
  154.  
  155.    while (c) {
  156.       if (c->text) free(c->text);
  157.       p = c;
  158.       c = c->next;
  159.       free(p);
  160.    }
  161.    Cache.next = NULL;
  162. }
  163.  
  164.