diff --git a/main.c b/main.c index 1bd4bbe..2c4d8cd 100644 --- a/main.c +++ b/main.c @@ -16,10 +16,7 @@ * along with this program. If not, see . */ -#define _BSD_SOURCE - #include -#include #include #include "convert.h" @@ -27,54 +24,8 @@ #include "eb/eb/eb.h" #include "eb/eb/error.h" -#include "eb/eb/text.h" -#define MAX_HITS 128 -#define MAX_TEXT 1024 - -typedef enum { - READ_MODE_TEXT, - READ_MODE_HEADING, -} ReadMode; - -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 data[MAX_TEXT]; - ssize_t data_length = 0; - EB_Error_Code error; - - switch (mode) { - case READ_MODE_TEXT: - error = eb_read_text( - book, - NULL, - NULL, - NULL, - MAX_TEXT - 1, - data, - &data_length - ); - break; - case READ_MODE_HEADING: - error = eb_read_heading( - book, - NULL, - NULL, - NULL, - MAX_TEXT - 1, - data, - &data_length - ); - break; - default: - return NULL; - } - - return error == EB_SUCCESS ? eucjp_to_utf8(data) : NULL; -} +#define MAX_HITS 256 static void export_subbook_entries(EB_Book* eb_book, Subbook* subbook) { if (subbook->entry_cap == 0) { @@ -211,22 +162,6 @@ static void export_book(const char path[], Book* book) { while(0); } -static void free_book(Book* book) { - for (int i = 0; i < book->subbook_count; ++i) { - Subbook* subbook = book->subbooks + i; - free(subbook->title); - free(subbook->copyright); - - for (int j = 0; j < subbook->entry_count; ++j) { - Entry* entry = subbook->entries + j; - free(entry->heading); - free(entry->text); - } - - free(subbook->entries); - } -} - int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "usage: %s path\n", argv[0]); diff --git a/util.c b/util.c index 5095985..23ca7f6 100644 --- a/util.c +++ b/util.c @@ -17,3 +17,65 @@ */ #include "util.h" +#include "convert.h" + +#include "eb/eb/eb.h" +#include "eb/eb/error.h" +#include "eb/eb/text.h" + +#define MAX_TEXT 1024 + +char* read_book_data(EB_Book* book, const EB_Position* position, ReadMode mode) { + if (eb_seek_text(book, position) != EB_SUCCESS) { + return NULL; + } + + char data[MAX_TEXT]; + ssize_t data_length = 0; + EB_Error_Code error; + + switch (mode) { + case READ_MODE_TEXT: + error = eb_read_text( + book, + NULL, + NULL, + NULL, + MAX_TEXT - 1, + data, + &data_length + ); + break; + case READ_MODE_HEADING: + error = eb_read_heading( + book, + NULL, + NULL, + NULL, + MAX_TEXT - 1, + data, + &data_length + ); + break; + default: + return NULL; + } + + return error == EB_SUCCESS ? eucjp_to_utf8(data) : NULL; +} + +void free_book(Book* book) { + for (int i = 0; i < book->subbook_count; ++i) { + Subbook* subbook = book->subbooks + i; + free(subbook->title); + free(subbook->copyright); + + for (int j = 0; j < subbook->entry_count; ++j) { + Entry* entry = subbook->entries + j; + free(entry->heading); + free(entry->text); + } + + free(subbook->entries); + } +} diff --git a/util.h b/util.h index d2367e0..1265a8e 100644 --- a/util.h +++ b/util.h @@ -64,4 +64,16 @@ typedef struct { char error[MAX_ERROR]; } Book; +/* + Book helpers +*/ + +typedef enum { + READ_MODE_TEXT, + READ_MODE_HEADING, +} ReadMode; + +char* read_book_data(EB_Book* book, const EB_Position* position, ReadMode mode); +void free_book(Book* book); + #endif