diff --git a/CMakeLists.txt b/CMakeLists.txt index a297f30..9a1b779 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,5 +2,5 @@ cmake_minimum_required(VERSION 3.5) project(zero-epwing) include_directories(eb) link_directories(eb/eb/.libs jansson/lib) -add_executable(zero-epwing main.c util.c convert.c hooks.c) +add_executable(zero-epwing main.c util.c gaiji.c convert.c hooks.c) target_link_libraries(zero-epwing libeb.a libz.a libjansson.a) diff --git a/convert.c b/convert.c index e921690..9eacf74 100644 --- a/convert.c +++ b/convert.c @@ -22,7 +22,11 @@ #include #include -/* Reference: https://stackoverflow.com/questions/2162390/iconv-encoding-conversion-problem */ +/* + * Local functions + */ + +/* https://stackoverflow.com/questions/2162390/iconv-encoding-conversion-problem */ static char * convert (const char *from_charset, const char *to_charset, const char *input) { size_t inleft, outleft, converted = 0; @@ -109,6 +113,10 @@ static char * convert (const char *from_charset, const char *to_charset, const c return output; } +/* + * Exported functions + */ + char* eucjp_to_utf8(const char src[]) { return convert("EUC-JP", "UTF-8", src); } diff --git a/convert.h b/convert.h index aeaeb01..9fa71b9 100644 --- a/convert.h +++ b/convert.h @@ -19,6 +19,10 @@ #ifndef CONVERT_H #define CONVERT_H +/* + * Functions + */ + char* eucjp_to_utf8(const char src[]); -#endif +#endif /* CONVERT_H */ diff --git a/gaiji.c b/gaiji.c new file mode 100644 index 0000000..543cc41 --- /dev/null +++ b/gaiji.c @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "eb/eb/eb.h" +#include "eb/eb/text.h" + +#include "gaiji.h" + +/* + * Exported functions + */ + +void gaiji_build_stub(char text[MAX_STUB_BYTES], int code, const Gaiji_context* context, Gaiji_width width) { + sprintf(text, "!!!"); + (void)code; + (void)text; + (void)context; + (void)width; +} + +void gaiji_fixup_stub(char output[], int size, const char input[], const Gaiji_context* context) { + (void)output; + (void)size; + (void)input; + (void)context; +} diff --git a/gaiji.h b/gaiji.h new file mode 100644 index 0000000..9fc3ff7 --- /dev/null +++ b/gaiji.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef GAIJI_H +#define GAIJI_H + +/* + * Constants + */ + +#define MAX_STUB_BYTES 9 + +/* + * Types + */ + +typedef struct { + int code; + int bytes; + char stub[MAX_STUB_BYTES]; +} Gaiji_entry; + +typedef struct { + Gaiji_entry* table; + int count; +} Gaiji_context; + +typedef enum { + GAIJI_WIDTH_WIDE, + GAIJI_WIDTH_NARROW, +} Gaiji_width; + +/* + * Functions + */ + +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[], const Gaiji_context* context); + +#endif /* GAIJI_H */ diff --git a/hooks.c b/hooks.c index e9265a3..2a42fe4 100644 --- a/hooks.c +++ b/hooks.c @@ -16,12 +16,16 @@ * along with this program. If not, see . */ +#include + #include "hooks.h" +#include "gaiji.h" + #include "eb/eb/eb.h" #include "eb/eb/text.h" /* - * Boilerplate + * Macros */ #define HOOK_FUNC_NAME(NAME) hook_##NAME @@ -44,10 +48,8 @@ return 0;\ } -#define MAX_REPLACEMENT 64 - /* - * Basic tag printers + * Tag printers */ HOOK_TAGGER(begin_candidate); /* EB_HOOK_BEGIN_CANDIDATE */ @@ -92,7 +94,7 @@ HOOK_TAGGER(wide_jisx0208); /* EB_HOOK_WIDE_JISX0208 */ HOOK_TAGGER(null); /* EB_HOOK_NULL */ /* - * Gaiji processing + * Local functions */ static EB_Error_Code hook_narrow_font( /* EB_HOOK_NARROW_FONT */ @@ -103,30 +105,14 @@ static EB_Error_Code hook_narrow_font( /* EB_HOOK_NARROW_FONT */ int argc, const unsigned int argv[] ) { - (void)book; + assert(argc > 0); + (void)appendix; - (void)container; (void)code; - (void)argc; - (void)argv; - /* char replacement[MAX_REPLACEMENT] = "?"; */ - - /* /1* Check if there is a UTF-8 replacement for this gaiji code *1/ */ - /* if(get_gaiji_replacment_elem(subbook_directory, 'n', (unsigned short)argv[0]) != NULL) */ - /* { */ - /* sprintf(replacement, "{#n%04X}", argv[0]); */ - /* } */ - /* else if(gaiji_option == GAIJI_OPTION_HTML_IMG) /1* Add HTML IMG tag *1/ */ - /* { */ - /* get_character_html_img(replacement, book, SAVE_BMP, SAVE_NARROW, argv[0]); */ - /* } */ - /* else */ - /* { */ - /* sprintf(replacement, "?"); */ - /* } */ - - /* eb_write_text_string(book, replacement); */ + char stub[MAX_STUB_BYTES]; + gaiji_build_stub(stub, argv[0], container, GAIJI_WIDTH_NARROW); + eb_write_text_string(book, stub); return 0; } @@ -139,36 +125,20 @@ static EB_Error_Code hook_wide_font( /* EB_HOOK_WIDE_FONT */ int argc, const unsigned int argv[] ) { - (void)book; + assert(argc > 0); + (void)appendix; - (void)container; (void)code; - (void)argc; - (void)argv; - /* char replacement[MAXLEN_PATH] = ""; */ - - /* /1* Check if there is a UTF-8 replacement for this gaiji code *1/ */ - /* if(get_gaiji_replacment_elem(subbook_directory, 'w', (unsigned short)argv[0]) != NULL) */ - /* { */ - /* sprintf(replacement, "{#w%04X}", argv[0]); */ - /* } */ - /* else if(gaiji_option == GAIJI_OPTION_HTML_IMG) /1* Add HTML IMG tag *1/ */ - /* { */ - /* get_character_html_img(replacement, book, SAVE_BMP, SAVE_WIDE, argv[0]); */ - /* } */ - /* else */ - /* { */ - /* sprintf(replacement, "?"); */ - /* } */ - - /* eb_write_text_string(book, replacement); */ + char stub[MAX_STUB_BYTES]; + gaiji_build_stub(stub, argv[0], container, GAIJI_WIDTH_WIDE); + eb_write_text_string(book, stub); return 0; } /* - * Hook table + * Local data */ static const EB_Hook s_hooks[] = { @@ -207,11 +177,11 @@ static const EB_Hook s_hooks[] = { { EB_HOOK_END_WAVE, HOOK_FUNC_NAME(end_wave) }, { EB_HOOK_GB2312, HOOK_FUNC_NAME(gb2312) }, { EB_HOOK_ISO8859_1, HOOK_FUNC_NAME(iso8859_1) }, - /* { EB_HOOK_NARROW_FONT, HOOK_FUNC_NAME(narrow_font) }, */ + { EB_HOOK_NARROW_FONT, HOOK_FUNC_NAME(narrow_font) }, /* { EB_HOOK_NARROW_JISX0208, HOOK_FUNC_NAME(narrow_jisx0208) }, */ { EB_HOOK_NEWLINE, HOOK_FUNC_NAME(newline) }, { EB_HOOK_SET_INDENT, HOOK_FUNC_NAME(set_indent) }, - /* { EB_HOOK_WIDE_FONT, HOOK_FUNC_NAME(wide_font) }, */ + { EB_HOOK_WIDE_FONT, HOOK_FUNC_NAME(wide_font) }, /* { EB_HOOK_WIDE_JISX0208, HOOK_FUNC_NAME(wide_jisx0208) }, */ { EB_HOOK_NULL, HOOK_FUNC_NAME(null) }, }; diff --git a/hooks.h b/hooks.h index 2741862..4b62a36 100644 --- a/hooks.h +++ b/hooks.h @@ -21,6 +21,10 @@ #include "eb/eb/eb.h" +/* + * Exported functions + */ + void hooks_install(EB_Hookset* hookset); -#endif +#endif /* HOOKS_H */ diff --git a/main.c b/main.c index bfd8eda..f19aa44 100644 --- a/main.c +++ b/main.c @@ -16,10 +16,8 @@ * along with this program. If not, see . */ -#include #include #include -#include #include "convert.h" #include "util.h" @@ -29,9 +27,17 @@ #include "eb/text.h" #include "eb/eb/error.h" +/* + * Constants + */ + #define MAX_HITS 256 -static void export_subbook_entries(EB_Book* eb_book, Subbook* subbook) { +/* + * Local functions + */ + +static void export_subbook_entries(EB_Book* eb_book, EB_Hookset* eb_hookset, Subbook* subbook) { if (subbook->entry_cap == 0) { subbook->entry_cap = 16384; subbook->entries = malloc(subbook->entry_cap * sizeof(Entry)); @@ -169,6 +175,10 @@ static void export_book(const char path[], Book* book) { while(0); } +/* + * Entry point + */ + int main(int argc, char *argv[]) { bool pretty_print = false; diff --git a/util.c b/util.c index e7edcfb..8892458 100644 --- a/util.c +++ b/util.c @@ -29,8 +29,62 @@ #include "jansson/include/jansson.h" +/* + * Constants + */ + #define MAX_TEXT 1024 +/* + * Local functions + */ + +static void encode_entry(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(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* read_book_data(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, ReadMode mode) { if (eb_seek_text(book, position) != EB_SUCCESS) { return NULL; @@ -86,48 +140,6 @@ void free_book(Book* book) { } } -static void encode_entry(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(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); -} - void dump_book(Book* book, bool pretty_print, FILE* fp) { json_t* book_json = json_object(); encode_book(book, book_json); diff --git a/util.h b/util.h index c6b40cd..d13cfe0 100644 --- a/util.h +++ b/util.h @@ -26,11 +26,15 @@ #include "eb/eb/eb.h" /* - Dictionary structures -*/ + * Constants + */ #define MAX_ERROR 256 +/* + * Types + */ + typedef struct { char* heading; char* text; @@ -58,19 +62,17 @@ typedef struct { int subbook_count; } Book; -/* - Book helpers -*/ - typedef enum { READ_MODE_TEXT, READ_MODE_HEADING, } ReadMode; -char* read_book_data(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, ReadMode mode); +/* + * Functions + */ +char* read_book_data(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, ReadMode mode); void free_book(Book* book); void dump_book(Book* book, bool pretty_print, FILE* fp); - -#endif +#endif /* UTIL_H */