File: pho-util.cpp

package info (click to toggle)
gcin 2.9.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 22,776 kB
  • sloc: cpp: 33,947; ansic: 9,313; makefile: 653; sh: 557
file content (296 lines) | stat: -rw-r--r-- 5,961 bytes parent folder | download | duplicates (3)
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
#include "gcin.h"
#include "pho.h"
#include <sys/stat.h>
#include <stdlib.h>
#if WIN32
#include <io.h>
#endif


char phofname[128]="";
extern char *TableDir;
u_short idxnum_pho;
PHO_IDX *idx_pho;
int ch_pho_ofs;
PHO_ITEM *ch_pho;
int ch_phoN;
char *pho_phrase_area;
int pho_phrase_area_sz;
static char pho_normal_tab[]="pho.tab2";
static char pho_huge_tab[]="pho-huge.tab2";
static char s_pho_normal_tab[]="s-pho.tab2";
static char s_pho_huge_tab[]="s-pho-huge.tab2";
void update_table_file(char *name, int version);
extern gboolean is_chs;

void pho_load()
{
  char *pho_tab;

  if (is_chs) {
    pho_tab = phonetic_huge_tab ? s_pho_huge_tab:s_pho_normal_tab;
  } else
    pho_tab = phonetic_huge_tab ? pho_huge_tab:pho_normal_tab;

  if (!getenv("GCIN_TABLE_DIR") && phonetic_char_dynamic_sequence) {
    get_gcin_user_fname(pho_tab, phofname);
#if UNIX
    if (access(phofname, W_OK) < 0){
#else
    if (_access(phofname, 02) < 0){
#endif
      char sys_file[256], vv[256];

      get_sys_table_file_name(sys_file, pho_tab);
#if UNIX
      sprintf(vv,"cp %s %s\n", sys_file, phofname);
      system(vv);
#else
      CopyFileA(sys_file, phofname, FALSE);
#endif
    }
  } else {
    get_sys_table_file_name(pho_tab, phofname);
    dbg("use system's pho, no dynamic adj\n");
  }

  update_table_file(pho_tab, 4);

  FILE *fr;

  if ((fr=fopen(phofname,"rb"))==NULL) {
//	char cwd[256];
//	dbg("cwd %s\n", getcwd(cwd, sizeof(cwd)));
    p_err("err fopen %s:%s\n", phofname, sys_err_strA());
  }

  fread(&idxnum_pho,sizeof(u_short),1,fr);
  fread(&idxnum_pho,sizeof(u_short),1,fr);
  fread(&ch_phoN,sizeof(int),1,fr);
  fread(&pho_phrase_area_sz, sizeof(pho_phrase_area_sz), 1,fr);

  if (idx_pho)
    free(idx_pho);
  idx_pho = tmalloc(PHO_IDX, idxnum_pho + 1);
  fread(idx_pho, sizeof(PHO_IDX), idxnum_pho, fr);
  dbg("idxnum_pho:%d ch_phoN:%d pho_phrase_area_sz:%d\n", idxnum_pho, ch_phoN, pho_phrase_area_sz);

  ch_pho_ofs = ftell(fr);

  if (ch_pho)
    free(ch_pho);

  if (!(ch_pho=tmalloc(PHO_ITEM, ch_phoN)))
    p_err("malloc error");

  fread(ch_pho,sizeof(PHO_ITEM), ch_phoN, fr);
//  dbg("ch_phoN:%d  %d\n", ch_phoN, idxnum_pho);
  if (pho_phrase_area) {
    free(pho_phrase_area);
    pho_phrase_area = NULL;
  }
  if (pho_phrase_area_sz) {
    pho_phrase_area = tmalloc(char, pho_phrase_area_sz);
    fread(pho_phrase_area, 1,pho_phrase_area_sz, fr);
#if 0
    dbg("pho_phrase loaded %d\n", pho_phrase_area_sz);
    int i;
    for(i=0; i <pho_phrase_area_sz; i+=strlen(pho_phrase_area+i)+1) {
      dbg("  %s\n", pho_phrase_area+i);
    }
#endif
  }

  fclose(fr);

  idx_pho[idxnum_pho].key=0xffff;
  idx_pho[idxnum_pho].start=ch_phoN;

#if 0
  int i;
  for(i=0; i <ch_phoN; i++) {
    char tt[5];

    utf8cpy(tt, ch_pho[i].ch);
    dbg("oooo %s\n", tt);
  }
#endif
}


char *pho_idx_str2(int idx, int *is_phrase)
{
  static char tt[CH_SZ+1];
  tt[0]=0;
  if (idx>=ch_phoN)
	  return tt;
  unsigned char *p = (u_char *)ch_pho[idx].ch;

  if (*p==PHO_PHRASE_ESCAPE) {
    p++;
    int ofs = (*p) | *(p+1)<<8 | *(p+2)<<16;

//    dbg("idx:%d ofs:%d %s\n", idx, ofs, pho_phrase_area+ofs);
    *is_phrase = TRUE;
    return pho_phrase_area+ofs;
  } else {
    *is_phrase = FALSE;
    utf8cpy(tt, (char *)p);
    return tt;
  }
}

char *pho_idx_str(int idx)
{
  int is_phrase;
  return pho_idx_str2(idx, &is_phrase);
}

int pho_idx_use_count(int idx)
{
  return ch_pho[idx].count;
}

void free_pho_mem()
{
  if (ch_pho) {
    free(ch_pho);
    ch_pho = NULL;
  }  
}

typedef struct {
  phokey_t key;
  short count;
} PH_COUNT;

static int qcmp_pho_count(const void *aa, const void *bb)
{
  PH_COUNT *a = (PH_COUNT *)aa;
  PH_COUNT *b = (PH_COUNT *)bb;

  return b->count - a->count;
}


int utf8_pho_keys(char *utf8, phokey_t *phkeys)
{
  int i;
  int ofs=0;
  int phkeysN=0;
  PH_COUNT phcou[256];

  do {
    for(; ofs < ch_phoN; ofs++)
      if (utf8_eq(utf8, pho_idx_str(ofs)))
        break;

    if (ofs==ch_phoN)
      goto ret;

    for(i=0; i < idxnum_pho; i++) {
      if (idx_pho[i].start<= ofs && ofs < idx_pho[i+1].start) {
//        dbg("ofs:%d %d  %d %d\n", ofs, i, idx_pho[i].start, idx_pho[i+1].start);
        phcou[phkeysN].count = ch_pho[ofs].count;
        phcou[phkeysN++].key = idx_pho[i].key;
        break;
      }
    }

    ofs++;
  } while (ofs < ch_phoN);

ret:

#if 0
    utf8_putchar(utf8);
    dbg("n %d\n", phkeysN);
#endif
  qsort(phcou, phkeysN, sizeof(PH_COUNT), qcmp_pho_count);

  for(i=0; i < phkeysN; i++)
    phkeys[i] = phcou[i].key;

  return phkeysN;
}

char *phokey_to_str2(phokey_t kk, int last_number)
{
  u_int k1,k2,k3,k4;
  static char phchars[PHO_CHAR_LEN * 4 + 1];
  int phcharsN=0;

  phokey_t okk = kk;
  k4=(kk&7);
  kk>>=3;
  k3=(kk&15) * PHO_CHAR_LEN;
  kk>>=4;
  k2=(kk&3) * PHO_CHAR_LEN;
  kk>>=2;
  k1=(kk&31) * PHO_CHAR_LEN;

  if (k1==BACK_QUOTE_NO * PHO_CHAR_LEN) {
    strcpy(phchars, _(_L("、")));
    int len=strlen(phchars);
    phchars[len++]=okk & 0x7f;
    phchars[len]=0;
    return phchars;
  }

  if (k1) {
    phcharsN+=u8cpy(phchars, &pho_chars[0][k1]);
  }

  if (k2) {
    phcharsN+=u8cpy(&phchars[phcharsN], &pho_chars[1][k2]);
  }

  if (k3)  {
    phcharsN+=u8cpy(&phchars[phcharsN], &pho_chars[2][k3]);
  }

  if (k4) {
//    dbg("k4 %d\n", k4);
    if (last_number)
      phchars[phcharsN++] = k4 + '0';
    else
      phcharsN+=u8cpy(&phchars[phcharsN], &pho_chars[3][k4 * PHO_CHAR_LEN]);
  }

  phchars[phcharsN] = 0;

  return phchars;
}


char *phokey_to_str(phokey_t kk)
{
  return phokey_to_str2(kk, 0);
}

void str_to_all_phokey_chars(char *u8_str, char *out)
{
  out[0]=0;

  while (*u8_str) {
    phokey_t phos[32];

    int n=utf8_pho_keys(u8_str, phos);
#if 0
    utf8_putchar(u8_str);
    dbg("n %d\n", n);
#endif
    int i;
    for(i=0; i < n; i++) {
      char *pstr = phokey_to_str(phos[i]);
      strcat(out, pstr);
      if (i < n -1)
        strcat(out, " ");
    }

    u8_str+=utf8_sz(u8_str);

    if (*u8_str)
      strcat(out, " | ");
  }
}