sj4/lib/sj3core/clstudy.c

256 lines
4.9 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: clstudy.c,v $
* $SonyRevision: 1.1 $
2008-01-11 11:57:08 +00:00
* $SonyDate: 1994/06/03 08:01:32 $
*/
#include "sj_kcnv.h"
#include "sj_kanakan.h"
2026-06-19 01:49:53 +09:00
static u_char *delcldata(), *srchclpos();
static int delclold();
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
int clstudy(u_char* yomi1, u_char* yomi2, STDYOUT* stdy) {
2008-01-11 11:57:08 +00:00
u_char ytmp[CLSTDY_TMPLEN];
2026-06-19 01:49:53 +09:00
u_char *dst, *ptr;
2008-01-11 11:57:08 +00:00
int ret;
int len1, len2;
int alen, blen;
int i;
2026-06-19 01:49:53 +09:00
if(!StudyExist()) return CLSTUDYNODICT;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(!yomi1 || !*yomi1) return CLSTUDYPARAERR;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(!yomi2 || !*yomi2) {
if(!sj2cd_str(yomi1, ytmp, CLSTDY_TMPLEN))
2008-01-11 11:57:08 +00:00
return CLSTUDYYOMIERR;
(void)delcldata(ytmp);
ret = CLSTUDYNORMEND;
2026-06-19 01:49:53 +09:00
} else if(!stdy)
2008-01-11 11:57:08 +00:00
return CLSTUDYPARAERR;
else {
2026-06-19 01:49:53 +09:00
if(!sj2cd_str(yomi1, ytmp, CLSTDY_TMPLEN))
2008-01-11 11:57:08 +00:00
return CLSTUDYYOMIERR;
len1 = sstrlen(ytmp);
2026-06-19 01:49:53 +09:00
if(!sj2cd_str(yomi2, ytmp + len1, CLSTDY_TMPLEN - len1))
2008-01-11 11:57:08 +00:00
return CLSTUDYYOMIERR;
2026-06-19 01:49:53 +09:00
len2 = len1 + stdy->len;
2008-01-11 11:57:08 +00:00
ytmp[len2] = 0;
alen = len2 + CL_HDLEN;
2026-06-19 01:49:53 +09:00
if(alen + 1 >= CLSTUDYLEN) return CLSTUDYNOAREA;
2008-01-11 11:57:08 +00:00
blen = CLSTUDYTAIL - delcldata(ytmp) - 1;
2026-06-19 01:49:53 +09:00
while(blen < alen) {
if(!(i = delclold())) break;
2008-01-11 11:57:08 +00:00
blen += i;
}
2026-06-19 01:49:53 +09:00
if(blen >= alen) {
2008-01-11 11:57:08 +00:00
dst = srchclpos(ytmp);
ptr = CLSTUDYTAIL - alen;
mvmemd(ptr, CLSTUDYTAIL, (ptr - dst));
2026-06-19 01:49:53 +09:00
ClYomiLen(dst) = (u_char)len2;
ClGramCode(dst) = stdy->hinshi;
2008-01-11 11:57:08 +00:00
ClYomi1Len(dst) = (u_char)len1;
ClSetNum(dst, 1);
memcpy(ClYomiPos(dst), ytmp, len2);
ret = CLSTUDYNORMEND;
2026-06-19 01:49:53 +09:00
} else
2008-01-11 11:57:08 +00:00
ret = CLSTUDYNOAREA;
}
mkclidx();
putcldic();
return ret;
}
2026-06-19 01:49:53 +09:00
static u_char*
delcldata(u_char* ycode) {
u_char* tagptr;
u_char* ptr1;
u_char* ptr2;
2008-01-11 11:57:08 +00:00
int len1;
tagptr = CLSTUDYDICT;
2026-06-19 01:49:53 +09:00
while(!iseocl(tagptr)) {
2008-01-11 11:57:08 +00:00
ptr1 = ycode;
ptr2 = ClYomiPos(tagptr);
len1 = ClYomiLen(tagptr);
2026-06-19 01:49:53 +09:00
while(*ptr1 && len1) {
if(*ptr1 != *ptr2) break;
2008-01-11 11:57:08 +00:00
ptr1++;
ptr2++;
len1--;
}
2026-06-19 01:49:53 +09:00
if(!*ptr1 || !len1) {
2008-01-11 11:57:08 +00:00
delclsub(tagptr);
2026-06-19 01:49:53 +09:00
} else {
2008-01-11 11:57:08 +00:00
tagptr = ClNextTag(tagptr);
}
}
return tagptr;
}
2026-06-19 01:49:53 +09:00
void delclsub(u_char* target) {
u_char* src;
2008-01-11 11:57:08 +00:00
int num;
int tmp;
int len;
num = ClGetNum(target);
len = ClBlkLen(target);
src = target + len;
mvmemi(src, target, (CLSTUDYTAIL - src));
memset(CLSTUDYTAIL - len, 0, len);
src = CLSTUDYDICT;
2026-06-19 01:49:53 +09:00
while(!iseocl(src)) {
2008-01-11 11:57:08 +00:00
tmp = ClGetNum(src);
2026-06-19 01:49:53 +09:00
if(tmp > num) {
2008-01-11 11:57:08 +00:00
tmp -= 1;
ClSetNum(src, tmp);
}
src = ClNextTag(src);
}
}
static int
2026-06-19 01:49:53 +09:00
delclold() {
u_char* tagptr;
u_char* target;
2008-01-11 11:57:08 +00:00
int num;
int tmp;
target = NULL;
2026-06-19 01:49:53 +09:00
num = 0;
2008-01-11 11:57:08 +00:00
tagptr = CLSTUDYDICT;
2026-06-19 01:49:53 +09:00
while(!iseocl(tagptr)) {
2008-01-11 11:57:08 +00:00
tmp = ClGetNum(tagptr);
2026-06-19 01:49:53 +09:00
if(tmp >= num) {
num = tmp;
2008-01-11 11:57:08 +00:00
target = tagptr;
}
tagptr = ClNextTag(tagptr);
}
2026-06-19 01:49:53 +09:00
if(target) {
2008-01-11 11:57:08 +00:00
num = ClBlkLen(tagptr);
delclsub(target);
}
return num;
}
2026-06-19 01:49:53 +09:00
static u_char*
srchclpos(u_char* ycode) {
u_char* tagptr;
u_char* target;
u_char* ptr1;
u_char* ptr2;
2008-01-11 11:57:08 +00:00
int len;
int tmp;
target = NULL;
tagptr = CLSTUDYDICT;
2026-06-19 01:49:53 +09:00
while(!iseocl(tagptr)) {
2008-01-11 11:57:08 +00:00
tmp = ClGetNum(tagptr);
tmp++;
ClSetNum(tagptr, tmp);
ptr1 = ycode;
ptr2 = ClYomiPos(tagptr);
len = ClYomiLen(tagptr);
2026-06-19 01:49:53 +09:00
while(*ptr1 && len) {
if(*ptr1 != *ptr2) break;
2008-01-11 11:57:08 +00:00
ptr1++;
ptr2++;
len--;
}
2026-06-19 01:49:53 +09:00
if(!target) {
if(!len)
2008-01-11 11:57:08 +00:00
;
2026-06-19 01:49:53 +09:00
else if(!*ptr1)
2008-01-11 11:57:08 +00:00
target = tagptr;
2026-06-19 01:49:53 +09:00
else if(*ptr1 < *ptr2)
2008-01-11 11:57:08 +00:00
target = tagptr;
}
tagptr = ClNextTag(tagptr);
}
return target ? target : tagptr;
}
2026-06-19 01:49:53 +09:00
void mkclidx() {
u_char* ptr;
2008-01-11 11:57:08 +00:00
int num;
int st, ed;
2026-06-19 01:49:53 +09:00
if(!StudyExist()) return;
2008-01-11 11:57:08 +00:00
ptr = CLSTUDYDICT;
2026-06-19 01:49:53 +09:00
for(num = st = 0; st < 0x100; num++, st = ed) {
2008-01-11 11:57:08 +00:00
ed = st + CLSTUDYSTEP;
2026-06-19 01:49:53 +09:00
while(!iseocl(ptr)) {
if((unsigned int)*ClYomiPos(ptr) >= st) break;
2008-01-11 11:57:08 +00:00
ptr = ClNextTag(ptr);
}
2026-06-19 01:49:53 +09:00
if(ClYomiLen(ptr) && ((int)*ClYomiPos(ptr) < ed))
2008-01-11 11:57:08 +00:00
CLSTUDYIDX[num] = ptr - CLSTUDYDICT;
else
CLSTUDYIDX[num] = (u_short)-1;
}
}