From 619ee1bb16b8669fcab8887e623f03df2fcc9582 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 30 Nov 2016 23:19:16 -0800 Subject: [PATCH] Command line cleanup --- book.c | 41 +++++++++++++++++++++++++---------------- book.h | 6 +++--- font.c | 1 - font.h | 8 ++++---- hooks.c | 4 ++-- hooks.h | 4 +--- main.c | 29 ++++++++++++++++------------- util.h | 6 ++++++ 8 files changed, 57 insertions(+), 42 deletions(-) diff --git a/book.c b/book.c index db14b9d..04ec809 100644 --- a/book.c +++ b/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)); diff --git a/book.h b/book.h index 154c68a..fee7fb2 100644 --- a/book.h +++ b/book.h @@ -19,8 +19,8 @@ #ifndef BOOK_H #define BOOK_H -#include #include +#include #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 */ diff --git a/font.c b/font.c index e7c7c12..d3ed02e 100644 --- a/font.c +++ b/font.c @@ -17,7 +17,6 @@ */ #include -#include #include #include diff --git a/font.h b/font.h index 96ee35e..8ebb870 100644 --- a/font.h +++ b/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 { diff --git a/hooks.c b/hooks.c index 5fbbcec..5676466 100644 --- a/hooks.c +++ b/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); } diff --git a/hooks.h b/hooks.h index e4f0b7e..46b7250 100644 --- a/hooks.h +++ b/hooks.h @@ -19,14 +19,12 @@ #ifndef HOOKS_H #define HOOKS_H -#include - #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 */ diff --git a/main.c b/main.c index 0363040..1965133 100644 --- a/main.c +++ b/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); diff --git a/util.h b/util.h index fdd07e2..49a617e 100644 --- a/util.h +++ b/util.h @@ -25,4 +25,10 @@ #define ARRSIZE(arr) (sizeof(arr) / sizeof(arr[0])) +enum { + FLAG_PRETTY_PRINT = 1 << 0, + FLAG_MARKUP = 1 << 1, + FLAG_POSITIONS = 1 << 2, +}; + #endif /* UTIL_H */