1
zero-epwing/book.c

655 lines
20 KiB
C
Raw Normal View History

2016-11-01 02:32:40 +00:00
/*
2017-04-23 19:32:22 +00:00
* Copyright (C) 2017 Alex Yatskov <alex@foosoft.net>
2016-11-01 02:32:40 +00:00
* Author: Alex Yatskov <alex@foosoft.net>
*
2017-04-23 19:32:22 +00:00
* 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.
2016-11-01 02:32:40 +00:00
*
2017-04-23 19:32:22 +00:00
* 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.
2016-11-01 02:32:40 +00:00
*
2017-04-23 19:32:22 +00:00
* 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 02:32:40 +00:00
*/
2016-11-03 03:06:40 +00:00
#include <string.h>
2016-11-03 02:06:58 +00:00
2016-11-21 02:23:50 +00:00
#include "book.h"
2016-11-22 06:31:01 +00:00
#include "hooks.h"
2016-11-02 16:14:14 +00:00
#include "convert.h"
2016-11-21 02:23:50 +00:00
#include "util.h"
2016-11-02 16:14:14 +00:00
#include "eb/eb/eb.h"
2017-02-12 18:42:30 +00:00
#include "eb/eb/font.h"
2016-11-02 16:14:14 +00:00
#include "eb/eb/text.h"
2017-02-12 18:42:30 +00:00
#include "eb/eb/error.h"
2016-11-02 16:14:14 +00:00
2016-11-03 03:06:40 +00:00
#include "jansson/include/jansson.h"
2016-11-22 06:31:01 +00:00
/*
* Local types
*/
typedef enum {
BOOK_MODE_TEXT,
BOOK_MODE_HEADING,
} Book_Mode;
2017-02-12 18:42:30 +00:00
typedef struct Page {
2016-12-01 05:16:59 +00:00
int* offsets;
int offset_count;
int offset_alloc;
} Page;
2017-02-12 18:42:30 +00:00
typedef struct Book_Block {
char* text;
int page;
int offset;
} Book_Block;
typedef struct Book_Entry{
Book_Block heading;
Book_Block text;
} Book_Entry;
typedef struct Book_Glyph {
char bitmap[EB_SIZE_WIDE_FONT_48];
int code;
} Book_Glyph;
typedef struct Book_Glyph_Set {
Book_Glyph* glyphs;
int bitmap_size;
int width;
int height;
int count;
} Book_Glyph_Set;
typedef struct Book_Font {
Book_Glyph_Set wide;
Book_Glyph_Set narrow;
} Book_Font;
typedef struct Book_Subbook {
char* title;
Book_Block copyright;
Book_Entry* entries;
int entry_count;
int entry_alloc;
Book_Font fonts[4];
} Book_Subbook;
typedef struct Book {
char char_code[32];
char disc_code[32];
Book_Subbook* subbooks;
int subbook_count;
} Book;
2016-11-19 20:23:06 +00:00
/*
2016-12-01 03:34:00 +00:00
* Helper functions
2016-11-19 20:23:06 +00:00
*/
static char* book_read(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, Book_Mode mode) {
2016-11-22 06:31:01 +00:00
if (eb_seek_text(book, position) != EB_SUCCESS) {
return NULL;
}
char data[10240] = {};
2016-11-22 06:31:01 +00:00
ssize_t data_length = 0;
EB_Error_Code error;
switch (mode) {
case BOOK_MODE_TEXT:
error = eb_read_text(
book,
NULL,
hookset,
NULL,
2016-11-22 06:31:01 +00:00
ARRSIZE(data) - 1,
data,
&data_length
);
break;
case BOOK_MODE_HEADING:
error = eb_read_heading(
book,
NULL,
hookset,
NULL,
2016-11-22 06:31:01 +00:00
ARRSIZE(data) - 1,
data,
&data_length
);
break;
default:
return NULL;
}
if (error != EB_SUCCESS) {
return NULL;
}
char * result = eucjp_to_utf8(data);
if (result == NULL) {
return NULL;
}
return result;
}
static Book_Block book_read_content(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, Book_Mode mode) {
2016-11-30 17:26:50 +00:00
Book_Block block = {};
block.text = book_read(book, hookset, position, mode);
2016-11-30 17:26:50 +00:00
block.page = position->page;
block.offset = position->offset;
return block;
2016-11-30 16:55:54 +00:00
}
2016-12-01 05:16:59 +00:00
static void subbook_undupe(Book_Subbook* subbook) {
int page_count = 0;
for (int i = 0; i < subbook->entry_count; ++i) {
const int page_index = subbook->entries[i].text.page + 1;
if (page_count < page_index) {
page_count = page_index;
}
}
if (page_count == 0) {
return;
}
Page* pages = calloc(page_count, sizeof(Page));
for (int i = 0; i < subbook->entry_count; ++i) {
Book_Entry* entry = subbook->entries + i;
Page* page = pages + entry->text.page;
2016-12-05 02:36:06 +00:00
int found = 0;
2016-12-01 05:16:59 +00:00
for (int j = 0; j < page->offset_count; ++j) {
if (entry->text.offset == page->offsets[j]){
2016-12-05 02:36:06 +00:00
found = 1;
2016-12-01 05:16:59 +00:00
break;
}
}
if (found) {
if (i + 1 < subbook->entry_count) {
subbook->entries[i] = subbook->entries[subbook->entry_count - 1];
}
--subbook->entry_count;
--i;
continue;
}
if (page->offset_count + 1 >= page->offset_alloc) {
if (page->offset_alloc == 0) {
page->offset_alloc = 32;
page->offsets = malloc(page->offset_alloc * sizeof(int));
}
else {
const int offset_alloc_new = page->offset_alloc * 2;
page->offsets = realloc(page->offsets, offset_alloc_new * sizeof(int));
page->offset_alloc = offset_alloc_new;
}
}
page->offsets[page->offset_count++] = entry->text.offset;
}
for (int i = 0; i < page_count; ++i) {
free(pages[i].offsets);
}
free(pages);
}
static void book_undupe(Book* book) {
for (int i = 0; i < book->subbook_count; ++i) {
subbook_undupe(book->subbooks + i);
}
}
2016-12-01 03:34:00 +00:00
/*
* Encoding to JSON
*/
2016-12-01 07:19:16 +00:00
static void entry_encode(json_t* entry_json, const Book_Entry* entry, int flags) {
2016-12-01 05:16:59 +00:00
if (entry->heading.text != NULL) {
json_object_set_new(entry_json, "heading", json_string(entry->heading.text));
}
2016-11-30 04:26:27 +00:00
2016-12-01 07:19:16 +00:00
if (flags & FLAG_POSITIONS) {
json_object_set_new(entry_json, "headingPage", json_integer(entry->heading.page));
json_object_set_new(entry_json, "headingOffset", json_integer(entry->heading.offset));
}
2016-12-01 05:16:59 +00:00
if (entry->text.text != NULL) {
json_object_set_new(entry_json, "text", json_string(entry->text.text));
}
2016-12-01 07:19:16 +00:00
if (flags & FLAG_POSITIONS) {
json_object_set_new(entry_json, "textPage", json_integer(entry->text.page));
json_object_set_new(entry_json, "textOffset", json_integer(entry->text.offset));
}
2016-11-19 20:23:06 +00:00
}
2017-02-12 18:16:25 +00:00
static void font_glyph_encode(json_t* glyph_json, const Book_Glyph* glyph, int bitmap_size) {
2017-02-12 03:46:46 +00:00
json_t* bitmap_json_array = json_array();
2017-02-12 18:16:25 +00:00
for (int i = 0; i < bitmap_size; ++i) {
2017-02-12 18:49:28 +00:00
json_array_append_new(bitmap_json_array, json_integer((unsigned char)glyph->bitmap[i]));
2017-02-12 03:46:46 +00:00
}
json_object_set_new(glyph_json, "bitmap", bitmap_json_array);
json_object_set_new(glyph_json, "code", json_integer(glyph->code));
}
static void font_glyph_set_encode(json_t* glyph_set_json, const Book_Glyph_Set* glyph_set) {
json_t* glyph_json_array = json_array();
for (int i = 0; i < glyph_set->count; ++i) {
json_t* glyph_json = json_object();
2017-02-12 18:16:25 +00:00
font_glyph_encode(glyph_json, glyph_set->glyphs + i, glyph_set->bitmap_size);
2017-02-12 03:46:46 +00:00
json_array_append_new(glyph_json_array, glyph_json);
}
json_object_set_new(glyph_set_json, "glyphs", glyph_json_array);
json_object_set_new(glyph_set_json, "width", json_integer(glyph_set->width));
json_object_set_new(glyph_set_json, "height", json_integer(glyph_set->height));
}
static void font_encode(json_t* font_json, const Book_Font* font) {
if (font->narrow.count > 0) {
json_t* narrow_json = json_object();
font_glyph_set_encode(narrow_json, &font->narrow);
json_object_set_new(font_json, "narrow", narrow_json);
}
if (font->wide.count > 0) {
json_t* wide_json = json_object();
font_glyph_set_encode(wide_json, &font->wide);
json_object_set_new(font_json, "wide", wide_json);
}
}
2016-12-01 07:19:16 +00:00
static void subbook_encode(json_t* subbook_json, const Book_Subbook* subbook, int flags) {
2016-11-19 20:23:06 +00:00
if (subbook->title != NULL) {
json_object_set_new(subbook_json, "title", json_string(subbook->title));
}
2016-11-30 16:55:54 +00:00
if (subbook->copyright.text != NULL) {
json_object_set_new(subbook_json, "copyright", json_string(subbook->copyright.text));
2016-11-19 20:23:06 +00:00
}
2016-12-01 07:19:16 +00:00
if (flags & FLAG_POSITIONS) {
json_object_set_new(subbook_json, "copyrightPage", json_integer(subbook->copyright.page));
json_object_set_new(subbook_json, "copyrightOffset", json_integer(subbook->copyright.offset));
}
2017-02-12 03:46:46 +00:00
if (flags & FLAG_FONTS) {
json_t* font_json_array = json_array();
for (unsigned i = 0; i < ARRSIZE(subbook->fonts); ++i) {
const Book_Font* font = subbook->fonts + i;
if (font->wide.count > 0 || font->narrow.count) {
json_t* font_json = json_object();
font_encode(font_json, font);
json_array_append_new(font_json_array, font_json);
}
}
json_object_set_new(subbook_json, "fonts", font_json_array);
}
2017-02-12 18:24:57 +00:00
if (flags & FLAG_ENTRIES) {
json_t* entry_json_array = json_array();
for (int i = 0; i < subbook->entry_count; ++i) {
json_t* entry_json = json_object();
entry_encode(entry_json, subbook->entries + i, flags);
json_array_append_new(entry_json_array, entry_json);
}
2016-11-19 20:23:06 +00:00
2017-02-12 18:24:57 +00:00
json_object_set_new(subbook_json, "entries", entry_json_array);
}
2016-11-19 20:23:06 +00:00
}
2016-12-01 07:19:16 +00:00
static void book_encode(json_t* book_json, const Book* book, int flags) {
2016-11-29 07:04:48 +00:00
json_object_set_new(book_json, "charCode", json_string(book->char_code));
2016-11-19 20:23:06 +00:00
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();
2016-12-01 07:19:16 +00:00
subbook_encode(subbook_json, book->subbooks + i, flags);
2016-11-29 07:04:48 +00:00
json_array_append_new(subbook_json_array, subbook_json);
2016-11-19 20:23:06 +00:00
}
2016-11-29 07:04:48 +00:00
json_object_set_new(book_json, "subbooks", subbook_json_array);
2016-11-19 20:23:06 +00:00
}
2016-12-01 03:34:00 +00:00
/*
* Importing from EPWING
*/
static void subbook_entries_import(Book_Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset) {
2016-11-29 05:24:51 +00:00
if (subbook->entry_alloc == 0) {
subbook->entry_alloc = 16384;
subbook->entries = malloc(subbook->entry_alloc * sizeof(Book_Entry));
2016-11-22 06:31:01 +00:00
}
2016-11-19 20:23:06 +00:00
2016-11-29 07:04:48 +00:00
EB_Hit hits[256] = {};
2016-11-22 06:31:01 +00:00
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;
2016-11-29 05:24:51 +00:00
if (subbook->entry_count == subbook->entry_alloc) {
subbook->entry_alloc *= 2;
subbook->entries = realloc(subbook->entries, subbook->entry_alloc * sizeof(Book_Entry));
2016-11-22 06:31:01 +00:00
}
Book_Entry* entry = subbook->entries + subbook->entry_count++;
entry->heading = book_read_content(eb_book, eb_hookset, &hit->heading, BOOK_MODE_HEADING);
entry->text = book_read_content(eb_book, eb_hookset, &hit->text, BOOK_MODE_TEXT);
2016-11-22 06:31:01 +00:00
}
2016-11-02 16:14:14 +00:00
}
2016-11-22 06:31:01 +00:00
while (hit_count > 0);
}
2016-11-02 16:14:14 +00:00
2017-02-12 03:46:46 +00:00
static void subbook_font_import(Book_Font* font, EB_Book* eb_book, EB_Font_Code code) {
if (eb_set_font(eb_book, code) != EB_SUCCESS) {
return;
}
do {
2017-02-12 18:16:25 +00:00
Book_Glyph_Set* glyph_set = &font->narrow;
2017-02-12 03:46:46 +00:00
switch (code) {
case EB_FONT_16:
2017-02-12 18:16:25 +00:00
glyph_set->width = EB_WIDTH_NARROW_FONT_16;
glyph_set->height = EB_HEIGHT_FONT_16;
glyph_set->bitmap_size = EB_SIZE_NARROW_FONT_16;
2017-02-12 03:46:46 +00:00
break;
case EB_FONT_24:
2017-02-12 18:16:25 +00:00
glyph_set->width = EB_WIDTH_NARROW_FONT_24;
glyph_set->height = EB_HEIGHT_FONT_24;
glyph_set->bitmap_size = EB_SIZE_NARROW_FONT_24;
2017-02-12 03:46:46 +00:00
break;
case EB_FONT_30:
2017-02-12 18:16:25 +00:00
glyph_set->width = EB_WIDTH_NARROW_FONT_30;
glyph_set->height = EB_HEIGHT_FONT_30;
glyph_set->bitmap_size = EB_SIZE_NARROW_FONT_30;
2017-02-12 03:46:46 +00:00
break;
case EB_FONT_48:
2017-02-12 18:16:25 +00:00
glyph_set->width = EB_WIDTH_NARROW_FONT_48;
glyph_set->height = EB_HEIGHT_FONT_48;
glyph_set->bitmap_size = EB_SIZE_NARROW_FONT_48;
2017-02-12 03:46:46 +00:00
break;
}
int font_code = 0;
if (eb_narrow_font_start(eb_book, &font_code) != EB_SUCCESS) {
break;
}
int glyph_alloc = 256;
2017-02-12 18:16:25 +00:00
glyph_set->glyphs = malloc(sizeof(Book_Glyph) * glyph_alloc);
2017-02-12 03:46:46 +00:00
for (;;) {
2017-02-12 18:16:25 +00:00
if (glyph_set->count == glyph_alloc) {
2017-02-12 03:46:46 +00:00
glyph_alloc *= 2;
2017-02-12 18:16:25 +00:00
glyph_set->glyphs = realloc(glyph_set->glyphs, sizeof(Book_Glyph) * glyph_alloc);
2017-02-12 03:46:46 +00:00
}
2017-02-12 18:16:25 +00:00
Book_Glyph* glyph = glyph_set->glyphs + glyph_set->count;
2017-02-12 03:46:46 +00:00
glyph->code = font_code;
2017-02-12 18:24:57 +00:00
memset(glyph->bitmap, 0, glyph_set->bitmap_size);
2017-02-12 03:46:46 +00:00
if (eb_narrow_font_character_bitmap(eb_book, font_code, glyph->bitmap) != EB_SUCCESS) {
break;
}
2017-02-12 18:16:25 +00:00
++glyph_set->count;
2017-02-12 03:46:46 +00:00
if (eb_forward_narrow_font_character(eb_book, 1, &font_code) != EB_SUCCESS) {
break;
}
}
}
while (0);
do {
2017-02-12 18:16:25 +00:00
Book_Glyph_Set* glyph_set = &font->wide;
2017-02-12 03:46:46 +00:00
switch (code) {
case EB_FONT_16:
2017-02-12 18:16:25 +00:00
glyph_set->width = EB_WIDTH_WIDE_FONT_16;
glyph_set->height = EB_HEIGHT_FONT_16;
glyph_set->bitmap_size = EB_SIZE_WIDE_FONT_16;
2017-02-12 03:46:46 +00:00
break;
case EB_FONT_24:
2017-02-12 18:16:25 +00:00
glyph_set->width = EB_WIDTH_WIDE_FONT_24;
glyph_set->height = EB_HEIGHT_FONT_24;
glyph_set->bitmap_size = EB_SIZE_WIDE_FONT_24;
2017-02-12 03:46:46 +00:00
break;
case EB_FONT_30:
2017-02-12 18:16:25 +00:00
glyph_set->width = EB_WIDTH_WIDE_FONT_30;
glyph_set->height = EB_HEIGHT_FONT_30;
glyph_set->bitmap_size = EB_SIZE_WIDE_FONT_30;
2017-02-12 03:46:46 +00:00
break;
case EB_FONT_48:
2017-02-12 18:16:25 +00:00
glyph_set->width = EB_WIDTH_WIDE_FONT_48;
glyph_set->height = EB_HEIGHT_FONT_48;
glyph_set->bitmap_size = EB_SIZE_WIDE_FONT_48;
2017-02-12 03:46:46 +00:00
break;
}
int font_code = 0;
if (eb_wide_font_start(eb_book, &font_code) != EB_SUCCESS) {
break;
}
int glyph_alloc = 256;
2017-02-12 18:16:25 +00:00
glyph_set->glyphs = malloc(sizeof(Book_Glyph) * glyph_alloc);
2017-02-12 03:46:46 +00:00
for (;;) {
2017-02-12 18:16:25 +00:00
if (glyph_set->count == glyph_alloc) {
2017-02-12 03:46:46 +00:00
glyph_alloc *= 2;
2017-02-12 18:16:25 +00:00
glyph_set->glyphs = realloc(glyph_set->glyphs, sizeof(Book_Glyph) * glyph_alloc);
2017-02-12 03:46:46 +00:00
}
2017-02-12 18:16:25 +00:00
Book_Glyph* glyph = glyph_set->glyphs + glyph_set->count;
2017-02-12 03:46:46 +00:00
glyph->code = font_code;
2017-02-12 18:24:57 +00:00
memset(glyph->bitmap, 0, glyph_set->bitmap_size);
2017-02-12 03:46:46 +00:00
if (eb_wide_font_character_bitmap(eb_book, font_code, glyph->bitmap) != EB_SUCCESS) {
break;
}
2017-02-12 18:16:25 +00:00
++glyph_set->count;
2017-02-12 03:46:46 +00:00
if (eb_forward_wide_font_character(eb_book, 1, &font_code) != EB_SUCCESS) {
break;
}
}
}
while (0);
}
static void subbook_import(Book_Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset, int flags) {
2016-11-22 06:31:01 +00:00
char title[EB_MAX_TITLE_LENGTH + 1];
if (eb_subbook_title(eb_book, title) == EB_SUCCESS) {
subbook->title = eucjp_to_utf8(title);
}
2016-11-02 16:14:14 +00:00
2016-11-22 06:31:01 +00:00
if (eb_have_copyright(eb_book)) {
EB_Position position;
if (eb_copyright(eb_book, &position) == EB_SUCCESS) {
subbook->copyright = book_read_content(eb_book, eb_hookset, &position, BOOK_MODE_TEXT);
2016-11-22 06:31:01 +00:00
}
2016-11-02 16:14:14 +00:00
}
2017-02-12 18:24:57 +00:00
if (flags & FLAG_ENTRIES) {
if (eb_search_all_alphabet(eb_book) == EB_SUCCESS) {
subbook_entries_import(subbook, eb_book, eb_hookset);
}
2016-11-21 01:45:04 +00:00
2017-02-12 18:24:57 +00:00
if (eb_search_all_kana(eb_book) == EB_SUCCESS) {
subbook_entries_import(subbook, eb_book, eb_hookset);
}
2016-11-21 01:45:04 +00:00
2017-02-12 18:24:57 +00:00
if (eb_search_all_asis(eb_book) == EB_SUCCESS) {
subbook_entries_import(subbook, eb_book, eb_hookset);
}
2016-11-22 06:31:01 +00:00
}
2017-02-12 03:46:46 +00:00
if (flags & FLAG_FONTS) {
const EB_Font_Code codes[] = {EB_FONT_16, EB_FONT_24, EB_FONT_30, EB_FONT_48};
for (unsigned i = 0; i < ARRSIZE(codes); ++i) {
2017-02-12 06:02:20 +00:00
subbook_font_import(subbook->fonts + i, eb_book, codes[i]);
2017-02-12 03:46:46 +00:00
}
}
2016-11-02 16:14:14 +00:00
}
2016-11-22 06:31:01 +00:00
/*
2016-12-01 03:34:00 +00:00
* imported functions
2016-11-22 06:31:01 +00:00
*/
2017-02-12 18:42:30 +00:00
Book* book_create() {
return calloc(1, sizeof(Book));
2016-11-22 06:41:44 +00:00
}
2017-02-12 18:42:30 +00:00
void book_destroy(Book* book) {
2016-11-02 16:14:14 +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 16:14:14 +00:00
free(subbook->title);
2016-11-30 16:55:54 +00:00
free(subbook->copyright.text);
2016-11-02 16:14:14 +00:00
for (int j = 0; j < subbook->entry_count; ++j) {
2016-11-21 02:23:50 +00:00
Book_Entry* entry = subbook->entries + j;
2016-11-30 16:55:54 +00:00
free(entry->heading.text);
free(entry->text.text);
2016-11-02 16:14:14 +00:00
}
2017-02-12 03:46:46 +00:00
for (unsigned j = 0; j < ARRSIZE(subbook->fonts); ++j) {
2017-02-12 06:02:20 +00:00
const Book_Font* font = subbook->fonts + j;
2017-02-12 03:46:46 +00:00
free(font->narrow.glyphs);
free(font->wide.glyphs);
}
2016-11-02 16:14:14 +00:00
free(subbook->entries);
}
2016-11-29 05:31:46 +00:00
memset(book, 0, sizeof(Book));
2016-11-02 16:14:14 +00:00
}
2016-11-03 03:06:40 +00:00
2016-12-05 02:36:06 +00:00
int book_export(FILE* fp, const Book* book, int flags) {
2016-11-03 03:06:40 +00:00
json_t* book_json = json_object();
2016-12-01 07:19:16 +00:00
book_encode(book_json, book, flags);
2016-11-03 03:06:40 +00:00
2016-12-01 07:19:16 +00:00
char* output = json_dumps(book_json, flags & FLAG_PRETTY_PRINT ? JSON_INDENT(4) : JSON_COMPACT);
2016-11-03 03:06:40 +00:00
if (output != NULL) {
fputs(output, fp);
}
free(output);
json_decref(book_json);
2016-11-29 05:04:55 +00:00
return output != NULL;
2016-11-03 03:06:40 +00:00
}
2016-11-22 06:31:01 +00:00
int book_import(Book* book, const char path[], int flags) {
2016-11-29 05:04:55 +00:00
EB_Error_Code error;
if ((error = eb_initialize_library()) != EB_SUCCESS) {
2017-01-01 00:08:28 +00:00
fprintf(stderr, "error: failed to initialize library (%s)\n", eb_error_message(error));
2016-12-05 02:36:06 +00:00
return 0;
2016-11-29 05:04:55 +00:00
}
2016-11-22 06:31:01 +00:00
2016-11-29 05:04:55 +00:00
EB_Book eb_book;
eb_initialize_book(&eb_book);
2016-11-22 06:31:01 +00:00
2016-11-29 05:04:55 +00:00
EB_Hookset eb_hookset;
eb_initialize_hookset(&eb_hookset);
2016-12-01 07:19:16 +00:00
hooks_install(&eb_hookset, flags);
2016-11-22 06:31:01 +00:00
2016-11-29 05:04:55 +00:00
if ((error = eb_bind(&eb_book, path)) != EB_SUCCESS) {
2017-01-01 00:08:28 +00:00
fprintf(stderr, "error: failed to bind book (%s)\n", eb_error_message(error));
2016-11-29 05:04:55 +00:00
eb_finalize_book(&eb_book);
eb_finalize_hookset(&eb_hookset);
eb_finalize_library();
2016-12-05 02:36:06 +00:00
return 0;
2016-11-29 05:04:55 +00:00
}
2016-11-22 06:31:01 +00:00
2016-11-29 05:31:46 +00:00
EB_Character_Code char_code;
if ((error = eb_character_code(&eb_book, &char_code)) == EB_SUCCESS) {
switch (char_code) {
2016-11-29 05:04:55 +00:00
case EB_CHARCODE_ISO8859_1:
2016-11-29 05:24:51 +00:00
strcpy(book->char_code, "iso8859-1");
2016-11-29 05:04:55 +00:00
break;
case EB_CHARCODE_JISX0208:
2016-11-29 05:24:51 +00:00
strcpy(book->char_code, "jisx0208");
2016-11-29 05:04:55 +00:00
break;
case EB_CHARCODE_JISX0208_GB2312:
2016-11-29 05:24:51 +00:00
strcpy(book->char_code, "jisx0208/gb2312");
2016-11-29 05:04:55 +00:00
break;
default:
2016-11-29 05:24:51 +00:00
strcpy(book->char_code, "invalid");
2016-11-29 05:04:55 +00:00
break;
2016-11-22 06:31:01 +00:00
}
2016-11-29 05:04:55 +00:00
}
2016-11-29 05:31:46 +00:00
else {
2017-01-01 00:08:28 +00:00
fprintf(stderr, "error: failed to get character code (%s)\n", eb_error_message(error));
2016-11-29 05:31:46 +00:00
}
2016-11-22 06:31:01 +00:00
2016-11-29 05:04:55 +00:00
EB_Disc_Code disc_code;
2016-11-29 05:31:46 +00:00
if ((error = eb_disc_type(&eb_book, &disc_code)) == EB_SUCCESS) {
2016-11-29 05:04:55 +00:00
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;
2016-11-22 06:31:01 +00:00
}
2016-11-29 05:04:55 +00:00
}
2016-11-29 05:31:46 +00:00
else {
2017-01-01 00:08:28 +00:00
fprintf(stderr, "error: failed to get disc code (%s)\n", eb_error_message(error));
2016-11-29 05:31:46 +00:00
}
2016-11-22 06:31:01 +00:00
2016-11-29 05:04:55 +00:00
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) {
2017-02-12 03:46:46 +00:00
subbook_import(subbook, &eb_book, &eb_hookset, flags);
2016-11-29 05:04:55 +00:00
}
else {
2017-01-01 00:08:28 +00:00
fprintf(stderr, "error: failed to set subbook (%s)\n", eb_error_message(error));
2016-11-22 06:31:01 +00:00
}
}
}
}
2016-11-29 05:04:55 +00:00
else {
2017-01-01 00:08:28 +00:00
fprintf(stderr, "error: failed to get subbook list (%s)\n", eb_error_message(error));
2016-11-29 05:04:55 +00:00
}
eb_finalize_book(&eb_book);
eb_finalize_hookset(&eb_hookset);
eb_finalize_library();
2016-12-01 05:16:59 +00:00
book_undupe(book);
2016-12-05 02:36:06 +00:00
return 1;
2016-11-22 06:31:01 +00:00
}