
#import <stdio.h>

#import <btree/BTree.h>
#import <btree/BTreeCursor.h>
#import <btree/BTreeFile.h>

/* main sections of the book */
#define dMain			0
#define dAbbr			1
#define dForeign		2
#define dBio			3
#define dGeo			4
#define dCollege		5
#define dAbbrD			6
#define dThesaurus		7

/* Tags for Webster Dictionary entries */
#define dEntry			1
#define dPronunciation		2
#define dVariant		3
#define dFunction		4
#define dInflection		5
#define dPicture		6
#define dDate			7
#define dSubject		8
#define dEtymology		9
#define dSense			11
#define dWordlist		12
#define dUsage			13

/* 1-byte escapes used in Webster */
#define dROMAN			14
#define dITALIC			15
#define dBOLD			16
#define dSCHWA			17
#define dHACCENT		18
#define dLACCENT		19
#define dBOLDCOLON		20
#define dELONG			21
#define dADIER			22
#define	dSUP			23
#define dSUB			24
#define	dOLONG			25
#define dALONG			26
#define dILONG			27
#define dGFONT			28
#define dODOT			29
#define dSMALLCAP		30
#define dBITALIC		31
#define dESCAPE1		10
#define dESCAPE2		127

/* Tags for the Collegiate Thesaurus: */
#define tEntry 			1
#define tVariant 		2
#define tFunction 		3
#define tSense			4
#define tSyn 			5
#define tAnt 			6
#define tCon 			7
#define tRel 			8
#define tIdiom 			9
#define tBracket 		10
#define tCompare 		11
#define tLimited 		12
#define tItalic 		13
#define tBold 			14
#define tRoman 			15
#define tBItalic 		16
#define tSmallCap 		17
#define tCommaSpace 		18
#define tSemicolonSpace 	19
#define tMaxSpecial 		19

typedef struct SenseList
/*
 * A SenseList is a tagged list of fields:
 *   s[0] is the tag (e.g., dEntry, dFunction, tEntry, etc)
 *   s+1 is the data.
 */
{
	char *s;
	int n;
	struct SenseList *next;
} SenseList;

typedef int entryID;

typedef struct
/*
 * A Definition entry contains any of a number of fields (all optional).
 * The field pointers are mostly taken from the SenseList,
 * so if there happens to be, e.g., more than one function field,
 * only the first function field appearing the list is pointed to
 * by the "function" field.  Thus the field pointers point to the
 * first of 0 or more such fields which may be present in the entry.
 */
{
	char *entry;		/* the main entry word */
	char *dotted;		/* the dotted form of the word (hyphenation info) */
	char *pronunciation;	/* the pronunciation field */
	char *function;		/* the grammatical function (n, v, adj, etc) */
	char *date;		/* date word was first used */
	char *etymology;	/* etymological description */
	char *inflection;	/* inflected forms (-ed, -ies, -ing, etc) */
	char *variant;		/* variant forms and inflections */
	char *picture;		/* pathname of file containing picture, if any */
	char *subject;		/* internal subject codes, per-sense */
	SenseList *l;		/* linked list of senses comprising the entry */
	int section;		/* one of: dMain, dAbbr, dForeign, dBio, dGeo, etc */
	entryID ID;		/* ID of entry in book */
	struct ReferenceBook *book;	/* what book it came from */
} Definition;

typedef struct ReferenceBook
/*
 * For now, a reference book from Webster includes a tagged source file,
 * an index, and possibly also a full-text index.
 * This data structure also contains pointers to book-specific i/o routines.
 */
{
	BTreeFile	*source;
	BTreeCursor	*entryCursor,
				*indexCursor,
				*headwordIndexCursor,
				*fullIndexCursor;
	entryID		*entryList;
	int			entryListNum, entryListCount;
	Definition	*(*getDef)();
	int			(*putDef)(), (*freeDef)();
} ReferenceBook;

/* conversion formats for special characters -- see dictionaryOutputFormat */
#define W_ASCII		0
#define W_PS		1
#define W_TROFF		2
#define W_VERBOSE	3

ReferenceBook *referenceOpen(char *name);
    /*
     * Open reference 'bookName' for searching and return it.
     * Currently 'bookName' may be one of 'Webster-Dictionary' or 'Webster-Thesaurus'.
     * Return the 'ReferenceBook' if successful, 0 if not.
     */

int referenceClose(ReferenceBook *r);
    /*
     * Close and free 'book'
     */

char *referencePath();

int freeDefinition(Definition *d);
    /*
     * Free up space used by 'd'.
     */

int dictionarySection(ReferenceBook *book, entryID itsID);
    /*
     * Return the the section in Webster's Ninth which includes 'offset'.
     * 'websterSectionName[...]' gives the string value for it.
     */

extern char *websterSectionName[]; /* ...[i] is the name of the ith section (dMain, etc) */

Definition *
nextDefinition(ReferenceBook *book);
    /*
     * Read and return the next definition from 'book'.
     * Use this to step through the list of definitions in the book:
     *	while (d = nextDefinition(dictionary))
     *		putDefinition(d,0);
     */

Definition *
getDefinition(char *word, ReferenceBook *book, int match);
    /*
     * Get the first definition for 'word' in 'book' and return it, or NULL if none found.
     * If 'exact' is true, the word must match exactly, otherwise
     * prefix matches are accepted.
     */

Definition *
getNextDefinition(char *word, ReferenceBook *book, int match);
    /*
     * Return the next definition matching 'word', NULL if no more.
     * Call this after 'getDefinition(...)'.
     */

Definition *
seekEntry( entryID itsID, ReferenceBook *book);
    /*
     * Return the definition with the given ID, if it exists.
     */

int useFullTextIndex( ReferenceBook *book,  int useFullText);
    /*
     * Used to set whether the full-text or headword index is used.
     * Returns 1 iff index requested exists.
    */

int freeDefinitions(Definition *D[]);
    /*
     * Free the definitions in 'D' (null-terminated; see 'getDefinitions()'.
     */
     
int getDefinitions(char *word, ReferenceBook *book, int exact, Definition *D[], int max);
    /*
     * Get up to 'max' definitions of 'word' and put them in 'D'.
     * 'D' will be null-terminated (thus, it must hold max+1 items).
     * Return the number found.
     */

int putDefinition(Definition *d, int (*output)());
    /*
     * Output the definition 'd', calling the function 'output'
     * for each character of data.  The information is preconverted
     * at lookup time to some output format (dictionaryOutputFormat,
     * one of W_ASCII, W_PS, etc).  If no output function is given
     * the information is written to the standard output.
     */

extern int
   dictionaryOutputFormat, /* format: W_ASCII (default), W_PS, W_TROFF, W_VERBOSE */
   dictionaryFoldSenses;   /* fold lines to this many characters (0 default) */



	  
