From 8e64583e4bcec2d4d71490b83bb69a79cc0feed8 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 19 Nov 2016 19:07:49 -0800 Subject: [PATCH] WIP --- gaiji.c | 43 ++++++++++++++++++++++++++++++++++++++++--- gaiji.h | 7 ++++--- hooks.c | 8 ++++---- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/gaiji.c b/gaiji.c index d5d3ec0..95624e9 100644 --- a/gaiji.c +++ b/gaiji.c @@ -44,6 +44,42 @@ static const Gaiji_context gaiji_contexts[] = { GAIJI_CONTEXT("大辞泉", daijisen), }; +/* + * Local functions + */ + +static void encode_sequence(char output[], int size, const char utf8[]) { + const char hex[] = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' + }; + + strncpy(output, "{{", size); + int offset = strlen(output); + + for (int i = 0; i < MAX_UTF8_BYTES; ++i) { + const char c = utf8[i]; + if (c == 0) { + break; + } + + if (offset >= size - 1) { + break; + } + + output[offset++] = hex[c >> 0x08]; + + if (offset >= size - 1) { + break; + } + + output[offset++] = hex[c & 0x0f]; + } + + output[offset] = 0; + strncat(output, "}}", size); +} + /* * Exported functions */ @@ -59,7 +95,7 @@ const Gaiji_context * gaiji_select_table(const char name[]) { return NULL; } -void gaiji_build_stub(char text[MAX_STUB_BYTES], int code, const Gaiji_context* context, Gaiji_width width) { +void gaiji_build_stub(char output[], int size, int code, const Gaiji_context* context, Gaiji_width width) { do { if (context == NULL) { break; @@ -84,7 +120,7 @@ void gaiji_build_stub(char text[MAX_STUB_BYTES], int code, const Gaiji_context* for (int i = 0; i < count; ++i) { const Gaiji_entry* entry = entries + i; if (entry->code == code) { - sprintf(text, "{{%.4x}}", code); + encode_sequence(output, size, entry->utf8); return; } } @@ -92,7 +128,8 @@ void gaiji_build_stub(char text[MAX_STUB_BYTES], int code, const Gaiji_context* } while (0); - strcpy(text, ""); + strncpy(output, "", size); + output[size - 1] = 0; } void gaiji_fixup_stub(char output[], int size, const char input[]) { diff --git a/gaiji.h b/gaiji.h index 24f9318..39109b6 100644 --- a/gaiji.h +++ b/gaiji.h @@ -23,7 +23,8 @@ * Constants */ -#define MAX_STUB_BYTES 10 +#define MAX_UTF8_BYTES 9 +#define MAX_STUB_BYTES 32 #define MAX_TABLE_NAME 256 /* @@ -32,7 +33,7 @@ typedef struct { int code; - char data[MAX_STUB_BYTES]; + char utf8[MAX_UTF8_BYTES]; } Gaiji_entry; typedef struct { @@ -53,7 +54,7 @@ typedef enum { */ const Gaiji_context * gaiji_select_context(const char name[]); -void gaiji_build_stub(char text[MAX_STUB_BYTES], int code, const Gaiji_context* context, Gaiji_width width); +void gaiji_build_stub(char output[], int size, int code, const Gaiji_context* context, Gaiji_width width); void gaiji_fixup_stub(char output[], int size, const char input[]); #endif /* GAIJI_H */ diff --git a/hooks.c b/hooks.c index 2a42fe4..08fb979 100644 --- a/hooks.c +++ b/hooks.c @@ -20,6 +20,7 @@ #include "hooks.h" #include "gaiji.h" +#include "util.h" #include "eb/eb/eb.h" #include "eb/eb/text.h" @@ -111,7 +112,7 @@ static EB_Error_Code hook_narrow_font( /* EB_HOOK_NARROW_FONT */ (void)code; char stub[MAX_STUB_BYTES]; - gaiji_build_stub(stub, argv[0], container, GAIJI_WIDTH_NARROW); + gaiji_build_stub(stub, ARRSIZE(stub), argv[0], container, GAIJI_WIDTH_NARROW); eb_write_text_string(book, stub); return 0; @@ -131,7 +132,7 @@ static EB_Error_Code hook_wide_font( /* EB_HOOK_WIDE_FONT */ (void)code; char stub[MAX_STUB_BYTES]; - gaiji_build_stub(stub, argv[0], container, GAIJI_WIDTH_WIDE); + gaiji_build_stub(stub, ARRSIZE(stub), argv[0], container, GAIJI_WIDTH_WIDE); eb_write_text_string(book, stub); return 0; @@ -191,8 +192,7 @@ static const EB_Hook s_hooks[] = { */ void hooks_install(EB_Hookset* hookset) { - const int count = sizeof(s_hooks) / sizeof(s_hooks[0]); - for (int i = 0; i < count; ++i) { + for (unsigned i = 0; i < ARRSIZE(s_hooks); ++i) { eb_set_hook(hookset, s_hooks + i); } }