1
This commit is contained in:
Alex Yatskov 2016-11-19 19:07:49 -08:00
parent 1a710d0ca6
commit 8e64583e4b
3 changed files with 48 additions and 10 deletions

43
gaiji.c
View File

@ -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[]) {

View File

@ -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 */

View File

@ -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);
}
}