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-21 02:23:50 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.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-21 02:23:50 +00:00
|
|
|
#include "book.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-21 16:50:13 +00:00
|
|
|
static void export_subbook_entries(Book_Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset, Gaiji_Table* table) {
|
2016-11-21 02:23:50 +00:00
|
|
|
if (subbook->entry_capacity == 0) {
|
|
|
|
subbook->entry_capacity = 16384;
|
|
|
|
subbook->entries = malloc(subbook->entry_capacity * sizeof(Book_Entry));
|
2016-11-02 16:06:58 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2016-11-21 02:23:50 +00:00
|
|
|
if (subbook->entry_count == subbook->entry_capacity) {
|
|
|
|
subbook->entry_capacity *= 2;
|
|
|
|
subbook->entries = realloc(subbook->entries, subbook->entry_capacity * sizeof(Book_Entry));
|
2016-11-02 16:06:58 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 02:23:50 +00:00
|
|
|
Book_Entry* entry = subbook->entries + subbook->entry_count++;
|
2016-11-21 16:50:13 +00:00
|
|
|
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);
|
2016-11-02 16:06:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (hit_count > 0);
|
|
|
|
}
|
|
|
|
|
2016-11-21 02:23:50 +00:00
|
|
|
static void export_subbook(Book_Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset) {
|
2016-11-21 16:50:13 +00:00
|
|
|
Gaiji_Table table = {};
|
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-21 16:50:13 +00:00
|
|
|
table = *gaiji_table_select(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-21 16:50:13 +00:00
|
|
|
subbook->copyright = book_read(eb_book, eb_hookset, &position, BOOK_MODE_TEXT, &table);
|
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-21 16:50:13 +00:00
|
|
|
export_subbook_entries(subbook, eb_book, eb_hookset, &table);
|
2016-11-02 16:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (eb_search_all_kana(eb_book) == EB_SUCCESS) {
|
2016-11-21 16:50:13 +00:00
|
|
|
export_subbook_entries(subbook, eb_book, eb_hookset, &table);
|
2016-11-02 16:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (eb_search_all_asis(eb_book) == EB_SUCCESS) {
|
2016-11-21 16:50:13 +00:00
|
|
|
export_subbook_entries(subbook, eb_book, eb_hookset, &table);
|
2016-11-02 16:06:58 +00:00
|
|
|
}
|
2016-11-01 05:24:20 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 02:54:57 +00:00
|
|
|
static void export_book(Book* book, const char path[], bool markup) {
|
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);
|
2016-11-21 02:54:57 +00:00
|
|
|
hooks_install(&eb_hookset, markup);
|
2016-11-19 07:25:00 +00:00
|
|
|
|
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) {
|
2016-11-21 02:23:50 +00:00
|
|
|
book->subbooks = calloc(book->subbook_count, sizeof(Book_Subbook));
|
2016-11-02 05:34:53 +00:00
|
|
|
for (int i = 0; i < book->subbook_count; ++i) {
|
2016-11-21 02:23:50 +00:00
|
|
|
Book_Subbook* subbook = book->subbooks + i;
|
2016-11-02 05:34:53 +00:00
|
|
|
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-21 02:54:57 +00:00
|
|
|
bool markup = false;
|
2016-11-03 03:37:11 +00:00
|
|
|
|
2016-11-19 19:31:20 +00:00
|
|
|
char opt = 0;
|
2016-11-21 03:00:59 +00:00
|
|
|
while ((opt = getopt(argc, argv, "pm")) != -1) {
|
2016-11-03 03:37:11 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'p':
|
|
|
|
pretty_print = true;
|
|
|
|
break;
|
2016-11-21 02:54:57 +00:00
|
|
|
case 'm':
|
|
|
|
markup = true;
|
|
|
|
break;
|
2016-11-03 03:37:11 +00:00
|
|
|
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-21 02:54:57 +00:00
|
|
|
export_book(&book, argv[optind], markup);
|
2016-11-21 02:23:50 +00:00
|
|
|
book_dump(&book, pretty_print, stdout);
|
|
|
|
book_free(&book);
|
2016-11-03 03:37:11 +00:00
|
|
|
|
|
|
|
return 0;
|
2016-10-29 23:58:09 +00:00
|
|
|
}
|