1 #define Uses_SCIM_IMENGINE
2 #define Uses_SCIM_ICONV
3 #define Uses_SCIM_CONFIG_BASE
4 #define Uses_SCIM_CONFIG_PATH
5 
6 #include "scim.h"
7 #include "scim_fcitx_imengine.h"
8 using namespace scim;
9 
10 #include "pyParser.h"
11 #include "ime.h"
12 
13 #include <stdio.h>
14 #include <string.h>
15 
16 #include "pyMapTable.h"
17 #include "PYFA.h"
18 #include "sp.h"
19 
20 
IsSyllabary(char * strPY,Bool bMode)21 int IsSyllabary (char *strPY, Bool bMode)
22 {
23     register int    i;
24 
25     for (i = 0; syllabaryMapTable[i].cMap; i++) {
26 	if (bMode) {
27 	    if (!strncmp (strPY, syllabaryMapTable[i].strPY, strlen (syllabaryMapTable[i].strPY)))
28 		return i;
29 	}
30 	else {
31 	    if (!strcmp (strPY, syllabaryMapTable[i].strPY))
32 		return i;
33 	}
34     }
35 
36     return -1;
37 }
38 
IsConsonant(char * strPY,Bool bMode)39 int IsConsonant (char *strPY, Bool bMode)
40 {
41     register int    i;
42 
43     for (i = 0; consonantMapTable[i].cMap; i++) {
44 	if (bMode) {
45 	    if (!strncmp (strPY, consonantMapTable[i].strPY, strlen (consonantMapTable[i].strPY)))
46 		return i;
47 	}
48 	else {
49 	    if (!strcmp (strPY, consonantMapTable[i].strPY))
50 		return i;
51 	}
52     }
53 
54     return -1;
55 }
56 
FindPYFAIndex(char * strPY,Bool bMode)57 int FindPYFAIndex (char *strPY, Bool bMode)
58 {
59     int             i;
60     int             iTemp;
61 
62     iTemp = -1;
63     for (i = 0; PYTable[i].strPY[0] != '\0'; i++) {
64 	if (bMode) {
65 	    if (!strncmp (strPY, PYTable[i].strPY, strlen (PYTable[i].strPY))) {
66 		if (!PYTable[i].pMH)
67 		    return i;
68 		else if (*(PYTable[i].pMH))
69 		    return i;
70 	    }
71 	}
72 	else {
73 	    if (!strcmp (strPY, PYTable[i].strPY)) {
74 		if (!PYTable[i].pMH)
75 		    return i;
76 		else if (*(PYTable[i].pMH))
77 		    return i;
78 	    }
79 	}
80     }
81 
82     return -1;
83 }
84 
ParsePY(char * strPY,ParsePYStruct * parsePY,PYPARSEINPUTMODE mode)85 void ParsePY (char *strPY, ParsePYStruct * parsePY, PYPARSEINPUTMODE mode)
86 {
87     char           *strP;
88     int             iIndex;
89     int             iTemp;
90     char            str_Map[3];
91     char            strTemp[7];
92 
93     parsePY->iMode = PARSE_SINGLEHZ;
94     strP = strPY;
95     parsePY->iHZCount = 0;
96 
97     if (bSP) {
98 	char            strQP[7];
99 	char            strJP[3];
100 
101 	strJP[2] = '\0';
102 
103 	while (*strP) {
104 	    strJP[0] = *strP++;
105 	    strJP[1] = *strP;
106 	    SP2QP (strJP, strQP);
107 	    MapPY (strQP, str_Map, mode);
108 
109 	    if (!*strP) {
110 		strcpy (parsePY->strMap[parsePY->iHZCount], str_Map);
111 		strcpy (parsePY->strPYParsed[parsePY->iHZCount++], strJP);
112 		break;
113 	    }
114 
115 	    iIndex = FindPYFAIndex (strQP, 0);
116 	    if (iIndex != -1) {
117 		strcpy (parsePY->strMap[parsePY->iHZCount], str_Map);
118 		strcpy (parsePY->strPYParsed[parsePY->iHZCount++], strJP);
119 		strP++;
120 	    }
121 	    else {
122 		strJP[1] = '\0';
123 		SP2QP (strJP, strQP);
124 		if (!MapPY (strQP, str_Map, mode))
125 		    strcpy (parsePY->strMap[parsePY->iHZCount], strJP);
126 		else
127 		    strcpy (parsePY->strMap[parsePY->iHZCount], str_Map);
128 		strcpy (parsePY->strPYParsed[parsePY->iHZCount++], strJP);
129 	    }
130 
131 	    if (*strP == PY_SEPERATOR) {
132 		strcat (parsePY->strPYParsed[parsePY->iHZCount - 1], PY_SEPERATOR_S);
133 		while (*strP == PY_SEPERATOR )
134 		    strP++;
135 	    }
136 	}
137     }
138     else {
139 	Bool            bSeperator = False;
140 
141 	do {
142 	    iIndex = FindPYFAIndex (strP, 1);
143 
144 	    if (iIndex != -1) {
145 		strTemp[0] = PYTable[iIndex].strPY[strlen (PYTable[iIndex].strPY) - 1];
146 		iTemp = -1;
147 		if (strTemp[0] == 'g' || strTemp[0] == 'n') {
148 		    strncpy (strTemp, strP, strlen (PYTable[iIndex].strPY) - 1);
149 		    strTemp[strlen (PYTable[iIndex].strPY) - 1] = '\0';
150 
151 		    iTemp = FindPYFAIndex (strTemp, 0);
152 		    if (iTemp != -1) {
153 			iTemp = FindPYFAIndex (strP + strlen (PYTable[iTemp].strPY), 1);
154 			if (iTemp != -1) {
155 			    if (strlen (PYTable[iTemp].strPY) == 1 || !strcmp ("ng", PYTable[iTemp].strPY))
156 				iTemp = -1;
157 			}
158 			if (iTemp != -1) {
159 			    strncpy (strTemp, strP, strlen (PYTable[iIndex].strPY) - 1);
160 			    strTemp[strlen (PYTable[iIndex].strPY) - 1] = '\0';
161 			}
162 		    }
163 		}
164 		if (iTemp == -1)
165 		    strcpy (strTemp, PYTable[iIndex].strPY);
166 		MapPY (strTemp, str_Map, mode);
167 		strcpy (parsePY->strMap[parsePY->iHZCount], str_Map);
168 		strP += strlen (strTemp);
169 
170 		if (bSeperator) {
171 		    bSeperator = False;
172 		    parsePY->strPYParsed[parsePY->iHZCount][0] = PY_SEPERATOR;
173 		    parsePY->strPYParsed[parsePY->iHZCount][1] = '\0';
174 		}
175 		else
176 		    parsePY->strPYParsed[parsePY->iHZCount][0] = '\0';
177 		strcat (parsePY->strPYParsed[parsePY->iHZCount++], strTemp);
178 	    }
179 	    else {
180 		if (bFullPY && *strP != PY_SEPERATOR)
181 		    parsePY->iMode = PARSE_ERROR;
182 
183 		iIndex = IsConsonant (strP, 1);
184 		if (-1 != iIndex) {
185 		    parsePY->iMode = PARSE_ERROR;
186 
187 		    if (bSeperator) {
188 			bSeperator = False;
189 			parsePY->strPYParsed[parsePY->iHZCount][0] = PY_SEPERATOR;
190 			parsePY->strPYParsed[parsePY->iHZCount][1] = '\0';
191 		    }
192 		    else
193 			parsePY->strPYParsed[parsePY->iHZCount][0] = '\0';
194 		    strcat (parsePY->strPYParsed[parsePY->iHZCount], consonantMapTable[iIndex].strPY);
195 		    MapPY (consonantMapTable[iIndex].strPY, str_Map, mode);
196 		    strcpy (parsePY->strMap[parsePY->iHZCount++], str_Map);
197 		    strP += strlen (consonantMapTable[iIndex].strPY);
198 		}
199 		else {
200 		    iIndex = IsSyllabary (strP, 1);
201 		    if (-1 != iIndex) {
202 			if (bSeperator) {
203 			    bSeperator = False;
204 			    parsePY->strPYParsed[parsePY->iHZCount][0] = PY_SEPERATOR;
205 			    parsePY->strPYParsed[parsePY->iHZCount][1] = '\0';
206 			}
207 			else
208 			    parsePY->strPYParsed[parsePY->iHZCount][0] = '\0';
209 			strcat (parsePY->strPYParsed[parsePY->iHZCount], syllabaryMapTable[iIndex].strPY);
210 			MapPY (syllabaryMapTable[iIndex].strPY, str_Map, mode);
211 			strcpy (parsePY->strMap[parsePY->iHZCount++], str_Map);
212 
213 			strP += strlen (syllabaryMapTable[iIndex].strPY);
214 			if (parsePY->iMode != PARSE_ERROR)
215 			    parsePY->iMode = PARSE_ABBR;
216 		    }
217 		    else {	//�ض��Ƿָ���
218 			strP++;
219 			bSeperator = True;
220 			parsePY->strPYParsed[parsePY->iHZCount][0] = PY_SEPERATOR;
221 			parsePY->strPYParsed[parsePY->iHZCount][1] = '\0';
222 			parsePY->strMap[parsePY->iHZCount][0] = '0';
223 			parsePY->strMap[parsePY->iHZCount][1] = '0';
224 			parsePY->strMap[parsePY->iHZCount][2] = '\0';
225 		    }
226 		}
227 	    }
228 	} while (*strP);
229     }
230 
231     if (strPY[strlen (strPY) - 1] == PY_SEPERATOR && !bSP)
232 	parsePY->iHZCount++;
233 
234     if (parsePY->iMode != PARSE_ERROR) {
235 	parsePY->iMode = parsePY->iMode & PARSE_ABBR;
236 	if (parsePY->iHZCount > 1)
237 	    parsePY->iMode = parsePY->iMode | PARSE_PHRASE;
238 	else
239 	    parsePY->iMode = parsePY->iMode | PARSE_SINGLEHZ;
240     }
241 }
242 
243 /*
244  * ��һ��ƴ��(������Ϊ��ĸ����ĸ)ת��Ϊƴ��ӳ��
245  * ����TrueΪת���ɹ�������ΪFalse(һ������ΪstrPY����һ����׼��ƴ��)
246  */
MapPY(char * strPY,char strMap[3],PYPARSEINPUTMODE mode)247 Bool MapPY (char *strPY, char strMap[3], PYPARSEINPUTMODE mode)
248 {
249     char            str[5];
250     int             iIndex;
251 
252     //���⴦��eng
253     if (!strcmp (strPY, "eng") && MHPY_C[1].bMode) {
254 	strcpy (strMap, "X0");
255 	return True;
256     }
257 
258     strMap[2] = '\0';
259     iIndex = IsSyllabary (strPY, 0);
260     if (-1 != iIndex) {
261 	strMap[0] = syllabaryMapTable[iIndex].cMap;
262 	strMap[1] = mode;
263 	return True;
264     }
265     iIndex = IsConsonant (strPY, 0);
266 
267     if (-1 != iIndex) {
268 	strMap[0] = mode;
269 	strMap[1] = consonantMapTable[iIndex].cMap;
270 	return True;
271     }
272 
273     str[0] = strPY[0];
274     str[1] = '\0';
275 
276     if (strPY[1] == 'h' || strPY[1] == 'g') {
277 	str[0] = strPY[0];
278 	str[1] = strPY[1];
279 	str[2] = '\0';
280 	iIndex = IsSyllabary (str, 0);
281 	strMap[0] = consonantMapTable[iIndex].cMap;
282 	iIndex = IsConsonant (strPY + 2, 0);
283 	strMap[1] = consonantMapTable[iIndex].cMap;
284     }
285     else {
286 	str[0] = strPY[0];
287 	str[1] = '\0';
288 	iIndex = IsSyllabary (str, 0);
289 	if (iIndex == -1)
290 	    return False;
291 	strMap[0] = consonantMapTable[iIndex].cMap;
292 	iIndex = IsConsonant (strPY + 1, 0);
293 	if (iIndex == -1)
294 	    return False;
295 	strMap[1] = consonantMapTable[iIndex].cMap;
296     }
297 
298     return True;
299 }
300 
301 /*
302  * ��һ��������ƴ��ӳ��ת��Ϊƴ��������False��ʾʧ�ܣ�
303  * һ��ԭ����ƴ��ӳ�䲻��ȷ
304  */
MapToPY(char strMap[3],char * strPY)305 Bool MapToPY (char strMap[3], char *strPY)
306 {
307     int             i;
308 
309     strPY[0] = '\0';
310     if (strMap[0] != '0') {
311 	i = 0;
312 	while (syllabaryMapTable[i].cMap) {
313 	    if (syllabaryMapTable[i].cMap == strMap[0]) {
314 		strcpy (strPY, syllabaryMapTable[i].strPY);
315 		break;
316 	    }
317 	    i++;
318 	}
319 	if (!strlen (strPY))
320 	    return False;
321     }
322 
323     if (strMap[1] != '0') {
324 	i = 0;
325 	while (consonantMapTable[i].cMap) {
326 	    if (consonantMapTable[i].cMap == strMap[1]) {
327 		strcat (strPY, consonantMapTable[i].strPY);
328 		return True;
329 	    }
330 	    i++;
331 	}
332     }
333 
334     return False;
335 }
336 
337 /*
338  * �Ƚ�һλƴ��ӳ��
339  * 0��ʾ���
340  * bָʾ����ĸ������ĸ��True��ʾ��ĸ
341  */
Cmp1Map(char map1,char map2,Bool b)342 int Cmp1Map (char map1, char map2, Bool b)
343 {
344     int             iVal1, iVal2;
345 
346     if (map2 == '0' || map1 == '0') {
347 	if (map1 == ' ' || map2 == ' ' || !bFullPY || bSP)
348 	    return 0;
349     }
350     else {
351 	if (b) {
352 	    iVal1 = GetMHIndex_S (map1);
353 	    iVal2 = GetMHIndex_S (map2);
354 	}
355 	else {
356 	    iVal1 = GetMHIndex_C (map1);
357 	    iVal2 = GetMHIndex_C (map2);
358 	}
359 	if (iVal1 == iVal2)
360 	    if (iVal1 >= 0)
361 		return 0;
362     }
363 
364     return (map1 - map2);
365 }
366 
367 /*
368  * �Ƚ���λƴ��ӳ��
369  * 0��ʾ���
370  * >0��ʾǰ�ߴ�
371  * <0��ʾ���ߴ�
372  */
Cmp2Map(char map1[3],char map2[3])373 int Cmp2Map (char map1[3], char map2[3])
374 {
375     int             i;
376 
377     i = Cmp1Map (map1[0], map2[0], True);
378     if (i)
379 	return i;
380 
381     return Cmp1Map (map1[1], map2[1], False);
382 }
383 
384 /*
385  * �ж�strMap2�Ƿ���strMap1��ƥ��
386  * �� ����ֵΪ0
387  * �� ����ֵ��Ϊ0
388  * *iMatchedLength ��¼�˶����ܹ�ƥ��ij���
389  */
CmpMap(char * strMap1,char * strMap2,int * iMatchedLength)390 int CmpMap (char *strMap1, char *strMap2, int *iMatchedLength)
391 {
392     int             val;
393 
394     *iMatchedLength = 0;
395     for (;;) {
396 	if (!strMap2[*iMatchedLength])
397 	    return (strMap1[*iMatchedLength] - strMap2[*iMatchedLength]);
398 	val = Cmp1Map (strMap1[*iMatchedLength], strMap2[*iMatchedLength], (*iMatchedLength + 1) % 2);
399 	if (val)
400 	    return Cmp1Map (strMap1[*iMatchedLength], strMap2[*iMatchedLength], (*iMatchedLength + 1) % 2);
401 	(*iMatchedLength)++;
402     }
403 
404     return 0;
405 }
406