#ifndef _CODETABLE_H_
#define _CODETABLE_H_

/* Limit Constant Defination */
#define MAX_CHNAME_LEN		32 
#define MAX_VERSION_LEN		4 
#define MAX_UUID_LEN		64 
#define MAX_ICON_PATH_LEN	128 
#define MAX_AUTHOR_NAME_LEN	56 
#define MAX_COPYRIGHT_LEN	128 
#define MAX_HINTING_LEN		128 

#define MAX_USEDCODES_NUM	126 
#define MAX_WILDCHAR_NUM	4 

/* Limit of Key Prompt */
#define MAX_KEYPROMPT_LEN	4 

/* Limit of Function Key */
#define MAX_FUNCTIONKEY_LEN	6

/* CodeTable File Distinguishment Flag */
#define CODETABLE_FLAG		"CODETABLE"

/* Version Defination */
#define CODETABLE_VERSION	2

/* Section Constant */
enum _Section_List {
	DEFAULT_SECTION = 0,
	DESCRIPTION_SECTION,
	COMMENT_SECTION	,
	KEYPROMPT_SECTION,
	FUNCTIONKEY_SECTION,
	PHRASE_SECTION	,
	SINGLE_SECTION	,
	OPTIONS_SECTION	,
	MAX_SECTION_NUM
};

/* Function Key Defination */
enum _Function_Key_ID {
	PAGEUP_KEY_ID = 0,
	PAGEDOWN_KEY_ID,
	BACKSPACE_KEY_ID,
	CLEARALL_KEY_ID	,
	MAX_FUNCTIONKEY_NUM
};

/* Options Mode Defination */
enum _Option_Mode {
	OFF_MODE = 0,
	ON_MODE	,
};

/* SelectKey Mode Defination */
enum _Select_Key_Mode {
	NUMBER0_MODE = 0,
	NUMBER_MODE,
	LOWER_MODE,
	UPPER_MODE,
};

/* Phrase Seperate Tag */
#define HZ_PHRASE_TAG 		'\001'

/* Bit Operations */
#define BITMASK(i) 		(1 << (i))
#define BITSET(a, i) 		((a) |= BITMASK(i))
#define BITCLEAR(a, i) 		((a) &= ~BITMASK(i))
#define GETBIT(a, i) 		((a) & BITMASK(i))

typedef struct _tableNode {
	unsigned char 	key; 	/* key of this node */
	unsigned char   num_NextKeys; 	/* number of possible next input key */
	unsigned short 	num_HZchoice; 	/* number of HZ choices */
	unsigned int 	pos_NextKey;	/* table node pos of next key */
	unsigned int 	pos_HZidx;	/* index of HZ list */
} tableNode;
	
typedef struct _keyPrompt {
	unsigned char	prompt[MAX_KEYPROMPT_LEN + 1];
} keyPrompt;

typedef struct _functionKey {
	unsigned char	keylist[MAX_FUNCTIONKEY_LEN + 1];
} functionKey;

/* codetable file header structure */
typedef struct {
	char		UUID[MAX_UUID_LEN];		/* UUID */
	char		Version[MAX_VERSION_LEN];	/* VERSION */
	char		Author[MAX_AUTHOR_NAME_LEN];    /* Author */
	char		Copyright[MAX_COPYRIGHT_LEN];   /* CopyRight */
	char		Hinting[MAX_HINTING_LEN];       /* Hingting */
	char		IconPath[MAX_ICON_PATH_LEN];	/* icon path */

	char		Encode;				/* UTF-8 or native: encode of dict */	
	unsigned char 	Cname[MAX_CHNAME_LEN];  	/* IM Name */
	char          	UsedCodes[MAX_USEDCODES_NUM];   /* Characters for valid input */
	unsigned char 	WildChar[MAX_WILDCHAR_NUM];	/* Characters for WildChar */
	char          	MaxCodes;			/* Max number of extern codes */

	char		bSectionsFlag;			/* sections exist flag */

	char		Output_Encode;			/* encode of output */
	unsigned 	nKeyByKeyMode 		:1;
	unsigned 	nHelpInfoMode 		:1;
	unsigned 	nAutoSelectMode 	:1;
	unsigned 	nKeyPromptMode 		:1;
	unsigned 	nDisplayOnSpotMode 	:1;
	unsigned 	nSelectKeyMode 		:2;
	unsigned 	nReserved 		:1;

	unsigned int 	sizeHZList; 			/* size of whole hz list */
	unsigned int 	sizeNodeList;			/* size of whole node list */
	unsigned char 	*hzList;			/* pointer of characters list */
	tableNode	*nodeList; 			/* pointer of Tree Node List */
	
	keyPrompt 	*keyprompt; 			/* prompt list of keys */
	functionKey	*functionkey;			/* Functions key defination list */
		
} CodeTableStruct;

#endif    /*  _CODETABLE_H_  */
