-
Notifications
You must be signed in to change notification settings - Fork 1
/
gtab-merge.cpp
449 lines (353 loc) · 8.57 KB
/
gtab-merge.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
Copyright (C) 2006-2008 Edward Der-Hua Liu, Hsin-Chu, Taiwan
*/
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#if FREEBSD
#include <sys/param.h>
#include <sys/stat.h>
#endif
#include <string.h>
#include "gcin.h"
#include "gtab.h"
#include "gcin-endian.h"
#include "gcin-version.h"
FILE *fr, *fw;
int lineno;
char *skip_spc(char *s)
{
while ((*s==' ' || *s=='\t') && *s) s++;
return s;
}
char *to_spc(char *s)
{
while (*s!=' ' && *s!='\t' && *s) s++;
return s;
}
void del_nl_spc(char *s)
{
char *t;
int len=strlen(s);
if (!*s) return;
t=s+len-1;
while (*t=='\n' || *t==' ' || (*t=='\t' && t > s))
t--;
*(t+1)=0;
}
void get_line(char *tt)
{
while (!feof(fr)) {
myfgets((char *)tt, 512, fr);
lineno++;
int len=strlen(tt);
if (tt[len-1]=='\n')
tt[len-1] = 0;
if (tt[0]=='#' || strlen(tt) < 3)
continue;
else
break;
}
}
void cmd_arg(char *s, char **cmd, char **arg)
{
char *t;
get_line(s);
if (!*s) {
*cmd=*arg=s;
return;
}
s=skip_spc(s);
t=to_spc(s);
*cmd=s;
if (!(*t)) {
*arg=t;
return;
}
*t=0;
t++;
t=skip_spc(t);
del_nl_spc(t);
char *p;
if ((p=strchr(t, '\t')))
*p = 0;
*arg=t;
}
int sequ(char *s, char *t)
{
return (!strcmp(s,t));
}
typedef struct {
u_int key;
u_char ch[CH_SZ];
int oseq;
} ITEM2;
typedef struct {
u_int64_t key;
u_char ch[CH_SZ];
int oseq;
} ITEM2_64;
#define MAX_K (500000)
ITEM2 itar[MAX_K];
ITEM2_64 itar64[MAX_K];
ITEM itout[MAX_K];
ITEM64 itout64[MAX_K];
int qcmp(const void *aa, const void *bb)
{
ITEM2 *a = (ITEM2 *)aa, *b = (ITEM2 *) bb;
if (a->key > b->key) return 1;
if (a->key < b->key) return -1;
return a->oseq - b->oseq;
}
int qcmp_64(const void *aa, const void *bb)
{
ITEM2_64 *a = (ITEM2_64 *)aa, *b = (ITEM2_64 *) bb;
if (a->key > b->key) return 1;
if (a->key < b->key) return -1;
return a->oseq - b->oseq;
}
#define mtolower(ch) (ch>='A'&&ch<='Z'?ch+0x20:ch)
static char kno[128];
#if WIN32
void init_gcin_program_files();
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
#endif
int main(int argc, char **argv)
{
int i;
char tt[512];
char *cmd, *arg;
struct TableHead th;
int KeyNum;
char kname[128][CH_SZ];
char keymap[128];
int chno;
gtab_idx1_t idx1[256];
char def1[256];
int *phridx=NULL, phr_cou=0;
char *phrbuf = NULL;
int prbf_cou=0;
gtk_init(&argc, &argv);
INMD tinmd, *inp = &tinmd, *cur_inmd = &tinmd;
if (argc != 4) {
dbg("\tgtab-merge for gcin " GCIN_VERSION "\n");
p_err("%s input_file.gtab phrase_file.append final-output.gtab", argv[0]);
}
if ((fr=fopen(argv[1], "rb"))==NULL)
p_err("cannot err open %s", argv[1]);
gboolean key64=64;
inp->tbl64 = itout64;
inp->tbl = itout;
fread(&th,1, sizeof(th), fr);
#if NEED_SWAP
swap_byte_4(&th.version);
swap_byte_4(&th.flag);
swap_byte_4(&th.space_style);
swap_byte_4(&th.KeyS);
swap_byte_4(&th.MaxPress);
swap_byte_4(&th.M_DUP_SEL);
swap_byte_4(&th.DefC);
for(i=0; i <= KeyNum; i++)
swap_byte_4(&idx1[i]);
#endif
KeyNum = th.KeyS;
dbg("keys %d\n",KeyNum);
if (!th.keybits)
th.keybits = 6;
inp->keybits = th.keybits;
dbg("keybits:%d\n", th.keybits);
if (th.MaxPress*th.keybits > 32) {
inp->max_keyN = 64 / th.keybits;
key64 = inp->key64 = TRUE;
dbg("it's a 64-bit .gtab\n");
} else {
inp->max_keyN = 32 / th.keybits;
key64 = inp->key64 = FALSE;
}
inp->last_k_bitn = (((inp->key64 ? 64:32) / inp->keybits) - 1) * inp->keybits;
dbg("inp->key64:%d\n", inp->key64);
u_int64_t keymask = KEY_MASK;
fread(keymap, 1, th.KeyS, fr);
fread(kname, CH_SZ, th.KeyS, fr);
fread(idx1, sizeof(gtab_idx1_t), KeyNum+1, fr);
for(i=0; i < th.KeyS; i++) {
kno[keymap[i]] = i;
}
for(i=0; i < th.DefC; i++) {
ITEM it;
ITEM64 it64;
if (key64) {
fread(&it64, sizeof(ITEM64), 1, fr);
itar64[i].oseq = i;
memcpy(itar64[i].ch, it64.ch, sizeof(it64.ch));
memcpy(&itar64[i].key, it64.key, sizeof(it64.key));
}
else {
fread(&it, sizeof(ITEM), 1, fr);
itar[i].oseq = i;
memcpy(itar[i].ch, it.ch, sizeof(it.ch));
memcpy(&itar[i].key, it.key, sizeof(it.key));
}
}
chno = th.DefC;
fread(&phr_cou, sizeof(int), 1, fr);
if (phr_cou) {
phridx = tmalloc(int, phr_cou+1);
fread(phridx, sizeof(int), phr_cou, fr);
phr_cou--;
prbf_cou = phridx[phr_cou];
phrbuf = (char *)malloc(prbf_cou);
fread(phrbuf, 1, prbf_cou, fr);
}
fclose(fr);
dbg("input phr_cou %d DefC:%d prbf_cou:%d\n", phr_cou, chno, prbf_cou);
if ((fr=fopen(argv[2], "rb"))==NULL)
p_err("cannot err open %s", argv[2]);
skip_utf8_sigature(fr);
puts("char def");
while (!feof(fr)) {
int len;
u_int64_t kk;
int k;
cmd_arg(tt, (char **)&cmd, (char **)&arg);
if (!cmd[0] || !arg[0])
continue;
if (cmd[0]=='%')
continue;
len=strlen(cmd);
if (len > inp->max_keyN)
p_err("%d: only <= %d keys is allowed '%s' %s", lineno, inp->max_keyN, cmd, tt);
kk=0;
for(i=0;i<len;i++) {
int key = BITON(th.flag, FLAG_KEEP_KEY_CASE) ?
cmd[i] : mtolower(cmd[i]);
k=kno[key];
kk|=(u_int64_t)k << ( LAST_K_bitN - i*cur_inmd->keybits);
}
if (key64) {
memcpy(&itar64[chno].key, &kk, 8);
itar64[chno].oseq=chno;
}
else {
u_int key32 = kk;
memcpy(&itar[chno].key, &key32, 4);
itar[chno].oseq=chno;
}
if ((len=strlen(arg)) <= CH_SZ && (arg[0] & 0x80)) {
char out[CH_SZ+1];
bzero(out, sizeof(out));
memcpy(out, arg, len);
if (key64)
bchcpy(itar64[chno].ch, out);
else
bchcpy(itar[chno].ch, out);
} else {
if (key64) {
itar64[chno].ch[0]=phr_cou>>16;
itar64[chno].ch[1]=(phr_cou >> 8) & 0xff;
itar64[chno].ch[2]=phr_cou&0xff;
}
else {
itar[chno].ch[0]=phr_cou>>16;
itar[chno].ch[1]=(phr_cou >> 8) & 0xff;
itar[chno].ch[2]=phr_cou&0xff;
}
if (len > MAX_CIN_PHR)
p_err("phrase too long: %s max:%d bytes\n", arg, MAX_CIN_PHR);
phridx = trealloc(phridx, int, phr_cou+1);
phridx[phr_cou++]=prbf_cou;
phrbuf = (char *)realloc(phrbuf, prbf_cou + len + 1);
strcpy(&phrbuf[prbf_cou],arg);
// printf("phrase:%d len:%d'%s'\n", phr_cou, len, arg);
prbf_cou+=len;
}
chno++;
}
fclose(fr);
#define _sort qsort
th.DefC=chno;
cur_inmd->DefChars = chno;
if (key64)
_sort(itar64,chno,sizeof(ITEM2_64),qcmp_64);
else
_sort(itar,chno,sizeof(ITEM2),qcmp);
if (key64) {
for(i=0;i<chno;i++)
memcpy(&itout64[i],&itar64[i],sizeof(ITEM64));
} else {
for(i=0;i<chno;i++)
memcpy(&itout[i],&itar[i],sizeof(ITEM));
}
bzero(def1,sizeof(def1));
bzero(idx1,sizeof(idx1));
for(i=0; i<chno; i++) {
u_int64_t key = CONVT2(cur_inmd, i);
#if 0
dbg("%d] %llx %d %d %d %d zzz\n", i, key,
cur_inmd->tbl[i].ch[0], cur_inmd->tbl[i].ch[1],
cur_inmd->tbl[i].ch[2], cur_inmd->tbl[i].ch[3]);
#endif
int kk = (key>>LAST_K_bitN) & keymask;
if (!def1[kk]) {
idx1[kk]=(gtab_idx1_t)i;
def1[kk]=1;
}
}
idx1[KeyNum]=chno;
for(i=KeyNum-1;i>0;i--)
if (!def1[i]) idx1[i]=idx1[i+1];
if ((fw=fopen(argv[3],"wb"))==NULL) {
p_err("Cannot create %s", argv[3]);
}
printf("Defined Characters:%d\n", chno);
#if NEED_SWAP
swap_byte_4(&th.version);
swap_byte_4(&th.flag);
swap_byte_4(&th.space_style);
swap_byte_4(&th.KeyS);
swap_byte_4(&th.MaxPress);
swap_byte_4(&th.M_DUP_SEL);
swap_byte_4(&th.DefC);
for(i=0; i <= KeyNum; i++)
swap_byte_4(&idx1[i]);
#endif
fwrite(&th,1,sizeof(th),fw);
fwrite(keymap, 1, KeyNum, fw);
fwrite(kname, CH_SZ, KeyNum, fw);
fwrite(idx1, sizeof(gtab_idx1_t), KeyNum+1, fw);
if (key64) {
#if NEED_SWAP
for(i=0; i < chno; i++) {
swap_byte_8(&itout64[i].key);
}
#endif
fwrite(itout64, sizeof(ITEM64), chno, fw);
#if 0
for(i=0; i < 100; i++)
dbg("%d] %c%c%c\n", i, itout64[i].ch[0], itout64[i].ch[1], itout64[i].ch[2]);
#endif
}
else {
#if NEED_SWAP
for(i=0; i < chno; i++) {
swap_byte_4(&itout[i].key);
}
#endif
fwrite(itout, sizeof(ITEM), chno, fw);
}
if (phr_cou) {
printf("phrase count:%d\n", phr_cou);
phridx[phr_cou++]=prbf_cou;
int ophr_cou = phr_cou;
#if NEED_SWAP
for(i=0; i < phr_cou; i++)
swap_byte_4(&phridx[i]);
swap_byte_4(&phr_cou);
#endif
fwrite(&phr_cou, sizeof(int), 1, fw);
fwrite(phridx, sizeof(int), ophr_cou, fw);
fwrite(phrbuf,1,prbf_cou,fw);
}
fclose(fw);
return 0;
}