1
zero-epwing/main.c

110 lines
2.5 KiB
C
Raw Normal View History

2016-10-29 23:58:09 +00:00
#include <stdio.h>
2016-10-30 02:05:38 +00:00
#include <string.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 02:13:15 +00:00
/* Constants */
2016-10-30 01:58:26 +00:00
2016-10-31 02:13:15 +00:00
const unsigned MAX_ENTRY_HITS = 128;
const unsigned MAX_ENTRY_TEXT = 1024;
2016-10-30 01:58:26 +00:00
2016-10-31 02:13:15 +00:00
/* Local functions */
2016-10-30 02:05:38 +00:00
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) {
char text[MAX_ENTRY_TEXT];
ssize_t text_length;
2016-10-30 03:53:10 +00:00
2016-10-31 02:13:15 +00:00
eb_seek_text(book, &hits[i].text);
eb_read_text(book, NULL, NULL, NULL, MAX_ENTRY_TEXT, text, &text_length);
2016-10-30 03:53:10 +00:00
2016-10-31 02:13:15 +00:00
puts(text);
}
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-10-31 02:13:15 +00:00
static int process(const char path[]) {
2016-10-30 01:58:26 +00:00
if (eb_initialize_library() != EB_SUCCESS) {
2016-10-30 06:41:19 +00:00
fprintf(stderr, "error: failed to initialize library\n");
2016-10-30 01:27:05 +00:00
return 1;
2016-10-30 00:38:50 +00:00
}
2016-10-29 23:58:09 +00:00
2016-10-30 01:58:26 +00:00
EB_Book book;
2016-10-31 02:13:15 +00:00
eb_initialize_book(&book);
2016-10-30 01:27:05 +00:00
2016-10-31 02:13:15 +00:00
do {
if (eb_bind(&book, path) != EB_SUCCESS) {
fprintf(stderr, "error: failed to bind book\n");
break;
}
2016-10-30 03:53:10 +00:00
2016-10-31 02:13:15 +00:00
EB_Subbook_Code sub_codes[EB_MAX_SUBBOOKS];
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");
}
}
}
while (0);
2016-10-30 01:58:26 +00:00
eb_finalize_book(&book);
2016-10-30 00:38:50 +00:00
eb_finalize_library();
2016-10-30 01:27:05 +00:00
return 0;
}
2016-10-31 02:13:15 +00:00
/* Entry point */
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-10-30 03:53:10 +00:00
return process(argv[1]);
2016-10-29 23:58:09 +00:00
}