1
zero-epwing/main.c

218 lines
6.6 KiB
C
Raw Normal View History

2016-10-31 16:04:15 +00:00
/*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
2016-11-01 03:40:52 +00:00
#include <string.h>
2016-11-03 03:37:11 +00:00
#include <getopt.h>
2016-11-01 02:32:40 +00:00
#include "convert.h"
#include "util.h"
2016-11-19 07:25:00 +00:00
#include "hooks.h"
2016-11-20 03:40:29 +00:00
#include "gaiji.h"
2016-10-30 00:38:50 +00:00
2016-10-31 02:13:15 +00:00
#include "eb/eb/eb.h"
2016-11-19 07:25:00 +00:00
#include "eb/text.h"
2016-10-31 02:13:15 +00:00
#include "eb/eb/error.h"
2016-11-01 05:24:20 +00:00
2016-11-19 20:23:06 +00:00
/*
* Local functions
*/
2016-11-20 03:45:37 +00:00
static void export_subbook_entries(Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset, Gaiji_Context* context) {
2016-11-02 16:06:58 +00:00
if (subbook->entry_cap == 0) {
subbook->entry_cap = 16384;
subbook->entries = malloc(subbook->entry_cap * sizeof(Entry));
}
2016-11-20 03:40:29 +00:00
EB_Hit hits[256];
2016-11-02 16:06:58 +00:00
int hit_count = 0;
do {
2016-11-20 03:40:29 +00:00
if (eb_hit_list(eb_book, ARRSIZE(hits), hits, &hit_count) != EB_SUCCESS) {
2016-11-02 16:06:58 +00:00
continue;
}
for (int i = 0; i < hit_count; ++i) {
EB_Hit* hit = hits + i;
if (subbook->entry_count == subbook->entry_cap) {
subbook->entry_cap *= 2;
subbook->entries = realloc(subbook->entries, subbook->entry_cap * sizeof(Entry));
}
Entry* entry = subbook->entries + subbook->entry_count++;
2016-11-20 03:40:29 +00:00
entry->heading = read_book_data(
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
);
2016-11-02 16:06:58 +00:00
}
}
while (hit_count > 0);
}
2016-11-20 03:40:29 +00:00
static void export_subbook(Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset) {
2016-11-20 03:45:37 +00:00
Gaiji_Context context = {};
2016-11-02 03:36:32 +00:00
char title[EB_MAX_TITLE_LENGTH + 1];
2016-11-02 05:34:53 +00:00
if (eb_subbook_title(eb_book, title) == EB_SUCCESS) {
subbook->title = eucjp_to_utf8(title);
2016-11-20 03:40:29 +00:00
context = *gaiji_select_context(subbook->title);
2016-11-01 05:24:20 +00:00
}
2016-11-02 05:34:53 +00:00
if (eb_have_copyright(eb_book)) {
2016-11-01 05:24:20 +00:00
EB_Position position;
2016-11-02 05:34:53 +00:00
if (eb_copyright(eb_book, &position) == EB_SUCCESS) {
2016-11-20 03:40:29 +00:00
subbook->copyright = read_book_data(eb_book, eb_hookset, &context, &position, READ_MODE_TEXT);
2016-11-01 05:24:20 +00:00
}
}
2016-11-02 16:06:58 +00:00
if (eb_search_all_alphabet(eb_book) == EB_SUCCESS) {
2016-11-20 03:40:29 +00:00
export_subbook_entries(subbook, eb_book, eb_hookset, &context);
2016-11-02 16:06:58 +00:00
}
if (eb_search_all_kana(eb_book) == EB_SUCCESS) {
2016-11-20 03:40:29 +00:00
export_subbook_entries(subbook, eb_book, eb_hookset, &context);
2016-11-02 16:06:58 +00:00
}
if (eb_search_all_asis(eb_book) == EB_SUCCESS) {
2016-11-20 03:40:29 +00:00
export_subbook_entries(subbook, eb_book, eb_hookset, &context);
2016-11-02 16:06:58 +00:00
}
2016-11-01 05:24:20 +00:00
}
2016-11-20 03:40:29 +00:00
static void export_book(Book* book, const char path[]) {
2016-10-31 02:13:15 +00:00
do {
2016-11-02 03:48:39 +00:00
EB_Error_Code error;
if ((error = eb_initialize_library()) != EB_SUCCESS) {
2016-11-17 03:10:44 +00:00
fprintf(stderr, "Failed to initialize library: %s\n", eb_error_message(error));
2016-11-01 03:40:52 +00:00
break;
}
2016-11-02 05:34:53 +00:00
EB_Book eb_book;
eb_initialize_book(&eb_book);
2016-11-19 07:25:00 +00:00
EB_Hookset eb_hookset;
eb_initialize_hookset(&eb_hookset);
hooks_install(&eb_hookset);
2016-11-02 05:34:53 +00:00
if ((error = eb_bind(&eb_book, path)) != EB_SUCCESS) {
2016-11-17 03:10:44 +00:00
fprintf(stderr, "Failed to bind book: %s\n", eb_error_message(error));
2016-11-02 05:34:53 +00:00
eb_finalize_book(&eb_book);
2016-11-19 07:25:00 +00:00
eb_finalize_hookset(&eb_hookset);
2016-11-01 03:40:52 +00:00
eb_finalize_library();
2016-10-31 02:13:15 +00:00
break;
}
2016-10-30 03:53:10 +00:00
2016-11-01 03:40:52 +00:00
EB_Character_Code character_code;
2016-11-02 05:34:53 +00:00
if (eb_character_code(&eb_book, &character_code) == EB_SUCCESS) {
2016-11-01 03:40:52 +00:00
switch (character_code) {
case EB_CHARCODE_ISO8859_1:
2016-11-02 05:34:53 +00:00
strcpy(book->character_code, "iso8859-1");
2016-11-01 03:40:52 +00:00
break;
case EB_CHARCODE_JISX0208:
2016-11-02 05:34:53 +00:00
strcpy(book->character_code, "jisx0208");
2016-11-01 03:40:52 +00:00
break;
case EB_CHARCODE_JISX0208_GB2312:
2016-11-02 05:34:53 +00:00
strcpy(book->character_code, "jisx0208/gb2312");
2016-11-01 03:40:52 +00:00
break;
2016-11-03 03:06:40 +00:00
default:
strcpy(book->character_code, "invalid");
break;
2016-11-01 03:40:52 +00:00
}
}
EB_Disc_Code disc_code;
2016-11-02 05:34:53 +00:00
if (eb_disc_type(&eb_book, &disc_code) == EB_SUCCESS) {
2016-11-01 03:40:52 +00:00
switch (disc_code) {
case EB_DISC_EB:
2016-11-02 05:34:53 +00:00
strcpy(book->disc_code, "eb");
2016-11-01 03:40:52 +00:00
break;
case EB_DISC_EPWING:
2016-11-02 05:34:53 +00:00
strcpy(book->disc_code, "epwing");
2016-11-01 03:40:52 +00:00
break;
2016-11-03 03:06:40 +00:00
default:
strcpy(book->disc_code, "invalid");
break;
2016-11-01 03:40:52 +00:00
}
}
2016-10-31 02:13:15 +00:00
EB_Subbook_Code sub_codes[EB_MAX_SUBBOOKS];
2016-11-02 05:34:53 +00:00
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(Subbook));
for (int i = 0; i < book->subbook_count; ++i) {
Subbook* subbook = book->subbooks + i;
if ((error = eb_set_subbook(&eb_book, sub_codes[i])) == EB_SUCCESS) {
2016-11-20 03:40:29 +00:00
export_subbook(subbook, &eb_book, &eb_hookset);
2016-11-02 03:48:39 +00:00
}
else {
2016-11-17 03:10:44 +00:00
fprintf(stderr, "Failed to set subbook: %s\n", eb_error_message(error));
2016-11-02 03:48:39 +00:00
}
2016-11-02 03:36:32 +00:00
}
}
}
2016-11-02 03:48:39 +00:00
else {
2016-11-17 03:10:44 +00:00
fprintf(stderr, "Failed to get subbook list: %s\n", eb_error_message(error));
2016-11-02 03:48:39 +00:00
}
2016-11-01 03:40:52 +00:00
2016-11-02 05:34:53 +00:00
eb_finalize_book(&eb_book);
2016-11-19 07:25:00 +00:00
eb_finalize_hookset(&eb_hookset);
2016-11-01 03:40:52 +00:00
eb_finalize_library();
2016-10-31 02:13:15 +00:00
}
2016-11-01 03:40:52 +00:00
while(0);
2016-10-30 01:27:05 +00:00
}
2016-11-19 20:23:06 +00:00
/*
* Entry point
*/
2016-10-30 01:27:05 +00:00
int main(int argc, char *argv[]) {
2016-11-03 03:37:11 +00:00
bool pretty_print = false;
2016-11-19 19:31:20 +00:00
char opt = 0;
2016-11-03 03:37:11 +00:00
while ((opt = getopt(argc, argv, "p")) != -1) {
switch (opt) {
case 'p':
pretty_print = true;
break;
default:
exit(EXIT_FAILURE);
break;
}
2016-10-30 01:27:05 +00:00
}
2016-11-03 03:37:11 +00:00
if (optind >= argc) {
fprintf(stderr, "Usage: %s [-p] dictionary_path\n", argv[0]);
exit(EXIT_FAILURE);
2016-11-03 03:06:40 +00:00
}
2016-11-03 03:37:11 +00:00
Book book = {};
2016-11-20 03:40:29 +00:00
export_book(&book, argv[optind]);
2016-11-03 03:37:11 +00:00
dump_book(&book, pretty_print, stdout);
free_book(&book);
return 0;
2016-10-29 23:58:09 +00:00
}