#ifndef CSCOPE_INVLIB_H
#define CSCOPE_INVLIB_H

#include <stdio.h>	/* need definition of FILE* */
#include <limits.h> /* need definition of CHAR_MAX */

/* inverted index definitions */

/* postings temporary file long number coding into characters */
#if CHAR_MAX == 255
# define BASE	   223 /* 255 - ' ' */
# define PRECISION 4   /* maximum digits after converting a long */
#else
# if CHAR_MAX == 127   /* assume sign-extension of a char when converted to an int */
#  define BASE		95 /* 127 - ' ' */
#  define PRECISION 5  /* maximum digits after converting a long */
# else
#  error Need a platform with 8 bits in a char value
# endif
#endif

/* inverted index access parameters */
#define INVAVAIL 0
#define INVBUSY	 1
#define INVALONE 2

/* boolean set operations */
#define bool_OR		  3
#define AND			  4
#define falseT		  5
#define REVERSEfalseT 6

/* note that the entire first block is for parameters */
typedef struct {
		long version;	/* inverted index format version */
		long filestat;	/* file status word  */
		long sizeblk;	/* size of logical block in bytes */
		long startbyte; /* first byte of superfinger */
		long supsize;	/* size of superfinger in bytes */
		long cntlsize;	/* size of max cntl space (should be a multiple of BUFSIZ) */
		long share;		/* flag whether to use shared memory */
} PARAM;

typedef struct {
		FILE			 *invfile;	/* the inverted file ptr */
		FILE			 *postfile; /* posting file ptr */
		PARAM			  param;	/* control parameters for the file */
		char			 *iindex;	/* ptr to space for superindex */
		union logicalblk *logblk;	/* ptr to space for a logical block */
		long			  numblk;	/* number of block presently at *logblk */
		long			  keypnt;	/* number item in present block found */
} INVCONTROL;

typedef struct {
		short		  offset; /* offset in this logical block */
		unsigned char size;	  /* size of term */
		unsigned char space;  /* number of longs of growth space */
		long		  post;	  /* number of postings for this entry */
} ENTRY;

typedef struct {
		long lineoffset;	 /* source line database offset */
		long fcnoffset;		 /* function name database offset */
		long fileindex : 24; /* source file name index */
		long type	   : 8;	 /* reference type (mark character) */
} POSTING;

extern long *srcoffset;	 /* source file name database offsets */
extern int	 nsrcoffset; /* number of file name database offsets */


void	 boolclear(void);
POSTING *boolfile(INVCONTROL *invcntl, long *num, int boolarg);
void	 invclose(INVCONTROL *invcntl);
void	 invdump(INVCONTROL *invcntl, char *term);
long	 invfind(INVCONTROL *invcntl, char *searchterm);
int		 invforward(INVCONTROL *invcntl);
int		 invopen(INVCONTROL *invcntl, char *invname, char *invpost, int status);
long	 invmake(char *invname, char *invpost, FILE *infile);
long	 invterm(INVCONTROL *invcntl, char *term);

#endif /* CSCOPE_INVLIB_H */
