sj4/lib/sj3core/mknumber.c

321 lines
6.4 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: mknumber.c,v $
* $SonyRevision: 1.1 $
2008-01-11 11:57:08 +00:00
* $SonyDate: 1994/06/03 08:02:05 $
*/
#include "sj_kcnv.h"
#include "sj_yomi.h"
#include "sj_suuji.h"
#include "sj_kanakan.h"
static int
2026-06-19 01:49:53 +09:00
word2char(u_short wd, u_char* tbl, int keta, int flg) {
2008-01-11 11:57:08 +00:00
int mask;
int num;
2026-06-19 01:49:53 +09:00
u_char* src;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
for(mask = 0x1000; mask; mask >>= 4) {
switch(num = (unsigned int)wd / mask) {
2008-01-11 11:57:08 +00:00
case 0:
break;
case _NUM0:
2026-06-19 01:49:53 +09:00
if(!flg) break;
2008-01-11 11:57:08 +00:00
default:
2026-06-19 01:49:53 +09:00
src = tbl + (num - _NUM0) * 2;
2008-01-11 11:57:08 +00:00
*kanjitmp++ = *src++;
*kanjitmp++ = *src;
2026-06-19 01:49:53 +09:00
flg = TRUE;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(keta && !(keta % 3)) {
2008-01-11 11:57:08 +00:00
*kanjitmp++ = COMMAHIGH;
*kanjitmp++ = COMMALOW;
}
}
2026-06-19 01:49:53 +09:00
if(keta) keta--;
2008-01-11 11:57:08 +00:00
wd &= (mask - 1);
}
return flg;
}
static void
2026-06-19 01:49:53 +09:00
words2num(u_short* wd, u_char* tbl, int flgc) {
2008-01-11 11:57:08 +00:00
int i;
int keta;
int flg;
2026-06-19 01:49:53 +09:00
u_char* kp = kanjitmp;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(flgc) {
2008-01-11 11:57:08 +00:00
keta = NUMKETALENGTH - 1;
2026-06-19 01:49:53 +09:00
flg = FALSE;
} else {
2008-01-11 11:57:08 +00:00
keta = 0;
2026-06-19 01:49:53 +09:00
flg = TRUE;
2008-01-11 11:57:08 +00:00
}
2026-06-19 01:49:53 +09:00
for(i = NUMWORDBUF; i-- > 0;) {
2008-01-11 11:57:08 +00:00
flg = word2char(*(wd + i), tbl, keta, flg);
2026-06-19 01:49:53 +09:00
if(keta) keta -= 4;
2008-01-11 11:57:08 +00:00
}
2026-06-19 01:49:53 +09:00
if(kp == kanjitmp) {
2008-01-11 11:57:08 +00:00
*kanjitmp++ = *tbl++;
*kanjitmp++ = *tbl;
}
}
2026-06-19 01:49:53 +09:00
void num_type00(u_char* s1, u_char* s2, JREC* jrec) {
u_short num[NUMWORDBUF];
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
setwdnum(s1, (int)jrec->numlen, num);
2008-01-11 11:57:08 +00:00
words2num(num, Num1tbl, FALSE);
}
2026-06-19 01:49:53 +09:00
void num_type01(u_char* s1, u_char* s2, JREC* jrec) {
u_short num[NUMWORDBUF];
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
setwdnum(s1, (int)jrec->numlen, num);
2008-01-11 11:57:08 +00:00
words2num(num, Num1tbl, FALSE);
}
2026-06-19 01:49:53 +09:00
void num_type02(u_char* s1, u_char* s2, JREC* jrec) {
u_short num[NUMWORDBUF];
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
setwdnum(s1, (int)jrec->numlen, num);
2008-01-11 11:57:08 +00:00
words2num(num, Num1tbl, TRUE);
}
2026-06-19 01:49:53 +09:00
void num_type03(u_char* s1, u_char* s2, JREC* jrec) {
u_short num[NUMWORDBUF];
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
setwdnum(s1, (int)jrec->numlen, num);
2008-01-11 11:57:08 +00:00
words2num(num, Num2tbl, FALSE);
}
2026-06-19 01:49:53 +09:00
void num_type04(u_char* s1, u_char* s2, JREC* jrec) {
u_short num[NUMWORDBUF];
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
setwdnum(s1, (int)jrec->numlen, num);
2008-01-11 11:57:08 +00:00
words2num(num, Num2tbl, FALSE);
}
2026-06-19 01:49:53 +09:00
void num_type05(u_char* s1, u_char* s2, JREC* jrec) {
u_short num[NUMWORDBUF];
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
setwdnum(s1, (int)jrec->numlen, num);
2008-01-11 11:57:08 +00:00
words2num(num, Num2tbl, TRUE);
}
2026-06-19 01:49:53 +09:00
void num_type06(u_char* s1, u_char* s2, JREC* jrec) {
u_short num[NUMWORDBUF];
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
setwdnum(s1, (int)jrec->numlen, num);
2008-01-11 11:57:08 +00:00
words2num(num, Num3tbl, FALSE);
}
2026-06-19 01:49:53 +09:00
void num_type07(u_char* s1, u_char* s2, JREC* jrec) {
u_short num[NUMWORDBUF];
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
setwdnum(s1, (int)jrec->numlen, num);
2008-01-11 11:57:08 +00:00
words2num(num, Num3tbl, FALSE);
}
static void
2026-06-19 01:49:53 +09:00
kan_num(u_short* wd, u_char* tbl1, u_char* tbl2) {
2008-01-11 11:57:08 +00:00
int ii;
2026-06-19 01:49:53 +09:00
u_short tmp;
2008-01-11 11:57:08 +00:00
int mask;
int num;
int flg;
2026-06-19 01:49:53 +09:00
u_char* kurai1;
u_char* kurai2;
u_char* src;
u_char* keep = kanjitmp;
2008-01-11 11:57:08 +00:00
kurai2 = Num6tbl - 2;
2026-06-19 01:49:53 +09:00
mask = 0;
2008-01-11 11:57:08 +00:00
wd += NUMWORDBUF - 1;
flg = FALSE;
2026-06-19 01:49:53 +09:00
for(ii = NUMKETALENGTH; ii-- > 0;) {
if(!mask) {
if(flg) {
2008-01-11 11:57:08 +00:00
*kanjitmp++ = *kurai2++;
*kanjitmp++ = *kurai2++;
2026-06-19 01:49:53 +09:00
} else
2008-01-11 11:57:08 +00:00
kurai2 += 2;
2026-06-19 01:49:53 +09:00
mask = 0x1000;
tmp = *wd--;
flg = FALSE;
2008-01-11 11:57:08 +00:00
kurai1 = tbl2;
}
2026-06-19 01:49:53 +09:00
if((num = (unsigned int)tmp / mask) > _NUM0) {
if((num != _NUM1) || !*kurai1) {
src = tbl1 + (num - _NUM0) * 2;
2008-01-11 11:57:08 +00:00
*kanjitmp++ = *src++;
*kanjitmp++ = *src;
}
2026-06-19 01:49:53 +09:00
if(*kurai1) {
2008-01-11 11:57:08 +00:00
*kanjitmp++ = *kurai1++;
*kanjitmp++ = *kurai1++;
}
flg = TRUE;
2026-06-19 01:49:53 +09:00
} else
2008-01-11 11:57:08 +00:00
kurai1 += 2;
tmp &= (mask - 1);
mask >>= 4;
}
2026-06-19 01:49:53 +09:00
if(kanjitmp == keep) {
2008-01-11 11:57:08 +00:00
*kanjitmp++ = *tbl1++;
*kanjitmp++ = *tbl1;
}
}
2026-06-19 01:49:53 +09:00
void num_type08(u_char* s1, u_char* s2, JREC* jrec) {
u_short num[NUMWORDBUF];
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
setwdnum(s1, (int)jrec->numlen, num);
2008-01-11 11:57:08 +00:00
kan_num(num, Num2tbl, Num4tbl);
}
2026-06-19 01:49:53 +09:00
void num_type09(u_char* s1, u_char* s2, JREC* jrec) {
u_short num[NUMWORDBUF];
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
setwdnum(s1, (int)jrec->numlen, num);
2008-01-11 11:57:08 +00:00
kan_num(num, Num3tbl, Num5tbl);
}
2026-06-19 01:49:53 +09:00
void num_type10(u_char* s1, u_char* s2, JREC* jrec) {
int i;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
for(i = jrec->numlen; i > 0; i--) {
if(Chrtbl[*s1++] & NUMBER) {
2008-01-11 11:57:08 +00:00
*kanjitmp++ = *s2++;
*kanjitmp++ = *s2++;
2026-06-19 01:49:53 +09:00
} else
2008-01-11 11:57:08 +00:00
s2 += euc_codesize(*s2);
}
}
2026-06-19 01:49:53 +09:00
void num_type11(u_char* s1, u_char* s2, JREC* jrec) {
int i;
int j;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
i = j = jrec->numlen;
if(!(Chrtbl[*(s1 + j - 4)] & NUMBER)) j -= (j - 1) / 4;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
for(; i > 0; i--) {
if(Chrtbl[*s1++] & NUMBER) {
2008-01-11 11:57:08 +00:00
*kanjitmp++ = *s2++;
*kanjitmp++ = *s2++;
2026-06-19 01:49:53 +09:00
if(--j > 0) {
if((j % 3) == 0) {
2008-01-11 11:57:08 +00:00
*kanjitmp++ = COMMAHIGH;
*kanjitmp++ = COMMALOW;
}
}
2026-06-19 01:49:53 +09:00
} else
2008-01-11 11:57:08 +00:00
s2 += euc_codesize(*s2);
}
}
2026-06-19 01:49:53 +09:00
void num_type12(u_char* s1, u_char* s2, JREC* jrec) {
int i;
int j;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
for(i = jrec->numlen; i > 0; i--) {
if(Chrtbl[j = *s1++] & NUMBER) {
j = (j - N_0) * 2;
2008-01-11 11:57:08 +00:00
*kanjitmp++ = Num2tbl[j];
*kanjitmp++ = Num2tbl[j + 1];
}
}
}
static void
2026-06-19 01:49:53 +09:00
num_kurai(u_char* p, int len, u_char* tbl) {
int i;
int j;
u_char tmp;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
for(i = 0; i < len; i++) {
2008-01-11 11:57:08 +00:00
tmp = *p++;
*kanjitmp++ = tbl[j = ((tmp & 0x0f) - _NUM0) * 2];
*kanjitmp++ = tbl[j + 1];
2026-06-19 01:49:53 +09:00
if(j = ((tmp >> 4) & 3)) {
2008-01-11 11:57:08 +00:00
*kanjitmp++ = Num4tbl[j = (3 - j) * 2];
*kanjitmp++ = Num4tbl[j + 1];
}
2026-06-19 01:49:53 +09:00
if(j = ((tmp >> 6) & 3)) {
2008-01-11 11:57:08 +00:00
*kanjitmp++ = Num6tbl[j = (3 - j) * 2];
*kanjitmp++ = Num6tbl[j + 1];
}
}
}
2026-06-19 01:49:53 +09:00
void num_type13(u_char* s1, u_char* s2, JREC* jrec) {
u_char num[NUMKETALENGTH];
int len;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
len = setucnum(s1, (int)jrec->numlen, num);
2008-01-11 11:57:08 +00:00
num_kurai(num, len, Num1tbl);
}
2026-06-19 01:49:53 +09:00
void num_type14(u_char* s1, u_char* s2, JREC* jrec) {
u_char num[NUMKETALENGTH];
int len;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
len = setucnum(s1, (int)jrec->numlen, num);
2008-01-11 11:57:08 +00:00
num_kurai(num, len, Num2tbl);
}