/*++++++++++++++
.IDENTIFICATION cache.h
.LANGUAGE       C
.AUTHOR         Francois Ochsenbein
.ENVIRONMENT
.KEYWORDS
.VERSION  1.0   02-Nov-2007
.VERSION  1.4   19-May-2014: insertion of age
.COMMENTS       Cache (managed with mmap)

The 'cache' file is made of 3 parts:

1. A header line (multiple of 4 bytes)
   *Sesame*cache  items=%d, size=%dk\n
2. The list of 'items' slots
3. The contents

---------------*/

#ifndef CACHE_DEF	/* To avoid recursive inclusions   */
#define CACHE_DEF	0

#ifndef _ARGS
#ifdef __STDC__
#define _ARGS(A)	A       /* ANSI */
#else
#define _ARGS(A)	()      /* Traditional */
#define const
#endif
#endif
#include <time.h>
#include <stdio.h>

/*==================================================================
		Structures
 *==================================================================*/

typedef struct {
    char *file;		/* Associated cache file */
    char *map;		/* Associated contents   */
    short size;		/* length of file name   */
    short fd;		/* opened file handle	 */
    int  max_age;	/* lifetime (s) of items */
    int  oslots;	/* Location of slots 	 */
    int  nslots;	/* Number of items       */
} CACHE;

/* Use a cache: each question is saved; the answers are :
    short qasked;		// Question option (QM_xx)
    short qfound;		// What's found    (QM_xx)
   NULL if not asked, NOT_FOUND for a negative result		*/
typedef struct {
    int reqno;			/* Question number		*/
    int count;			/* Count of question asked	*/
    time_t t_create;		/* Time of query (modification)	*/
    time_t t_access;		/* Time of query (access date)	*/
    char *text;			/* Text of question + answers   */
} CACHE_ITEM;

/*==================================================================
		Routine declarations
 *==================================================================*/

CACHE      *cache_creat  _ARGS((char *cachname, int items, int max_age));
CACHE      *cache_open   _ARGS((char *cachname));
int         cache_close  _ARGS((CACHE *cache));
int         cache_free   _ARGS((CACHE *cache, char *name));
CACHE_ITEM *cache_item   _ARGS((CACHE *cache, char *name));
int         cache_add    _ARGS((CACHE *cache, char *name, char *text));
int         cache_add3   _ARGS((CACHE *cache, char *name, char *intro, 
	    						  char *text));
int         cache_append _ARGS((CACHE *cache, char *name, char *text));
int         cache_update _ARGS((CACHE *cache, char *name, char *text));
int         cache_token  _ARGS((CACHE *cache));	/* Reserve token */
	    /* option=0: number; 1=files; 2=full; 3=file+age */
int	    cache_print  _ARGS((CACHE *cache, FILE *out, int option));
int         cache_clean  _ARGS((CACHE *cache, int max_elements, int max_age));

/* CACHE_ITEM *cache_save  _ARGS((CACHE *cache, CACHE_ITEM *item));*/

#endif	/* CACHE_DEF */
