2 * ¼½ñÁàºîÍѤΥ桼¥Æ¥£¥ê¥Æ¥£¥³¥Þ¥ó¥É
4 * ¼½ñ¤Î¥é¥¤¥Ö¥é¥êÆâÉô¤Î·Á¼°¤È³°Éô¤Î·Á¼°¤ÎÁê¸ßÊÑ´¹¤ò¹Ô¤¦
14 * Funded by IPA̤Ƨ¥½¥Õ¥È¥¦¥§¥¢ÁϤ»ö¶È 2001 9/22
16 * Copyright (C) 2000-2007 TABATA Yusuke
19 This library is free software; you can redistribute it and/or
20 modify it under the terms of the GNU Lesser General Public
21 License as published by the Free Software Foundation; either
22 version 2 of the License, or (at your option) any later version.
24 This library is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 Lesser General Public License for more details.
29 You should have received a copy of the GNU Lesser General Public
30 License along with this library; if not, write to the Free Software
31 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 #include <anthy/anthy.h>
38 #include <anthy/dicutil.h>
40 #include <anthy/xstr.h>
48 #define TYPETAB "typetab"
49 #define USAGE_TEXT "dic-tool-usage.txt"
52 "Anthy-dic-util [options]\n"\
53 " --help: Show this usage text\n"\
54 " --version: Show version\n"\
55 " --dump: Dump dictionary\n"\
56 " --load: Load dictionary\n"\
57 " --append: Append dictionary\n"\
58 " --utf8: Use utf8 encoding\n"\
59 " --personality=NAME: use NAME as a name of personality\n"
62 static int command = UNSPEC;
63 static int encoding = ANTHY_EUC_JP_ENCODING;
66 static const char *personality = "";
68 /* ÊÑ¿ô̾¤ÈÃͤΥڥ¢ */
75 /* ÉÊ»ì¤Î¥Ñ¥é¥á¡¼¥¿¤«¤éÉÊ»ì̾¤òÆÀ¤ë¤¿¤á¤Î¥Æ¡¼¥Ö¥ë */
77 struct trans_tab *next;
78 char *type_name; /* ÆâÉô¤Ç¤Î·¿¤Î̾Á° T35¤È¤« */
79 struct var var_list; /* ·¿¤ò·èÄꤹ¤ë¤¿¤á¤Î¥Ñ¥é¥á¡¼¥¿ */
94 fp = fopen(TYPETAB, "r");
98 fn = strdup(anthy_dic_util_get_anthydir());
99 fn = realloc(fn, strlen(fn) + strlen(TYPETAB) + 4);
107 open_usage_file(void)
110 /* ¥«¥ì¥ó¥È¥Ç¥£¥ì¥¯¥È¥ê¤Ë¤¢¤ë¾ì¹ç¤Ï¡¢¤½¤ì¤ò»ÈÍѤ¹¤ë */
111 fp = fopen(USAGE_TEXT, "r");
113 /* ¥¤¥ó¥¹¥È¡¼¥ë¤µ¤ì¤¿¤â¤Î¤ò»ÈÍÑ */
115 fn = strdup(anthy_dic_util_get_anthydir());
116 fn = realloc(fn, strlen(fn) + strlen(USAGE_TEXT) + 10);
117 strcat(fn, "/" USAGE_TEXT);
124 print_usage_text(void)
127 FILE *fp = open_usage_file();
129 printf("# Anthy-dic-tool\n#\n");
132 fprintf(stdout, "#" PACKAGE " " VERSION "\n");
133 if (encoding == ANTHY_UTF8_ENCODING) {
136 /* ¤½¤Î¤Þ¤Þ¥Õ¥¡¥¤¥ë¤ÎÆâÍƤò½ÐÎÏ */
137 while (fgets(buf, 256, fp)) {
138 if (encoding == ANTHY_UTF8_ENCODING) {
140 s = anthy_conv_euc_to_utf8(buf);
151 read_line(char *buf, int len, FILE *fp)
153 while (fgets(buf, len, fp)) {
157 if (l > 0 && buf[l-1] == '\n') {
160 if (l > 1 && buf[l-2] == '\r') {
171 read_typetab_var(struct var *head, FILE *fp, int table)
174 char var[256], eq[256], val[256];
176 if (!read_line(buf, 256, fp)) {
179 if (sscanf(buf, "%s %s %s", var, eq, val) != 3) {
183 v = malloc(sizeof(struct var));
184 if (encoding == ANTHY_UTF8_ENCODING && table) {
186 v->var_name = anthy_conv_euc_to_utf8(var);
187 v->val = anthy_conv_euc_to_utf8(val);
190 v->var_name = strdup(var);
191 v->val = strdup(val);
195 v->next = head->next;
202 read_typetab_entry(FILE *fp)
204 char buf[256], type_name[257];
207 /* °ì¹ÔÌܤÎÉÊ»ì̾¤òÆɤà */
209 res = read_line(buf, 256, fp);
213 } while (res[0] == '#' || res[0] == 0);
214 t = malloc(sizeof(struct trans_tab));
215 sprintf(type_name, "#%s", buf);
216 t->type_name = strdup(type_name);
217 t->var_list.next = 0;
218 /* ¥Ñ¥é¥á¡¼¥¿¤òÆɤà */
219 while(!read_typetab_var(&t->var_list, fp, 1));
221 t->next = trans_tab_list.next;
222 trans_tab_list.next = t;
229 FILE *fp = open_typetab();
231 printf("Failed to open type table.\n");
234 while (!read_typetab_entry(fp));
237 static struct trans_tab *
238 find_trans_tab_by_name(char *name)
241 for (t = trans_tab_list.next; t; t = t->next) {
242 if (!strcmp(t->type_name, name)) {
250 print_word_type(struct trans_tab *t)
253 for (v = t->var_list.next; v; v = v->next) {
254 printf("%s\t=\t%s\n", v->var_name, v->val);
262 if (anthy_priv_dic_select_first_entry() == -1) {
263 printf("# Failed to read private dictionary\n"
264 "# There are no words or error occurred?\n"
269 char idx[100], wt[100], w[100];
271 if (anthy_priv_dic_get_index(idx, 100) &&
272 anthy_priv_dic_get_wtype(wt, 100) &&
273 anthy_priv_dic_get_word(w, 100)) {
275 freq = anthy_priv_dic_get_freq();
276 t = find_trans_tab_by_name(wt);
278 printf("%s %d %s\n", idx, freq, w);
282 printf("# Failed to determine word type of %s(%s).\n", w, wt);
285 } while (anthy_priv_dic_select_next_entry() == 0);
289 open_input_file(void)
294 fp_in = fopen(fn, "r");
301 /* v¤¬ s¤ÎÃæ¤Ë¤¢¤ë¤« */
303 match_var(struct var *v, struct var *s)
306 for (i = s->next; i; i = i->next) {
307 if (!strcmp(v->var_name, i->var_name) &&
308 !strcmp(v->val, i->val)) {
315 /* v1¤¬v2¤ÎÉôʬ½¸¹ç¤«¤É¤¦¤« */
317 var_list_subset_p(struct var *v1, struct var *v2)
320 for (v = v1->next; v; v = v->next) {
321 if (!match_var(v, v2)) {
334 while(!read_typetab_var(&v, fp_in, 0));
335 for (t = trans_tab_list.next; t; t = t->next) {
336 if (var_list_subset_p(&t->var_list, &v) &&
337 var_list_subset_p(&v, &t->var_list)) {
345 find_head(char *yomi, char *freq, char *w)
349 if (!read_line(buf, 256, fp_in)) {
352 } while (sscanf(buf, "%s %s %[^\n]",yomi, freq, w) != 3);
359 char yomi[256], freq[256], w[256];
360 while (!find_head(yomi, freq, w)) {
361 char *wt = find_wt();
364 ret = anthy_priv_dic_add_entry(yomi, w, wt, atoi(freq));
366 printf("Failed to register %s\n", yomi);
368 printf("Word %s is registered as %s\n", yomi, wt);
371 printf("Failed to find the type of %s.\n", yomi);
379 printf("Anthy-dic-util "VERSION".\n");
384 parse_args(int argc, char **argv)
387 for (i = 1 ; i < argc ; i++) {
388 if (!strncmp(argv[i], "--", 2)) {
389 char *opt = &argv[i][2];
390 if (!strcmp(opt, "help")) {
392 } else if (!strcmp(opt, "version")){
394 } else if (!strcmp(opt, "dump")) {
396 } else if (!strcmp(opt,"append") ){
397 command = APPEND_DIC;
398 } else if (!strncmp(opt, "personality=", 12)) {
399 personality = &opt[12];
400 } else if (!strcmp(opt, "utf8")) {
401 encoding = ANTHY_UTF8_ENCODING;
402 } else if (!strcmp(opt, "eucjp")) {
403 encoding = ANTHY_EUC_JP_ENCODING;
404 } else if (!strcmp(opt, "load")) {
416 anthy_dic_util_init();
417 anthy_dic_util_set_encoding(encoding);
422 main(int argc,char **argv)
425 parse_args(argc, argv);
434 anthy_priv_dic_delete();