2016-10-31 16:04:15 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2016-11-29 04:42:10 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <argp.h>
|
2016-11-01 02:32:40 +00:00
|
|
|
|
2016-11-29 04:42:10 +00:00
|
|
|
#include "util.h"
|
2016-11-21 02:23:50 +00:00
|
|
|
#include "book.h"
|
2016-11-29 05:16:43 +00:00
|
|
|
#include "font.h"
|
2016-10-30 00:38:50 +00:00
|
|
|
|
2016-11-29 04:42:10 +00:00
|
|
|
/*
|
|
|
|
* Local types
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char dict_path[256];
|
|
|
|
char font_path[256];
|
|
|
|
bool pretty_print;
|
|
|
|
bool markup;
|
|
|
|
} Options;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
static error_t argp_parser(int key, char* arg, struct argp_state* state) {
|
2016-11-29 05:11:24 +00:00
|
|
|
Options* options = (Options*)state->input;
|
2016-11-29 04:42:10 +00:00
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case 'd':
|
|
|
|
if (arg != NULL) {
|
|
|
|
strncpy(options->dict_path, arg, ARRSIZE(options->dict_path));
|
|
|
|
options->dict_path[ARRSIZE(options->dict_path) - 1] = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
if (arg != NULL) {
|
|
|
|
strncpy(options->font_path, arg, ARRSIZE(options->font_path));
|
|
|
|
options->font_path[ARRSIZE(options->font_path) - 1] = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
options->markup = true;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
options->pretty_print = true;
|
|
|
|
break;
|
|
|
|
case ARGP_KEY_END:
|
2016-11-29 05:04:55 +00:00
|
|
|
if (*options->dict_path == 0) {
|
2016-11-29 04:42:10 +00:00
|
|
|
argp_usage(state);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return ARGP_ERR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-19 20:23:06 +00:00
|
|
|
/*
|
|
|
|
* Entry point
|
|
|
|
*/
|
|
|
|
|
2016-10-30 01:27:05 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2016-11-29 04:42:10 +00:00
|
|
|
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 },
|
|
|
|
{ }
|
|
|
|
};
|
2016-11-03 03:37:11 +00:00
|
|
|
|
2016-11-29 04:42:10 +00:00
|
|
|
const struct argp argp = {
|
|
|
|
argp_options,
|
|
|
|
argp_parser,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
};
|
2016-11-03 03:37:11 +00:00
|
|
|
|
2016-11-29 04:42:10 +00:00
|
|
|
Options options = { };
|
|
|
|
argp_parse(&argp, argc, argv, 0, 0, &options);
|
2016-11-03 03:37:11 +00:00
|
|
|
|
2016-11-29 05:16:43 +00:00
|
|
|
Font_Context context;
|
|
|
|
if (!font_context_init(&context, *options.font_path == 0 ? NULL : options.font_path)) {
|
2016-11-29 05:04:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2016-11-22 06:15:47 +00:00
|
|
|
|
2016-11-22 06:41:44 +00:00
|
|
|
Book book;
|
|
|
|
book_init(&book);
|
2016-11-29 05:04:55 +00:00
|
|
|
|
|
|
|
const bool success =
|
|
|
|
book_export(&book, &context, options.dict_path, options.markup) &&
|
|
|
|
book_dump(&book, options.pretty_print, stdout);
|
|
|
|
|
2016-11-21 02:23:50 +00:00
|
|
|
book_free(&book);
|
2016-11-03 03:37:11 +00:00
|
|
|
|
2016-11-29 05:16:43 +00:00
|
|
|
font_context_destroy(&context);
|
2016-11-29 05:04:55 +00:00
|
|
|
return success ? 0 : 1;
|
2016-10-29 23:58:09 +00:00
|
|
|
}
|