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 <cstring>
11 #include <stdio.h>
12 #include <sys/stat.h>
13 #include <limits.h>
14 #include <ctype.h>
15 #include <unistd.h> // for access()
16 #include <cstring>
17
18 #include "main.h"
19 #include "sp.h"
20 #include "pyMapTable.h"
21 #include "pyParser.h"
22
23
24 SP_C SPMap_C[] = {
25 {"ai", 'l'}
26 ,
27 {"an", 'j'}
28 ,
29 {"ang", 'h'}
30 ,
31 {"ao", 'k'}
32 ,
33 {"ei", 'z'}
34 ,
35 {"en", 'f'}
36 ,
37 {"eng", 'g'}
38 ,
39 {"er", 'r'}
40 ,
41 {"ia", 'w'}
42 ,
43 {"ian", 'm'}
44 ,
45 {"iang", 'd'}
46 ,
47 {"iao", 'c'}
48 ,
49 {"ie", 'x'}
50 ,
51 {"in", 'n'}
52 ,
53 {"ing", 'y'}
54 ,
55 {"iong", 's'}
56 ,
57 {"iu", 'q'}
58 ,
59 {"ng", 'g'}
60 ,
61 {"ong", 's'}
62 ,
63 {"ou", 'b'}
64 ,
65 {"ua", 'w'}
66 ,
67 {"uai", 'y'}
68 ,
69 {"uan", 'r'}
70 ,
71 {"uang", 'd'}
72 ,
73 {"ue", 't'}
74 ,
75 {"ui", 'v'}
76 ,
77 {"un", 'p'}
78 ,
79 {"uo", 'o'}
80 ,
81 {"ve", 't'}
82 ,
83
84 {"\0", '\0'}
85 };
86
87 SP_S SPMap_S[] = {
88 {"ch", 'i'}
89 ,
90 {"sh", 'u'}
91 ,
92 {"zh", 'v'}
93 ,
94 {"\0", '\0'}
95 };
96
97 Bool bSP_UseSemicolon = False;
98 Bool bSP = False;
99 char cNonS = 'o';
100
LoadSPData(void)101 void LoadSPData (void)
102 {
103 FILE *fp;
104 char strPath[PATH_MAX];
105 char str[20], strS[5], *pstr;
106 int i;
107
108 strcpy (strPath, (char *) getenv ("HOME"));
109 strcat (strPath, "/.fcim/");
110
111 if (access (strPath, 0))
112 mkdir (strPath, S_IRWXU);
113
114 strcat (strPath, "sp.dat");
115 fp = fopen (strPath, "rt");
116
117 if (!fp)
118 return;
119
120 while (1) {
121 if (!fgets (str, 20, fp))
122 break;
123
124 i = strlen (str) - 1;
125 while (str[i] == ' ' || str[i] == '\n')
126 str[i--] = '\0';
127 pstr = str;
128 if (*pstr == ' ' || *pstr == '\t')
129 pstr++;
130 if (!strlen (pstr) || pstr[0] == '#')
131 continue;
132
133 if (pstr[0] == '=') //������ĸ����
134 cNonS = tolower (pstr[1]);
135 else {
136 i = 0;
137 while (pstr[i]) {
138 if (pstr[i] == '=') {
139 strncpy (strS, pstr, i);
140 strS[i] = '\0';
141
142 pstr += i;
143 i = GetSPIndexQP_S (strS);
144 if (i != -1)
145 SPMap_S[i].cJP = tolower (pstr[1]);
146 else {
147 i = GetSPIndexQP_C (strS);
148 if (i != -1)
149 SPMap_C[i].cJP = tolower (pstr[1]);
150 }
151 break;
152 }
153 i++;
154 }
155 }
156 }
157
158 fclose (fp);
159
160 //�����ж��Ƿ�ʹ����';'
161 i = 0;
162 while (SPMap_C[i].strQP[0]) {
163 if (SPMap_C[i++].cJP == ';')
164 bSP_UseSemicolon = True;
165 }
166 if (!bSP_UseSemicolon) {
167 i = 0;
168 while (SPMap_S[i].strQP[0]) {
169 if (SPMap_S[i++].cJP == ';')
170 bSP_UseSemicolon = True;
171 }
172 }
173 if (!bSP_UseSemicolon) {
174 if (cNonS == ';')
175 bSP_UseSemicolon = True;
176 }
177 }
178
179 /*
180 * ��һ��ȫƴת��Ϊ˫ƴ
181 * strQPֻ����һ������ȫƴ����������������
182 */
183 /*void QP2SP (char *strQP, char *strSP)
184 {
185 //IsConsonant
186 int iIndex;
187
188 strSP[2] = '\0';
189 //"ang"��Ψһһ���ɵ���ʹ�á���Ϊ3��ƴ������������
190 if (!strcmp (strQP, "ang")) {
191 strSP[0] = cNonS;
192 strSP[1] = SPMap_C[GetSPIndexQP_C (strQP)].cJP;
193 return;
194 }
195 if (strlen (strQP) == 1) {
196 strSP[0] = cNonS;
197 strSP[1] = *strQP;
198 return;
199 }
200 if (strlen (strQP) == 2) {
201 iIndex = GetSPIndexQP_C (strQP);
202 if (iIndex == -1)
203 strcpy (strSP, strQP);
204 else {
205 strSP[0] = cNonS;
206 strSP[1] = SPMap_C[iIndex].cJP;
207 }
208
209 return;
210 }
211
212 iIndex = IsSyllabary (strQP, True);
213
214 strQP += strlen (syllabaryMapTable[iIndex].strPY);
215 if (*strQP) {
216 if (strlen (syllabaryMapTable[iIndex].strPY) == 1)
217 strSP[0] = syllabaryMapTable[iIndex].strPY[0];
218 else
219 strSP[0] = SPMap_S[GetSPIndexQP_S (syllabaryMapTable[iIndex].strPY)].cJP;
220 if (strlen (strQP) == 1)
221 strSP[1] = strQP[0];
222 else
223 strSP[1] = SPMap_C[GetSPIndexQP_C (strQP)].cJP;
224 }
225 else {
226 strSP[0] = cNonS;
227 strSP[1] = SPMap_C[GetSPIndexQP_C (syllabaryMapTable[iIndex].strPY)].cJP;
228 }
229 }
230 */
231 /*
232 * �˴�ֻת������˫ƴ�����Ҳ�������
233 */
SP2QP(char * strSP,char * strQP)234 void SP2QP (char *strSP, char *strQP)
235 {
236 int iIndex1 = 0, iIndex2 = 0;
237 char strTmp[2];
238 char str_QP[MAX_PY_LENGTH + 1];
239
240 strTmp[1] = '\0';
241 strQP[0] = '\0';
242
243 if (strSP[0] != cNonS) {
244 iIndex1 = GetSPIndexJP_S (*strSP);
245 if (iIndex1 == -1) {
246 strTmp[0] = strSP[0];
247 strcat (strQP, strTmp);
248 }
249 else
250 strcat (strQP, SPMap_S[iIndex1].strQP);
251 }
252 else if (!strSP[1])
253 strcpy (strQP, strSP);
254
255 if (strSP[1]) {
256 iIndex2 = -1;
257 while (1) {
258 iIndex2 = GetSPIndexJP_C (strSP[1], iIndex2 + 1);
259 if (iIndex2 == -1) {
260 strTmp[0] = strSP[1];
261 strcat (strQP, strTmp);
262 break;
263 }
264
265 strcpy (str_QP, strQP);
266 strcat (strQP, SPMap_C[iIndex2].strQP);
267 if (FindPYFAIndex (strQP, False) != -1)
268 break;
269
270 strcpy (strQP, str_QP);
271 }
272 }
273
274 if (FindPYFAIndex (strQP, False) != -1)
275 iIndex2 = 0; //��ֻ�ǽ�iIndex2��Ϊ��-1,���������ж�
276
277 strTmp[0] = strSP[0];
278 strTmp[1] = '\0';
279 if ((iIndex1 == -1 && !(IsSyllabary (strTmp, 0))) || iIndex2 == -1) {
280 iIndex1 = FindPYFAIndex (strSP, False);
281 if (iIndex1 != -1)
282 strcpy (strQP, strSP);
283 }
284 }
285
GetSPIndexQP_S(char * str)286 int GetSPIndexQP_S (char *str)
287 {
288 int i;
289
290 i = 0;
291 while (SPMap_S[i].strQP[0]) {
292 if (!strcmp (str, SPMap_S[i].strQP))
293 return i;
294
295 i++;
296 }
297
298 return -1;
299 }
300
GetSPIndexQP_C(char * str)301 int GetSPIndexQP_C (char *str)
302 {
303 int i;
304
305 i = 0;
306 while (SPMap_C[i].strQP[0]) {
307 if (!strcmp (str, SPMap_C[i].strQP))
308 return i;
309 i++;
310 }
311
312 return -1;
313 }
314
GetSPIndexJP_S(char c)315 int GetSPIndexJP_S (char c)
316 {
317 int i;
318
319 i = 0;
320 while (SPMap_S[i].strQP[0]) {
321 if (c == SPMap_S[i].cJP)
322 return i;
323
324 i++;
325 }
326
327 return -1;
328 }
329
GetSPIndexJP_C(char c,int iStart)330 int GetSPIndexJP_C (char c, int iStart)
331 {
332 int i;
333
334 i = iStart;
335 while (SPMap_C[i].strQP[0]) {
336 if (c == SPMap_C[i].cJP)
337 return i;
338 i++;
339 }
340
341 return -1;
342 }
343
SwitchSP(void)344 void SwitchSP (void)
345 {
346 bSP = !bSP;
347 // DisplayMainWindow ();
348
349 if (bSP)
350 LoadSPData ();
351 }
352
SPInit(void)353 void SPInit (void)
354 {
355 bSP = True;
356 bSingleHZMode = False;
357
358 LoadSPData ();
359 }
360