several fixes

This commit is contained in:
Nishi 2026-09-23 11:11:15 +09:00
commit 75ca07fc8c
3 changed files with 38 additions and 7 deletions

View file

@ -190,7 +190,7 @@ int sj4_from_utf16(u_char* out, const u_char* in, int len) {
out += eucjp_write(out, n);
}
return (out - o_out) * sizeof(wchar_t);
return out - o_out;
}
static u_int eucjp_read(const u_char* in) {
@ -287,7 +287,7 @@ int sj4_to_utf16(u_char* out, const u_char* in, int len) {
}
}
return (w_out - o_out) * sizeof(wchar_t);
return w_out - o_out;
}
#endif

View file

@ -48,7 +48,7 @@ Sj4Lib* sj4_open(int charset, const char* dic) {
len = sj4_charset_to(ctx->charset, ctx->wrk2buf, ctx->wrkbuf, len); \
if(ctx->charset == SJ4UTF16) { \
int i; \
for(i = 0; i < sizeof(wchar_t); i++) ctx->wrk2buf[len * sizeof(wchar_t) + i] = 0; \
((wchar_t*)ctx->wrk2buf)[len] = 0; \
} else { \
ctx->wrk2buf[len] = 0; \
} \
@ -64,6 +64,10 @@ Sj4Lib* sj4_open(int charset, const char* dic) {
int sj4_getkan(Sj4Lib* ctx, const void* input, int len, Sj4Kouho* kouho) {
int len2;
if(sizeof(ctx->inbuf) < (len * ((ctx->charset == SJ4UTF16 || ctx->charset == SJ4SJIS) ? sizeof(wchar_t) : 1))) {
return 0;
}
memset(kouho, 0, sizeof(*kouho));
ctx->kouho = kouho;

View file

@ -1,24 +1,47 @@
#define UTF16
#include <sj4lib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef UTF16
#include <wchar.h>
#include <locale.h>
#endif
#ifdef UTF16
#define CHARSET SJ4UTF16
#define LEN wcslen
#else
#ifdef _WIN32
#define CHARSET SJ4SJIS
#else
#define CHARSET SJ4UTF8
#endif
#define LEN strlen
#endif
int main(int argc, char** argv) {
char c[8 * 1024];
#ifdef UTF16
wchar_t c[8 * 1024];
#else
char c[8 * 1024];
#endif
Sj4Lib* ctx = sj4_open(CHARSET, "sj4main.dic");
char b;
#ifdef UTF16
unsigned short b;
#else
char b;
#endif
if(ctx == NULL) {
fprintf(stderr, "Failed to create context\n");
return 1;
}
#ifdef UTF16
setlocale(LC_ALL, "");
#endif
c[0] = 0;
while(fread(&b, 1, sizeof(b), stdin)) {
@ -27,7 +50,7 @@ int main(int argc, char** argv) {
if(b == '\n') {
} else if(b != '\r') {
int l = strlen(c);
int l = LEN(c);
c[l] = b;
c[l + 1] = 0;
@ -36,13 +59,17 @@ int main(int argc, char** argv) {
}
i = 0;
while((strlen(c) - i) > 0 && (n = sj4_getkan(ctx, c + i, strlen(c + i), &kouho)) != 0) {
while((LEN(c) - i) > 0 && (n = sj4_getkan(ctx, c + i, LEN(c + i), &kouho)) != 0) {
printf(">");
do {
#ifdef UTF16
printf(" %ls", kouho.buffer.utf16);
#else
#ifdef _WIN32
printf(" %s", kouho.buffer.sjis);
#else
printf(" %s", kouho.buffer.utf8);
#endif
#endif
} while(sj4_nextkan(ctx) != 0);
printf("\n");