summaryrefslogtreecommitdiff
path: root/language/korean/hcode1_1/conv.c
blob: 843cb94074d34784d2726d8b4583255907e2f941 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#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]--;
	}
}