From 2124c9d2fa347b30a91971a2945a659b0b45600e Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 21 Nov 2016 22:31:01 -0800 Subject: [PATCH] WIP --- book.c | 245 ++++++++++++++++++++++++++++++++++++++++++++++----------- book.h | 8 +- main.c | 155 +----------------------------------- 3 files changed, 201 insertions(+), 207 deletions(-) diff --git a/book.c b/book.c index 8189e6f..3cb1850 100644 --- a/book.c +++ b/book.c @@ -19,6 +19,7 @@ #include #include "book.h" +#include "hooks.h" #include "convert.h" #include "util.h" @@ -28,57 +29,20 @@ #include "jansson/include/jansson.h" +/* + * Local types + */ + +typedef enum { + BOOK_MODE_TEXT, + BOOK_MODE_HEADING, +} Book_Mode; + /* * Local functions */ -static void encode_entry(Book_Entry* entry, json_t* entry_json) { - json_object_set_new(entry_json, "heading", json_string(entry->heading)); - json_object_set_new(entry_json, "text", json_string(entry->text)); -} - -static void encode_subbook(Book_Subbook* subbook, json_t* subbook_json) { - if (subbook->title != NULL) { - json_object_set_new(subbook_json, "title", json_string(subbook->title)); - } - - if (subbook->copyright != NULL) { - json_object_set_new(subbook_json, "copyright", json_string(subbook->copyright)); - } - - json_t* entry_json_array = json_array(); - for (int i = 0; i < subbook->entry_count; ++i) { - json_t* entry_json = json_object(); - encode_entry(subbook->entries + i, entry_json); - json_array_append(entry_json_array, entry_json); - json_decref(entry_json); - } - - json_object_set(subbook_json, "entries", entry_json_array); - json_decref(entry_json_array); -} - -static void encode_book(Book* book, json_t* book_json) { - json_object_set_new(book_json, "characterCode", json_string(book->character_code)); - json_object_set_new(book_json, "discCode", json_string(book->disc_code)); - - json_t* subbook_json_array = json_array(); - for (int i = 0; i < book->subbook_count; ++i) { - json_t* subbook_json = json_object(); - encode_subbook(book->subbooks + i, subbook_json); - json_array_append(subbook_json_array, subbook_json); - json_decref(subbook_json); - } - - json_object_set(book_json, "subbooks", subbook_json_array); - json_decref(subbook_json_array); -} - -/* - * Exported functions - */ - -char* book_read(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, Book_Mode mode, Gaiji_Table* table) { +static char* book_read(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, Book_Mode mode, Gaiji_Table* table) { if (eb_seek_text(book, position) != EB_SUCCESS) { return NULL; } @@ -127,6 +91,110 @@ char* book_read(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, return result; } +static void encode_entry(Book_Entry* entry, json_t* entry_json) { + json_object_set_new(entry_json, "heading", json_string(entry->heading)); + json_object_set_new(entry_json, "text", json_string(entry->text)); +} + +static void encode_subbook(Book_Subbook* subbook, json_t* subbook_json) { + if (subbook->title != NULL) { + json_object_set_new(subbook_json, "title", json_string(subbook->title)); + } + + if (subbook->copyright != NULL) { + json_object_set_new(subbook_json, "copyright", json_string(subbook->copyright)); + } + + json_t* entry_json_array = json_array(); + for (int i = 0; i < subbook->entry_count; ++i) { + json_t* entry_json = json_object(); + encode_entry(subbook->entries + i, entry_json); + json_array_append(entry_json_array, entry_json); + json_decref(entry_json); + } + + json_object_set(subbook_json, "entries", entry_json_array); + json_decref(entry_json_array); +} + +static void encode_book(Book* book, json_t* book_json) { + json_object_set_new(book_json, "characterCode", json_string(book->character_code)); + json_object_set_new(book_json, "discCode", json_string(book->disc_code)); + + json_t* subbook_json_array = json_array(); + for (int i = 0; i < book->subbook_count; ++i) { + json_t* subbook_json = json_object(); + encode_subbook(book->subbooks + i, subbook_json); + json_array_append(subbook_json_array, subbook_json); + json_decref(subbook_json); + } + + json_object_set(book_json, "subbooks", subbook_json_array); + json_decref(subbook_json_array); +} + +static void export_subbook_entries(Book_Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset, Gaiji_Table* table) { + if (subbook->entry_capacity == 0) { + subbook->entry_capacity = 16384; + subbook->entries = malloc(subbook->entry_capacity * sizeof(Book_Entry)); + } + + EB_Hit hits[256]; + int hit_count = 0; + + do { + if (eb_hit_list(eb_book, ARRSIZE(hits), hits, &hit_count) != EB_SUCCESS) { + continue; + } + + for (int i = 0; i < hit_count; ++i) { + EB_Hit* hit = hits + i; + + if (subbook->entry_count == subbook->entry_capacity) { + subbook->entry_capacity *= 2; + subbook->entries = realloc(subbook->entries, subbook->entry_capacity * sizeof(Book_Entry)); + } + + Book_Entry* entry = subbook->entries + subbook->entry_count++; + entry->heading = book_read(eb_book, eb_hookset, &hit->heading, BOOK_MODE_HEADING, table); + entry->text = book_read(eb_book, eb_hookset, &hit->text, BOOK_MODE_TEXT, table); + } + } + while (hit_count > 0); +} + +static void export_subbook(Book_Subbook* subbook, const Gaiji_Context* context, EB_Book* eb_book, EB_Hookset* eb_hookset) { + Gaiji_Table table = {}; + char title[EB_MAX_TITLE_LENGTH + 1]; + if (eb_subbook_title(eb_book, title) == EB_SUCCESS) { + subbook->title = eucjp_to_utf8(title); + table = *gaiji_table_select(context, subbook->title); + } + + if (eb_have_copyright(eb_book)) { + EB_Position position; + if (eb_copyright(eb_book, &position) == EB_SUCCESS) { + subbook->copyright = book_read(eb_book, eb_hookset, &position, BOOK_MODE_TEXT, &table); + } + } + + if (eb_search_all_alphabet(eb_book) == EB_SUCCESS) { + export_subbook_entries(subbook, eb_book, eb_hookset, &table); + } + + if (eb_search_all_kana(eb_book) == EB_SUCCESS) { + export_subbook_entries(subbook, eb_book, eb_hookset, &table); + } + + if (eb_search_all_asis(eb_book) == EB_SUCCESS) { + export_subbook_entries(subbook, eb_book, eb_hookset, &table); + } +} + +/* + * Exported functions + */ + void book_free(Book* book) { for (int i = 0; i < book->subbook_count; ++i) { Book_Subbook* subbook = book->subbooks + i; @@ -155,3 +223,86 @@ void book_dump(Book* book, bool pretty_print, FILE* fp) { json_decref(book_json); } + + +void book_export(Book* book, const Gaiji_Context* context, const char path[], bool markup) { + do { + EB_Error_Code error; + if ((error = eb_initialize_library()) != EB_SUCCESS) { + fprintf(stderr, "Failed to initialize library: %s\n", eb_error_message(error)); + break; + } + + EB_Book eb_book; + eb_initialize_book(&eb_book); + + EB_Hookset eb_hookset; + eb_initialize_hookset(&eb_hookset); + hooks_install(&eb_hookset, markup); + + if ((error = eb_bind(&eb_book, path)) != EB_SUCCESS) { + fprintf(stderr, "Failed to bind book: %s\n", eb_error_message(error)); + eb_finalize_book(&eb_book); + eb_finalize_hookset(&eb_hookset); + eb_finalize_library(); + break; + } + + EB_Character_Code character_code; + if (eb_character_code(&eb_book, &character_code) == EB_SUCCESS) { + switch (character_code) { + case EB_CHARCODE_ISO8859_1: + strcpy(book->character_code, "iso8859-1"); + break; + case EB_CHARCODE_JISX0208: + strcpy(book->character_code, "jisx0208"); + break; + case EB_CHARCODE_JISX0208_GB2312: + strcpy(book->character_code, "jisx0208/gb2312"); + break; + default: + strcpy(book->character_code, "invalid"); + break; + } + } + + EB_Disc_Code disc_code; + if (eb_disc_type(&eb_book, &disc_code) == EB_SUCCESS) { + switch (disc_code) { + case EB_DISC_EB: + strcpy(book->disc_code, "eb"); + break; + case EB_DISC_EPWING: + strcpy(book->disc_code, "epwing"); + break; + default: + strcpy(book->disc_code, "invalid"); + break; + } + } + + EB_Subbook_Code sub_codes[EB_MAX_SUBBOOKS]; + if ((error = eb_subbook_list(&eb_book, sub_codes, &book->subbook_count)) == EB_SUCCESS) { + if (book->subbook_count > 0) { + book->subbooks = calloc(book->subbook_count, sizeof(Book_Subbook)); + for (int i = 0; i < book->subbook_count; ++i) { + Book_Subbook* subbook = book->subbooks + i; + if ((error = eb_set_subbook(&eb_book, sub_codes[i])) == EB_SUCCESS) { + export_subbook(subbook, context, &eb_book, &eb_hookset); + } + else { + fprintf(stderr, "Failed to set subbook: %s\n", eb_error_message(error)); + } + } + } + } + else { + fprintf(stderr, "Failed to get subbook list: %s\n", eb_error_message(error)); + } + + eb_finalize_book(&eb_book); + eb_finalize_hookset(&eb_hookset); + eb_finalize_library(); + } + while(0); +} diff --git a/book.h b/book.h index 7665695..eea5b2a 100644 --- a/book.h +++ b/book.h @@ -52,17 +52,13 @@ typedef struct { int subbook_count; } Book; -typedef enum { - BOOK_MODE_TEXT, - BOOK_MODE_HEADING, -} Book_Mode; - /* * Functions */ -char* book_read(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, Book_Mode mode, Gaiji_Table* table); +void book_init(Book* book); void book_free(Book* book); +void book_export(Book* book, const Gaiji_Context* context, const char path[], bool markup); void book_dump(Book* book, bool pretty_print, FILE* fp); #endif /* BOOK_H */ diff --git a/main.c b/main.c index 6bf0fc2..602a234 100644 --- a/main.c +++ b/main.c @@ -16,165 +16,12 @@ * along with this program. If not, see . */ -#include -#include #include #include -#include "convert.h" -#include "util.h" #include "book.h" -#include "hooks.h" #include "gaiji.h" -#include "eb/eb/eb.h" -#include "eb/text.h" -#include "eb/eb/error.h" - -/* - * Local functions - */ - -static void export_subbook_entries(Book_Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset, Gaiji_Table* table) { - if (subbook->entry_capacity == 0) { - subbook->entry_capacity = 16384; - subbook->entries = malloc(subbook->entry_capacity * sizeof(Book_Entry)); - } - - EB_Hit hits[256]; - int hit_count = 0; - - do { - if (eb_hit_list(eb_book, ARRSIZE(hits), hits, &hit_count) != EB_SUCCESS) { - continue; - } - - for (int i = 0; i < hit_count; ++i) { - EB_Hit* hit = hits + i; - - if (subbook->entry_count == subbook->entry_capacity) { - subbook->entry_capacity *= 2; - subbook->entries = realloc(subbook->entries, subbook->entry_capacity * sizeof(Book_Entry)); - } - - Book_Entry* entry = subbook->entries + subbook->entry_count++; - entry->heading = book_read(eb_book, eb_hookset, &hit->heading, BOOK_MODE_HEADING, table); - entry->text = book_read(eb_book, eb_hookset, &hit->text, BOOK_MODE_TEXT, table); - } - } - while (hit_count > 0); -} - -static void export_subbook(Book_Subbook* subbook, const Gaiji_Context* context, EB_Book* eb_book, EB_Hookset* eb_hookset) { - Gaiji_Table table = {}; - char title[EB_MAX_TITLE_LENGTH + 1]; - if (eb_subbook_title(eb_book, title) == EB_SUCCESS) { - subbook->title = eucjp_to_utf8(title); - table = *gaiji_table_select(context, subbook->title); - } - - if (eb_have_copyright(eb_book)) { - EB_Position position; - if (eb_copyright(eb_book, &position) == EB_SUCCESS) { - subbook->copyright = book_read(eb_book, eb_hookset, &position, BOOK_MODE_TEXT, &table); - } - } - - if (eb_search_all_alphabet(eb_book) == EB_SUCCESS) { - export_subbook_entries(subbook, eb_book, eb_hookset, &table); - } - - if (eb_search_all_kana(eb_book) == EB_SUCCESS) { - export_subbook_entries(subbook, eb_book, eb_hookset, &table); - } - - if (eb_search_all_asis(eb_book) == EB_SUCCESS) { - export_subbook_entries(subbook, eb_book, eb_hookset, &table); - } -} - -static void export_book(Book* book, const Gaiji_Context* context, const char path[], bool markup) { - do { - EB_Error_Code error; - if ((error = eb_initialize_library()) != EB_SUCCESS) { - fprintf(stderr, "Failed to initialize library: %s\n", eb_error_message(error)); - break; - } - - EB_Book eb_book; - eb_initialize_book(&eb_book); - - EB_Hookset eb_hookset; - eb_initialize_hookset(&eb_hookset); - hooks_install(&eb_hookset, markup); - - if ((error = eb_bind(&eb_book, path)) != EB_SUCCESS) { - fprintf(stderr, "Failed to bind book: %s\n", eb_error_message(error)); - eb_finalize_book(&eb_book); - eb_finalize_hookset(&eb_hookset); - eb_finalize_library(); - break; - } - - EB_Character_Code character_code; - if (eb_character_code(&eb_book, &character_code) == EB_SUCCESS) { - switch (character_code) { - case EB_CHARCODE_ISO8859_1: - strcpy(book->character_code, "iso8859-1"); - break; - case EB_CHARCODE_JISX0208: - strcpy(book->character_code, "jisx0208"); - break; - case EB_CHARCODE_JISX0208_GB2312: - strcpy(book->character_code, "jisx0208/gb2312"); - break; - default: - strcpy(book->character_code, "invalid"); - break; - } - } - - EB_Disc_Code disc_code; - if (eb_disc_type(&eb_book, &disc_code) == EB_SUCCESS) { - switch (disc_code) { - case EB_DISC_EB: - strcpy(book->disc_code, "eb"); - break; - case EB_DISC_EPWING: - strcpy(book->disc_code, "epwing"); - break; - default: - strcpy(book->disc_code, "invalid"); - break; - } - } - - EB_Subbook_Code sub_codes[EB_MAX_SUBBOOKS]; - if ((error = eb_subbook_list(&eb_book, sub_codes, &book->subbook_count)) == EB_SUCCESS) { - if (book->subbook_count > 0) { - book->subbooks = calloc(book->subbook_count, sizeof(Book_Subbook)); - for (int i = 0; i < book->subbook_count; ++i) { - Book_Subbook* subbook = book->subbooks + i; - if ((error = eb_set_subbook(&eb_book, sub_codes[i])) == EB_SUCCESS) { - export_subbook(subbook, context, &eb_book, &eb_hookset); - } - else { - fprintf(stderr, "Failed to set subbook: %s\n", eb_error_message(error)); - } - } - } - } - else { - fprintf(stderr, "Failed to get subbook list: %s\n", eb_error_message(error)); - } - - eb_finalize_book(&eb_book); - eb_finalize_hookset(&eb_hookset); - eb_finalize_library(); - } - while(0); -} - /* * Entry point */ @@ -207,7 +54,7 @@ int main(int argc, char *argv[]) { gaiji_context_init(&context, "gaiji.json"); Book book = {}; - export_book(&book, &context, argv[optind], markup); + book_export(&book, &context, argv[optind], markup); book_dump(&book, pretty_print, stdout); book_free(&book);