home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 January
/
usenetsourcesnewsgroupsinfomagicjanuary1994.iso
/
sources
/
misc
/
volume17
/
remind
/
part03
/
cache.c
next >
Wrap
C/C++ Source or Header
|
1991-02-19
|
5KB
|
164 lines
/***************************************************************/
/* */
/* CACHE.C */
/* */
/* Contains routines for caching reminder file to improve */
/* calendar performance. */
/* */
/* By David Skoll - 15 November 1990 */
/***************************************************************/
#include <stdio.h>
#ifndef NO_MALLOC_H
#include <malloc.h>
#endif
#include <string.h>
#include "defines.h"
#include "globals.h"
#include "protos.h"
#include "cache.h"
/* Define a cached line */
typedef struct cached_line {
char *text;
struct cached_line *next;
} Centry;
Centry Cache, *Current;
static int CacheDone, CacheFailed;
/***************************************************************/
/* */
/* InitCache */
/* */
/* Initializes the caching system. */
/* */
/***************************************************************/
#ifdef __STDC__
void InitCache(void)
#else
void InitCache()
#endif
{
CacheDone = 0;
CacheFailed = 0;
Cache.next = NULL;
Current = &Cache;
}
/***************************************************************/
/* */
/* GetLine */
/* */
/* This function either reads a line from the file, or gets */
/* it from memory if it is cached. */
/* */
/* Returns 0 if more data to be read; otherwise, non-zero. */
/* */
/* */
/***************************************************************/
#ifdef __STDC__
int GetLine(void)
#else
int GetLine()
#endif
{
int ret;
Token tok;
char *s;
Centry *c;
if (CacheFailed) return ReadLine();
if (!CacheDone) {
ret = ReadLine();
if (ret) {
CacheDone = 1;
strcpy(FileName, "* cache *");
CurLine = 0;
return ret;
}
/* Check if we should cache this line */
s = Line;
tok = ParseToken(&s);
if (tok.type == Clear_t || tok.type == Push_t ||
tok.type == Pop_t || tok.type == Rem_t || tok.type == Omit_t) {
c = (Centry *) malloc(sizeof(Centry));
if (c == NULL) {
CacheFailed = 1;
DestroyCache();
return 0;
}
c->text = (char *) malloc(strlen(Line)+1);
if (c->text == NULL) {
CacheFailed = 1;
DestroyCache();
free(c);
return 0;
}
/* Insert the cache entry */
c->next = NULL;
strcpy(c->text, Line);
Current->next = c;
Current = c;
}
return ret;
} else { /* Over here, we've finished caching, so just return the line */
if (Current == NULL) return 1;
else {
strcpy(Line, Current->text);
Current = Current->next;
return 0;
}
}
}
/***************************************************************/
/* */
/* ResetCache */
/* Reset the cache to beginning, or reopen file if caching */
/* failed. */
/* */
/***************************************************************/
#ifdef __STDC__
void ResetCache(void)
#else
void ResetCache()
#endif
{
/* Reset the OMIT context */
ClearOmitContext();
/* Get rid of any spurious stacked OMIT contexts */
FreeStackedOmits();
if (CacheFailed) OpenFile(FileName);
else Current = Cache.next;
}
/***************************************************************/
/* */
/* DestroyCache */
/* Frees all memory used by the cache. */
/* */
/***************************************************************/
#ifdef __STDC__
void DestroyCache(void)
#else
void DestroyCache()
#endif
{
Centry *p = &Cache;
Centry *c = p->next;
while (c) {
if (c->text) free(c->text);
p = c;
c = c->next;
free(p);
}
Cache.next = NULL;
}