1
This commit is contained in:
Alex Yatskov 2016-11-01 20:36:32 -07:00
parent d35f25b366
commit a842a7265e

82
main.c
View File

@ -33,6 +33,11 @@
#define MAX_TEXT 1024
#define MAX_HEADING 512
typedef enum {
READ_MODE_TEXT,
READ_MODE_HEADING,
} ReadMode;
static void dump_hits(EB_Book* book) {
EB_Hit hits[MAX_ENTRY_HITS];
int hit_count = 0;
@ -84,45 +89,47 @@ static void dump_book(EB_Book* book) {
}
}
static char* read_book_text(EB_Book* book, const EB_Position* position) {
static char* read_book_data(EB_Book* book, const EB_Position* position, ReadMode mode) {
if (eb_seek_text(book, position) != EB_SUCCESS) {
return NULL;
}
char text[MAX_TEXT + 1];
ssize_t text_length = 0;
char data[1024];
ssize_t data_length = 0;
if (eb_read_text(book, NULL, NULL, NULL, MAX_TEXT, text, &text_length) != EB_SUCCESS) {
switch (mode) {
case READ_MODE_TEXT:
if (eb_read_text(book, NULL, NULL, NULL, 1023, data, &data_length) != EB_SUCCESS) {
return NULL;
}
break;
case READ_MODE_HEADING:
if (eb_read_heading(book, NULL, NULL, NULL, 1023, data, &data_length) != EB_SUCCESS) {
return NULL;
}
break;
default:
return NULL;
}
return eucjp_to_utf8(text);
}
static char* read_book_heading(EB_Book* book, const EB_Position* position) {
if (eb_seek_text(book, position) != EB_SUCCESS) {
return NULL;
}
char heading[MAX_TEXT + 1];
ssize_t heading_length = 0;
if (eb_read_heading(book, NULL, NULL, NULL, MAX_TEXT, heading, &heading_length) != EB_SUCCESS) {
return NULL;
}
return eucjp_to_utf8(heading);
return eucjp_to_utf8(data);
}
static void export_subbook(EB_Book* book, Subbook* subbook_data) {
char title[EB_MAX_TITLE_LENGTH + 1];
if (eb_subbook_title(book, title) == EB_SUCCESS) {
subbook_data->title = eucjp_to_utf8(title);
}
if (eb_have_copyright(book)) {
EB_Position position;
if (eb_copyright(book, &position) == EB_SUCCESS) {
subbook_data->copyright = read_book_text(book, &position);
subbook_data->copyright = read_book_data(book, &position, READ_MODE_TEXT);
}
}
puts(subbook_data->copyright);
}
}
}
static void export_book(const char path[], Book* book_data) {
do {
@ -181,23 +188,18 @@ static void export_book(const char path[], Book* book_data) {
break;
}
/* int sub_count = 0; */
/* if (eb_subbook_list(&book, sub_codes, &sub_count) != EB_SUCCESS) { */
/* fprintf(stderr, "error: failed to get sub-book list\n"); */
/* break; */
/* } */
/* for (int i = 0; i < sub_count; ++i) { */
/* if (eb_set_subbook(&book, sub_codes[i]) == EB_SUCCESS) { */
/* dump_book(&book); */
/* } */
/* else { */
/* fprintf(stderr, "error: failed to set sub-book\n"); */
/* } */
/* } */
if (book_data->subbook_count > 0) {
book_data->subbooks = calloc(book_data->subbook_count, sizeof(Subbook));
for (int i = 0; i < book_data->subbook_count; ++i) {
Subbook* subbook_data = book_data->subbooks + i;
if (eb_set_subbook(&book, sub_codes[i]) == EB_SUCCESS) {
export_subbook(&book, subbook_data);
}
else {
strcpy(subbook_data->error, "failed to set subbook");
}
}
}
eb_finalize_book(&book);
eb_finalize_library();