1
zero-epwing/main.c

221 lines
6.2 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
#define _BSD_SOURCE
2016-10-29 23:58:09 +00:00
#include <stdio.h>
2016-11-01 03:40:52 +00:00
#include <stdbool.h>
#include <string.h>
2016-11-01 02:32:40 +00:00
#include "convert.h"
#include "util.h"
2016-10-30 00:38:50 +00:00
2016-10-31 02:13:15 +00:00
#include "eb/eb/eb.h"
#include "eb/eb/error.h"
#include "eb/eb/text.h"
2016-10-30 01:58:26 +00:00
2016-10-31 15:58:24 +00:00
#define MAX_ENTRY_HITS 128
2016-11-01 05:24:20 +00:00
#define MAX_TEXT 1024
#define MAX_HEADING 512
2016-10-31 15:58:24 +00:00
2016-11-02 03:36:32 +00:00
typedef enum {
READ_MODE_TEXT,
READ_MODE_HEADING,
} ReadMode;
2016-10-31 02:13:15 +00:00
static void dump_hits(EB_Book* book) {
EB_Hit hits[MAX_ENTRY_HITS];
int hit_count = 0;
2016-10-30 03:53:10 +00:00
2016-10-31 02:13:15 +00:00
do {
if (eb_hit_list(book, MAX_ENTRY_HITS, hits, &hit_count) != EB_SUCCESS) {
fprintf(stderr, "error: could not get hit list\n");
break;
2016-10-30 03:53:10 +00:00
}
2016-10-31 02:13:15 +00:00
for (int i = 0; i < hit_count; ++i) {
2016-11-01 05:24:20 +00:00
char text[MAX_TEXT];
2016-10-31 15:58:24 +00:00
ssize_t text_length = 0;
2016-10-30 03:53:10 +00:00
2016-10-31 02:13:15 +00:00
eb_seek_text(book, &hits[i].text);
2016-11-01 05:24:20 +00:00
eb_read_text(book, NULL, NULL, NULL, MAX_TEXT, text, &text_length);
2016-10-30 03:53:10 +00:00
2016-10-31 16:04:15 +00:00
const char* text_utf8 = eucjp_to_utf8(text);
2016-11-01 02:32:40 +00:00
if (text_utf8 != NULL) {
puts(text_utf8);
}
2016-10-31 16:14:27 +00:00
/* (void) text_utf8; */
2016-10-31 02:13:15 +00:00
}
2016-10-30 03:53:10 +00:00
}
2016-10-31 02:13:15 +00:00
while (hit_count > 0);
2016-10-30 01:58:26 +00:00
}
2016-10-31 02:13:15 +00:00
static void dump_book(EB_Book* book) {
if (eb_search_all_alphabet(book) == EB_SUCCESS) {
dump_hits(book);
2016-10-30 03:53:10 +00:00
}
2016-10-31 02:13:15 +00:00
else {
printf("notice: skiping alphabet search\n");
2016-10-30 03:53:10 +00:00
}
2016-10-31 02:13:15 +00:00
if (eb_search_all_kana(book) == EB_SUCCESS) {
dump_hits(book);
}
else {
printf("notice: skiping kana search\n");
2016-10-30 03:53:10 +00:00
}
2016-10-31 02:13:15 +00:00
if (eb_search_all_asis(book) == EB_SUCCESS) {
dump_hits(book);
}
else {
printf("notice: skiping asis search\n");
}
2016-10-30 03:53:10 +00:00
}
2016-11-02 03:36:32 +00:00
static char* read_book_data(EB_Book* book, const EB_Position* position, ReadMode mode) {
2016-11-01 05:24:20 +00:00
if (eb_seek_text(book, position) != EB_SUCCESS) {
return NULL;
}
2016-11-02 03:36:32 +00:00
char data[1024];
ssize_t data_length = 0;
2016-11-01 05:24:20 +00:00
2016-11-02 03:36:32 +00:00
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;
2016-11-01 05:24:20 +00:00
}
2016-11-02 03:36:32 +00:00
return eucjp_to_utf8(data);
2016-11-01 05:24:20 +00:00
}
2016-11-02 03:36:32 +00:00
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);
2016-11-01 05:24:20 +00:00
}
if (eb_have_copyright(book)) {
EB_Position position;
if (eb_copyright(book, &position) == EB_SUCCESS) {
2016-11-02 03:36:32 +00:00
subbook_data->copyright = read_book_data(book, &position, READ_MODE_TEXT);
2016-11-01 05:24:20 +00:00
}
}
2016-11-02 03:36:32 +00:00
puts(subbook_data->copyright);
2016-11-01 05:24:20 +00:00
}
static void export_book(const char path[], Book* book_data) {
2016-10-31 02:13:15 +00:00
do {
2016-11-01 03:40:52 +00:00
if (eb_initialize_library() != EB_SUCCESS) {
2016-11-01 05:24:20 +00:00
strcpy(book_data->error, "failed to initialize library");
2016-11-01 03:40:52 +00:00
break;
}
EB_Book book;
eb_initialize_book(&book);
2016-10-31 02:13:15 +00:00
if (eb_bind(&book, path) != EB_SUCCESS) {
2016-11-01 05:24:20 +00:00
strcpy(book_data->error, "failed to bind book to path");
eb_finalize_book(&book);
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;
if (eb_character_code(&book, &character_code) == EB_SUCCESS) {
switch (character_code) {
case EB_CHARCODE_ISO8859_1:
2016-11-02 03:36:32 +00:00
strcpy(book_data->character_code, "iso8859-1");
2016-11-01 03:40:52 +00:00
break;
case EB_CHARCODE_JISX0208:
2016-11-02 03:36:32 +00:00
strcpy(book_data->character_code, "jisx0208");
2016-11-01 03:40:52 +00:00
break;
case EB_CHARCODE_JISX0208_GB2312:
2016-11-02 03:36:32 +00:00
strcpy(book_data->character_code, "jisx0208/gb2312");
2016-11-01 03:40:52 +00:00
break;
default:
2016-11-01 05:24:20 +00:00
strcpy(book_data->character_code, "invalid");
2016-11-01 03:40:52 +00:00
break;
}
}
EB_Disc_Code disc_code;
if (eb_disc_type(&book, &disc_code) == EB_SUCCESS) {
switch (disc_code) {
case EB_DISC_EB:
2016-11-01 05:24:20 +00:00
strcpy(book_data->disc_code, "eb");
2016-11-01 03:40:52 +00:00
break;
case EB_DISC_EPWING:
2016-11-01 05:24:20 +00:00
strcpy(book_data->disc_code, "epwing");
2016-11-01 03:40:52 +00:00
break;
default:
2016-11-01 05:24:20 +00:00
strcpy(book_data->disc_code, "invalid");
2016-11-01 03:40:52 +00:00
break;
}
}
2016-10-31 02:13:15 +00:00
EB_Subbook_Code sub_codes[EB_MAX_SUBBOOKS];
2016-11-01 05:24:20 +00:00
if (eb_subbook_list(&book, sub_codes, &book_data->subbook_count) != EB_SUCCESS) {
eb_finalize_book(&book);
eb_finalize_library();
2016-10-31 02:13:15 +00:00
break;
}
2016-11-02 03:36:32 +00:00
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");
}
}
}
2016-11-01 03:40:52 +00:00
eb_finalize_book(&book);
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
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "usage: %s path\n", argv[0]);
2016-10-30 01:58:26 +00:00
return 2;
2016-10-30 01:27:05 +00:00
}
2016-11-01 05:24:20 +00:00
Book book_data = {};
export_book(argv[1], &book_data);
return 1;
2016-10-29 23:58:09 +00:00
}