Command line cleanup
This commit is contained in:
parent
59325dc750
commit
619ee1bb16
41
book.c
41
book.c
@ -175,49 +175,58 @@ static void book_undupe(Book* book) {
|
||||
* Encoding to JSON
|
||||
*/
|
||||
|
||||
static void entry_encode(json_t* entry_json, const Book_Entry* entry) {
|
||||
/* json_object_set_new(entry_json, "headingPage", json_integer(entry->heading.page)); */
|
||||
/* json_object_set_new(entry_json, "headingOffset", json_integer(entry->heading.offset)); */
|
||||
static void entry_encode(json_t* entry_json, const Book_Entry* entry, int flags) {
|
||||
if (entry->heading.text != NULL) {
|
||||
json_object_set_new(entry_json, "heading", json_string(entry->heading.text));
|
||||
}
|
||||
|
||||
/* json_object_set_new(entry_json, "textPage", json_integer(entry->text.page)); */
|
||||
/* json_object_set_new(entry_json, "textOffset", json_integer(entry->text.offset)); */
|
||||
if (flags & FLAG_POSITIONS) {
|
||||
json_object_set_new(entry_json, "headingPage", json_integer(entry->heading.page));
|
||||
json_object_set_new(entry_json, "headingOffset", json_integer(entry->heading.offset));
|
||||
}
|
||||
|
||||
if (entry->text.text != NULL) {
|
||||
json_object_set_new(entry_json, "text", json_string(entry->text.text));
|
||||
}
|
||||
|
||||
if (flags & FLAG_POSITIONS) {
|
||||
json_object_set_new(entry_json, "textPage", json_integer(entry->text.page));
|
||||
json_object_set_new(entry_json, "textOffset", json_integer(entry->text.offset));
|
||||
}
|
||||
}
|
||||
|
||||
static void subbook_encode(json_t* subbook_json, const Book_Subbook* subbook) {
|
||||
static void subbook_encode(json_t* subbook_json, const Book_Subbook* subbook, int flags) {
|
||||
if (subbook->title != NULL) {
|
||||
json_object_set_new(subbook_json, "title", json_string(subbook->title));
|
||||
}
|
||||
|
||||
/* json_object_set_new(subbook_json, "copyrightPage", json_integer(subbook->copyright.page)); */
|
||||
/* json_object_set_new(subbook_json, "copyrightOffset", json_integer(subbook->copyright.offset)); */
|
||||
if (subbook->copyright.text != NULL) {
|
||||
json_object_set_new(subbook_json, "copyright", json_string(subbook->copyright.text));
|
||||
}
|
||||
|
||||
if (flags & FLAG_POSITIONS) {
|
||||
json_object_set_new(subbook_json, "copyrightPage", json_integer(subbook->copyright.page));
|
||||
json_object_set_new(subbook_json, "copyrightOffset", json_integer(subbook->copyright.offset));
|
||||
}
|
||||
|
||||
json_t* entry_json_array = json_array();
|
||||
for (int i = 0; i < subbook->entry_count; ++i) {
|
||||
json_t* entry_json = json_object();
|
||||
entry_encode(entry_json, subbook->entries + i);
|
||||
entry_encode(entry_json, subbook->entries + i, flags);
|
||||
json_array_append_new(entry_json_array, entry_json);
|
||||
}
|
||||
|
||||
json_object_set_new(subbook_json, "entries", entry_json_array);
|
||||
}
|
||||
|
||||
static void book_encode(json_t* book_json, const Book* book) {
|
||||
static void book_encode(json_t* book_json, const Book* book, int flags) {
|
||||
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_t* subbook_json_array = json_array();
|
||||
for (int i = 0; i < book->subbook_count; ++i) {
|
||||
json_t* subbook_json = json_object();
|
||||
subbook_encode(subbook_json, book->subbooks + i);
|
||||
subbook_encode(subbook_json, book->subbooks + i, flags);
|
||||
json_array_append_new(subbook_json_array, subbook_json);
|
||||
}
|
||||
|
||||
@ -312,11 +321,11 @@ void book_free(Book* book) {
|
||||
memset(book, 0, sizeof(Book));
|
||||
}
|
||||
|
||||
bool book_export(FILE* fp, const Book* book, bool pretty_print) {
|
||||
bool book_export(FILE* fp, const Book* book, int flags) {
|
||||
json_t* book_json = json_object();
|
||||
book_encode(book_json, book);
|
||||
book_encode(book_json, book, flags);
|
||||
|
||||
char* output = json_dumps(book_json, pretty_print ? JSON_INDENT(4) : JSON_COMPACT);
|
||||
char* output = json_dumps(book_json, flags & FLAG_PRETTY_PRINT ? JSON_INDENT(4) : JSON_COMPACT);
|
||||
if (output != NULL) {
|
||||
fputs(output, fp);
|
||||
}
|
||||
@ -327,7 +336,7 @@ bool book_export(FILE* fp, const Book* book, bool pretty_print) {
|
||||
}
|
||||
|
||||
|
||||
bool book_import(Book* book, const Font_Context* context, const char path[], bool markup) {
|
||||
bool book_import(Book* book, const Font_Context* context, const char path[], int flags) {
|
||||
EB_Error_Code error;
|
||||
if ((error = eb_initialize_library()) != EB_SUCCESS) {
|
||||
fprintf(stderr, "Failed to initialize library: %s\n", eb_error_message(error));
|
||||
@ -339,7 +348,7 @@ bool book_import(Book* book, const Font_Context* context, const char path[], boo
|
||||
|
||||
EB_Hookset eb_hookset;
|
||||
eb_initialize_hookset(&eb_hookset);
|
||||
hooks_install(&eb_hookset, markup);
|
||||
hooks_install(&eb_hookset, flags);
|
||||
|
||||
if ((error = eb_bind(&eb_book, path)) != EB_SUCCESS) {
|
||||
fprintf(stderr, "Failed to bind book: %s\n", eb_error_message(error));
|
||||
|
6
book.h
6
book.h
@ -19,8 +19,8 @@
|
||||
#ifndef BOOK_H
|
||||
#define BOOK_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "font.h"
|
||||
|
||||
@ -60,7 +60,7 @@ typedef struct {
|
||||
|
||||
void book_init(Book* book);
|
||||
void book_free(Book* book);
|
||||
bool book_import(Book* book, const Font_Context* context, const char path[], bool markup);
|
||||
bool book_export(FILE* fp, const Book* book, bool pretty_print);
|
||||
bool book_import(Book* book, const Font_Context* context, const char path[], int flags);
|
||||
bool book_export(FILE* fp, const Book* book, int flags);
|
||||
|
||||
#endif /* BOOK_H */
|
||||
|
1
font.c
1
font.c
@ -17,7 +17,6 @@
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
|
8
font.h
8
font.h
@ -38,16 +38,16 @@ typedef struct {
|
||||
} Font_Entry;
|
||||
|
||||
typedef struct {
|
||||
char name[256];
|
||||
char name[256];
|
||||
const Font_Entry* table_wide;
|
||||
int table_wide_size;
|
||||
int table_wide_size;
|
||||
const Font_Entry* table_narrow;
|
||||
int table_narrow_size;
|
||||
int table_narrow_size;
|
||||
} Font_Table;
|
||||
|
||||
typedef struct {
|
||||
Font_Table* tables;
|
||||
int table_count;
|
||||
int table_count;
|
||||
} Font_Context;
|
||||
|
||||
typedef enum {
|
||||
|
4
hooks.c
4
hooks.c
@ -202,12 +202,12 @@ static const EB_Hook s_hooks_basic[] = {
|
||||
* Exported functions
|
||||
*/
|
||||
|
||||
void hooks_install(EB_Hookset* hookset, bool markup) {
|
||||
void hooks_install(EB_Hookset* hookset, int flags) {
|
||||
for (unsigned i = 0; i < ARRSIZE(s_hooks_basic); ++i) {
|
||||
eb_set_hook(hookset, s_hooks_basic + i);
|
||||
}
|
||||
|
||||
if (markup) {
|
||||
if (flags & FLAG_MARKUP) {
|
||||
for (unsigned i = 0; i < ARRSIZE(s_hooks_markup); ++i) {
|
||||
eb_set_hook(hookset, s_hooks_markup + i);
|
||||
}
|
||||
|
4
hooks.h
4
hooks.h
@ -19,14 +19,12 @@
|
||||
#ifndef HOOKS_H
|
||||
#define HOOKS_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "eb/eb/eb.h"
|
||||
|
||||
/*
|
||||
* Exported functions
|
||||
*/
|
||||
|
||||
void hooks_install(EB_Hookset* hookset, bool markup);
|
||||
void hooks_install(EB_Hookset* hookset, int flags);
|
||||
|
||||
#endif /* HOOKS_H */
|
||||
|
29
main.c
29
main.c
@ -31,8 +31,7 @@
|
||||
typedef struct {
|
||||
char dict_path[256];
|
||||
char font_path[256];
|
||||
bool pretty_print;
|
||||
bool markup;
|
||||
int flags;
|
||||
} Options;
|
||||
|
||||
/*
|
||||
@ -55,11 +54,14 @@ static error_t argp_parser(int key, char* arg, struct argp_state* state) {
|
||||
options->font_path[ARRSIZE(options->font_path) - 1] = 0;
|
||||
}
|
||||
break;
|
||||
case 'm':
|
||||
options->markup = true;
|
||||
break;
|
||||
case 'p':
|
||||
options->pretty_print = true;
|
||||
options->flags |= FLAG_PRETTY_PRINT;
|
||||
break;
|
||||
case 'm':
|
||||
options->flags |= FLAG_MARKUP;
|
||||
break;
|
||||
case 's':
|
||||
options->flags |= FLAG_POSITIONS;
|
||||
break;
|
||||
case ARGP_KEY_END:
|
||||
if (*options->dict_path == 0) {
|
||||
@ -79,10 +81,11 @@ static error_t argp_parser(int key, char* arg, struct argp_state* state) {
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
const struct argp_option argp_options[] = {
|
||||
{ "dict", 'd', "PATH", 0, "dictionary resource to process", 0 },
|
||||
{ "font", 'f', "PATH", OPTION_ARG_OPTIONAL, "definition file for font translation", 0 },
|
||||
{ "pretty-print", 'p', NULL, OPTION_ARG_OPTIONAL, "pretty print JSON output", 0 },
|
||||
{ "markup", 'm', NULL, OPTION_ARG_OPTIONAL, "markup JSON output with formatting tags", 0 },
|
||||
{ "dict", 'd', "PATH", 0, "dictionary resource to process", 0 },
|
||||
{ "font", 'f', "PATH", OPTION_ARG_OPTIONAL, "definition file for font translation", 0 },
|
||||
{ "pretty-print", 'p', NULL, OPTION_ARG_OPTIONAL, "output pretty-printed JSON", 0 },
|
||||
{ "markup", 'm', NULL, OPTION_ARG_OPTIONAL, "output formatting tags", 0 },
|
||||
{ "positions", 's', NULL, OPTION_ARG_OPTIONAL, "output positional data", 0 },
|
||||
{ }
|
||||
};
|
||||
|
||||
@ -96,7 +99,7 @@ int main(int argc, char *argv[]) {
|
||||
NULL,
|
||||
};
|
||||
|
||||
Options options = { };
|
||||
Options options = {};
|
||||
argp_parse(&argp, argc, argv, 0, 0, &options);
|
||||
|
||||
Font_Context context;
|
||||
@ -108,8 +111,8 @@ int main(int argc, char *argv[]) {
|
||||
book_init(&book);
|
||||
|
||||
const bool success =
|
||||
book_import(&book, &context, options.dict_path, options.markup) &&
|
||||
book_export(stdout, &book, options.pretty_print);
|
||||
book_import(&book, &context, options.dict_path, options.flags) &&
|
||||
book_export(stdout, &book, options.flags);
|
||||
|
||||
book_free(&book);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user