1
This commit is contained in:
Alex Yatskov 2016-11-19 19:40:29 -08:00
parent 8e64583e4b
commit e0bd97c505
6 changed files with 42 additions and 43 deletions

View File

@ -84,7 +84,7 @@ static void encode_sequence(char output[], int size, const char utf8[]) {
* Exported functions * Exported functions
*/ */
const Gaiji_context * gaiji_select_table(const char name[]) { const Gaiji_context* gaiji_select_context(const char name[]) {
for (unsigned i = 0; i < ARRSIZE(gaiji_contexts); ++i) { for (unsigned i = 0; i < ARRSIZE(gaiji_contexts); ++i) {
const Gaiji_context* context = gaiji_contexts + i; const Gaiji_context* context = gaiji_contexts + i;
if (strcmp(context->name, name) == 0) { if (strcmp(context->name, name) == 0) {

View File

@ -53,7 +53,7 @@ typedef enum {
* Functions * Functions
*/ */
const Gaiji_context * gaiji_select_context(const char name[]); const Gaiji_context* gaiji_select_context(const char name[]);
void gaiji_build_stub(char output[], int size, 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[]); void gaiji_fixup_stub(char output[], int size, const char input[]);

12
hooks.c
View File

@ -50,7 +50,7 @@
} }
/* /*
* Tag printers * Local functions
*/ */
HOOK_TAGGER(begin_candidate); /* EB_HOOK_BEGIN_CANDIDATE */ HOOK_TAGGER(begin_candidate); /* EB_HOOK_BEGIN_CANDIDATE */
@ -94,10 +94,6 @@ HOOK_TAGGER(set_indent); /* EB_HOOK_SET_INDENT */
HOOK_TAGGER(wide_jisx0208); /* EB_HOOK_WIDE_JISX0208 */ HOOK_TAGGER(wide_jisx0208); /* EB_HOOK_WIDE_JISX0208 */
HOOK_TAGGER(null); /* EB_HOOK_NULL */ HOOK_TAGGER(null); /* EB_HOOK_NULL */
/*
* Local functions
*/
static EB_Error_Code hook_narrow_font( /* EB_HOOK_NARROW_FONT */ static EB_Error_Code hook_narrow_font( /* EB_HOOK_NARROW_FONT */
EB_Book* book, EB_Book* book,
EB_Appendix* appendix, EB_Appendix* appendix,
@ -106,11 +102,10 @@ static EB_Error_Code hook_narrow_font( /* EB_HOOK_NARROW_FONT */
int argc, int argc,
const unsigned int argv[] const unsigned int argv[]
) { ) {
assert(argc > 0);
(void)appendix; (void)appendix;
(void)code; (void)code;
assert(argc > 0);
char stub[MAX_STUB_BYTES]; char stub[MAX_STUB_BYTES];
gaiji_build_stub(stub, ARRSIZE(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); eb_write_text_string(book, stub);
@ -126,11 +121,10 @@ static EB_Error_Code hook_wide_font( /* EB_HOOK_WIDE_FONT */
int argc, int argc,
const unsigned int argv[] const unsigned int argv[]
) { ) {
assert(argc > 0);
(void)appendix; (void)appendix;
(void)code; (void)code;
assert(argc > 0);
char stub[MAX_STUB_BYTES]; char stub[MAX_STUB_BYTES];
gaiji_build_stub(stub, ARRSIZE(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); eb_write_text_string(book, stub);

47
main.c
View File

@ -22,32 +22,27 @@
#include "convert.h" #include "convert.h"
#include "util.h" #include "util.h"
#include "hooks.h" #include "hooks.h"
#include "gaiji.h"
#include "eb/eb/eb.h" #include "eb/eb/eb.h"
#include "eb/text.h" #include "eb/text.h"
#include "eb/eb/error.h" #include "eb/eb/error.h"
/*
* Constants
*/
#define MAX_HITS 256
/* /*
* Local functions * Local functions
*/ */
static void export_subbook_entries(EB_Book* eb_book, EB_Hookset* eb_hookset, Subbook* subbook) { static void export_subbook_entries(Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset, Gaiji_context* context) {
if (subbook->entry_cap == 0) { if (subbook->entry_cap == 0) {
subbook->entry_cap = 16384; subbook->entry_cap = 16384;
subbook->entries = malloc(subbook->entry_cap * sizeof(Entry)); subbook->entries = malloc(subbook->entry_cap * sizeof(Entry));
} }
EB_Hit hits[MAX_HITS]; EB_Hit hits[256];
int hit_count = 0; int hit_count = 0;
do { do {
if (eb_hit_list(eb_book, MAX_HITS, hits, &hit_count) != EB_SUCCESS) { if (eb_hit_list(eb_book, ARRSIZE(hits), hits, &hit_count) != EB_SUCCESS) {
continue; continue;
} }
@ -60,40 +55,54 @@ static void export_subbook_entries(EB_Book* eb_book, EB_Hookset* eb_hookset, Sub
} }
Entry* entry = subbook->entries + subbook->entry_count++; Entry* entry = subbook->entries + subbook->entry_count++;
entry->heading = read_book_data(eb_book, eb_hookset, &hit->heading, READ_MODE_HEADING); entry->heading = read_book_data(
entry->text = read_book_data(eb_book, eb_hookset, &hit->text, READ_MODE_TEXT); eb_book,
eb_hookset,
context,
&hit->heading,
READ_MODE_HEADING
);
entry->text = read_book_data(
eb_book,
eb_hookset,
context,
&hit->text,
READ_MODE_TEXT
);
} }
} }
while (hit_count > 0); while (hit_count > 0);
} }
static void export_subbook(EB_Book* eb_book, EB_Hookset* eb_hookset, Subbook* subbook) { static void export_subbook(Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset) {
Gaiji_context context = {};
char title[EB_MAX_TITLE_LENGTH + 1]; char title[EB_MAX_TITLE_LENGTH + 1];
if (eb_subbook_title(eb_book, title) == EB_SUCCESS) { if (eb_subbook_title(eb_book, title) == EB_SUCCESS) {
subbook->title = eucjp_to_utf8(title); subbook->title = eucjp_to_utf8(title);
context = *gaiji_select_context(subbook->title);
} }
if (eb_have_copyright(eb_book)) { if (eb_have_copyright(eb_book)) {
EB_Position position; EB_Position position;
if (eb_copyright(eb_book, &position) == EB_SUCCESS) { if (eb_copyright(eb_book, &position) == EB_SUCCESS) {
subbook->copyright = read_book_data(eb_book, eb_hookset, &position, READ_MODE_TEXT); subbook->copyright = read_book_data(eb_book, eb_hookset, &context, &position, READ_MODE_TEXT);
} }
} }
if (eb_search_all_alphabet(eb_book) == EB_SUCCESS) { if (eb_search_all_alphabet(eb_book) == EB_SUCCESS) {
export_subbook_entries(eb_book, eb_hookset, subbook); export_subbook_entries(subbook, eb_book, eb_hookset, &context);
} }
if (eb_search_all_kana(eb_book) == EB_SUCCESS) { if (eb_search_all_kana(eb_book) == EB_SUCCESS) {
export_subbook_entries(eb_book, eb_hookset, subbook); export_subbook_entries(subbook, eb_book, eb_hookset, &context);
} }
if (eb_search_all_asis(eb_book) == EB_SUCCESS) { if (eb_search_all_asis(eb_book) == EB_SUCCESS) {
export_subbook_entries(eb_book, eb_hookset, subbook); export_subbook_entries(subbook, eb_book, eb_hookset, &context);
} }
} }
static void export_book(const char path[], Book* book) { static void export_book(Book* book, const char path[]) {
do { do {
EB_Error_Code error; EB_Error_Code error;
if ((error = eb_initialize_library()) != EB_SUCCESS) { if ((error = eb_initialize_library()) != EB_SUCCESS) {
@ -156,7 +165,7 @@ static void export_book(const char path[], Book* book) {
for (int i = 0; i < book->subbook_count; ++i) { for (int i = 0; i < book->subbook_count; ++i) {
Subbook* subbook = book->subbooks + i; Subbook* subbook = book->subbooks + i;
if ((error = eb_set_subbook(&eb_book, sub_codes[i])) == EB_SUCCESS) { if ((error = eb_set_subbook(&eb_book, sub_codes[i])) == EB_SUCCESS) {
export_subbook(&eb_book, &eb_hookset, subbook); export_subbook(subbook, &eb_book, &eb_hookset);
} }
else { else {
fprintf(stderr, "Failed to set subbook: %s\n", eb_error_message(error)); fprintf(stderr, "Failed to set subbook: %s\n", eb_error_message(error));
@ -200,7 +209,7 @@ int main(int argc, char *argv[]) {
} }
Book book = {}; Book book = {};
export_book(argv[optind], &book); export_book(&book, argv[optind]);
dump_book(&book, pretty_print, stdout); dump_book(&book, pretty_print, stdout);
free_book(&book); free_book(&book);

18
util.c
View File

@ -29,12 +29,6 @@
#include "jansson/include/jansson.h" #include "jansson/include/jansson.h"
/*
* Constants
*/
#define MAX_TEXT 1024
/* /*
* Local functions * Local functions
*/ */
@ -85,12 +79,12 @@ static void encode_book(Book* book, json_t* book_json) {
* Exported functions * Exported functions
*/ */
char* read_book_data(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, ReadMode mode) { char* read_book_data(EB_Book* book, EB_Hookset* hookset, Gaiji_context* context, const EB_Position* position, ReadMode mode) {
if (eb_seek_text(book, position) != EB_SUCCESS) { if (eb_seek_text(book, position) != EB_SUCCESS) {
return NULL; return NULL;
} }
char data[MAX_TEXT]; char data[1024];
ssize_t data_length = 0; ssize_t data_length = 0;
EB_Error_Code error; EB_Error_Code error;
@ -100,8 +94,8 @@ char* read_book_data(EB_Book* book, EB_Hookset* hookset, const EB_Position* posi
book, book,
NULL, NULL,
hookset, hookset,
NULL, context,
MAX_TEXT - 1, ARRSIZE(data) - 1,
data, data,
&data_length &data_length
); );
@ -111,8 +105,8 @@ char* read_book_data(EB_Book* book, EB_Hookset* hookset, const EB_Position* posi
book, book,
NULL, NULL,
hookset, hookset,
NULL, context,
MAX_TEXT - 1, ARRSIZE(data) - 1,
data, data,
&data_length &data_length
); );

4
util.h
View File

@ -25,6 +25,8 @@
#include "eb/eb/eb.h" #include "eb/eb/eb.h"
#include "gaiji.h"
/* /*
* Macros * Macros
*/ */
@ -71,7 +73,7 @@ typedef enum {
* Functions * Functions
*/ */
char* read_book_data(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, ReadMode mode); char* read_book_data(EB_Book* book, EB_Hookset* hookset, Gaiji_context* context, const EB_Position* position, ReadMode mode);
void free_book(Book* book); void free_book(Book* book);
void dump_book(Book* book, bool pretty_print, FILE* fp); void dump_book(Book* book, bool pretty_print, FILE* fp);