1
/* Copyright (C) 2006-2011 Edward Der-Hua Liu, Hsin-Chu, Taiwan
3
* This library is free software; you can redistribute it and/or
4
* modify it under the terms of the GNU Lesser General Public
5
* License as published by the Free Software Foundation; either
6
* version 2.1 of the License, or (at your option) any later version.
8
* This library is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public
14
* License along with this library; if not, write to the Free Software
15
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
#include <sys/types.h>
22
#include <sys/param.h>
28
#include "hime-endian.h"
29
#include "hime-version.h"
35
char *skip_spc(char *s)
37
while ((*s==' ' || *s=='\t') && *s) s++;
43
while (*s!=' ' && *s!='\t' && *s) s++;
47
void del_nl_spc(char *s)
56
while (*t=='\n' || *t==' ' || (*t=='\t' && t > s))
63
void get_line(char *tt)
66
myfgets((char *)tt, 512, fr);
73
if (tt[0]=='#' || strlen(tt) < 3)
80
void cmd_arg(char *s, char **cmd, char **arg)
106
if ((p=strchr(t, '\t')))
112
int sequ(char *s, char *t)
114
return (!strcmp(s,t));
130
#define MAX_K (500000)
133
ITEM2_64 itar64[MAX_K];
136
ITEM64 itout64[MAX_K];
139
int qcmp(const void *aa, const void *bb)
141
ITEM2 *a = (ITEM2 *)aa, *b = (ITEM2 *) bb;
143
if (a->key > b->key) return 1;
144
if (a->key < b->key) return -1;
146
return a->oseq - b->oseq;
150
int qcmp_64(const void *aa, const void *bb)
152
ITEM2_64 *a = (ITEM2_64 *)aa, *b = (ITEM2_64 *) bb;
154
if (a->key > b->key) return 1;
155
if (a->key < b->key) return -1;
157
return a->oseq - b->oseq;
161
#define mtolower(ch) (ch>='A'&&ch<='Z'?ch+0x20:ch)
163
static char kno[128];
165
int main(int argc, char **argv)
172
char kname[128][CH_SZ];
175
gtab_idx1_t idx1[256];
177
int *phridx=NULL, phr_cou=0;
181
gtk_init(&argc, &argv);
183
INMD tinmd, *inp = &tinmd, *cur_inmd = &tinmd;
186
dbg("\thime-gtab-merge for hime " HIME_VERSION "\n");
187
p_err("%s input_file.gtab phrase_file.append final-output.gtab", argv[0]);
190
if ((fr=fopen(argv[1], "rb"))==NULL)
191
p_err("cannot err open %s", argv[1]);
195
inp->tbl64 = itout64;
198
fread(&th,1, sizeof(th), fr);
200
swap_byte_4(&th.version);
201
swap_byte_4(&th.flag);
202
swap_byte_4(&th.space_style);
203
swap_byte_4(&th.KeyS);
204
swap_byte_4(&th.MaxPress);
205
swap_byte_4(&th.M_DUP_SEL);
206
swap_byte_4(&th.DefC);
207
for(i=0; i <= KeyNum; i++)
208
swap_byte_4(&idx1[i]);
211
dbg("keys %d\n",KeyNum);
215
inp->keybits = th.keybits;
216
dbg("keybits:%d\n", th.keybits);
218
if (th.MaxPress*th.keybits > 32) {
219
inp->max_keyN = 64 / th.keybits;
220
key64 = inp->key64 = TRUE;
221
dbg("it's a 64-bit .gtab\n");
223
inp->max_keyN = 32 / th.keybits;
224
key64 = inp->key64 = FALSE;
227
inp->last_k_bitn = (((inp->key64 ? 64:32) / inp->keybits) - 1) * inp->keybits;
228
dbg("inp->key64:%d\n", inp->key64);
230
u_int64_t keymask = KEY_MASK;
232
fread(keymap, 1, th.KeyS, fr);
233
fread(kname, CH_SZ, th.KeyS, fr);
234
fread(idx1, sizeof(gtab_idx1_t), KeyNum+1, fr);
236
for(i=0; i < th.KeyS; i++) {
240
for(i=0; i < th.DefC; i++) {
245
fread(&it64, sizeof(ITEM64), 1, fr);
247
memcpy(itar64[i].ch, it64.ch, sizeof(it64.ch));
248
memcpy(&itar64[i].key, it64.key, sizeof(it64.key));
251
fread(&it, sizeof(ITEM), 1, fr);
253
memcpy(itar[i].ch, it.ch, sizeof(it.ch));
254
memcpy(&itar[i].key, it.key, sizeof(it.key));
259
fread(&phr_cou, sizeof(int), 1, fr);
263
phridx = tmalloc(int, phr_cou+1);
264
fread(phridx, sizeof(int), phr_cou, fr);
266
prbf_cou = phridx[phr_cou];
267
phrbuf = (char *)malloc(prbf_cou);
268
fread(phrbuf, 1, prbf_cou, fr);
272
dbg("input phr_cou %d DefC:%d prbf_cou:%d\n", phr_cou, chno, prbf_cou);
274
if ((fr=fopen(argv[2], "rb"))==NULL)
275
p_err("cannot err open %s", argv[2]);
277
skip_utf8_sigature(fr);
285
cmd_arg(tt, (char **)&cmd, (char **)&arg);
286
if (!cmd[0] || !arg[0])
293
if (len > inp->max_keyN)
294
p_err("%d: only <= %d keys is allowed '%s' %s", lineno, inp->max_keyN, cmd, tt);
298
int key = BITON(th.flag, FLAG_KEEP_KEY_CASE) ?
299
cmd[i] : mtolower(cmd[i]);
302
kk|=(u_int64_t)k << ( LAST_K_bitN - i*cur_inmd->keybits);
306
memcpy(&itar64[chno].key, &kk, 8);
307
itar64[chno].oseq=chno;
312
memcpy(&itar[chno].key, &key32, 4);
313
itar[chno].oseq=chno;
316
if ((len=strlen(arg)) <= CH_SZ && (arg[0] & 0x80)) {
319
bzero(out, sizeof(out));
320
memcpy(out, arg, len);
323
bchcpy(itar64[chno].ch, out);
325
bchcpy(itar[chno].ch, out);
329
itar64[chno].ch[0]=phr_cou>>16;
330
itar64[chno].ch[1]=(phr_cou >> 8) & 0xff;
331
itar64[chno].ch[2]=phr_cou&0xff;
334
itar[chno].ch[0]=phr_cou>>16;
335
itar[chno].ch[1]=(phr_cou >> 8) & 0xff;
336
itar[chno].ch[2]=phr_cou&0xff;
339
if (len > MAX_CIN_PHR)
340
p_err("phrase too long: %s max:%d bytes\n", arg, MAX_CIN_PHR);
342
phridx = trealloc(phridx, int, phr_cou+1);
343
phridx[phr_cou++]=prbf_cou;
344
phrbuf = (char *)realloc(phrbuf, prbf_cou + len + 1);
345
strcpy(&phrbuf[prbf_cou],arg);
346
// printf("phrase:%d len:%d'%s'\n", phr_cou, len, arg);
357
cur_inmd->DefChars = chno;
360
_sort(itar64,chno,sizeof(ITEM2_64),qcmp_64);
362
_sort(itar,chno,sizeof(ITEM2),qcmp);
366
memcpy(&itout64[i],&itar64[i],sizeof(ITEM64));
369
memcpy(&itout[i],&itar[i],sizeof(ITEM));
373
bzero(def1,sizeof(def1));
374
bzero(idx1,sizeof(idx1));
377
for(i=0; i<chno; i++) {
378
u_int64_t key = CONVT2(cur_inmd, i);
380
dbg("%d] %llx %d %d %d %d zzz\n", i, key,
381
cur_inmd->tbl[i].ch[0], cur_inmd->tbl[i].ch[1],
382
cur_inmd->tbl[i].ch[2], cur_inmd->tbl[i].ch[3]);
384
int kk = (key>>LAST_K_bitN) & keymask;
387
idx1[kk]=(gtab_idx1_t)i;
393
for(i=KeyNum-1;i>0;i--)
394
if (!def1[i]) idx1[i]=idx1[i+1];
396
if ((fw=fopen(argv[3],"wb"))==NULL) {
397
p_err("Cannot create %s", argv[3]);
400
printf("Defined Characters:%d\n", chno);
403
swap_byte_4(&th.version);
404
swap_byte_4(&th.flag);
405
swap_byte_4(&th.space_style);
406
swap_byte_4(&th.KeyS);
407
swap_byte_4(&th.MaxPress);
408
swap_byte_4(&th.M_DUP_SEL);
409
swap_byte_4(&th.DefC);
410
for(i=0; i <= KeyNum; i++)
411
swap_byte_4(&idx1[i]);
413
fwrite(&th,1,sizeof(th),fw);
414
fwrite(keymap, 1, KeyNum, fw);
415
fwrite(kname, CH_SZ, KeyNum, fw);
416
fwrite(idx1, sizeof(gtab_idx1_t), KeyNum+1, fw);
420
for(i=0; i < chno; i++) {
421
swap_byte_8(&itout64[i].key);
424
fwrite(itout64, sizeof(ITEM64), chno, fw);
426
for(i=0; i < 100; i++)
427
dbg("%d] %c%c%c\n", i, itout64[i].ch[0], itout64[i].ch[1], itout64[i].ch[2]);
432
for(i=0; i < chno; i++) {
433
swap_byte_4(&itout[i].key);
436
fwrite(itout, sizeof(ITEM), chno, fw);
440
printf("phrase count:%d\n", phr_cou);
441
phridx[phr_cou++]=prbf_cou;
443
int ophr_cou = phr_cou;
445
for(i=0; i < phr_cou; i++)
446
swap_byte_4(&phridx[i]);
447
swap_byte_4(&phr_cou);
449
fwrite(&phr_cou, sizeof(int), 1, fw);
450
fwrite(phridx, sizeof(int), ophr_cou, fw);
451
fwrite(phrbuf,1,prbf_cou,fw);