sj4/lib/sj3core/adddic.c

499 lines
10 KiB
C
Raw Normal View History

2008-01-11 11:57:08 +00:00
/*
* Copyright (c) 1991-1994 Sony Corporation
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL SONY CORPORATION BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* Except as contained in this notice, the name of Sony Corporation
* shall not be used in advertising or otherwise to promote the sale, use
* or other dealings in this Software without prior written authorization
* from Sony Corporation.
*
*/
/*
2026-06-19 01:49:53 +09:00
* $SonyRCSfile: adddic.c,v $
* $SonyRevision: 1.1 $
2008-01-11 11:57:08 +00:00
* $SonyDate: 1994/06/03 08:01:26 $
*/
#include <stdlib.h>
#include <string.h>
#include "sj_euc.h"
#include "sj_kcnv.h"
#include "sj_kanakan.h"
2026-06-19 01:49:53 +09:00
static void sprt_seg(TypeDicSeg, TypeDicOfs);
static void apnd_uidx(TypeDicSeg, u_char*, int);
static int cal_nextym(u_char*);
static int usr_freelen();
static u_int checkdict(u_char*, TypeGram);
u_int adddic(u_char* yomi, u_char* kanji, TypeGram hinsi) {
u_int err;
u_char yptr[MAXWDYOMILEN + 1];
u_char kptr[MAXWDKANJILEN + 1];
int klen;
TypeDicSeg segnum;
int saml;
TypeDicOfs dstofs;
TypeDicOfs douofs;
TypeDicOfs hnsofs;
TypeDicOfs knjofs;
TypeDicOfs nxtask;
TypeDicOfs newsiz;
TypeDicOfs oldsiz;
TypeDicOfs fresiz;
TypeIdxOfs freidx;
u_char* douptr;
u_char* dstptr;
TypeDicOfs pos;
u_char* p1;
int i;
int nlen, plen;
int size;
STDYIN* stdy;
STDYOUT stdydat;
if(err = addel_arg(yomi, kanji, hinsi, yptr, sizeof(yptr)))
2008-01-11 11:57:08 +00:00
return err;
inputyomi = yomi;
cnvstart = ystart = yptr;
2026-06-19 01:49:53 +09:00
cnvlen = sstrlen(yptr);
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(err = checkdict(kanji, hinsi))
2008-01-11 11:57:08 +00:00
return err;
2026-06-19 01:49:53 +09:00
if(!curdict->maxunit)
2008-01-11 11:57:08 +00:00
return AD_NOTUSERDICT;
klen = cvtknj(yomi, kanji, kptr);
2026-06-19 01:49:53 +09:00
newsiz = DOUONBLKSIZENUMBER + cnvlen + (klen != 0 ? klen + 1 : 0) + 2;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(newsiz > MAXDOUONBLK)
2008-01-11 11:57:08 +00:00
return AD_OVFLWDOUBLK;
2026-06-19 01:49:53 +09:00
if(newsiz > curdict->seglen)
2008-01-11 11:57:08 +00:00
return AD_OVFLWDOUBLK;
segnum = srchidx((TypeDicSeg)DICSEGBASE, cnvlen);
2026-06-19 01:49:53 +09:00
for(;;) {
2008-01-11 11:57:08 +00:00
(*curdict->getdic)(curdict, segnum);
2026-06-19 01:49:53 +09:00
i = srchkana(&p1, &saml);
2008-01-11 11:57:08 +00:00
dstofs = douofs = p1 - dicbuf;
hnsofs = knjofs = 0;
2026-06-19 01:49:53 +09:00
if(i) {
i = srchgram(p1, &p1, hinsi);
2008-01-11 11:57:08 +00:00
dstofs = hnsofs = p1 - dicbuf;
2026-06-19 01:49:53 +09:00
if(i) {
2008-01-11 11:57:08 +00:00
srchkanji(&p1, kptr, klen);
2026-06-19 01:49:53 +09:00
if(*p1 != HINSIBLKTERM)
2008-01-11 11:57:08 +00:00
return AD_AREXIST;
dstofs = knjofs = p1 - dicbuf;
}
}
p1 = dicbuf + douofs;
2026-06-19 01:49:53 +09:00
if(!hnsofs) {
2008-01-11 11:57:08 +00:00
oldsiz = 0;
2026-06-19 01:49:53 +09:00
nlen = cnvlen - saml;
newsiz = DOUONBLKSIZENUMBER + nlen;
2008-01-11 11:57:08 +00:00
nxtask = cal_nextym(p1);
2026-06-19 01:49:53 +09:00
} else {
2008-01-11 11:57:08 +00:00
oldsiz = newsiz = getsize(p1);
2026-06-19 01:49:53 +09:00
nxtask = 0;
2008-01-11 11:57:08 +00:00
}
2026-06-19 01:49:53 +09:00
if(!knjofs) newsiz += 2;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
newsiz += (klen != 0 ? klen + 1 : 0);
2008-01-11 11:57:08 +00:00
fresiz = usr_freelen();
2026-06-19 01:49:53 +09:00
if((unsigned int)(newsiz - oldsiz - nxtask) <=
(unsigned int)fresiz) break;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(segend(p1) && (segnum + 1 < curdict->segunit)) {
2008-01-11 11:57:08 +00:00
segnum++;
continue;
}
2026-06-19 01:49:53 +09:00
if(curdict->segunit >= curdict->maxunit)
2008-01-11 11:57:08 +00:00
return AD_OVFLWUSRDIC;
freidx = count_uidx();
2026-06-19 01:49:53 +09:00
if((unsigned int)(douofs + newsiz) <= curdict->seglen) {
if(hnsofs)
2008-01-11 11:57:08 +00:00
p1 = getntag(p1);
pos = p1 - dicbuf;
nlen = getnlen(p1);
2026-06-19 01:49:53 +09:00
if((unsigned int)freidx < getplen(p1) + nlen + 1)
2008-01-11 11:57:08 +00:00
return AD_OVFLWINDEX;
2026-06-19 01:49:53 +09:00
if((*curdict->putdic)(curdict, curdict->segunit))
2008-01-11 11:57:08 +00:00
return AD_OVFLWUSRDIC;
sprt_seg(segnum, pos);
nxtask = 0;
break;
}
2026-06-19 01:49:53 +09:00
if(segend(p1)) {
if((unsigned int)freidx < cnvlen + 1)
2008-01-11 11:57:08 +00:00
return AD_OVFLWINDEX;
2026-06-19 01:49:53 +09:00
if((*curdict->putdic)(curdict, curdict->segunit))
2008-01-11 11:57:08 +00:00
return AD_OVFLWUSRDIC;
curdict->segunit++;
(*curdict->getdic)(curdict, curdict->segunit - 1);
memset(dicbuf, DICSEGTERM, (int)curdict->seglen);
dicbuf[0] = 0;
(*curdict->putdic)(curdict, curdict->segunit - 1);
(*curdict->rszdic)(curdict, curdict->segunit);
mkidxtbl(curdict);
segnum++;
2026-06-19 01:49:53 +09:00
} else {
2008-01-11 11:57:08 +00:00
nlen = getnlen(p1);
2026-06-19 01:49:53 +09:00
if((unsigned int)freidx < getplen(p1) + nlen + 1)
2008-01-11 11:57:08 +00:00
return AD_OVFLWINDEX;
2026-06-19 01:49:53 +09:00
if((*curdict->putdic)(curdict, curdict->segunit))
2008-01-11 11:57:08 +00:00
return AD_OVFLWUSRDIC;
sprt_seg(segnum, douofs);
segnum++;
}
}
douptr = dicbuf + douofs;
2026-06-19 01:49:53 +09:00
if(nxtask) {
u_char* tp;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
size = getsize(douptr) - nxtask;
plen = getplen(douptr) + nxtask;
nlen = getnlen(douptr) - nxtask;
2008-01-11 11:57:08 +00:00
*douptr = (size >> 8);
2026-06-19 01:49:53 +09:00
if(nlen & 0x10)
2008-01-11 11:57:08 +00:00
*douptr |= NOKORIYOMITOP;
2026-06-19 01:49:53 +09:00
if(plen & 0x10)
2008-01-11 11:57:08 +00:00
*douptr |= ASSYUKUYOMITOP;
*(douptr + 1) = size;
*(douptr + 2) = ((nlen & 0x0f) | ((plen & 0x0f) << 4));
p1 = douptr + DOUONBLKSIZENUMBER;
tp = p1 + nxtask;
mvmemi(tp, p1, dicbuf + curdict->seglen - tp);
}
dstptr = dicbuf + dstofs;
2026-06-19 01:49:53 +09:00
size = newsiz - oldsiz;
p1 = dicbuf + curdict->seglen;
2008-01-11 11:57:08 +00:00
mvmemd(p1 - size, p1, p1 - dstptr - size);
2026-06-19 01:49:53 +09:00
if(!hnsofs) {
if(douofs == segtop() - dicbuf) {
2008-01-11 11:57:08 +00:00
chg_uidx(segnum, yptr, cnvlen);
}
2026-06-19 01:49:53 +09:00
*dstptr = 0;
nlen = cnvlen - saml;
if(nlen & 0x10)
2008-01-11 11:57:08 +00:00
*dstptr |= NOKORIYOMITOP;
2026-06-19 01:49:53 +09:00
if(saml & 0x10)
2008-01-11 11:57:08 +00:00
*dstptr |= ASSYUKUYOMITOP;
*dstptr++ |= (size >> 8);
2026-06-19 01:49:53 +09:00
*dstptr++ = size;
*dstptr++ = ((nlen & 0x0f) | ((saml & 0x0f) << 4));
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
while(saml < cnvlen) *dstptr++ = yptr[saml++];
} else {
plen = getplen(douptr);
nlen = getnlen(douptr);
2008-01-11 11:57:08 +00:00
*douptr = (newsiz >> 8);
2026-06-19 01:49:53 +09:00
if(nlen & 0x10)
2008-01-11 11:57:08 +00:00
*douptr |= NOKORIYOMITOP;
2026-06-19 01:49:53 +09:00
if(plen & 0x10)
2008-01-11 11:57:08 +00:00
*douptr |= ASSYUKUYOMITOP;
*(douptr + 1) = newsiz;
*(douptr + 2) = ((nlen & 0x0f) | ((plen & 0x0f) << 4));
}
2026-06-19 01:49:53 +09:00
if(!knjofs)
2008-01-11 11:57:08 +00:00
*dstptr++ = hinsi;
stdydat.stdy1.offset = dstptr - dicbuf;
2026-06-19 01:49:53 +09:00
for(saml = 0; saml < klen;)
2008-01-11 11:57:08 +00:00
*dstptr++ = kptr[saml++];
2026-06-19 01:49:53 +09:00
if(klen > 0)
2008-01-11 11:57:08 +00:00
*dstptr++ = KANJISTREND;
2026-06-19 01:49:53 +09:00
if(!knjofs)
2008-01-11 11:57:08 +00:00
*dstptr++ = HINSIBLKTERM;
(*curdict->putdic)(curdict, segnum);
2026-06-19 01:49:53 +09:00
if(StudyExist()) {
2008-01-11 11:57:08 +00:00
size -= nxtask;
2026-06-19 01:49:53 +09:00
for(i = STUDYCOUNT, stdy = STUDYDICT; i--; stdy++) {
if(stdy->dicid != curdict->dicid)
2008-01-11 11:57:08 +00:00
continue;
2026-06-19 01:49:53 +09:00
if(stdy->seg != segnum)
2008-01-11 11:57:08 +00:00
continue;
2026-06-19 01:49:53 +09:00
if(stdy->offset >= dstofs)
stdy->offset += size;
2008-01-11 11:57:08 +00:00
}
2026-06-19 01:49:53 +09:00
for(p1 = CLSTUDYDICT; !iseocl(p1); p1 = ClNextTag(p1)) {
err = sstrncmp(yptr, (u_char*)ClYomiPos(p1),
(int)ClYomiLen(p1));
if(err == MATCH) {
2008-01-11 11:57:08 +00:00
delclsub(p1);
mkclidx();
putcldic();
break;
2026-06-19 01:49:53 +09:00
} else if(err == OVER)
2008-01-11 11:57:08 +00:00
break;
}
}
2026-06-19 01:49:53 +09:00
stdydat.stdy1.seg = segnum;
stdydat.stdy1.dicid = curdict->dicid;
stdydat.stdy1.styno = 0;
stdydat.stdy1.sttkj = 0;
stdydat.stdy1.ka_kj = 0;
stdydat.stdy1.nmflg = 0;
stdydat.hinshi = hinsi;
stdydat.len = cnvlen;
stdydat.sttlen = 0;
stdydat.sttfg = 0;
stdydat.ka_fg = 0;
2008-01-11 11:57:08 +00:00
(void)study(&stdydat);
return AD_DONE;
}
static int
2026-06-19 01:49:53 +09:00
checksub(u_char* kanji, TypeGram grm) {
u_char* tagp;
u_char* ptr;
u_char* endp;
2008-01-11 11:57:08 +00:00
int flg, nlen;
int dounum;
u_char buf[MAXWDKANJILEN + 1];
2026-06-19 01:49:53 +09:00
dicinl = cnvlen;
2008-01-11 11:57:08 +00:00
dicsaml = 0;
prevseg = (TypeDicSeg)-1;
2026-06-19 01:49:53 +09:00
for(;;) {
if(!(tagp = srchdict(tagp)))
2008-01-11 11:57:08 +00:00
return 0;
2026-06-19 01:49:53 +09:00
if(cnvlen == getnlen(tagp) + getplen(tagp))
2008-01-11 11:57:08 +00:00
break;
}
dounum = 0;
nlen = getnlen(tagp);
2026-06-19 01:49:53 +09:00
ptr = tagp + DOUONBLKSIZENUMBER + nlen;
2008-01-11 11:57:08 +00:00
endp = tagp + getsize(tagp);
2026-06-19 01:49:53 +09:00
while(ptr < endp) {
2008-01-11 11:57:08 +00:00
flg = (grm == *ptr++);
2026-06-19 01:49:53 +09:00
while(*ptr != HINSIBLKTERM) {
if(flg) {
2008-01-11 11:57:08 +00:00
buf[getkanji(inputyomi, cnvlen, ptr, buf)] = 0;
2026-06-19 01:49:53 +09:00
if(cmpstr(kanji, buf))
2008-01-11 11:57:08 +00:00
return -1;
}
dounum++;
ptr = skipkstr(ptr);
}
ptr++;
}
return dounum;
}
static u_int
2026-06-19 01:49:53 +09:00
checkdict(u_char* kanji, TypeGram grm) {
DICTL* dp;
int dounum = 0;
int tmp;
DICT* tmpdict;
2008-01-11 11:57:08 +00:00
tmpdict = curdict;
2026-06-19 01:49:53 +09:00
for(dp = dictlist; dp; dp = dp->next) {
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
curdict = dp->dict;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if((tmp = checksub(kanji, grm)) < 0) {
2008-01-11 11:57:08 +00:00
curdict = tmpdict;
return AD_AREXIST;
}
dounum += tmp;
}
curdict = tmpdict;
2026-06-19 01:49:53 +09:00
if(MAXKOUHONUMBER <= dounum)
2008-01-11 11:57:08 +00:00
return AD_OVFLWDOUNUM;
return 0;
}
static int
2026-06-19 01:49:53 +09:00
cal_nextym(u_char* ptr) {
2008-01-11 11:57:08 +00:00
int count = 0;
2026-06-19 01:49:53 +09:00
u_char* src;
2008-01-11 11:57:08 +00:00
int len;
2026-06-19 01:49:53 +09:00
if(!segend(ptr)) {
2008-01-11 11:57:08 +00:00
src = cnvstart + getplen(ptr);
len = getnlen(ptr);
ptr += DOUONBLKSIZENUMBER;
2026-06-19 01:49:53 +09:00
while(len--) {
if(*ptr++ == *src++)
2008-01-11 11:57:08 +00:00
count++;
else
break;
}
}
return count;
}
static int
2026-06-19 01:49:53 +09:00
usr_freelen() {
u_char* ptr;
2008-01-11 11:57:08 +00:00
ptr = segtop();
2026-06-19 01:49:53 +09:00
while(!segend(ptr))
2008-01-11 11:57:08 +00:00
ptr += getsize(ptr);
return curdict->seglen - (ptr - dicbuf);
}
static void
2026-06-19 01:49:53 +09:00
sprt_seg(TypeDicSeg seg, TypeDicOfs ofs) {
TypeDicSeg s;
u_char* pos;
u_char* p;
u_char* q;
int i;
int j;
int ylen, nlen;
TypeDicID dicid;
u_char yomi[MAXWDYOMILEN];
STDYIN* stdy;
for(s = curdict->segunit - 1; s >= seg; s--) {
2008-01-11 11:57:08 +00:00
(*curdict->getdic)(curdict, s);
(*curdict->putdic)(curdict, s + 1);
}
curdict->segunit++;
(*curdict->rszdic)(curdict, curdict->segunit);
(*curdict->getdic)(curdict, seg + 1);
pos = dicbuf + ofs;
2026-06-19 01:49:53 +09:00
for(p = segtop(); (p <= pos) && !segend(p); p = q) {
q = getntag(p);
i = getplen(p);
2008-01-11 11:57:08 +00:00
nlen = getnlen(p);
2026-06-19 01:49:53 +09:00
j = (nlen != 0) ? nlen + 1 : 0;
2008-01-11 11:57:08 +00:00
p += DOUONBLKSIZENUMBER;
2026-06-19 01:49:53 +09:00
while(j-- > 0)
2008-01-11 11:57:08 +00:00
yomi[i++] = *p++;
}
nlen = getnlen(pos);
2026-06-19 01:49:53 +09:00
ylen = (i = getplen(pos)) + (j = nlen);
2008-01-11 11:57:08 +00:00
apnd_uidx(seg, yomi, ylen);
2026-06-19 01:49:53 +09:00
set_size(segtop(), getsize(pos) + i, 0, ylen);
2008-01-11 11:57:08 +00:00
mvmemi(yomi, (p = segtop() + DOUONBLKSIZENUMBER), ylen);
q = pos + DOUONBLKSIZENUMBER + j;
i = dicbuf + curdict->seglen - q;
mvmemi(q, p += ylen, i);
j = q - p;
p += i;
memset(p, DICSEGTERM, (dicbuf + curdict->seglen - p));
(*curdict->putdic)(curdict, seg + 1);
2026-06-19 01:49:53 +09:00
if(StudyExist()) {
dicid = curdict->dicid;
for(i = STUDYCOUNT, stdy = STUDYDICT; i-- > 0; stdy++) {
if(stdy->dicid != dicid)
2008-01-11 11:57:08 +00:00
continue;
2026-06-19 01:49:53 +09:00
else if(stdy->seg < seg)
2008-01-11 11:57:08 +00:00
continue;
2026-06-19 01:49:53 +09:00
else if(stdy->seg > seg)
stdy->seg += 1;
else if(stdy->offset >= ofs) {
stdy->seg += 1;
stdy->offset -= j;
2008-01-11 11:57:08 +00:00
}
}
}
(*curdict->getdic)(curdict, seg);
memset(dicbuf + ofs, DICSEGTERM, (int)(curdict->seglen - ofs));
(*curdict->putdic)(curdict, seg);
}
static void
2026-06-19 01:49:53 +09:00
apnd_uidx(TypeDicSeg seg, u_char* yomi, int len) {
u_char* p;
u_char* q;
2008-01-11 11:57:08 +00:00
p = get_idxptr(seg);
2026-06-19 01:49:53 +09:00
while(*p++);
2008-01-11 11:57:08 +00:00
q = idxbuf + curdict->idxlen;
mvmemd(q - (len + 1), q, (int)(q - (len + 1) - p));
2026-06-19 01:49:53 +09:00
while(len--)
2008-01-11 11:57:08 +00:00
*p++ = *yomi++;
*p = 0;
(*curdict->putidx)(curdict, 0);
mkidxtbl(curdict);
}