1
This commit is contained in:
Alex Yatskov 2016-11-20 18:23:50 -08:00
parent 3aeee4b0ee
commit 7d049e1f0a
6 changed files with 102 additions and 97 deletions

View File

@ -2,5 +2,5 @@ cmake_minimum_required(VERSION 3.5)
project(zero-epwing)
include_directories(eb)
link_directories(eb/eb/.libs jansson/lib)
add_executable(zero-epwing main.c util.c gaiji.c convert.c hooks.c)
add_executable(zero-epwing main.c book.c gaiji.c convert.c hooks.c)
target_link_libraries(zero-epwing libeb.a libz.a libjansson.a)

View File

@ -16,12 +16,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include "util.h"
#include "book.h"
#include "convert.h"
#include "util.h"
#include "eb/eb/eb.h"
#include "eb/eb/error.h"
@ -33,12 +32,12 @@
* Local functions
*/
static void encode_entry(Entry* entry, json_t* entry_json) {
static void encode_entry(Book_Entry* entry, json_t* entry_json) {
json_object_set_new(entry_json, "heading", json_string(entry->heading));
json_object_set_new(entry_json, "text", json_string(entry->text));
}
static void encode_subbook(Subbook* subbook, json_t* subbook_json) {
static void encode_subbook(Book_Subbook* subbook, json_t* subbook_json) {
if (subbook->title != NULL) {
json_object_set_new(subbook_json, "title", json_string(subbook->title));
}
@ -79,7 +78,7 @@ static void encode_book(Book* book, json_t* book_json) {
* Exported functions
*/
char* read_book_data(EB_Book* book, EB_Hookset* hookset, Gaiji_Context* context, const EB_Position* position, Read_Mode mode) {
char* book_read(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, Book_Mode mode, Gaiji_Context* context) {
if (eb_seek_text(book, position) != EB_SUCCESS) {
return NULL;
}
@ -89,7 +88,7 @@ char* read_book_data(EB_Book* book, EB_Hookset* hookset, Gaiji_Context* context,
EB_Error_Code error;
switch (mode) {
case READ_MODE_TEXT:
case BOOK_MODE_TEXT:
error = eb_read_text(
book,
NULL,
@ -100,7 +99,7 @@ char* read_book_data(EB_Book* book, EB_Hookset* hookset, Gaiji_Context* context,
&data_length
);
break;
case READ_MODE_HEADING:
case BOOK_MODE_HEADING:
error = eb_read_heading(
book,
NULL,
@ -128,14 +127,14 @@ char* read_book_data(EB_Book* book, EB_Hookset* hookset, Gaiji_Context* context,
return result;
}
void free_book(Book* book) {
void book_free(Book* book) {
for (int i = 0; i < book->subbook_count; ++i) {
Subbook* subbook = book->subbooks + i;
Book_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;
Book_Entry* entry = subbook->entries + j;
free(entry->heading);
free(entry->text);
}
@ -144,7 +143,7 @@ void free_book(Book* book) {
}
}
void dump_book(Book* book, bool pretty_print, FILE* fp) {
void book_dump(Book* book, bool pretty_print, FILE* fp) {
json_t* book_json = json_object();
encode_book(book, book_json);

68
book.h Normal file
View File

@ -0,0 +1,68 @@
/*
* 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/>.
*/
#ifndef BOOK_H
#define BOOK_H
#include <stdbool.h>
#include <stdio.h>
#include "eb/eb/eb.h"
#include "gaiji.h"
/*
* Types
*/
typedef struct {
char* heading;
char* text;
} Book_Entry;
typedef struct {
char* title;
char* copyright;
Book_Entry* entries;
int entry_count;
int entry_capacity;
} Book_Subbook;
typedef struct {
char character_code[32];
char disc_code[32];
Book_Subbook* subbooks;
int subbook_count;
} Book;
typedef enum {
BOOK_MODE_TEXT,
BOOK_MODE_HEADING,
} Book_Mode;
/*
* Functions
*/
char* book_read(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, Book_Mode mode, Gaiji_Context* context);
void book_free(Book* book);
void book_dump(Book* book, bool pretty_print, FILE* fp);
#endif /* BOOK_H */

View File

@ -16,7 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <iconv.h>
#include <string.h>
#include <errno.h>

47
main.c
View File

@ -17,10 +17,13 @@
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "convert.h"
#include "util.h"
#include "book.h"
#include "hooks.h"
#include "gaiji.h"
@ -32,10 +35,10 @@
* Local functions
*/
static void export_subbook_entries(Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset, Gaiji_Context* context) {
if (subbook->entry_cap == 0) {
subbook->entry_cap = 16384;
subbook->entries = malloc(subbook->entry_cap * sizeof(Entry));
static void export_subbook_entries(Book_Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset, Gaiji_Context* context) {
if (subbook->entry_capacity == 0) {
subbook->entry_capacity = 16384;
subbook->entries = malloc(subbook->entry_capacity * sizeof(Book_Entry));
}
EB_Hit hits[256];
@ -49,32 +52,20 @@ static void export_subbook_entries(Subbook* subbook, EB_Book* eb_book, EB_Hookse
for (int i = 0; i < hit_count; ++i) {
EB_Hit* hit = hits + i;
if (subbook->entry_count == subbook->entry_cap) {
subbook->entry_cap *= 2;
subbook->entries = realloc(subbook->entries, subbook->entry_cap * sizeof(Entry));
if (subbook->entry_count == subbook->entry_capacity) {
subbook->entry_capacity *= 2;
subbook->entries = realloc(subbook->entries, subbook->entry_capacity * sizeof(Book_Entry));
}
Entry* entry = subbook->entries + subbook->entry_count++;
entry->heading = read_book_data(
eb_book,
eb_hookset,
context,
&hit->heading,
READ_MODE_HEADING
);
entry->text = read_book_data(
eb_book,
eb_hookset,
context,
&hit->text,
READ_MODE_TEXT
);
Book_Entry* entry = subbook->entries + subbook->entry_count++;
entry->heading = book_read(eb_book, eb_hookset, &hit->heading, BOOK_MODE_HEADING, context);
entry->text = book_read(eb_book, eb_hookset, &hit->text, BOOK_MODE_TEXT, context);
}
}
while (hit_count > 0);
}
static void export_subbook(Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset) {
static void export_subbook(Book_Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset) {
Gaiji_Context context = {};
char title[EB_MAX_TITLE_LENGTH + 1];
if (eb_subbook_title(eb_book, title) == EB_SUCCESS) {
@ -85,7 +76,7 @@ static void export_subbook(Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_ho
if (eb_have_copyright(eb_book)) {
EB_Position position;
if (eb_copyright(eb_book, &position) == EB_SUCCESS) {
subbook->copyright = read_book_data(eb_book, eb_hookset, &context, &position, READ_MODE_TEXT);
subbook->copyright = book_read(eb_book, eb_hookset, &position, BOOK_MODE_TEXT, &context);
}
}
@ -161,9 +152,9 @@ static void export_book(Book* book, const char path[]) {
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(Subbook));
book->subbooks = calloc(book->subbook_count, sizeof(Book_Subbook));
for (int i = 0; i < book->subbook_count; ++i) {
Subbook* subbook = book->subbooks + i;
Book_Subbook* subbook = book->subbooks + i;
if ((error = eb_set_subbook(&eb_book, sub_codes[i])) == EB_SUCCESS) {
export_subbook(subbook, &eb_book, &eb_hookset);
}
@ -210,8 +201,8 @@ int main(int argc, char *argv[]) {
Book book = {};
export_book(&book, argv[optind]);
dump_book(&book, pretty_print, stdout);
free_book(&book);
book_dump(&book, pretty_print, stdout);
book_free(&book);
return 0;
}

58
util.h
View File

@ -16,65 +16,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef UTIL_H
#define UTIL_H
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include "eb/eb/eb.h"
#include "gaiji.h"
/*
* Macros
*/
#ifndef UTIL_H
#define UTIL_H
#define ARRSIZE(arr) (sizeof(arr) / sizeof(arr[0]))
/*
* Types
*/
typedef struct {
char* heading;
char* text;
} Text_Block;
typedef struct {
char* heading;
char* text;
} Entry;
typedef struct {
char* title;
char* copyright;
Entry* entries;
int entry_count;
int entry_cap;
} Subbook;
typedef struct {
char character_code[32];
char disc_code[32];
Subbook* subbooks;
int subbook_count;
} Book;
typedef enum {
READ_MODE_TEXT,
READ_MODE_HEADING,
} Read_Mode;
/*
* Functions
*/
char* read_book_data(EB_Book* book, EB_Hookset* hookset, Gaiji_Context* context, const EB_Position* position, Read_Mode mode);
void free_book(Book* book);
void dump_book(Book* book, bool pretty_print, FILE* fp);
#endif /* UTIL_H */