1
/* Copyright (C) 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
24
char phofname[128]="";
25
extern char *TableDir;
31
char *pho_phrase_area;
32
int pho_phrase_area_sz;
33
static char pho_normal_tab[]="pho.tab2";
34
static char pho_huge_tab[]="pho-huge.tab2";
35
static char s_pho_normal_tab[]="s-pho.tab2";
36
static char s_pho_huge_tab[]="s-pho-huge.tab2";
37
void update_table_file(char *name, int version);
44
pho_tab = phonetic_huge_tab ? s_pho_huge_tab:s_pho_normal_tab;
46
pho_tab = phonetic_huge_tab ? pho_huge_tab:pho_normal_tab;
48
if (!getenv("HIME_TABLE_DIR") && phonetic_char_dynamic_sequence) {
49
get_hime_user_fname(pho_tab, phofname);
50
if (access(phofname, W_OK) < 0){
51
char sys_file[256], vv[256];
52
get_sys_table_file_name(sys_file, pho_tab);
53
sprintf(vv,"cp %s %s\n", sys_file, phofname);
57
get_sys_table_file_name(pho_tab, phofname);
58
dbg("use system's pho, no dynamic adj\n");
61
update_table_file(pho_tab, 4);
65
if ((fr=fopen(phofname,"rb"))==NULL)
66
p_err("err %s\n", phofname);
68
fread(&idxnum_pho,sizeof(u_short),1,fr);
69
fread(&idxnum_pho,sizeof(u_short),1,fr);
70
fread(&ch_phoN,sizeof(int),1,fr);
71
fread(&pho_phrase_area_sz, sizeof(pho_phrase_area_sz), 1,fr);
75
idx_pho = tmalloc(PHO_IDX, idxnum_pho + 1);
76
fread(idx_pho, sizeof(PHO_IDX), idxnum_pho, fr);
78
ch_pho_ofs = ftell(fr);
83
if (!(ch_pho=tmalloc(PHO_ITEM, ch_phoN)))
84
p_err("malloc error");
86
fread(ch_pho,sizeof(PHO_ITEM), ch_phoN, fr);
87
// dbg("ch_phoN:%d %d\n", ch_phoN, idxnum_pho);
88
if (pho_phrase_area) {
89
free(pho_phrase_area);
90
pho_phrase_area = NULL;
92
if (pho_phrase_area_sz) {
93
pho_phrase_area = tmalloc(char, pho_phrase_area_sz);
94
fread(pho_phrase_area, 1,pho_phrase_area_sz, fr);
96
dbg("pho_phrase loaded %d\n", pho_phrase_area_sz);
98
for(i=0; i <pho_phrase_area_sz; i+=strlen(pho_phrase_area+i)+1) {
99
dbg(" %s\n", pho_phrase_area+i);
106
idx_pho[idxnum_pho].key=0xffff;
107
idx_pho[idxnum_pho].start=ch_phoN;
111
for(i=0; i <ch_phoN; i++) {
114
utf8cpy(tt, ch_pho[i].ch);
115
dbg("oooo %s\n", tt);
121
char *pho_idx_str2(int idx, int *is_phrase)
123
static char tt[CH_SZ+1];
125
unsigned char *p = (u_char *)ch_pho[idx].ch;
127
if (*p==PHO_PHRASE_ESCAPE) {
129
int ofs = (*p) | *(p+1)<<8 | *(p+2)<<16;
131
// dbg("idx:%d ofs:%d %s\n", idx, ofs, pho_phrase_area+ofs);
133
return pho_phrase_area+ofs;
136
utf8cpy(tt, (char *)p);
141
char *pho_idx_str(int idx)
144
return pho_idx_str2(idx, &is_phrase);
158
static int qcmp_pho_count(const void *aa, const void *bb)
160
PH_COUNT *a = (PH_COUNT *)aa;
161
PH_COUNT *b = (PH_COUNT *)bb;
163
return b->count - a->count;
167
int utf8_pho_keys(char *utf8, phokey_t *phkeys)
175
for(; ofs < ch_phoN; ofs++)
176
if (utf8_eq(utf8, pho_idx_str(ofs)))
182
for(i=0; i < idxnum_pho; i++) {
183
if (idx_pho[i].start<= ofs && ofs < idx_pho[i+1].start) {
184
// dbg("ofs:%d %d %d %d\n", ofs, i, idx_pho[i].start, idx_pho[i+1].start);
185
phcou[phkeysN].count = ch_pho[ofs].count;
186
phcou[phkeysN++].key = idx_pho[i].key;
192
} while (ofs < ch_phoN);
198
dbg("n %d\n", phkeysN);
200
qsort(phcou, phkeysN, sizeof(PH_COUNT), qcmp_pho_count);
202
for(i=0; i < phkeysN; i++)
203
phkeys[i] = phcou[i].key;
208
char *phokey_to_str2(phokey_t kk, int last_number)
211
static char phchars[PHO_CHAR_LEN * 4 + 1];
217
k3=(kk&15) * PHO_CHAR_LEN;
219
k2=(kk&3) * PHO_CHAR_LEN;
221
k1=(kk&31) * PHO_CHAR_LEN;
223
if (k1==BACK_QUOTE_NO * PHO_CHAR_LEN) {
224
strcpy(phchars, "、");
225
int len=strlen(phchars);
226
phchars[len++]=okk & 0x7f;
232
phcharsN+=u8cpy(phchars, &pho_chars[0][k1]);
236
phcharsN+=u8cpy(&phchars[phcharsN], &pho_chars[1][k2]);
240
phcharsN+=u8cpy(&phchars[phcharsN], &pho_chars[2][k3]);
244
// dbg("k4 %d\n", k4);
246
phchars[phcharsN++] = k4 + '0';
248
phcharsN+=u8cpy(&phchars[phcharsN], &pho_chars[3][k4 * PHO_CHAR_LEN]);
251
phchars[phcharsN] = 0;
257
char *phokey_to_str(phokey_t kk)
259
return phokey_to_str2(kk, 0);
262
void str_to_all_phokey_chars(char *u8_str, char *out)
269
int n=utf8_pho_keys(u8_str, phos);
271
utf8_putchar(u8_str);
275
for(i=0; i < n; i++) {
276
char *pstr = phokey_to_str(phos[i]);
282
u8_str+=utf8_sz(u8_str);