1
This commit is contained in:
Alex Yatskov 2016-11-30 09:26:50 -08:00
parent 0a5ec374c6
commit d24450448e
3 changed files with 23 additions and 23 deletions

26
book.c
View File

@ -91,15 +91,15 @@ static char* book_read(EB_Book* book, EB_Hookset* hookset, const EB_Position* po
return result; return result;
} }
static Book_Content book_read_content(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, Book_Mode mode, const Font_Table* table) { static Book_Block book_read_content(EB_Book* book, EB_Hookset* hookset, const EB_Position* position, Book_Mode mode, const Font_Table* table) {
Book_Content content = {}; Book_Block block = {};
content.text = book_read(book, hookset, position, mode, table); block.text = book_read(book, hookset, position, mode, table);
content.page = position->page; block.page = position->page;
content.offset = position->offset; block.offset = position->offset;
return content; return block;
} }
static void entry_encode(Book_Entry* entry, json_t* entry_json) { static void entry_encode(json_t* entry_json, Book_Entry* entry) {
json_object_set_new(entry_json, "heading", json_string(entry->heading.text)); json_object_set_new(entry_json, "heading", json_string(entry->heading.text));
/* json_object_set_new(entry_json, "headingPage", json_integer(entry->heading.page)); */ /* json_object_set_new(entry_json, "headingPage", json_integer(entry->heading.page)); */
/* json_object_set_new(entry_json, "headingOffset", json_integer(entry->heading.offset)); */ /* json_object_set_new(entry_json, "headingOffset", json_integer(entry->heading.offset)); */
@ -109,7 +109,7 @@ static void entry_encode(Book_Entry* entry, json_t* entry_json) {
/* json_object_set_new(entry_json, "textOffset", json_integer(entry->text.offset)); */ /* json_object_set_new(entry_json, "textOffset", json_integer(entry->text.offset)); */
} }
static void subbok_encode(Book_Subbook* subbook, json_t* subbook_json) { static void subbook_encode(json_t* subbook_json, const Book_Subbook* subbook) {
if (subbook->title != NULL) { if (subbook->title != NULL) {
json_object_set_new(subbook_json, "title", json_string(subbook->title)); json_object_set_new(subbook_json, "title", json_string(subbook->title));
} }
@ -123,21 +123,21 @@ static void subbok_encode(Book_Subbook* subbook, json_t* subbook_json) {
json_t* entry_json_array = json_array(); json_t* entry_json_array = json_array();
for (int i = 0; i < subbook->entry_count; ++i) { for (int i = 0; i < subbook->entry_count; ++i) {
json_t* entry_json = json_object(); json_t* entry_json = json_object();
entry_encode(subbook->entries + i, entry_json); entry_encode(entry_json, subbook->entries + i);
json_array_append_new(entry_json_array, entry_json); json_array_append_new(entry_json_array, entry_json);
} }
json_object_set_new(subbook_json, "entries", entry_json_array); json_object_set_new(subbook_json, "entries", entry_json_array);
} }
static void book_encode(Book* book, json_t* book_json) { static void book_encode(json_t* book_json, const Book* book) {
json_object_set_new(book_json, "charCode", json_string(book->char_code)); json_object_set_new(book_json, "charCode", json_string(book->char_code));
json_object_set_new(book_json, "discCode", json_string(book->disc_code)); json_object_set_new(book_json, "discCode", json_string(book->disc_code));
json_t* subbook_json_array = json_array(); json_t* subbook_json_array = json_array();
for (int i = 0; i < book->subbook_count; ++i) { for (int i = 0; i < book->subbook_count; ++i) {
json_t* subbook_json = json_object(); json_t* subbook_json = json_object();
subbok_encode(book->subbooks + i, subbook_json); subbook_encode(subbook_json, book->subbooks + i);
json_array_append_new(subbook_json_array, subbook_json); json_array_append_new(subbook_json_array, subbook_json);
} }
@ -228,9 +228,9 @@ void book_free(Book* book) {
memset(book, 0, sizeof(Book)); memset(book, 0, sizeof(Book));
} }
bool book_dump(Book* book, bool pretty_print, FILE* fp) { bool book_dump(FILE* fp, const Book* book, bool pretty_print) {
json_t* book_json = json_object(); json_t* book_json = json_object();
book_encode(book, book_json); book_encode(book_json, book);
char* output = json_dumps(book_json, pretty_print ? JSON_INDENT(4) : JSON_COMPACT); char* output = json_dumps(book_json, pretty_print ? JSON_INDENT(4) : JSON_COMPACT);
if (output != NULL) { if (output != NULL) {

10
book.h
View File

@ -32,16 +32,16 @@ typedef struct {
char* text; char* text;
int page; int page;
int offset; int offset;
} Book_Content; } Book_Block;
typedef struct { typedef struct {
Book_Content heading; Book_Block heading;
Book_Content text; Book_Block text;
} Book_Entry; } Book_Entry;
typedef struct { typedef struct {
char* title; char* title;
Book_Content copyright; Book_Block copyright;
Book_Entry* entries; Book_Entry* entries;
int entry_count; int entry_count;
int entry_alloc; int entry_alloc;
@ -61,6 +61,6 @@ typedef struct {
void book_init(Book* book); void book_init(Book* book);
void book_free(Book* book); void book_free(Book* book);
bool book_export(Book* book, const Font_Context* context, const char path[], bool markup); bool book_export(Book* book, const Font_Context* context, const char path[], bool markup);
bool book_dump(Book* book, bool pretty_print, FILE* fp); bool book_dump(FILE* fp, const Book* book, bool pretty_print);
#endif /* BOOK_H */ #endif /* BOOK_H */

2
main.c
View File

@ -109,7 +109,7 @@ int main(int argc, char *argv[]) {
const bool success = const bool success =
book_export(&book, &context, options.dict_path, options.markup) && book_export(&book, &context, options.dict_path, options.markup) &&
book_dump(&book, options.pretty_print, stdout); book_dump(stdout, &book, options.pretty_print);
book_free(&book); book_free(&book);