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

« back to all changes in this revision

Viewing changes to src/cin2gtab.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) 1995-2011 Edward 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
char tt[1024];
 
34
 
 
35
 
 
36
char *skip_spc(char *s)
 
37
{
 
38
        while ((*s==' ' || *s=='\t') && *s) s++;
 
39
        return s;
 
40
}
 
41
 
 
42
char *to_spc(char *s)
 
43
{
 
44
        while (*s!=' ' && *s!='\t' && *s) s++;
 
45
        return s;
 
46
}
 
47
 
 
48
void del_nl_spc(char *s)
 
49
{
 
50
  char *t;
 
51
 
 
52
  int len=strlen(s);
 
53
  if (!*s) return;
 
54
 
 
55
  t=s+len-1;
 
56
 
 
57
  while (*t=='\n' || *t==' ' || (*t=='\t' && t > s))
 
58
    t--;
 
59
 
 
60
  *(t+1)=0;
 
61
}
 
62
 
 
63
 
 
64
void get_line()
 
65
{
 
66
  while (!feof(fr)) {
 
67
    bzero(tt, sizeof(tt));
 
68
        myfgets(tt, sizeof(tt), fr);
 
69
    lineno++;
 
70
 
 
71
    int len=strlen(tt);
 
72
    if (tt[len-1]=='\n')
 
73
      tt[len-1] = 0;
 
74
 
 
75
    if (tt[0]=='#' || strlen(tt) < 3)
 
76
      continue;
 
77
    else
 
78
      break;
 
79
  }
 
80
}
 
81
 
 
82
void cmd_arg(char **cmd, char **arg)
 
83
{
 
84
  char *t;
 
85
 
 
86
  get_line();
 
87
  char *s=tt;
 
88
 
 
89
  if (!*s) {
 
90
    *cmd=*arg=s;
 
91
    return;
 
92
  }
 
93
 
 
94
  s=skip_spc(s);
 
95
  t=to_spc(s);
 
96
  *cmd=s;
 
97
  if (!(*t)) {
 
98
    *arg=t;
 
99
    return;
 
100
  }
 
101
 
 
102
  *t=0;
 
103
  t++;
 
104
 
 
105
  t=skip_spc(t);
 
106
  del_nl_spc(t);
 
107
 
 
108
  char *p;
 
109
  if ((p=strchr(t, '\t')))
 
110
    *p = 0;
 
111
 
 
112
  *arg=t;
 
113
}
 
114
 
 
115
int sequ(char *s, char *t)
 
116
{
 
117
  return (!strcmp(s,t));
 
118
}
 
119
 
 
120
typedef struct {
 
121
  u_int key;
 
122
  u_char ch[CH_SZ];
 
123
  int oseq;
 
124
} ITEM2;
 
125
 
 
126
typedef struct {
 
127
  u_int64_t key;
 
128
  u_char ch[CH_SZ];
 
129
  int oseq;
 
130
} ITEM2_64;
 
131
 
 
132
 
 
133
#define MAX_K (500000)
 
134
 
 
135
ITEM2 itar[MAX_K];
 
136
ITEM2_64 itar64[MAX_K];
 
137
 
 
138
ITEM itout[MAX_K];
 
139
ITEM64 itout64[MAX_K];
 
140
 
 
141
 
 
142
int qcmp(const void *aa, const void *bb)
 
143
{
 
144
  ITEM2 *a = (ITEM2 *)aa, *b = (ITEM2 *) bb;
 
145
 
 
146
  if (a->key > b->key) return 1;
 
147
  if (a->key < b->key) return -1;
 
148
 
 
149
  return a->oseq - b->oseq;
 
150
}
 
151
 
 
152
 
 
153
int qcmp_64(const void *aa, const void *bb)
 
154
{
 
155
  ITEM2_64 *a = (ITEM2_64 *)aa, *b = (ITEM2_64 *) bb;
 
156
 
 
157
  if (a->key > b->key) return 1;
 
158
  if (a->key < b->key) return -1;
 
159
 
 
160
  return a->oseq - b->oseq;
 
161
}
 
162
 
 
163
 
 
164
#define mtolower(ch) (ch>='A'&&ch<='Z'?ch+0x20:ch)
 
165
 
 
166
static char kno[128];
 
167
 
 
168
int main(int argc, char **argv)
 
169
{
 
170
  int i;
 
171
  char fname[64];
 
172
  char fname_cin[64];
 
173
  char fname_tab[64];
 
174
  char *cmd, *arg;
 
175
  struct TableHead th;
 
176
  int KeyNum;
 
177
  char kname[128][CH_SZ];
 
178
  char keymap[128];
 
179
  int chno;
 
180
  gtab_idx1_t idx1[256];
 
181
  char def1[256];
 
182
  int quick_def;
 
183
  int *phridx=NULL, phr_cou=0;
 
184
  char *phrbuf = NULL;
 
185
  int prbf_cou=0;
 
186
 
 
187
  if (!getenv("NO_GTK_INIT"))
 
188
    gtk_init(&argc, &argv);
 
189
 
 
190
  printf("-- cin2gtab encoding UTF-8 --\n");
 
191
  printf("--- please use iconv -f big5 -t utf-8 if your file is in big5 encoding\n");
 
192
 
 
193
  if (argc<=1) {
 
194
          printf("Enter table file name [.cin] : ");
 
195
          scanf("%s", fname);
 
196
  } else strcpy(fname,argv[1]);
 
197
 
 
198
 
 
199
  if (!strcmp(fname, "-v") || !strcmp(fname, "--version")) {
 
200
    dbg("cin2gtab for hime " HIME_VERSION "\n");
 
201
    exit(0);
 
202
  }
 
203
 
 
204
  char *p;
 
205
  if((p=strstr(fname, ".cin")))
 
206
    *p = 0;
 
207
 
 
208
  strcpy(fname_cin,fname);
 
209
  strcpy(fname_tab,fname);
 
210
  strcat(fname_cin,".cin");
 
211
  strcat(fname_tab,".gtab");
 
212
 
 
213
  if ((fr=fopen(fname_cin,"rb"))==NULL)
 
214
          p_err("Cannot open %s\n", fname_cin);
 
215
 
 
216
  skip_utf8_sigature(fr);
 
217
 
 
218
 
 
219
  bzero(&th,sizeof(th));
 
220
  bzero(kno,sizeof(kno));
 
221
  bzero(keymap,sizeof(keymap));
 
222
 
 
223
  bzero(itar,sizeof(itar));
 
224
  bzero(itout,sizeof(itout));
 
225
  bzero(itar64,sizeof(itar64));
 
226
  bzero(itout64,sizeof(itout64));
 
227
 
 
228
  cmd_arg(&cmd, &arg);
 
229
  if (sequ(cmd, "%gen_inp")) {
 
230
    dbg("skip gen_inp\n");
 
231
    cmd_arg(&cmd, &arg);
 
232
  }
 
233
 
 
234
  if (!sequ(cmd,"%ename") || !(*arg) )
 
235
    p_err("%d:  %%ename english_name  expected", lineno);
 
236
  arg[15]=0;
 
237
//  strcpy(th.ename,arg);
 
238
 
 
239
  cmd_arg(&cmd, &arg);
 
240
  if (!(sequ(cmd,"%prompt") || sequ(cmd,"%cname")) || !(*arg) )
 
241
    p_err("%d:  %%prompt prompt_name  expected", lineno);
 
242
  strncpy(th.cname, arg, MAX_CNAME);
 
243
  dbg("cname %s\n", th.cname);
 
244
 
 
245
  cmd_arg(&cmd, &arg);
 
246
  if (!sequ(cmd,"%selkey") || !(*arg) )
 
247
    p_err("%d:  %%selkey select_key_list expected", lineno);
 
248
 
 
249
 
 
250
  if (strlen(arg) >= sizeof(th.selkey)) {
 
251
    memcpy(th.selkey, arg, sizeof(th.selkey));
 
252
    strcpy(th.selkey2, arg+sizeof(th.selkey));
 
253
    dbg("th.selkey2 %s\n", th.selkey2);
 
254
  } else
 
255
    strcpy(th.selkey,arg);
 
256
 
 
257
  cmd_arg(&cmd, &arg);
 
258
  if (!sequ(cmd,"%dupsel") || !(*arg) ) {
 
259
    if (th.selkey[sizeof(th.selkey)-1])
 
260
      th.M_DUP_SEL = sizeof(th.selkey) + strlen(th.selkey2);
 
261
    else
 
262
      th.M_DUP_SEL = strlen(th.selkey);
 
263
  }
 
264
  else {
 
265
    th.M_DUP_SEL=atoi(arg);
 
266
    cmd_arg(&cmd, &arg);
 
267
  }
 
268
 
 
269
  for(;;) {
 
270
    if (sequ(cmd,"%endkey")) {
 
271
      strcpy(th.endkey, arg);
 
272
      cmd_arg(&cmd, &arg);
 
273
    } else
 
274
    if (sequ(cmd,"%space_style")) {
 
275
      th.space_style = (GTAB_space_pressed_E)atoi(arg);
 
276
      cmd_arg(&cmd, &arg);
 
277
    } else
 
278
    if (sequ(cmd,"%keep_key_case")) {
 
279
      th.flag |= FLAG_KEEP_KEY_CASE;
 
280
      cmd_arg(&cmd, &arg);
 
281
    } else
 
282
    if (sequ(cmd,"%symbol_kbm")) {
 
283
      th.flag |= FLAG_GTAB_SYM_KBM;
 
284
      cmd_arg(&cmd, &arg);
 
285
    } else
 
286
    if (sequ(cmd,"%phase_auto_skip_endkey")) {
 
287
      th.flag |= FLAG_PHRASE_AUTO_SKIP_ENDKEY;
 
288
      cmd_arg(&cmd, &arg);
 
289
    } else
 
290
    if (sequ(cmd,"%flag_auto_select_by_phrase")) {
 
291
      dbg("flag_auto_select_by_phrase\n");
 
292
      th.flag |= FLAG_AUTO_SELECT_BY_PHRASE;
 
293
      cmd_arg(&cmd, &arg);
 
294
    } else
 
295
    if (sequ(cmd,"%flag_disp_partial_match")) {
 
296
      dbg("flag_disp_partial_match\n");
 
297
      th.flag |= FLAG_GTAB_DISP_PARTIAL_MATCH;
 
298
      cmd_arg(&cmd, &arg);
 
299
    } else
 
300
    if (sequ(cmd,"%flag_disp_full_match")) {
 
301
      dbg("flag_disp_full_match\n");
 
302
      th.flag |= FLAG_GTAB_DISP_FULL_MATCH;
 
303
      cmd_arg(&cmd, &arg);
 
304
    } else
 
305
    if (sequ(cmd,"%flag_vertical_selection")) {
 
306
      dbg("flag_vertical_selection\n");
 
307
      th.flag |= FLAG_GTAB_VERTICAL_SELECTION;
 
308
      cmd_arg(&cmd, &arg);
 
309
    } else
 
310
    if (sequ(cmd,"%flag_press_full_auto_send")) {
 
311
      dbg("flag_press_full_auto_send\n");
 
312
      th.flag |= FLAG_GTAB_PRESS_FULL_AUTO_SEND;
 
313
      cmd_arg(&cmd, &arg);
 
314
    } else
 
315
    if (sequ(cmd,"%flag_unique_auto_send")) {
 
316
      dbg("flag_unique_auto_send\n");
 
317
      th.flag |= FLAG_GTAB_UNIQUE_AUTO_SEND;
 
318
      cmd_arg(&cmd, &arg);
 
319
    } else
 
320
      break;
 
321
  }
 
322
 
 
323
 
 
324
  if (!sequ(cmd,"%keyname") || !sequ(arg,"begin")) {
 
325
    p_err("%d:  %%keyname begin   expected, instead of %s %s", lineno, cmd, arg);
 
326
  }
 
327
 
 
328
  for(KeyNum=0;;) {
 
329
    char k;
 
330
 
 
331
    cmd_arg(&cmd, &arg);
 
332
    if (sequ(cmd,"%keyname")) break;
 
333
    if (BITON(th.flag, FLAG_KEEP_KEY_CASE))
 
334
      k=cmd[0];
 
335
    else
 
336
      k=mtolower(cmd[0]);
 
337
 
 
338
    if (kno[(int)k])
 
339
      p_err("%d:  key %c is already used",lineno, k);
 
340
 
 
341
    kno[(int)k]=++KeyNum;
 
342
    keymap[KeyNum]=k;
 
343
    bchcpy(&kname[KeyNum][0], arg);
 
344
  }
 
345
 
 
346
  keymap[0]=kname[0][0]=kname[0][1]=' ';
 
347
  KeyNum++;
 
348
  th.KeyS=KeyNum;    /* include space */
 
349
 
 
350
  cmd_arg(&cmd, &arg);
 
351
 
 
352
  if (sequ(cmd,"%quick") && sequ(arg,"begin")) {
 
353
    dbg(".. quick keys defined\n");
 
354
    for(quick_def=0;;) {
 
355
      char k;
 
356
 
 
357
      cmd_arg(&cmd, &arg);
 
358
      if (sequ(cmd,"%quick")) break;
 
359
      k=kno[mtolower(cmd[0])]-1;
 
360
 
 
361
      int N = 0;
 
362
      char *p = arg;
 
363
 
 
364
      if (strlen(cmd)==1) {
 
365
        while (*p) {
 
366
          int len=u8cpy(th.qkeys.quick1[(int)k][N++], p);
 
367
          p+=len;
 
368
        }
 
369
      } else
 
370
      if (strlen(cmd)==2) {
 
371
        int k1=kno[mtolower(cmd[1])]-1;
 
372
        while (*p) {
 
373
          char tp[4];
 
374
          int len=u8cpy(tp, p);
 
375
 
 
376
          if (utf8_eq(tp,"□"))
 
377
             tp[0]=0;
 
378
 
 
379
          u8cpy(th.qkeys.quick2[(int)k][(int)k1][N++], tp);
 
380
          p+=len;
 
381
        }
 
382
      } else
 
383
        p_err("%d:  %quick only 1&2 keys are allowed '%s'", lineno, cmd);
 
384
 
 
385
      quick_def++;
 
386
    }
 
387
  }
 
388
 
 
389
  long pos=ftell(fr);
 
390
  int olineno = lineno;
 
391
  gboolean key64 = FALSE;
 
392
  int max_key_len = 0;
 
393
 
 
394
  while (!feof(fr)) {
 
395
    int len;
 
396
 
 
397
    cmd_arg(&cmd,&arg);
 
398
    if (!cmd[0] || !arg[0])
 
399
      continue;
 
400
 
 
401
    if (!strcmp(cmd, "%chardef")) {
 
402
      if (!strcmp(arg, "end"))
 
403
        break;
 
404
      else
 
405
        continue;
 
406
    }
 
407
 
 
408
    len=strlen(cmd);
 
409
 
 
410
    if (max_key_len < len)
 
411
      max_key_len = len;
 
412
  }
 
413
 
 
414
 
 
415
  fseek(fr, pos, SEEK_SET);
 
416
  lineno=olineno;
 
417
 
 
418
  INMD inmd, *cur_inmd = &inmd;
 
419
 
 
420
  cur_inmd->key64 = key64;
 
421
  cur_inmd->tbl64 = itout64;
 
422
  cur_inmd->tbl = itout;
 
423
 
 
424
  if (KeyNum < 64)
 
425
    cur_inmd->keybits = 6;
 
426
  else
 
427
    cur_inmd->keybits = 7;
 
428
 
 
429
  if (cur_inmd->keybits * max_key_len > 32) {
 
430
   cur_inmd->key64 = key64 = TRUE;
 
431
  }
 
432
 
 
433
  if (key64)
 
434
    dbg("key64\n");
 
435
 
 
436
  printf("KeyNum:%d keybits:%d\n", KeyNum, cur_inmd->keybits);
 
437
 
 
438
  th.keybits = cur_inmd->keybits;
 
439
  cur_inmd->last_k_bitn = (((cur_inmd->key64 ? 64:32) / cur_inmd->keybits) - 1) * cur_inmd->keybits;
 
440
 
 
441
 
 
442
  puts("char def");
 
443
  chno=0;
 
444
  while (!feof(fr)) {
 
445
    int len;
 
446
    u_int64_t kk;
 
447
    int k;
 
448
 
 
449
    cmd_arg(&cmd, &arg);
 
450
    if (!cmd[0] || !arg[0])
 
451
      continue;
 
452
 
 
453
    if (!strcmp(cmd, "%chardef")) {
 
454
      if (!strcmp(arg, "end"))
 
455
        break;
 
456
      else
 
457
        continue;
 
458
    }
 
459
 
 
460
    len=strlen(cmd);
 
461
 
 
462
    if (len > th.MaxPress) {
 
463
      th.MaxPress=len;
 
464
    }
 
465
 
 
466
    if (len > 10)
 
467
      p_err("%d:  only <= 10 keys is allowed '%s'", lineno, cmd);
 
468
 
 
469
    kk=0;
 
470
    for(i=0;i<len;i++) {
 
471
      int key = BITON(th.flag, FLAG_KEEP_KEY_CASE) ?
 
472
        cmd[i] : mtolower(cmd[i]);
 
473
 
 
474
      k=kno[key];
 
475
 
 
476
      if (!k)
 
477
        p_err("%d: key undefined in keyname '%c'\n", lineno, cmd[i]);
 
478
 
 
479
      kk|=(u_int64_t)k << ( LAST_K_bitN - i*th.keybits);
 
480
    }
 
481
 
 
482
//    dbg("%s kk:%llx\n", cmd, kk);
 
483
 
 
484
    if (key64) {
 
485
      memcpy(&itar64[chno].key, &kk, 8);
 
486
      itar64[chno].oseq=chno;
 
487
    }
 
488
    else {
 
489
      u_int key32 = (u_int)kk;
 
490
 
 
491
      memcpy(&itar[chno].key, &key32, 4);
 
492
      itar[chno].oseq=chno;
 
493
    }
 
494
 
 
495
    if ((len=strlen(arg)) <= CH_SZ && (arg[0] & 0x80)) {
 
496
      char out[CH_SZ+1];
 
497
 
 
498
      bzero(out, sizeof(out));
 
499
      memcpy(out, arg, len);
 
500
 
 
501
      if (key64)
 
502
        bchcpy(itar64[chno].ch, out);
 
503
      else
 
504
        bchcpy(itar[chno].ch, out);
 
505
 
 
506
    } else {
 
507
      if (key64) {
 
508
          itar64[chno].ch[0]=phr_cou>>16;
 
509
          itar64[chno].ch[1]=(phr_cou >> 8) & 0xff;
 
510
          itar64[chno].ch[2]=phr_cou&0xff;
 
511
      }
 
512
      else {
 
513
          itar[chno].ch[0]=phr_cou>>16;
 
514
          itar[chno].ch[1]=(phr_cou >> 8) & 0xff;
 
515
          itar[chno].ch[2]=phr_cou&0xff;
 
516
      }
 
517
 
 
518
      if (len > MAX_CIN_PHR)
 
519
        p_err("phrase too long: %s  max:%d bytes\n", arg, MAX_CIN_PHR);
 
520
 
 
521
      phridx = trealloc(phridx, int, phr_cou+1);
 
522
      phridx[phr_cou++]=prbf_cou;
 
523
      phrbuf = (char *)realloc(phrbuf, prbf_cou + len + 1);
 
524
      strcpy(&phrbuf[prbf_cou],arg);
 
525
//      printf("phrase:%d  len:%d'%s'\n", phr_cou, len, arg);
 
526
      prbf_cou+=len;
 
527
    }
 
528
 
 
529
    chno++;
 
530
  }
 
531
  fclose(fr);
 
532
 
 
533
#define _sort qsort
 
534
 
 
535
  printf("MaxPress: %d\n", th.MaxPress);
 
536
 
 
537
  th.DefC=chno;
 
538
  cur_inmd->DefChars = chno;
 
539
 
 
540
  if (key64)
 
541
    _sort(itar64,chno,sizeof(ITEM2_64),qcmp_64);
 
542
  else
 
543
    _sort(itar,chno,sizeof(ITEM2),qcmp);
 
544
 
 
545
  if (key64) {
 
546
    for(i=0;i<chno;i++)
 
547
      memcpy(&itout64[i],&itar64[i],sizeof(ITEM64));
 
548
  } else {
 
549
    for(i=0;i<chno;i++)
 
550
      memcpy(&itout[i],&itar[i],sizeof(ITEM));
 
551
  }
 
552
 
 
553
 
 
554
  bzero(def1,sizeof(def1));
 
555
  bzero(idx1,sizeof(idx1));
 
556
 
 
557
 
 
558
  u_int64_t keymask = KEY_MASK;
 
559
  for(i=0; i<chno; i++) {
 
560
    u_int64_t key = CONVT2(cur_inmd, i);
 
561
    int kk = (int)((key>>LAST_K_bitN) & keymask);
 
562
 
 
563
    if (!def1[kk]) {
 
564
      idx1[kk]=(gtab_idx1_t)i;
 
565
      def1[kk]=1;
 
566
    }
 
567
  }
 
568
 
 
569
  idx1[KeyNum]=chno;
 
570
  for(i=KeyNum-1;i>0;i--)
 
571
    if (!def1[i]) idx1[i]=idx1[i+1];
 
572
 
 
573
  if ((fw=fopen(fname_tab,"wb"))==NULL) {
 
574
    p_err("Cannot create");
 
575
  }
 
576
 
 
577
  printf("Defined Characters:%d\n", chno);
 
578
 
 
579
#if NEED_SWAP
 
580
  swap_byte_4(&th.version);
 
581
  swap_byte_4(&th.flag);
 
582
  swap_byte_4(&th.space_style);
 
583
  swap_byte_4(&th.KeyS);
 
584
  swap_byte_4(&th.MaxPress);
 
585
  swap_byte_4(&th.M_DUP_SEL);
 
586
  swap_byte_4(&th.DefC);
 
587
  for(i=0; i <= KeyNum; i++)
 
588
    swap_byte_4(&idx1[i]);
 
589
#endif
 
590
  fwrite(&th,1,sizeof(th),fw);
 
591
  fwrite(keymap, 1, KeyNum, fw);
 
592
  fwrite(kname, CH_SZ, KeyNum, fw);
 
593
 
 
594
  fwrite(idx1, sizeof(gtab_idx1_t), KeyNum+1, fw);
 
595
 
 
596
 
 
597
  if (key64) {
 
598
#if NEED_SWAP
 
599
    for(i=0; i < chno; i++) {
 
600
      swap_byte_8(&itout64[i].key);
 
601
    }
 
602
#endif
 
603
    fwrite(itout64, sizeof(ITEM64), chno, fw);
 
604
#if 0
 
605
    for(i=0; i < 100; i++)
 
606
      dbg("%d] %c%c%c\n", i, itout64[i].ch[0], itout64[i].ch[1], itout64[i].ch[2]);
 
607
#endif
 
608
  }
 
609
  else {
 
610
#if NEED_SWAP
 
611
    for(i=0; i < chno; i++) {
 
612
      swap_byte_4(&itout[i].key);
 
613
    }
 
614
#endif
 
615
    fwrite(itout, sizeof(ITEM), chno, fw);
 
616
  }
 
617
 
 
618
  if (phr_cou) {
 
619
    phridx[phr_cou++]=prbf_cou;
 
620
    printf("phrase count:%d\n", phr_cou);
 
621
 
 
622
    int ophr_cou = phr_cou;
 
623
#if NEED_SWAP
 
624
    for(i=0; i < phr_cou; i++)
 
625
      swap_byte_4(&phridx[i]);
 
626
    swap_byte_4(&phr_cou);
 
627
#endif
 
628
    fwrite(&phr_cou, sizeof(int), 1, fw);
 
629
    fwrite(phridx, sizeof(int), ophr_cou, fw);
 
630
    fwrite(phrbuf,1,prbf_cou,fw);
 
631
  }
 
632
 
 
633
  fclose(fw);
 
634
 
 
635
#if 0
 
636
  char bzip2[128];
 
637
  strcat(strcpy(bzip2, "bzip2 -f -k "), fname_tab);
 
638
  system(bzip2);
 
639
#endif
 
640
 
 
641
  return 0;
 
642
}