#include  <stdio.h>
#include  "hcode.h"
#include  "h2Bcode.h"
#include  "h3Bcode.h"

/*  --------------------------------------------------------------
	From InputCode To OutputCode Convertion Routine
	First find a key in Sorted Section of 2Byte table by bisection search
	And then look up UnSorted Section of the table by siquential search
	Hanbuf[ 1:Hanbuf[0] ] will be filled with OutputCode
	F,T : Input and Output 2 Byte code Number in h2Bcode.h
   --------------------------------------------------------------- */

void code2conv(Hanbuf, F, T)
int Hanbuf[], F, T;
{
	int i,l,m,u,test,find=0;

	for ( i=1; i<=Hanbuf[0]; i++ ) {
		l = NoUnSorted; u = NoKSC-1;
		while ( l<=u ) {
			test = TwoConvert[(m=(l+u)/2)][F];
			if ( test < Hanbuf[i] ) l=m+1;
			else if ( test > Hanbuf[i] ) u=m-1;
			else {
				find = 1;
				break;
			}
		}
		if ( find == 0 ) {
			for (m=0; m<NoUnSorted; m++) 
				if ( TwoConvert[m][F] == Hanbuf[i] ) {
					find=1;
					break;
				}
		}
		if ( find == 1 ) {
			Hanbuf[i] = TwoConvert[m][T];
			find = 0;
		} else {
		   fprintf(stderr,"%x is unknown %d Code\n",Hanbuf[i],F);
		}
	}
}


/*  --------------------------------------------------------------
	Hangul Char in Multibyte is defined like 
		(C*   V+   C*)  (C   V+  C*) *    (C  {V+|EOF} )
	Pointer  p0   p1   p2        p3
	Next_Step                p0  p1  p2        p3
    --------------------------------------------------------------
	This section consists of Three routines and one aux routine.
		void m2code(buf,IC,HanMode,Hanbuf)
		int getOne(buf, IC, p0, p1)
		int HanCode(buf,IC, p0,p1,p2,p3)
		int strNcmp(test,table,size)
   --------------------------------------------------------------- */

#define IsConso(x,y)  (x>=ChoSung[2][y][0]&&x<=ChoSung[20][y][0])
#define IsVowel(x,y)  (x>=JungSung[3][y][0]&&x<=JungSung[29][y][0])

int strNcmp(test,table,size)
unsigned char test[], table[];
int size;
{	int i;
	for ( i=0; i<size; i++ )
		if ( test[i] > table[i] ) return(1);
		else if ( test[i] < table[i] ) return(-1);
	if ( table[size] != '\0' ) return(-1);
	return(0);
}
		

/* ------------------------------------------------
	ChoSung and JungSung and Jongsung is already
		separated by p0,p1,p2,p3 in buf
	convert each portion by 3B table look up method
	And then join three of them into one Integer Code
	If any of them is missing, then
		correspoding code will be the lower limit (01,02,01).
   ------------------------------------------------ */

int HanCode(buf,IC, p0,p1,p2,p3)
unsigned char buf[];
int IC, p0,p1,p2,p3;
{
	int ch,ju,jo,  l,t,u,order;

	l=01; u=20;
	while ( l<= u ) {
		ch = (l+u) / 2;
		order=strNcmp(buf+p0,ChoSung[ch][IC],p1-p0);
		if ( order > 0 ) l=ch+1;
		else if ( order < 0 ) u=ch-1;
		else break;
	}
	l=02; u=29;
	while ( l<= u ) {
		ju = t = (l+u) / 2;
		if ( (ju&6) == 0 ) {
			ju = (ju&0x00f8) - 1;
			t = ju+2;
		}
		order = strNcmp(buf+p1,JungSung[ju][IC],p2-p1);
		if ( order > 0 ) l=t+1;
		else if ( order < 0 ) u=ju-1;
		else break;
	}

	l=02; u=29;
	if ( p3 == p2 ) jo = 1;
	else if (strNcmp(buf+p2,JongSung[3][IC],p3-p2)==0) jo=3;
	else 
		while ( l<= u ) {
			jo = t = (l+u) / 2;
			if ( jo == 18 ) {
				jo=17;
			}
			order = strNcmp(buf+p2,JongSung[jo][IC],p3-p2);
			if ( order > 0 ) l=t+1;
			else if ( order < 0 ) u=jo-1;
			else break;
		}

	return ( 0x8000|(ch<<10)|(ju<<5)|jo );
}

/* ------------------------------------------------
	Hangul AutoMata To find one Char until end_of_buf
		(C*   V+   C*)  (C   V+  C*) *    (C  {V+|EOF} )
	Pointer  p0   p1   p2        p3
	Next_Step                p0  p1  p2        p3
   ------------------------------------------------ */

int getOne(buf, IC, p0, p1)
unsigned char buf[];
int IC, *p0, *p1;
{
	int p2, p3, np, result;
	if ( *p1 == *p0 )
		for ( ; IsConso(buf[*p1],IC); (*p1)++ )
			if ( *p1 == buf[0] ) {
				result=HanCode(buf,IC,*p0,*p1+1,*p1+1,*p1+1);
				*p1 = buf[0] + 1;
				return(result);
			}
	for ( p2= (*p1); IsVowel(buf[p2],IC); p2++ )
		if ( p2 == buf[0] ) {
			result=HanCode(buf,IC,*p0,*p1,p2+1,p2+1);
			*p1 = buf[0] + 1;
			return(result);
		}
	for ( p3= p2; IsConso(buf[p3],IC); p3++ )
		if ( p3 == buf[0] ) {
			result=HanCode(buf,IC,*p0,*p1,p2,p3+1);
			*p1 = buf[0] + 1;
			return(result);
		}
	result = HanCode(buf,IC,*p0,*p1,p2,p3-1);
	*p0 = p3 - 1;
	*p1 = p3;
	return(result);
}

/* ------------------------------------------------
	MultiByte inputs in buf to Hanbuf with Trigem Code
	If Hangul_input_Mode is not ENDHAN, then
		undo last one code conversion.
   ------------------------------------------------ */

void m2code(buf,IC,HanMode,Hanbuf)
unsigned char buf[];
int  IC, HanMode, Hanbuf[];
{
	int p0, p1, n;
	p0 = p1 = 1;
	while ( p1 <= buf[0] ) {
		Hanbuf[++Hanbuf[0]] = getOne(buf,IC,&p0,&p1);
	}
	if ( HanMode == ENDHAN ) {
		buf[0] = 0;
	} else {
		buf[0] = buf[0] - p0 + 1;
		for ( n=0; n < buf[0];  n++ )
			buf[n+1] = buf[n+p0];
		Hanbuf[0]--;
	}
}

