#include "mat.h"

typedef short int int2;
typedef long  int4;
typedef word  uint2;
typedef dword uint4;
typedef unsigned char      bool;

#define reg

#define TRUE       1
#define FALSE      0
#define OK         0
#define ERR        (-1)

#define MAX_WORD_LEN	40
#define cmag2(x,y)	((x)|((y)<<8))
#define MAG_HYPH	cmag2('W','Y')
#define MAG_SPEL	cmag2('W','S')
#define MAG_THES	cmag2('W','H')

/* --- any header structure --- */
#define HEADER_SIZE	128
typedef struct 
  { char name[64];	/* title of file 		*/
    int2 magic;		/* magic number for this file	*/
    byte version;	/* version of file structure	*/
    byte checksum;	/* check sum for name		*/
    int2 language;	/* language identification	*/
    int2 reser;		/* reserved			*/
  } any_header;

/*----------------------------------------------------------------
  LEMMA.H    Constants and macros for lemma algorith
----------------------------------------------------------------*/

#define LEM_KIND_OFS	8
#define LEM_SGPL_OFS	7
#define LEM_MORE_OFS	6
#define LEM_VOIC_OFS	3
#define LEM_CASE_OFS	0
#define LEM_PERS_OFS	0

#define LEM_KIND_SIZ	4
#define LEM_SGPL_SIZ	1
#define LEM_MORE_SIZ	1
#define LEM_VOIC_SIZ	3
#define LEM_CASE_SIZ	3
#define LEM_PERS_SIZ	3

#define lem_get_inf(inf,ofs,s)	(((inf)>>(ofs))&((1<<(s))-1))
#define lem_put_inf(inf,ofs,n)	(inf|=(n)<<(ofs))

#define lem_get_kind(inf)	lem_get_inf(inf,LEM_KIND_OFS,LEM_KIND_SIZ)
#define lem_get_sgpl(inf)	lem_get_inf(inf,LEM_SGPL_OFS,LEM_SGPL_SIZ)
#define lem_get_more(inf)	lem_get_inf(inf,LEM_MORE_OFS,LEM_MORE_SIZ)
#define lem_get_voic(inf)	lem_get_inf(inf,LEM_VOIC_OFS,LEM_VOIC_SIZ)
#define lem_get_case(inf)	lem_get_inf(inf,LEM_CASE_OFS,LEM_CASE_SIZ)
#define lem_get_pers(inf)	lem_get_inf(inf,LEM_PERS_OFS,LEM_PERS_SIZ)

#define LEM_KIND_SUBS	1	/* podst.jm	*/
#define LEM_KIND_ADJ	   2	/* prid.jm.	*/
#define LEM_KIND_PRON	3	/* zajmena	*/
#define LEM_KIND_NUM	   4	/* cislovky	*/
#define LEM_KIND_VERB	5	/* slovesa	*/
#define LEM_KIND_ADV	   6	/* prislovce	*/
#define LEM_KIND_PREP	7	/* predlozky	*/
#define LEM_KIND_CONJ	8	/* spojky	*/
#define LEM_KIND_PART	9	/* castice	*/
#define LEM_KIND_INTR	10	/* citoslovce	*/
#define LEM_KIND_POSS	12	/* prid.jm.privl*/


/*----------------------------------------------------------------
  SPELL.H    Header file for spell algorithm
----------------------------------------------------------------*/
#ifndef _SPELL_H
#define _SPELL_H

/* spell modes:		*/
#define SPL_MOD_MATCH	1	/* find all matched words	*/
#define SPL_MOD_LEMMA	2	/* get lemma & morphologic info	*/

#define SPL_LEM_NORM	0
#define SPL_LEM_PREF	1
#define SPL_LEM_KIND	2

/* spell constants:	*/
#define SPL_PATT	0x03ff		/* pattern number	*/
#define SPL_MOST	0x0400		/* prefix nej-		*/
#define SPL_NEGA	0x0800		/* prefix ne-		*/
#define SPL_1BIG	0x1000		/* big first letter 	*/
#define SPL_IMPF	0x2000		/* imperfect verb	*/
#define SPL_NUMB	0x4000		/* number flag		*/
#define SPL_ABIG	SPL_MOST	/* big all letters	*/
#define SPL_ADOT	SPL_IMPF	/* dot past word	*/
#define SPL_SUPR	0x8000		/* export superlative	*/

#define SPL_ENDSEG	0x8000

/* spell and ths header */
typedef struct {	/* 128 byte */
    any_header H;
    byte  contents;		/* contents of this file		*/
    byte  alg_type;		/* type of speller algorithm		*/
    byte  max_code;		/* maximum code in dictionary		*/
    byte  reser2[5];
    uint4 stem_begi;		/* offset to stem dictionary		*/
    uint4 stem_size;		/* number of stem trie			*/
    uint4 user_begi;		/* begin of user dictionary (if any)	*/
    uint4 user_size;		/* size of user dictionary (if any)	*/
    uint4 thes_begi;		/* begin of thesaurus dictionary	*/
    uint2 thes_word_num;	/* number of words in ths dictionary	*/
    uint2 thes_offs_siz;	/* size of offset array of thesaurus	*/
    byte  thes_grps_siz;	/* size of groups in thes buffer	*/
    byte  thes_lett_num;	/* number of various letters in thes 	*/
    byte  thes_xxch_num;	/* length of information for ch-letters	*/
    byte  thes_spac_cod;	/* code of space			*/
    uint2 reser4[10];
} spl_ths_header;

typedef struct {          /* 128 byte */
    uint2 splinfo_len;    /* length of splinfo block		*/
    uint2 prefixs_len;    /* length of prefixs block		*/
    uint2 endings_len;    /* lenght of engings block            */
    uint2 suffixs_len;    /* lenght of suffixs block            */
    uint2 pattern_num;    /* number of patterns                 */
    uint2 gramset_num;    /* number of grammar sets             */
    uint2 res1[2];
    uint2 patt_vals_len;
    uint2 suff_vals_len;
    uint2 gset_vals_len;
    uint2 morf_vals_len;
    uint2 altr_vals_len;
    uint2 res2[3];

    uint2 most_gset;	/* grammar sets for superlativ		*/
    uint2 perf_gset;	/* grammar sets for perfect verbs	*/
    uint2 impf_gset;	/* grammar sets for imperfect verbs	*/
    uint2 res4[5];
    uint2 nega_tofs;	/* prefix ne-				*/
    uint2 most_tofs;	/* prefix nej- (naj-)			*/
    uint2 grge_tofs;	/* prefix ge-				*/
    uint2 res3[5];

    uint2 ptyp[10];	/* number of first pattern for type	*/
    uint2 res5[22];
  } spell_header;

#define spl_ths_swim(hd)	\
	(swim4(hd.stem_begi), swim4(hd.stem_size),		\
	 swim4(hd.user_begi), swim4(hd.user_size),		\
	 swim4(hd.thes_begi),					\
	 swim2(hd.thes_word_num), swim2(hd.thes_offs_siz)	\
	)
#define spell_swim(hd)		\
	(swim2(hd.splinfo_len),   swim2(hd.prefixs_len),	\
	 swim2(hd.endings_len),   swim2(hd.suffixs_len),	\
	 swim2(hd.pattern_num),   swim2(hd.gramset_num),	\
	 swim2(hd.patt_vals_len), swim2(hd.suff_vals_len),	\
	 swim2(hd.gset_vals_len), swim2(hd.morf_vals_len),	\
	 swim2(hd.altr_vals_len), 				\
	 swim2(hd.most_gset), swim2(hd.perf_gset), swim2(hd.impf_gset),	\
	 swim2(hd.nega_tofs), swim2(hd.most_tofs), swim2(hd.grge_tofs),	\
	 swim2(hd.ptyp[0]), swim2(hd.ptyp[1]), swim2(hd.ptyp[2]),	\
	 swim2(hd.ptyp[3]), swim2(hd.ptyp[4]), swim2(hd.ptyp[5]),	\
	 swim2(hd.ptyp[6]), swim2(hd.ptyp[7]), swim2(hd.ptyp[8]),	\
	 swim2(hd.ptyp[9])						\
	)

typedef struct {
    int2  file;
    char *word;
    uint2 flag;
    byte  mode;
    byte  num;
    byte  len;
    byte  pos;
    byte  err;
    byte  max_code;
    uint4 stem_begi;
    uint4 stem_size;
    byte *most_gset;
    byte *perf_gset;
    byte *impf_gset;
    byte *nega_text;
    byte *most_text;
  } spell_struct;
extern spell_struct spl_hd;

typedef struct {
    byte  stem;
    byte  suff;
    byte  endi;
    byte  eseg;
    int2  info;
    int2 *morf;
  } spell_result;

extern spell_result spl_res[];

extern void (*add_sim)(char *w, uint2 flag);	/* 1BIG,ABIG,ADOT */
extern void (*add_all)(char *w, uint2 flag);	/* 1BIG,ABIG,ADOT,NEGA,SUPR */

/* main spell functions */
#define spell_set_proc(sim,all)	(add_sim=sim, add_all=all)
int2	spell_init_file	(char *fname, byte mode, uint2 pag);
void	spell_done_file	();
int2	spell_find_word	(char *word, uint2 flag);
void	spell_find_sims	(char *word);
int2	spell_find_lemm	(char *stem, uint2 patt, uint2 find, byte mod);
int2	spell_stnd_lemm	(char *lemma, byte n, byte mod);

#endif

#ifndef _G_XORD_H
#define _G_XORD_H

#define XLOWER		1
#define XUPPER		2
#define iscslower(c)	(xflg[(byte)(c)]&XLOWER)
#define iscsupper(c)	(xflg[(byte)(c)]&XUPPER)
#define iscschar(c)	(xord[(byte)(c)] && xord[(byte)(c)]<50)

#endif

/*----------------------------------------------------------------
  THES.H    Thesaurus interface functions
----------------------------------------------------------------*/
#ifndef _THES_H
#define _THES_H

/* thesaurus modes:		*/
#define THS_MOD_FULL	1	/* full matching		*/
#define THS_MOD_LEMM	2	/* use lemmatization 		*/
#define THS_MOD_RELA	4	/* try related lemma		*/

/* exported thes constants:	*/
#define THS_STY_FLAGS	" dehkoszr?"
#define THS_FLG_MASK	0x0F	/* mask for group flags		*/
#define THS_ASKED	0x10	/* this word was asked by user	*/
#define THS_GROUP	0x20	/* this word is first in group	*/
#define THS_ANTON	0x40	/* this word is antonym		*/
#define THS_ENTRY	0x80	/* this word is entry		*/

/* internal thes constants:	*/
#define THS_ENTR	0x80
#define THS_LINK	0x40
#define THS_NEXT	0x20
#define THS_INFO	0x80
#define THS_KIND	0x40
#define THS_GRP_SIZE	16
#define MAX_GRP_SIZE	32
#define MAX_ITEM_SIZE	35

extern	byte ths_mode;
extern   void (*ths_pri)(char *word, uint2 num, byte flag, byte mode);
#define  thes_is_mode(m)	(ths_mode&(m))
#define  thes_set_mode(m,b)	((b)?(ths_mode|=(m)):(ths_mode&=~(m)))
#define  thes_set_proc(f)	(ths_pri=f)

int2	 thes_init_file (char *ths_name);
void	 thes_done_file	(void);
uint2	 thes_get_num	(char *word, byte kind, bool *right);
char	*thes_get_word	(uint2 num, char *word, byte *flg);
void	 thes_get_info	(uint2 num);
bool	 thes_isit_entry(uint2 num);
uint2	 thes_prev_entry(uint2 num);
uint2	 thes_next_entry(uint2 num);
byte 	 thes_main	(char *word, uint2 flag, uint2 *find, bool *righ);

#endif

void speller_refresh(int  thsmode, byte *buff, int bsize);
int  xord_word (char *n, char *old, int last);
extern byte *xchr;
//int get_xsrt_weig (unsigned char **st);
int  getxs(int c);
void xord_use();
void xord_end();
