~ubuntu-branches/ubuntu/precise/hime/precise

« back to all changes in this revision

Viewing changes to src/hime-gtab-merge.c

  • Committer: Package Import Robot
  • Author(s): Yao Wei (魏銘廷)
  • Date: 2012-01-14 00:24:08 UTC
  • Revision ID: package-import@ubuntu.com-20120114002408-e79gagbeg1rt8npv
Tags: upstream-0.9.9
Import upstream version 0.9.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2006-2011 Edward Der-Hua Liu, Hsin-Chu, Taiwan
 
2
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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
 
16
 */
 
17
 
 
18
#include <stdio.h>
 
19
#include <stdarg.h>
 
20
#include <sys/types.h>
 
21
#if FREEBSD
 
22
#include <sys/param.h>
 
23
#include <sys/stat.h>
 
24
#endif
 
25
#include <string.h>
 
26
#include "hime.h"
 
27
#include "gtab.h"
 
28
#include "hime-endian.h"
 
29
#include "hime-version.h"
 
30
 
 
31
FILE *fr, *fw;
 
32
int lineno;
 
33
 
 
34
 
 
35
char *skip_spc(char *s)
 
36
{
 
37
  while ((*s==' ' || *s=='\t') && *s) s++;
 
38
   return s;
 
39
}
 
40
 
 
41
char *to_spc(char *s)
 
42
{
 
43
  while (*s!=' ' && *s!='\t' && *s) s++;
 
44
    return s;
 
45
}
 
46
 
 
47
void del_nl_spc(char *s)
 
48
{
 
49
  char *t;
 
50
 
 
51
  int len=strlen(s);
 
52
  if (!*s) return;
 
53
 
 
54
  t=s+len-1;
 
55
 
 
56
  while (*t=='\n' || *t==' ' || (*t=='\t' && t > s))
 
57
    t--;
 
58
 
 
59
  *(t+1)=0;
 
60
}
 
61
 
 
62
 
 
63
void get_line(char *tt)
 
64
{
 
65
  while (!feof(fr)) {
 
66
    myfgets((char *)tt, 512, fr);
 
67
    lineno++;
 
68
 
 
69
    int len=strlen(tt);
 
70
    if (tt[len-1]=='\n')
 
71
      tt[len-1] = 0;
 
72
 
 
73
    if (tt[0]=='#' || strlen(tt) < 3)
 
74
      continue;
 
75
    else
 
76
      break;
 
77
  }
 
78
}
 
79
 
 
80
void cmd_arg(char *s, char **cmd, char **arg)
 
81
{
 
82
  char *t;
 
83
 
 
84
  get_line(s);
 
85
 
 
86
  if (!*s) {
 
87
    *cmd=*arg=s;
 
88
    return;
 
89
  }
 
90
 
 
91
  s=skip_spc(s);
 
92
  t=to_spc(s);
 
93
  *cmd=s;
 
94
  if (!(*t)) {
 
95
    *arg=t;
 
96
    return;
 
97
  }
 
98
 
 
99
  *t=0;
 
100
  t++;
 
101
 
 
102
  t=skip_spc(t);
 
103
  del_nl_spc(t);
 
104
 
 
105
  char *p;
 
106
  if ((p=strchr(t, '\t')))
 
107
    *p = 0;
 
108
 
 
109
  *arg=t;
 
110
}
 
111
 
 
112
int sequ(char *s, char *t)
 
113
{
 
114
  return (!strcmp(s,t));
 
115
}
 
116
 
 
117
typedef struct {
 
118
  u_int key;
 
119
  u_char ch[CH_SZ];
 
120
  int oseq;
 
121
} ITEM2;
 
122
 
 
123
typedef struct {
 
124
  u_int64_t key;
 
125
  u_char ch[CH_SZ];
 
126
  int oseq;
 
127
} ITEM2_64;
 
128
 
 
129
 
 
130
#define MAX_K (500000)
 
131
 
 
132
ITEM2 itar[MAX_K];
 
133
ITEM2_64 itar64[MAX_K];
 
134
 
 
135
ITEM itout[MAX_K];
 
136
ITEM64 itout64[MAX_K];
 
137
 
 
138
 
 
139
int qcmp(const void *aa, const void *bb)
 
140
{
 
141
  ITEM2 *a = (ITEM2 *)aa, *b = (ITEM2 *) bb;
 
142
 
 
143
  if (a->key > b->key) return 1;
 
144
  if (a->key < b->key) return -1;
 
145
 
 
146
  return a->oseq - b->oseq;
 
147
}
 
148
 
 
149
 
 
150
int qcmp_64(const void *aa, const void *bb)
 
151
{
 
152
  ITEM2_64 *a = (ITEM2_64 *)aa, *b = (ITEM2_64 *) bb;
 
153
 
 
154
  if (a->key > b->key) return 1;
 
155
  if (a->key < b->key) return -1;
 
156
 
 
157
  return a->oseq - b->oseq;
 
158
}
 
159
 
 
160
 
 
161
#define mtolower(ch) (ch>='A'&&ch<='Z'?ch+0x20:ch)
 
162
 
 
163
static char kno[128];
 
164
 
 
165
int main(int argc, char **argv)
 
166
{
 
167
  int i;
 
168
  char tt[512];
 
169
  char *cmd, *arg;
 
170
  struct TableHead th;
 
171
  int KeyNum;
 
172
  char kname[128][CH_SZ];
 
173
  char keymap[128];
 
174
  int chno;
 
175
  gtab_idx1_t idx1[256];
 
176
  char def1[256];
 
177
  int *phridx=NULL, phr_cou=0;
 
178
  char *phrbuf = NULL;
 
179
  int prbf_cou=0;
 
180
 
 
181
  gtk_init(&argc, &argv);
 
182
 
 
183
  INMD tinmd, *inp = &tinmd, *cur_inmd = &tinmd;
 
184
 
 
185
  if (argc != 4) {
 
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]);
 
188
  }
 
189
 
 
190
  if ((fr=fopen(argv[1], "rb"))==NULL)
 
191
      p_err("cannot err open %s", argv[1]);
 
192
 
 
193
  gboolean key64=64;
 
194
 
 
195
  inp->tbl64 = itout64;
 
196
  inp->tbl = itout;
 
197
 
 
198
  fread(&th,1, sizeof(th), fr);
 
199
#if NEED_SWAP
 
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]);
 
209
#endif
 
210
  KeyNum = th.KeyS;
 
211
  dbg("keys %d\n",KeyNum);
 
212
 
 
213
  if (!th.keybits)
 
214
    th.keybits = 6;
 
215
  inp->keybits = th.keybits;
 
216
  dbg("keybits:%d\n", th.keybits);
 
217
 
 
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");
 
222
  } else {
 
223
    inp->max_keyN = 32 / th.keybits;
 
224
    key64 = inp->key64 = FALSE;
 
225
  }
 
226
 
 
227
  inp->last_k_bitn = (((inp->key64 ? 64:32) / inp->keybits) - 1) * inp->keybits;
 
228
  dbg("inp->key64:%d\n", inp->key64);
 
229
 
 
230
  u_int64_t keymask = KEY_MASK;
 
231
 
 
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);
 
235
 
 
236
  for(i=0; i < th.KeyS; i++) {
 
237
    kno[keymap[i]] = i;
 
238
  }
 
239
 
 
240
  for(i=0; i < th.DefC; i++) {
 
241
    ITEM it;
 
242
    ITEM64 it64;
 
243
 
 
244
    if (key64) {
 
245
      fread(&it64, sizeof(ITEM64), 1, fr);
 
246
      itar64[i].oseq = i;
 
247
      memcpy(itar64[i].ch, it64.ch, sizeof(it64.ch));
 
248
      memcpy(&itar64[i].key, it64.key, sizeof(it64.key));
 
249
    }
 
250
    else {
 
251
      fread(&it, sizeof(ITEM), 1, fr);
 
252
      itar[i].oseq = i;
 
253
      memcpy(itar[i].ch, it.ch, sizeof(it.ch));
 
254
      memcpy(&itar[i].key, it.key, sizeof(it.key));
 
255
    }
 
256
  }
 
257
 
 
258
  chno = th.DefC;
 
259
  fread(&phr_cou, sizeof(int), 1, fr);
 
260
 
 
261
 
 
262
  if (phr_cou) {
 
263
    phridx = tmalloc(int, phr_cou+1);
 
264
    fread(phridx, sizeof(int), phr_cou, fr);
 
265
    phr_cou--;
 
266
    prbf_cou = phridx[phr_cou];
 
267
    phrbuf = (char *)malloc(prbf_cou);
 
268
    fread(phrbuf, 1, prbf_cou, fr);
 
269
  }
 
270
 
 
271
  fclose(fr);
 
272
  dbg("input phr_cou %d  DefC:%d  prbf_cou:%d\n", phr_cou, chno, prbf_cou);
 
273
 
 
274
  if ((fr=fopen(argv[2], "rb"))==NULL)
 
275
      p_err("cannot err open %s", argv[2]);
 
276
 
 
277
  skip_utf8_sigature(fr);
 
278
 
 
279
  puts("char def");
 
280
  while (!feof(fr)) {
 
281
    int len;
 
282
    u_int64_t kk;
 
283
    int k;
 
284
 
 
285
    cmd_arg(tt, (char **)&cmd, (char **)&arg);
 
286
    if (!cmd[0] || !arg[0])
 
287
      continue;
 
288
    if (cmd[0]=='%')
 
289
      continue;
 
290
 
 
291
    len=strlen(cmd);
 
292
 
 
293
    if (len > inp->max_keyN)
 
294
      p_err("%d:  only <= %d keys is allowed '%s'  %s", lineno, inp->max_keyN, cmd, tt);
 
295
 
 
296
    kk=0;
 
297
    for(i=0;i<len;i++) {
 
298
      int key =  BITON(th.flag, FLAG_KEEP_KEY_CASE) ?
 
299
        cmd[i] : mtolower(cmd[i]);
 
300
 
 
301
      k=kno[key];
 
302
      kk|=(u_int64_t)k << ( LAST_K_bitN - i*cur_inmd->keybits);
 
303
    }
 
304
 
 
305
    if (key64) {
 
306
      memcpy(&itar64[chno].key, &kk, 8);
 
307
      itar64[chno].oseq=chno;
 
308
    }
 
309
    else {
 
310
      u_int key32 = kk;
 
311
 
 
312
      memcpy(&itar[chno].key, &key32, 4);
 
313
      itar[chno].oseq=chno;
 
314
    }
 
315
 
 
316
    if ((len=strlen(arg)) <= CH_SZ && (arg[0] & 0x80)) {
 
317
      char out[CH_SZ+1];
 
318
 
 
319
      bzero(out, sizeof(out));
 
320
      memcpy(out, arg, len);
 
321
 
 
322
      if (key64)
 
323
        bchcpy(itar64[chno].ch, out);
 
324
      else
 
325
        bchcpy(itar[chno].ch, out);
 
326
 
 
327
    } else {
 
328
      if (key64) {
 
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;
 
332
      }
 
333
      else {
 
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;
 
337
      }
 
338
 
 
339
      if (len > MAX_CIN_PHR)
 
340
        p_err("phrase too long: %s  max:%d bytes\n", arg, MAX_CIN_PHR);
 
341
 
 
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);
 
347
      prbf_cou+=len;
 
348
    }
 
349
 
 
350
    chno++;
 
351
  }
 
352
  fclose(fr);
 
353
 
 
354
#define _sort qsort
 
355
 
 
356
  th.DefC=chno;
 
357
  cur_inmd->DefChars = chno;
 
358
 
 
359
  if (key64)
 
360
    _sort(itar64,chno,sizeof(ITEM2_64),qcmp_64);
 
361
  else
 
362
    _sort(itar,chno,sizeof(ITEM2),qcmp);
 
363
 
 
364
  if (key64) {
 
365
    for(i=0;i<chno;i++)
 
366
      memcpy(&itout64[i],&itar64[i],sizeof(ITEM64));
 
367
  } else {
 
368
    for(i=0;i<chno;i++)
 
369
      memcpy(&itout[i],&itar[i],sizeof(ITEM));
 
370
  }
 
371
 
 
372
 
 
373
  bzero(def1,sizeof(def1));
 
374
  bzero(idx1,sizeof(idx1));
 
375
 
 
376
 
 
377
  for(i=0; i<chno; i++) {
 
378
    u_int64_t key = CONVT2(cur_inmd, i);
 
379
#if 0
 
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]);
 
383
#endif
 
384
    int kk = (key>>LAST_K_bitN) & keymask;
 
385
 
 
386
    if (!def1[kk]) {
 
387
      idx1[kk]=(gtab_idx1_t)i;
 
388
      def1[kk]=1;
 
389
    }
 
390
  }
 
391
 
 
392
  idx1[KeyNum]=chno;
 
393
  for(i=KeyNum-1;i>0;i--)
 
394
    if (!def1[i]) idx1[i]=idx1[i+1];
 
395
 
 
396
  if ((fw=fopen(argv[3],"wb"))==NULL) {
 
397
    p_err("Cannot create %s", argv[3]);
 
398
  }
 
399
 
 
400
  printf("Defined Characters:%d\n", chno);
 
401
 
 
402
#if NEED_SWAP
 
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]);
 
412
#endif
 
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);
 
417
 
 
418
  if (key64) {
 
419
#if NEED_SWAP
 
420
    for(i=0; i < chno; i++) {
 
421
      swap_byte_8(&itout64[i].key);
 
422
    }
 
423
#endif
 
424
    fwrite(itout64, sizeof(ITEM64), chno, fw);
 
425
#if 0
 
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]);
 
428
#endif
 
429
  }
 
430
  else {
 
431
#if NEED_SWAP
 
432
    for(i=0; i < chno; i++) {
 
433
      swap_byte_4(&itout[i].key);
 
434
    }
 
435
#endif
 
436
    fwrite(itout, sizeof(ITEM), chno, fw);
 
437
  }
 
438
 
 
439
  if (phr_cou) {
 
440
    printf("phrase count:%d\n", phr_cou);
 
441
    phridx[phr_cou++]=prbf_cou;
 
442
 
 
443
    int ophr_cou = phr_cou;
 
444
#if NEED_SWAP
 
445
    for(i=0; i < phr_cou; i++)
 
446
      swap_byte_4(&phridx[i]);
 
447
    swap_byte_4(&phr_cou);
 
448
#endif
 
449
    fwrite(&phr_cou, sizeof(int), 1, fw);
 
450
    fwrite(phridx, sizeof(int), ophr_cou, fw);
 
451
    fwrite(phrbuf,1,prbf_cou,fw);
 
452
  }
 
453
 
 
454
  fclose(fw);
 
455
 
 
456
  return 0;
 
457
}