Add helper functions for number formatting

And use it from H, HH, MH, and MHH commands.
This commit is contained in:
kichikuou
2020-07-24 17:06:30 +09:00
parent fc5f89912e
commit b5010df10d
6 changed files with 71 additions and 81 deletions
+3 -8
View File
@@ -33,6 +33,7 @@
#include "message.h"
#include "music.h"
#include "utfsjis.h"
#include "hankaku.h"
#include "ags.h"
#include "graphicsdevice.h"
#include "ald_manager.h"
@@ -736,15 +737,9 @@ void commands2F44() {
int num1 = getCaliValue();
int fig = getCaliValue();
int num2 = getCaliValue();
char s[256];
char buf[256];
if (fig) {
sprintf(s, "%*d", fig, num2);
v_strcpy(num1 - 1, s + strlen(s) - fig);
} else {
sprintf(s, "%d", num2);
v_strcpy(num1 - 1, s);
}
v_strcpy(num1 - 1, format_number(num2, fig, buf));
DEBUG_COMMAND("MHH %d, %d, %d:\n", num1, fig, num2);
}
+8 -33
View File
@@ -30,44 +30,19 @@
void commandH() {
int fig = sys_getc();
int num = getCaliValue();
char _work1[10], _work2[512];
char *work1 = _work1, *work2 = _work2;
int len;
*work2 = 0;
sprintf(work1, "%d", num);
if (fig != 0) {
len = strlen(work1);
if (fig > len) {
/* 空白でうめる */
len = fig - len;
while(len--) {
strcat(work2, num2sjis(10));
}
} else {
work1 += (len - fig);
}
}
while(*work1) {
strcat(work2, num2sjis((*work1) - '0')); work1++;
}
sys_addMsg(work2);
char buf[512];
sys_addMsg(format_number_zenkaku(num, fig, buf));
DEBUG_COMMAND("H %d,%d:\n",fig,num);
}
void commandHH(void) {
int fig = sys_getc();
int num = getCaliValue();
char s[256];
if( fig ) {
char *ss="%%%dd";
char sss[256];
sprintf(sss,ss,fig);
sprintf(s,sss,num);
} else {
sprintf(s,"%d",num);
}
sys_addMsg(s);
char buf[256];
sys_addMsg(format_number(num, fig, buf));
DEBUG_COMMAND("HH %d,%d:\n",fig,num);
}
+4 -23
View File
@@ -195,29 +195,10 @@ void commandMH() {
int num1 = getCaliValue();
int fig = getCaliValue();
int num2 = getCaliValue();
char _work1[10],_work2[200];
char *work1 = _work1, *work2 = _work2;
int len;
*work2 = 0;
sprintf(work1,"%d",num2);
if (fig != 0) {
len = strlen(work1);
if (fig > len) {
/* 空白でうめる */
len = fig - len;
while(len--) {
strcat(work2, num2sjis(10));
}
} else {
work1 += (len - fig);
}
}
while(*work1) {
strcat(work2, num2sjis((*work1) - '0')); work1++;
}
v_strcpy(num1 - 1, work2);
char buf[512];
v_strcpy(num1 - 1, format_number_zenkaku(num2, fig, buf));
DEBUG_COMMAND("MH %d,%d,%d:\n",num1,fig,num2);
}
+34 -16
View File
@@ -29,7 +29,7 @@
#include "portab.h"
#include "hankaku.h"
static BYTE hankakutable[3][192] = {{
static const BYTE hankakutable[3][192] = {{
// 0x8140 - 0x81ff
' ', 0xa4, 0xa1, ',', '.', 0xa5, ':', ';',
'?', '!', 0xde, 0xdf, 0, '`', 0, '^',
@@ -126,6 +126,20 @@ static const BYTE kanatbl[][2] = {
0x82, 0xed, 0x82, 0xf1, 0x81, 0x4a, 0x81, 0x4b
};
static const char zenkaku_digits[][3] = {
0x82,0x4f,0x00, /* 0 */
0x82,0x50,0x00, /* 1 */
0x82,0x51,0x00, /* 2 */
0x82,0x52,0x00, /* 3 */
0x82,0x53,0x00, /* 4 */
0x82,0x54,0x00, /* 5 */
0x82,0x55,0x00, /* 6 */
0x82,0x56,0x00, /* 7 */
0x82,0x57,0x00, /* 8 */
0x82,0x58,0x00, /* 9 */
0x81,0x40,0x00, /* space */
};
BYTE *zen2han(const BYTE *src) {
BYTE c0, c1;
char *dst, *_dst;
@@ -187,19 +201,23 @@ BYTE *han2zen(const BYTE *src) {
return _dst;
}
char *num2sjis(int num) {
static char suji[][3] = {
0x82,0x4f,0x00, /* 0 */
0x82,0x50,0x00, /* 1 */
0x82,0x51,0x00, /* 2 */
0x82,0x52,0x00, /* 3 */
0x82,0x53,0x00, /* 4 */
0x82,0x54,0x00, /* 5 */
0x82,0x55,0x00, /* 6 */
0x82,0x56,0x00, /* 7 */
0x82,0x57,0x00, /* 8 */
0x82,0x58,0x00, /* 9 */
0x81,0x40,0x00, /* space */
};
return suji[num];
char *format_number(int n, int width, char *buf) {
if (width) {
sprintf(buf, "%*d", width, n);
return buf + strlen(buf) - width;
}
sprintf(buf, "%d", n);
return buf;
}
char *format_number_zenkaku(int n, int width, char *buf) {
char work[256];
char *dst = buf;
for (char *s = format_number(n, width, work); *s; s++) {
char *p = zenkaku_digits[*s == ' ' ? 10 : *s - '0'];
while (*p)
*dst++ = *p++;
}
*dst = '\0';
return buf;
}
+2 -1
View File
@@ -27,6 +27,7 @@
extern BYTE *zen2han(const BYTE *src);
extern BYTE *han2zen(const BYTE *src);
extern char *num2sjis(int num);
extern char *format_number(int n, int width, char *buf);
extern char *format_number_zenkaku(int n, int width, char *buf);
#endif /* __HANKAKU_H__ */
+20
View File
@@ -57,6 +57,26 @@ static void zen2han_test(void) {
ASSERT_STRCMP(sjis2utf(zen2han(utf2sjis(unconvertable))), unconvertable);
}
static void format_number_test(void) {
char buf[256];
ASSERT_STRCMP(format_number(0, 0, buf), "0");
ASSERT_STRCMP(format_number(42, 0, buf), "42");
ASSERT_STRCMP(format_number(0, 3, buf), " 0");
ASSERT_STRCMP(format_number(42, 3, buf), " 42");
ASSERT_STRCMP(format_number(65535, 3, buf), "535");
}
static void format_number_zenkaku_test(void) {
char buf[512];
ASSERT_STRCMP(sjis2utf(format_number_zenkaku(0, 0, buf)), "");
ASSERT_STRCMP(sjis2utf(format_number_zenkaku(42, 0, buf)), "42");
ASSERT_STRCMP(sjis2utf(format_number_zenkaku(0, 3, buf)), "  ");
ASSERT_STRCMP(sjis2utf(format_number_zenkaku(42, 3, buf)), " 42");
ASSERT_STRCMP(sjis2utf(format_number_zenkaku(65535, 3, buf)), "535");
}
void hankaku_test(void) {
zen2han_test();
format_number_test();
format_number_zenkaku_test();
}