1

Command line cleanup

This commit is contained in:
Alex Yatskov 2016-11-30 23:19:16 -08:00
parent 59325dc750
commit 619ee1bb16
8 changed files with 57 additions and 42 deletions

41
book.c
View File

@ -175,49 +175,58 @@ static void book_undupe(Book* book) {
* Encoding to JSON * Encoding to JSON
*/ */
static void entry_encode(json_t* entry_json, const Book_Entry* entry) { static void entry_encode(json_t* entry_json, const Book_Entry* entry, int flags) {
/* 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->heading.text != NULL) { if (entry->heading.text != NULL) {
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, "textPage", json_integer(entry->text.page)); */ if (flags & FLAG_POSITIONS) {
/* json_object_set_new(entry_json, "textOffset", json_integer(entry->text.offset)); */ 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) { if (entry->text.text != NULL) {
json_object_set_new(entry_json, "text", json_string(entry->text.text)); 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) { 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));
} }
/* 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) { if (subbook->copyright.text != NULL) {
json_object_set_new(subbook_json, "copyright", json_string(subbook->copyright.text)); 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(); 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(entry_json, subbook->entries + i); entry_encode(entry_json, subbook->entries + i, flags);
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(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, "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();
subbook_encode(subbook_json, book->subbooks + i); subbook_encode(subbook_json, book->subbooks + i, flags);
json_array_append_new(subbook_json_array, subbook_json); json_array_append_new(subbook_json_array, subbook_json);
} }
@ -312,11 +321,11 @@ void book_free(Book* book) {
memset(book, 0, sizeof(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(); 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) { if (output != NULL) {
fputs(output, fp); 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; EB_Error_Code error;
if ((error = eb_initialize_library()) != EB_SUCCESS) { if ((error = eb_initialize_library()) != EB_SUCCESS) {
fprintf(stderr, "Failed to initialize library: %s\n", eb_error_message(error)); 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_Hookset eb_hookset;
eb_initialize_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) { if ((error = eb_bind(&eb_book, path)) != EB_SUCCESS) {
fprintf(stderr, "Failed to bind book: %s\n", eb_error_message(error)); fprintf(stderr, "Failed to bind book: %s\n", eb_error_message(error));

6
book.h
View File

@ -19,8 +19,8 @@
#ifndef BOOK_H #ifndef BOOK_H
#define BOOK_H #define BOOK_H
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h>
#include "font.h" #include "font.h"
@ -60,7 +60,7 @@ typedef struct {
void book_init(Book* book); void book_init(Book* book);
void book_free(Book* book); void book_free(Book* book);
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);
bool book_export(FILE* fp, const Book* book, bool pretty_print); bool book_export(FILE* fp, const Book* book, int flags);
#endif /* BOOK_H */ #endif /* BOOK_H */

1
font.c
View File

@ -17,7 +17,6 @@
*/ */
#include <string.h> #include <string.h>
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>

8
font.h
View File

@ -38,16 +38,16 @@ typedef struct {
} Font_Entry; } Font_Entry;
typedef struct { typedef struct {
char name[256]; char name[256];
const Font_Entry* table_wide; const Font_Entry* table_wide;
int table_wide_size; int table_wide_size;
const Font_Entry* table_narrow; const Font_Entry* table_narrow;
int table_narrow_size; int table_narrow_size;
} Font_Table; } Font_Table;
typedef struct { typedef struct {
Font_Table* tables; Font_Table* tables;
int table_count; int table_count;
} Font_Context; } Font_Context;
typedef enum { typedef enum {

View File

@ -202,12 +202,12 @@ static const EB_Hook s_hooks_basic[] = {
* Exported functions * 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) { for (unsigned i = 0; i < ARRSIZE(s_hooks_basic); ++i) {
eb_set_hook(hookset, 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) { for (unsigned i = 0; i < ARRSIZE(s_hooks_markup); ++i) {
eb_set_hook(hookset, s_hooks_markup + i); eb_set_hook(hookset, s_hooks_markup + i);
} }

View File

@ -19,14 +19,12 @@
#ifndef HOOKS_H #ifndef HOOKS_H
#define HOOKS_H #define HOOKS_H
#include <stdbool.h>
#include "eb/eb/eb.h" #include "eb/eb/eb.h"
/* /*
* Exported functions * Exported functions
*/ */
void hooks_install(EB_Hookset* hookset, bool markup); void hooks_install(EB_Hookset* hookset, int flags);
#endif /* HOOKS_H */ #endif /* HOOKS_H */

29
main.c
View File

@ -31,8 +31,7 @@
typedef struct { typedef struct {
char dict_path[256]; char dict_path[256];
char font_path[256]; char font_path[256];
bool pretty_print; int flags;
bool markup;
} Options; } 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; options->font_path[ARRSIZE(options->font_path) - 1] = 0;
} }
break; break;
case 'm':
options->markup = true;
break;
case 'p': 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; break;
case ARGP_KEY_END: case ARGP_KEY_END:
if (*options->dict_path == 0) { 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[]) { int main(int argc, char *argv[]) {
const struct argp_option argp_options[] = { const struct argp_option argp_options[] = {
{ "dict", 'd', "PATH", 0, "dictionary resource to process", 0 }, { "dict", 'd', "PATH", 0, "dictionary resource to process", 0 },
{ "font", 'f', "PATH", OPTION_ARG_OPTIONAL, "definition file for font translation", 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 }, { "pretty-print", 'p', NULL, OPTION_ARG_OPTIONAL, "output pretty-printed JSON", 0 },
{ "markup", 'm', NULL, OPTION_ARG_OPTIONAL, "markup JSON output with formatting tags", 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, NULL,
}; };
Options options = { }; Options options = {};
argp_parse(&argp, argc, argv, 0, 0, &options); argp_parse(&argp, argc, argv, 0, 0, &options);
Font_Context context; Font_Context context;
@ -108,8 +111,8 @@ int main(int argc, char *argv[]) {
book_init(&book); book_init(&book);
const bool success = const bool success =
book_import(&book, &context, options.dict_path, options.markup) && book_import(&book, &context, options.dict_path, options.flags) &&
book_export(stdout, &book, options.pretty_print); book_export(stdout, &book, options.flags);
book_free(&book); book_free(&book);

6
util.h
View File

@ -25,4 +25,10 @@
#define ARRSIZE(arr) (sizeof(arr) / sizeof(arr[0])) #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 */ #endif /* UTIL_H */