~ubuntu-branches/ubuntu/trusty/hime/trusty

« back to all changes in this revision

Viewing changes to src/hime-tsd2a32.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) 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 "hime.h"
 
19
#include "pho.h"
 
20
#include "tsin.h"
 
21
 
 
22
int phcount;
 
23
void prph2(FILE *fp, phokey_t kk);
 
24
 
 
25
void get_keymap_str(u_int64_t k, char *keymap, int keybits, char tkey[]);
 
26
char *phokey2pinyin(phokey_t k);
 
27
gboolean is_pinyin_kbm();
 
28
char *sys_err_strA();
 
29
void init_TableDir();
 
30
 
 
31
int main(int argc, char **argv)
 
32
{
 
33
  FILE *fp;
 
34
  int i;
 
35
  char clen;
 
36
  usecount_t usecount;
 
37
  gboolean pr_usecount = TRUE;
 
38
  char *fname;
 
39
  char *fname_out = NULL;
 
40
 
 
41
  gtk_init(&argc, &argv);
 
42
 
 
43
  if (argc <= 1) {
 
44
    printf("%s: file name expected\n", argv[0]);
 
45
    exit(1);
 
46
  }
 
47
 
 
48
  init_TableDir();
 
49
 
 
50
  gboolean b_pinyin = is_pinyin_kbm();
 
51
 
 
52
  for(i=1; i < argc;) {
 
53
    if (!strcmp(argv[i], "-nousecount")) {
 
54
      i++;
 
55
      pr_usecount = FALSE;
 
56
      b_pinyin = FALSE;
 
57
    } else
 
58
    if (!strcmp(argv[i], "-o")) {
 
59
      if (i==argc-1)
 
60
        p_err("-o need out file name");
 
61
        fname_out = argv[i+1];
 
62
        i+=2;
 
63
    } else
 
64
      fname = argv[i++];
 
65
  }
 
66
 
 
67
  FILE *fp_out;
 
68
 
 
69
  if (!fname_out) {
 
70
    fp_out = stdout;
 
71
  } else {
 
72
    dbg("%s use %s\n", argv[0], fname_out);
 
73
    fp_out = fopen(fname_out, "w");
 
74
    if (!fp_out)
 
75
      p_err("cannot create %s\n", fname_out);
 
76
 
 
77
  }
 
78
 
 
79
  if (b_pinyin)
 
80
    fprintf(fp_out, "!!pinyin\n");
 
81
 
 
82
  if ((fp=fopen(fname,"rb"))==NULL)
 
83
    p_err("Cannot open %s %s", fname, sys_err_strA());
 
84
 
 
85
 
 
86
  TSIN_GTAB_HEAD head;
 
87
  int phsz = 2;
 
88
 
 
89
  fread(&head, sizeof(head), 1, fp);
 
90
  if (!strcmp(head.signature, TSIN_GTAB_KEY)) {
 
91
    if (head.maxkey * head.keybits > 32)
 
92
      phsz = 8;
 
93
    else
 
94
      phsz = 4;
 
95
  } else
 
96
    rewind(fp);
 
97
 
 
98
  if (phsz > 2) {
 
99
    fprintf(stderr, "phsz %d keybits:%d\n", phsz, head.keybits);
 
100
    fprintf(stderr, "keymap '%s'\n", head.keymap);
 
101
    fprintf(fp_out,TSIN_GTAB_KEY" %d %d %s\n", head.keybits, head.maxkey, head.keymap+1);
 
102
  }
 
103
 
 
104
  while (!feof(fp)) {
 
105
    phokey_t phbuf[MAX_PHRASE_LEN];
 
106
    u_int phbuf32[MAX_PHRASE_LEN];
 
107
    u_int64_t phbuf64[MAX_PHRASE_LEN];
 
108
    gboolean is_deleted = FALSE;
 
109
 
 
110
    fread(&clen,1,1,fp);
 
111
    if (clen < 0) {
 
112
      clen = - clen;
 
113
      is_deleted = TRUE;
 
114
    }
 
115
 
 
116
    fread(&usecount, sizeof(usecount_t), 1,fp);
 
117
    if (!pr_usecount)
 
118
      usecount = 0;
 
119
 
 
120
    if (phsz==2)
 
121
      fread(phbuf, sizeof(phokey_t), clen, fp);
 
122
    else
 
123
    if (phsz==4)
 
124
      fread(phbuf32, 4, clen, fp);
 
125
    else
 
126
    if (phsz==8)
 
127
      fread(phbuf64, 8, clen, fp);
 
128
 
 
129
 
 
130
    char tt[512];
 
131
    int ttlen=0;
 
132
    tt[0]=0;
 
133
    for(i=0;i<clen;i++) {
 
134
      char ch[CH_SZ];
 
135
 
 
136
      int n = fread(ch, 1, 1, fp);
 
137
      if (n<=0)
 
138
        goto stop;
 
139
 
 
140
      int len=utf8_sz(ch);
 
141
 
 
142
      fread(&ch[1], 1, len-1, fp);
 
143
 
 
144
      memcpy(tt+ttlen, ch, len);
 
145
      ttlen+=len;
 
146
    }
 
147
    tt[ttlen]=0;
 
148
 
 
149
    if (!tt[0])
 
150
      continue;
 
151
 
 
152
    if (is_deleted)
 
153
      continue;
 
154
 
 
155
    fprintf(fp_out, "%s ", tt);
 
156
 
 
157
    for(i=0;i<clen;i++) {
 
158
      if (phsz==2) {
 
159
        if (b_pinyin) {
 
160
          char *t = phokey2pinyin(phbuf[i]);
 
161
//          dbg("z %s\n", t);
 
162
          fprintf(fp_out, "%s", t);
 
163
        } else
 
164
          prph2(fp_out, phbuf[i]);
 
165
      } else {
 
166
        u_int64_t k;
 
167
        if (phsz==4)
 
168
          k = phbuf32[i];
 
169
        else
 
170
          k = phbuf64[i];
 
171
 
 
172
        char tkey[16];
 
173
        get_keymap_str(k, head.keymap, head.keybits, tkey);
 
174
        fprintf(fp_out, "%s", tkey);
 
175
      }
 
176
 
 
177
      if (i!=clen-1)
 
178
        fprintf(fp_out, " ");
 
179
    }
 
180
 
 
181
    fprintf(fp_out, " %d\n", usecount);
 
182
  }
 
183
 
 
184
stop:
 
185
  fclose(fp);
 
186
  fclose(fp_out);
 
187
 
 
188
  exit(0);
 
189
}