1 /*
2  * KAKASI (Kanji Kana Simple inversion program)
3  * $Id: mkkanwa.c,v 1.6 2006-06-06 06:43:10 knok Exp $
4  * Copyright (C) 1992
5  * Hironobu Takahashi (takahasi@tiny.or.jp)
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either versions 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with KAKASI, see the file COPYING.  If not, write to the Free
19  * Software Foundation Inc., 59 Temple Place - Suite 330, Boston, MA
20  * 02111-1307, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26 
27 #include <stdio.h>
28 #ifdef HAVE_STRING_H
29 # include <string.h>
30 #else
31 # include <strings.h>
32 #endif
33 #include <stdlib.h>
34 #include "getopt.h"
35 #include "kakasi.h"
36 
37 /* new dictionary format flag */
38 int new_format = 1;
39 
40 static void
usage(argv)41 usage(argv)
42      char **argv;
43 {
44     (void)fprintf(stderr, "usage: %s kanwadict dict1 [dict2,,,]\n", argv[0]);
45     exit (2);
46 }
47 
48 static void
make_kanwa_dict(kdict)49 make_kanwa_dict(kdict)
50      FILE *kdict;
51 {
52     unsigned char length;
53     int i, j, count;
54     struct kanji_yomi *ptr;
55 
56     char magic[] = "KAKASI";
57     unsigned short emark = 0xFEFF; /* endian mark */
58     int kanwa_offset_offset = 0;
59     int kanwa_offset = 0;
60     int opt_type, opt_length;
61 
62     if (new_format) {
63 	/* new dict format support 2003/03/11 ukai
64 	 * see kakasi.h
65 	 */
66 	fwrite(magic, 6, 1, kdict);
67 	fwrite(&emark, 2, 1, kdict);
68 	kanwa_offset_offset = ftell(kdict);
69 	fwrite(&kanwa_offset, sizeof(int), 1, kdict);
70 	opt_type = 0; opt_length = 0;
71 	fwrite(&opt_type, sizeof(int), 1, kdict);
72 	fwrite(&opt_length, sizeof(int), 1, kdict);
73 
74 	kanwa_offset = ftell(kdict);
75 	fseek(kdict, kanwa_offset_offset, SEEK_SET);
76 	fwrite(&kanwa_offset, sizeof(int), 1, kdict);
77 
78 	fseek(kdict, kanwa_offset + sizeof kanwa, SEEK_SET);
79     }
80 
81     for (i = 0x20; i < 0x7f; ++ i)
82 	for (j = 0x20; j < 0x7f; ++ j) {
83 	    kanwa[i-0x20][j-0x20].index = ftell(kdict);
84 	    count = 0;
85 	    for (ptr = jisyo_table[i][j]; ptr != NULL;
86 		 ptr = ptr->next) {
87 		fwrite(&(ptr->tail), 1, 1, kdict);
88 		length = ptr->length - ((ptr->tail == 0) ? 2 : 3);
89 		fwrite(&length, 1, 1, kdict);
90 		fwrite(ptr->kanji, (int)length, 1, kdict);
91 		length = strlen((const char*)ptr->yomi);
92 		fwrite(&length, 1, 1, kdict);
93 		fwrite(ptr->yomi, (int)length, 1, kdict);
94 		++count;
95 	    }
96 	    kanwa[i-0x20][j-0x20].entry = count;
97 	}
98 
99     if (new_format)
100       fseek(kdict, kanwa_offset, SEEK_SET);
101     else
102       fseek(kdict, 0, 0L);
103     fwrite((char *)kanwa, sizeof kanwa, 1, kdict);
104 }
105 
106 int
main(argc,argv)107 main(argc, argv)
108      int argc;
109      char **argv;
110 {
111     FILE *kdict;
112     static char options[]="ho";
113     int c;
114     extern int optind;
115 
116     /* An optional extraction. = It must not be this version. */
117     while ((c = getopt(argc, argv, options)) != -1) {
118 	switch (c) {
119 	  case 'o':
120 	    new_format = 0;
121 	    break;
122 	  case 'h':
123 	  default:
124 	    usage(argv);
125 	}
126     }
127     if (optind >= argc)
128 	usage(argv);
129     if ((kdict = fopen(argv[optind],"wb")) == NULL) {
130 	perror(argv[optind]);
131 	exit(2);
132     }
133 
134     /* The preparation of the change table of ITAIJI. */
135     mkitaijitbl();
136 
137     /* Reading of the dictionary. */
138     init_jisyo();
139     for (optind ++ ; optind < argc; optind++)
140 	add_jisyo(argv[optind]);
141 
142     make_kanwa_dict(kdict);
143     fclose(kdict);
144     return 0;
145 }
146