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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
/*
* KAKASI (Kanji Kana Simple inversion program)
* $Header: /home/rug/cvs/kakasi/src/dict.c,v 1.2 1999/10/27 17:28:35 rug Exp $
* Copyright (C) 1992
* Hironobu Takahashi (takahasi@tiny.or.jp)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either versions 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KAKASI, see the file COPYING. If not, write to the Free
* Software Foundation Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
/*
Modified by NOKUBI Takatsugu <knok@daionet.gr.jp>
1999/03/04
Rename PERLMOD macro to LIBRARY
1999/01/11
Add PERLMOD macro.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#ifdef HAVE_MALLOC_C
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include "kakasi.h"
#include "jj2.h"
#define BUFLEN 1024
#define IALLOCSIZE (1024*100)
#define CELLALLOC 5000
#ifndef KANWADICT
#define KANWADICT "./kanwadict"
#endif
static unsigned char *
charalloc(length)
int length;
{
static int point;
static unsigned char *ptr = NULL;
unsigned char *ret;
if ((ptr == NULL) || (point+length >= IALLOCSIZE)) {
ptr = (unsigned char *)malloc(IALLOCSIZE);
point = 0;
}
ret = ptr+point;
point += length;
return ret;
}
static struct kanji_yomi *
cellalloc()
{
static int point;
static struct kanji_yomi *ptr = NULL;
if ((ptr == NULL) || (point >= CELLALLOC)) {
char *cptr;
cptr = malloc((CELLALLOC+1)*sizeof(struct kanji_yomi));
if ((int)cptr & 7) cptr += 8 - ((int)cptr & 7);
ptr = (struct kanji_yomi *) cptr;
point = 0;
}
++ point;
return ptr ++;
}
void init_jisyo()
{
int c1, c2;
for(c1 = 0; c1 < 0x80; c1 ++)
for(c2 = 0; c2 < 0x80; c2 ++)
jisyo_table[c1][c2] = NULL;
}
static void jis2ujis_jisyo(buffer)
unsigned char *buffer;
{
unsigned char *p, *q;
int kanji=0;
p = q = buffer;
while(*p != '\0') {
if (*p == '\033') {
if ((p[1] == '$') &&
((p[2] == '@') || (p[2] == 'B'))) {
kanji = 1;
p += 2;
} else if ((p[1] == '(') &&
((p[2] == 'B') || (p[2] == 'J'))) {
kanji = 0;
p += 2;
} else {
*(q ++) = *p;
}
} else {
if (kanji) {
*(q ++) = *(p ++) | 0x80;
*(q ++) = *p | 0x80;
} else {
*(q ++) = *p;
}
}
++ p;
}
*q = '\0';
}
static void add_item(yomi, kanji, tail)
unsigned char *yomi;
unsigned char *kanji;
int tail;
{
unsigned char *q, *ptr_kanji, *ptr_yomi;
struct kanji_yomi *ptr_kanji_yomi, **ptr;
int length, c1, c2;
/* Is the head a kanji? */
if (kanji[0] < 0xb0) return;
/* Isn't a HANKAKU character contained? Convert ITAIJI. */
for (q = kanji;; q += 2) {
c1 = q[0]; c2 = q[1];
if ((c1 == '\0') || (c2 == '\0')) break;
if ((c1 <= 0xa0) || (c2 <= 0xa0)) return;
itaijiknj(&c1, &c2);
q[0] = c1; q[1] = c2;
}
/* Isn't the one except for the KANA contained? A KATAKANA changes into the HIRAGANA. */
for (q = yomi; (q[0] != '\0') && (q[1] != '\0'); q += 2) {
if (*q < 0xa1) return;
if (*q == 0xa5) *q = 0xa4;
if ((*q != 0xa4) &&
((q[0] != 0xa1) || (q[1] != 0xbc)) && /* Prolonged sound */
((q[0] != 0xa1) || (q[1] != 0xab)) && /* Sonant */
((q[0] != 0xa1) || (q[1] != 0xac))) /* Half-sonant */
return;
}
/* A cell because of the reading is made. */
#ifdef __BEOS__
length = strlen((const char *)kanji);
ptr_kanji = charalloc(length-1);
strcpy((char *)ptr_kanji, (const char *)(kanji+2));
ptr_yomi = charalloc(strlen((const char *)yomi)+1);
strcpy((char *)ptr_yomi, (const char *)yomi);
#else
length = strlen(kanji);
ptr_kanji = charalloc(length-1);
strcpy(ptr_kanji, kanji+2);
ptr_yomi = charalloc(strlen(yomi)+1);
strcpy(ptr_yomi, yomi);
#endif
ptr_kanji_yomi = cellalloc();
ptr_kanji_yomi->next = NULL;
ptr_kanji_yomi->length = tail ? length+1 : length;
ptr_kanji_yomi->kanji = ptr_kanji;
ptr_kanji_yomi->yomi = ptr_yomi;
ptr_kanji_yomi->tail = tail;
/* It is connected in search of the end of the link of the internal dictionary. */
for (ptr = &(jisyo_table[kanji[0]-0x80][kanji[1]-0x80]);
*ptr != NULL;
ptr = &((*ptr)->next));
*ptr = ptr_kanji_yomi;
}
void add_jisyo(filename)
char *filename;
{
FILE *jisyo_fp;
unsigned char buffer[BUFLEN];
unsigned char *p;
unsigned char *yomi, *kanji;
int tail;
extern char *ialloc();
if ((jisyo_fp = fopen(filename, "rb")) == NULL) {
perror(filename);
exit(0);
}
#ifdef __BEOS__
while(fgets((char *)buffer, BUFLEN, jisyo_fp)) {
#else
while(fgets(buffer, BUFLEN, jisyo_fp)) {
#endif
/* If there is the one except for the KANA at the head, to the next */
if ((buffer[0] < 0xa0) && (buffer[0] != '\033')) continue;
/* A line is changed into ujis. */
jis2ujis_jisyo(buffer);
yomi = buffer;
/* The next ward is looked for. */
for (p = buffer; (*p != ' ') && (*p != '\011') && (*p != ','); ++ p) {
if ((*p == '\0') || (*p == '\n')) goto next_line;
}
if (isalpha(p[-1])) { /* An OKURIGANA is given if the last character is an alphabet. */
tail = p[-1];
p[-1] = '\0';
} else {
tail = 0;
p[0] = '\0';
}
/* The next ward is looked for. */
for (++ p; (*p == ' ') || (*p == '\011') || (*p == ','); ++ p) {
if ((*p == '\0') || (*p == '\n')) goto next_line;
}
if (*p == '/') { /* It seems to be the dictionary of SKK. */
for (;;) {
kanji = p+1;
/* The next ward is looked for. */
for (++ p; (*p != '/'); ++ p) {
if ((*p == '\0')||(*p == '\n')||(*p == '[')) goto next_line;
}
*p = '\0';
add_item(yomi, kanji, tail);
}
} else { /* It seems to be a standard dictionary. */
kanji = p;
/* The next ward is looked for. */
for (++ p;
(*p != ' ') && (*p != '\n') && (*p != '\011') &&
(*p != '\0') && (*p != ',')
; ++ p) {
;
}
*p = '\0';
add_item(yomi, kanji, tail);
}
next_line:;
}
fclose(jisyo_fp);
}
/* The initialization of kanwa is done. Reading kanwa_load is actually cleared in
kanwa the part at the head. */
#ifdef LIBRARY
FILE *kanwadict = NULL;
#else
static FILE *kanwadict;
#endif
void init_kanwa()
{
int i, j;
char *kanwadictpath;
kanwadictpath = (char*)getenv("KANWADICTPATH");
if (kanwadictpath == (char*)NULL)
kanwadictpath = (char*)getenv("KANWADICT");
if (kanwadictpath == (char*)NULL)
kanwadictpath = KANWADICT;
if ((kanwadict = fopen(kanwadictpath,"rb")) == NULL) {
perror(kanwadictpath);
exit(2);
}
if (fread((char *)kanwa, sizeof kanwa, 1, kanwadict) != 1) {
perror(kanwadictpath);
}
for (i = 0; i < 0x80; ++ i)
for (j = 0; j < 0x80; ++ j)
kanwa_load[i][j] = 0;
}
/* An applicable part from kanwa if necessary is drawn. */
void add_kanwa(c1, c2)
int c1;
int c2;
{
unsigned char *ptr_yomi, *ptr_kanji;
struct kanji_yomi *ptr_kanji_yomi, **ptr;
int i;
unsigned char tail, length;
c1 &= 0x7f;
c2 &= 0x7f;
if (kanwa_load[c1][c2]) return;
kanwa_load[c1][c2] = 1;
/* It is finished when there is no description in the dictionary just in case. */
if (kanwa[c1-0x20][c2-0x20].entry == 0) return;
/* It is moved to the fixed position of kanwadict. */
fseek(kanwadict, (long)(kanwa[c1-0x20][c2-0x20].index), 0L);
/* The end of the link of the internal dictionary is looked for. */
for (ptr = &(jisyo_table[c1][c2]);
*ptr != NULL;
ptr = &((*ptr)->next));
for (i = 0; i < kanwa[c1-0x20][c2-0x20].entry; ++ i) {
ptr_kanji_yomi = cellalloc();
fread(&tail, 1, 1, kanwadict);
ptr_kanji_yomi->tail = tail;
fread(&length, 1, 1, kanwadict);
ptr_kanji = charalloc(length+1);
fread(ptr_kanji, (int)length, 1, kanwadict);
ptr_kanji[length] = '\0';
ptr_kanji_yomi->kanji = ptr_kanji;
ptr_kanji_yomi->length = length + ((tail == 0) ? 2 : 3);
fread(&length, 1, 1, kanwadict);
ptr_yomi = charalloc(length+1);
fread(ptr_yomi, (int)length, 1, kanwadict);
ptr_yomi[length] = '\0';
ptr_kanji_yomi->yomi = ptr_yomi;
ptr_kanji_yomi->next = NULL;
*ptr = ptr_kanji_yomi;
ptr = &(ptr_kanji_yomi->next);
}
}
|