1
This commit is contained in:
Alex Yatskov 2016-11-19 18:27:00 -08:00
parent 6bcca3739e
commit 1a710d0ca6
2 changed files with 45 additions and 20 deletions

57
gaiji.c
View File

@ -17,6 +17,7 @@
*/ */
#include <string.h> #include <string.h>
#include <assert.h>
#include "util.h" #include "util.h"
#include "gaiji.h" #include "gaiji.h"
@ -25,7 +26,7 @@
* Macros * Macros
*/ */
#define GAIJI_TABLE(name, ents) {\ #define GAIJI_CONTEXT(name, ents) {\
name,\ name,\
gaiji_table_##ents##_wide,\ gaiji_table_##ents##_wide,\
ARRSIZE(gaiji_table_##ents##_wide),\ ARRSIZE(gaiji_table_##ents##_wide),\
@ -39,35 +40,59 @@
#include "gaiji_table_daijisen.h" #include "gaiji_table_daijisen.h"
static const Gaiji_table gaiji_tables[] = { static const Gaiji_context gaiji_contexts[] = {
GAIJI_TABLE("大辞泉", daijisen), GAIJI_CONTEXT("大辞泉", daijisen),
}; };
/* /*
* Exported functions * Exported functions
*/ */
const Gaiji_table * gaiji_select_table(const char name[]) { const Gaiji_context * gaiji_select_table(const char name[]) {
for (unsigned i = 0; i < ARRSIZE(gaiji_tables); ++i) { for (unsigned i = 0; i < ARRSIZE(gaiji_contexts); ++i) {
const Gaiji_table* table = gaiji_tables + i; const Gaiji_context* context = gaiji_contexts + i;
if (strcmp(table->name, name) == 0) { if (strcmp(context->name, name) == 0) {
return table; return context;
} }
} }
return NULL; return NULL;
} }
void gaiji_build_stub(char text[MAX_STUB_BYTES], int code, const Gaiji_table* table, Gaiji_width width) { void gaiji_build_stub(char text[MAX_STUB_BYTES], int code, const Gaiji_context* context, Gaiji_width width) {
if (table == NULL) { do {
strcpy(text, "<?>"); if (context == NULL) {
return; break;
} }
(void)code; const Gaiji_entry* entries = NULL;
(void)text; int count = 0;
(void)table;
(void)width; switch (width) {
case GAIJI_WIDTH_WIDE:
entries = context->table_wide;
count = context->count_wide;
break;
case GAIJI_WIDTH_NARROW:
entries = context->table_narrow;
count = context->count_narrow;
break;
}
assert(entries != NULL);
for (int i = 0; i < count; ++i) {
const Gaiji_entry* entry = entries + i;
if (entry->code == code) {
sprintf(text, "{{%.4x}}", code);
return;
}
}
}
while (0);
strcpy(text, "<?>");
} }
void gaiji_fixup_stub(char output[], int size, const char input[]) { void gaiji_fixup_stub(char output[], int size, const char input[]) {

View File

@ -41,7 +41,7 @@ typedef struct {
int count_wide; int count_wide;
const Gaiji_entry* table_narrow; const Gaiji_entry* table_narrow;
int count_narrow; int count_narrow;
} Gaiji_table; } Gaiji_context;
typedef enum { typedef enum {
GAIJI_WIDTH_WIDE, GAIJI_WIDTH_WIDE,
@ -52,8 +52,8 @@ typedef enum {
* Functions * Functions
*/ */
const Gaiji_table * gaiji_select_table(const char name[]); const Gaiji_context * gaiji_select_context(const char name[]);
void gaiji_build_stub(char text[MAX_STUB_BYTES], int code, const Gaiji_table* table, Gaiji_width width); void gaiji_build_stub(char text[MAX_STUB_BYTES], int code, const Gaiji_context* context, Gaiji_width width);
void gaiji_fixup_stub(char output[], int size, const char input[]); void gaiji_fixup_stub(char output[], int size, const char input[]);
#endif /* GAIJI_H */ #endif /* GAIJI_H */