WIP sj4lib

This commit is contained in:
Nishi 2026-06-21 20:32:04 +09:00
commit 8fdd90499b
8 changed files with 19247 additions and 26 deletions

View file

@ -31,14 +31,22 @@ target_include_directories(sj4rkcv PUBLIC include/sj4common include/sj4rkcv)
target_include_directories(sj4rkcv PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) target_include_directories(sj4rkcv PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(sj4rkcv PUBLIC sj4common) target_link_libraries(sj4rkcv PUBLIC sj4common)
if(NOT SJ4_GLOBAL)
file(GLOB_RECURSE SJ4LIB_SRCS lib/sj4lib/**.c)
add_library(sj4lib STATIC ${SJ4LIB_SRCS})
target_include_directories(sj4lib PUBLIC include/sj4common include/sj4core include/sj4lib)
target_include_directories(sj4lib PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(sj4lib PUBLIC sj4common sj4core)
endif()
file(GLOB_RECURSE SJ4MKDIC_SRCS src/sj4mkdic/**.c) file(GLOB_RECURSE SJ4MKDIC_SRCS src/sj4mkdic/**.c)
add_executable(sj4mkdic ${SJ4MKDIC_SRCS}) add_executable(sj4mkdic ${SJ4MKDIC_SRCS})
target_include_directories(sj4mkdic PRIVATE include/sj4common include/sj4core) target_include_directories(sj4mkdic PRIVATE include/sj4common include/sj4core)
target_compile_definitions(sj4mkdic PRIVATE UTF8 ENGLISH) target_compile_definitions(sj4mkdic PRIVATE UTF8 ENGLISH)
target_link_libraries(sj4mkdic PUBLIC sj4common) target_link_libraries(sj4mkdic PRIVATE sj4common)
if(NOT SJ4_GLOBAL) if(NOT SJ4_GLOBAL)
file(GLOB_RECURSE SJ4TEST_SRCS src/sj4test/**.c) file(GLOB_RECURSE SJ4TEST_SRCS src/sj4test/**.c)
add_executable(sj4test ${SJ4TEST_SRCS}) add_executable(sj4test ${SJ4TEST_SRCS})
target_link_libraries(sj4test PRIVATE sj4core) target_link_libraries(sj4test PRIVATE sj4lib)
endif() endif()

18969
include/sj4common/sj_euc2ucs.h Executable file

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

35
include/sj4lib/sj4lib.h Normal file
View file

@ -0,0 +1,35 @@
#ifndef __SJ4LIB_H__
#define __SJ4LIB_H__
#include <wchar.h>
/* increase this as needed... */
#define SJ4BUFSZ (16 * 1024)
typedef union sj4kouhobuffer {
unsigned char sjis[SJ4BUFSZ];
unsigned char eucjp[SJ4BUFSZ];
char utf8[SJ4BUFSZ];
wchar_t utf16[SJ4BUFSZ / sizeof(wchar_t)];
unsigned char raw[SJ4BUFSZ];
} Sj4KouhoBuffer;
typedef struct sj4kouho {
Sj4KouhoBuffer buffer;
} Sj4Kouho;
typedef struct sj4lib Sj4Lib;
enum SJ4CHARSET {
SJ4EUCJP = 0,
SJ4SJIS,
SJ4UTF8,
SJ4UTF16
};
Sj4Lib* sj4_open(int, const char*);
int sj4_getkan(Sj4Lib*, const void*, int len, Sj4Kouho*);
void sj4_close(Sj4Lib*);
#endif

214
lib/sj4lib/sj.c Normal file
View file

@ -0,0 +1,214 @@
#include <sj4lib.h>
#include <sj_kanakan.h>
#include <stdlib.h>
#include <sj_euc2ucs.h>
#include <sj_ucs2euc.h>
static u_int ucs_to_euc(u_int in) {
if(0 <= in && in <= 0xffff) {
const T_U2E_BITMAP_INDEX* b = &utf16_to_euc_jp_table[(in >> 8) & 0xff];
if(b->byType == 2) {
b = &utf16_to_euc_jp_table[b->dwBitmapIndex + (in & 0xff)];
if(b->byType == 3) {
return b->dwEucJpCode;
}
}
return in;
}
return 0;
}
static u_int euc_to_ucs(u_int in) {
const T_E2U_BITMAP_INDEX* b = NULL;
if(in <= 0xff) {
b = &euc_jp_to_utf16_table[in];
if(b->byType != 3) return 0;
return b->dwUtf16Code;
} else if(in <= 0xffff) {
b = &euc_jp_to_utf16_table[(in >> 8) & 0xff];
if(b->byType != 2) return 0;
b = &euc_jp_to_utf16_table[b->dwBitmapIndex + ((in >> 0) & 0xff)];
if(b->byType != 3) return 0;
return b->dwUtf16Code;
} else {
b = &euc_jp_to_utf16_table[(in >> 16) & 0xff];
if(b->byType != 2) return 0;
b = &euc_jp_to_utf16_table[b->dwBitmapIndex + ((in >> 8) & 0xff)];
if(b->byType != 2) return 0;
b = &euc_jp_to_utf16_table[b->dwBitmapIndex + ((in >> 0) & 0xff)];
if(b->byType != 3) return 0;
return b->dwUtf16Code;
}
return 0;
}
static int from_sjis(u_char* out, const u_char* in, int len) {
}
static int utf8_later(unsigned char c) {
return 0x80 <= c && c < 0xc0;
}
#define CAST_I32(x) ((int)(x))
static int from_utf8(u_char* out, const u_char* in, int len) {
const u_char* o_in = in;
u_char* o_out = out;
while((in - o_in) < len) {
u_char c = *in;
u_int n = 0;
if(c < 0x80) {
n = c;
in += 1;
} else if(0xc2 <= c && c < 0xe0) {
if(!utf8_later(in[1])) return 0;
if((in[0] & 0x1e) == 0) return 0;
n = CAST_I32(in[0] & 0x1f) << 6;
n |= CAST_I32(in[1] & 0x3f);
in += 2;
} else if(0xe0 <= c && c < 0xf0) {
if(!utf8_later(in[1]) || !utf8_later(in[2])) return 0;
if((in[0] & 0x0f) == 0 && (in[1] & 0x20) == 0) return 0;
n = CAST_I32(in[0] & 0x0f) << 12;
n |= CAST_I32(in[1] & 0x3f) << 6;
n |= CAST_I32(in[2] & 0x3f);
in += 3;
} else if(0xf0 <= c && c < 0xf8) {
if(!utf8_later(in[1]) || !utf8_later(in[2]) || !utf8_later(in[3])) return 0;
if((in[0] & 0x07) == 0 && (in[1] & 0x30) == 0) return 0;
n = CAST_I32(in[0] & 0x07) << 18;
n |= CAST_I32(in[1] & 0x3f) << 12;
n |= CAST_I32(in[2] & 0x3f) << 6;
n |= CAST_I32(in[3] & 0x3f);
in += 4;
}
n = ucs_to_euc(n);
if(n <= 0xff) {
*out++ = n;
} else if(n <= 0xffff) {
*out++ = (n >> 8) & 0xff;
*out++ = (n >> 0) & 0xff;
} else {
*out++ = (n >> 16) & 0xff;
*out++ = (n >> 8) & 0xff;
*out++ = (n >> 0) & 0xff;
}
}
return out - o_out;
}
static int from_utf16(u_char* out, const u_char* in, int len) {
}
static int to_sjis(u_char* out, const u_char* in, int len) {
}
static int to_utf8(u_char* out, const u_char* in, int len) {
const u_char* o_in = in;
u_char* o_out = out;
while((in - o_in) < len) {
u_char c = *in;
u_int n = 0;
if(c <= 0x7f) {
n = c;
in += 1;
} else if(c != 0x8f) {
n = CAST_I32(in[0]) << 8;
n |= CAST_I32(in[1]);
in += 2;
} else {
n = CAST_I32(in[0]) << 16;
n |= CAST_I32(in[1]) << 8;
n |= CAST_I32(in[2]);
in += 3;
}
}
return out - o_out;
}
static int to_utf16(u_char* out, const u_char* in, int len) {
}
struct sj4lib {
Sj4Context* ctx;
int charset;
u_char inbuf[SJ4BUFSZ];
u_char outbuf[SJ4BUFSZ];
};
Sj4Lib* sj4_open(int charset, const char* dic) {
Sj4Lib* ctx;
if((ctx = calloc(1, sizeof(*ctx))) == NULL) return NULL;
if((ctx->ctx = alloccontext(dic)) == NULL) {
free(ctx);
return NULL;
}
ctx->charset = charset;
return ctx;
}
#define ICONV(to, from, len, mode) \
if(ctx->charset == SJ4EUCJP) { \
memcpy(to, from, len); \
} else if(ctx->charset == SJ4SJIS) { \
len = mode##_sjis(to, from, len); \
} else if(ctx->charset == SJ4UTF8) { \
len = mode##_utf8(to, from, len); \
} else if(ctx->charset == SJ4UTF16) { \
len = mode##_utf16(to, from, len); \
}
int sj4_getkan(Sj4Lib* ctx, const void* input, int len, Sj4Kouho* kouho) {
memset(kouho, 0, sizeof(*kouho));
ICONV(ctx->inbuf, input, len, from);
len = cl2knj(ctx->ctx, ctx->inbuf, len, ctx->outbuf);
ICONV(kouho->buffer.raw, ctx->outbuf, len, to);
return len;
}
void sj4_close(Sj4Lib* ctx) {
free(ctx);
}

View file

@ -37,7 +37,7 @@
#include "sj4mkdic.h" #include "sj4mkdic.h"
#include "ucstable.h" #include "sj_ucs2euc.h"
int bubun_str(u_char* p1, int l1, u_char* p2, int l2) { int bubun_str(u_char* p1, int l1, u_char* p2, int l2) {
u_char* p; u_char* p;
@ -204,7 +204,7 @@ void utf8_print(char* out, int c) {
int unicode_to_eucjp(int utf) { int unicode_to_eucjp(int utf) {
if(0 <= utf && utf <= 0xffff) { if(0 <= utf && utf <= 0xffff) {
const T_BITMAP_INDEX* b = &utf16_to_euc_jp_table[(utf >> 8) & 0xff]; const T_U2E_BITMAP_INDEX* b = &utf16_to_euc_jp_table[(utf >> 8) & 0xff];
if(b->byType == 2) { if(b->byType == 2) {
if(b->dwBitmapIndex != 0) { if(b->dwBitmapIndex != 0) {

View file

@ -1,11 +1,12 @@
#include <sj_kanakan.h> #include <sj4lib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) { int main(int argc, char** argv) {
char out[64 * 1024]; char c[8 * 1024];
int i = 0; Sj4Lib* ctx = sj4_open(SJ4UTF8, "sj4main.dic");
char c[64 * 1024]; char b;
char b;
Sj4Context* ctx = alloccontext("sj4main.dic");
if(ctx == NULL) { if(ctx == NULL) {
fprintf(stderr, "Failed to create context\n"); fprintf(stderr, "Failed to create context\n");
@ -15,7 +16,8 @@ int main(int argc, char** argv) {
c[0] = 0; c[0] = 0;
while(fread(&b, 1, 1, stdin) == 1) { while(fread(&b, 1, 1, stdin) == 1) {
int n; int n, i;
Sj4Kouho kouho;
if(b == '\n') { if(b == '\n') {
} else if(b != '\r') { } else if(b != '\r') {
@ -28,22 +30,15 @@ int main(int argc, char** argv) {
} }
i = 0; i = 0;
while((strlen(c) - i) > 0 && (i += (n = cl2knj(ctx, (u_char*)c + i, strlen(c) - i, (u_char*)out)))) { while((strlen(c) - i) > 0 && (n = sj4_getkan(ctx, c + i, strlen(c + i), &kouho))) {
STDYOUT* s = (STDYOUT*)out; printf("> %s\n", kouho.buffer);
char* str = malloc(n + 1);
memcpy(str, c + i - n, n); i += n;
str[i] = 0;
printf("> %s -> %s\n", str, out + sizeof(STDYOUT));
free(str);
} }
c[0] = 0; c[0] = 0;
} }
free_context(ctx); sj4_close(ctx);
return 0; return 0;
} }

View file

@ -2,4 +2,4 @@
while [ ! -d .git ]; do while [ ! -d .git ]; do
cd .. cd ..
done done
clang-format --verbose -i `find src lib include "(" -name "*.c" -or -name "*.h" ")" -and -not -name ucstable.h -and -not -name maindic"*".c` clang-format --verbose -i `find src lib include "(" -name "*.c" -or -name "*.h" ")" -and -not -name sj_euc2ucs.h -and -not -name sj_ucs2euc.h -and -not -name maindic"*".c`