File: dic_util.c

package info (click to toggle)
anthy 6300d-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,400 kB
  • ctags: 2,270
  • sloc: ansic: 17,009; sh: 13,554; lisp: 1,039; makefile: 252; ruby: 212; perl: 10
file content (295 lines) | stat: -rw-r--r-- 6,311 bytes parent folder | download
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
/*
 * ĿͼѤδؿ
 *
 * Funded by IPA̤Ƨեȥ¤ 2001 10/24
 *
 * Copyright (C) 2001-2004 TABATA Yusuke
 *
 */
/*
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <anthy.h>
#include <conf.h>
#include <dic.h>
#include <record.h>
#include <dicutil.h>

static int gIsInit;

/* recordƱindexβܤͤФ */
static int gOffset;

static int dic_util_encoding;

/** Ŀͼ饤֥ */
void
anthy_dic_util_init(void)
{
  if (gIsInit) {
    return ;
  }
  if (anthy_init_dic() == -1) {
    return ;
  }
  anthy_dic_set_personality("default");
  gIsInit = 1;
  dic_util_encoding = ANTHY_EUC_JP_ENCODING;
}

/** 饤֥ */
void
anthy_dic_util_quit(void)
{
  if (gIsInit) {
    anthy_quit_dic();
  }
}

/** 桼ƥƥAPIΥ󥳡ǥ󥰤ꤹ */
int
anthy_dic_util_set_encoding(int enc)
{
#ifdef USE_UCS4
  if (enc == ANTHY_EUC_JP_ENCODING ||
      enc == ANTHY_UTF8_ENCODING) {
    dic_util_encoding = enc;
  }
  return dic_util_encoding;
#else
  (void)enc;
  return ANTHY_EUC_JP_ENCODING;
#endif
}

void
anthy_dic_util_set_personality(const char *id)
{
  anthy_dic_set_personality(id);
}

/** Ŀͼä */
void
anthy_priv_dic_delete(void)
{
  if (anthy_select_section("PRIVATEDIC", 0) == -1) {
    return ;
  }
  /* sectionƤ˴ */
  anthy_release_section();
}

/** ǽñ򤹤 */
int
anthy_priv_dic_select_first_entry(void)
{
  if (anthy_select_section("PRIVATEDIC", 0) == -1) {
    return -1;
  }
  gOffset = 0;
  return anthy_select_first_column();
}

/** 򤵤Ƥñμñ򤹤 */
int
anthy_priv_dic_select_next_entry(void)
{
  int nr = anthy_get_nr_values();
  if (nr > gOffset + 3) {
    gOffset += 3;
    return 0;
  }
  gOffset = 0;
  return anthy_select_next_column();
}

/** ̤ */
int
anthy_priv_dic_select_entry(const char *index)
{
  (void)index;
  return 0;
}

/** 򤵤Ƥñɤߤ */
char *
anthy_priv_dic_get_index(char *buf, int len)
{
  xstr *xs;
  xs = anthy_get_index_xstr();
  if (!xs) {
    return NULL;
  }
  if (anthy_snputxstr(buf, len, xs, dic_util_encoding) == len) {
    return NULL;
  }
  return buf;
}

/** 򤵤Ƥñɤߤ */
int
anthy_priv_dic_get_freq(void)
{
  return anthy_get_nth_value(gOffset + 2);
}

/** 򤵤Ƥñʻ */
char *
anthy_priv_dic_get_wtype(char *buf, int len)
{
  xstr *xs = anthy_get_nth_xstr(gOffset + 1);
  if (!xs) {
    return 0;
  }
  if (anthy_snputxstr(buf, len, xs, dic_util_encoding) == len) {
    return 0;
  }
  return buf;
}

/** 򤵤Ƥñ */
char *
anthy_priv_dic_get_word(char *buf, int len)
{
  xstr *xs = anthy_get_nth_xstr(gOffset);
  if (!xs) {
    return 0;
  }
  if (anthy_snputxstr(buf, len, xs, dic_util_encoding) == len) {
    return 0;
  }
  return buf;
}

/* ñϿ׻ */
static int
get_offset(xstr *word, xstr *wt)
{
  int i, nr = anthy_get_nr_values();
  for (i = 0; i < nr; i+= 3) {
    xstr *xs;
    xs = anthy_get_nth_xstr(i);
    if (!anthy_xstrcmp(word, xs)) {
      xs = anthy_get_nth_xstr(i + 1);
      if (!anthy_xstrcmp(wt, xs)) {
	return i;
      }
    }
  }
  return nr;
}

/** Ԥshrink/0ΤΤä
 */
static void
shrink_column(void)
{
  int nr, i, nth, res;
  struct priv_ent {
    xstr *word_xs;
    xstr *wt_xs;
    int freq;
  } * priv_array;
  xstr *index = anthy_get_index_xstr();

  nr = anthy_get_nr_values() / 3;
  priv_array = alloca(sizeof(struct priv_ent) * nr);
  nth = 0;
  for (i = 0; i < nr; i++) {
    int freq = anthy_get_nth_value(i * 3 + 2);
    if (freq) {
      priv_array[nth].word_xs = anthy_get_nth_xstr(i * 3);
      priv_array[nth].wt_xs = anthy_get_nth_xstr(i * 3 + 1);
      priv_array[nth].freq = freq;
      nth ++;
    }
  }
  if (nth == nr) {
    /* 0ΤΤ̵ä */
    return ;
  }
  if (!nth) {
    /* ĤʤäΤǡcolumnȺ */
    anthy_release_column();
    return ;
  }
  /* columnƺʤ */
  index = anthy_xstr_dup(index);
  anthy_release_column();
  res = anthy_select_column(index, 1);
  if (res == -1) {
    anthy_free_xstr(index);
    return ;
  }
  for (i = 0; i < nth; i++) {
    anthy_set_nth_xstr(nth * 3, priv_array[nth].word_xs);
    anthy_set_nth_xstr(nth * 3 + 1, priv_array[nth].wt_xs);
    anthy_set_nth_value(nth * 3 + 2, priv_array[nth].freq);
  }
  anthy_free_xstr(index);
}

/** ñϿ */
int
anthy_priv_dic_add_entry(const char *yomi, const char *word,
			 const char *wt_name, int freq)
{
  xstr *xs, *word_xs, *wt_xs;
  int offset, res;

  if (anthy_select_section("PRIVATEDIC", 1) == -1) {
    return ANTHY_DIC_UTIL_ERROR;
  }
  /* ɤߤ򤹤*/
  xs = anthy_cstr_to_xstr(yomi, 0);
  if (anthy_select_column(xs, 1) == -1) {
    anthy_free_xstr(xs);
    return ANTHY_DIC_UTIL_ERROR;
  }
  anthy_free_xstr(xs);

  /* Ʊñ줬Ǥˤä餽˾񤭡ʤкǸ */
  word_xs = anthy_cstr_to_xstr(word, 0);
  wt_xs = anthy_cstr_to_xstr(wt_name, 0);
  offset = get_offset(word_xs, wt_xs);
  if (offset < anthy_get_nr_values()) {
    res = ANTHY_DIC_UTIL_DUPLICATE;
  } else {
    res = ANTHY_DIC_UTIL_OK;
  }
  /* ñϿ */
  anthy_set_nth_xstr(offset, word_xs);
  anthy_free_xstr(word_xs);
  /* ʻϿ */
  anthy_set_nth_xstr(offset + 1, wt_xs);
  anthy_free_xstr(wt_xs);
  /*  */
  anthy_set_nth_value(offset + 2, freq);
  anthy_mark_column_used();
  /**/
  shrink_column();

  return res;
}

const char *
anthy_dic_util_get_anthydir(void)
{
  return anthy_conf_get_str("ANTHYDIR");
}