[util] Make main_font_text a struct again

Going to move to G_OPTION_REMAINING.
This commit is contained in:
Behdad Esfahbod 2021-08-11 11:53:32 -06:00
parent 5bd463f130
commit 9d8bbe3e53
6 changed files with 41 additions and 38 deletions

View File

@ -27,9 +27,9 @@
typedef int (*main_func_t) (int argc, char **argv);
template <bool report_status=false>
template <typename main_t, bool report_status=false>
int
batch_main (main_func_t main_func, int argc, char **argv)
batch_main (int argc, char **argv)
{
if (argc == 2 && !strcmp (argv[1], "--batch"))
{
@ -53,7 +53,7 @@ batch_main (main_func_t main_func, int argc, char **argv)
args[argc++] = p = e;
}
int result = main_func (argc, args);
int result = main_t () (argc, args);
if (report_status)
fprintf (stdout, result == 0 ? "success\n" : "failure\n");
@ -64,7 +64,7 @@ batch_main (main_func_t main_func, int argc, char **argv)
return ret;
}
return main_func (argc, argv);
return main_t () (argc, argv);
}
#endif

View File

@ -114,5 +114,5 @@ struct shape_closure_consumer_t
int
main (int argc, char **argv)
{
return main_font_text<shape_closure_consumer_t, font_options_t, text_options_t> (argc, argv);
return main_font_text<shape_closure_consumer_t, font_options_t, text_options_t> () (argc, argv);
}

View File

@ -162,6 +162,6 @@ struct output_buffer_t : output_options_t
int
main (int argc, char **argv)
{
auto main_func = main_font_text<shape_consumer_t<output_buffer_t>, font_options_t, text_options_t>;
return batch_main<> (main_func, argc, argv);
using main_t = main_font_text<shape_consumer_t<output_buffer_t>, font_options_t, text_options_t>;
return batch_main<main_t> (argc, argv);
}

View File

@ -129,6 +129,6 @@ struct subset_consumer_t : subset_options_t, output_options_t
int
main (int argc, char **argv)
{
auto main_func = main_font_text<subset_consumer_t, face_options_t, text_options_t>;
return batch_main<true> (main_func, argc, argv);
using main_t = main_font_text<subset_consumer_t, face_options_t, text_options_t>;
return batch_main<main_t, true> (argc, argv);
}

View File

@ -37,5 +37,5 @@ const unsigned SUBPIXEL_BITS = 6;
int
main (int argc, char **argv)
{
return main_font_text<shape_consumer_t<view_cairo_t>, font_options_t, text_options_t> (argc, argv);
return main_font_text<shape_consumer_t<view_cairo_t>, font_options_t, text_options_t> () (argc, argv);
}

View File

@ -32,40 +32,43 @@
/* main() body for utilities taking font and processing text.*/
template <typename consumer_t, typename font_options_t, typename text_options_t>
int
main_font_text (int argc, char **argv)
struct main_font_text : font_options_t, text_options_t, consumer_t
{
void add_options (struct option_parser_t *parser)
{
font_options_t::add_options (parser);
text_options_t::add_options (parser);
consumer_t::add_options (parser);
}
font_options_t font_opts;
text_options_t input;
consumer_t consumer;
int
operator () (int argc, char **argv)
{
option_parser_t options ("[FONT-FILE] [TEXT]");
font_opts.add_options (&options);
input.add_options (&options);
consumer.add_options (&options);
add_options (&options);
options.parse (&argc, &argv);
argc--, argv++;
if (argc && !font_opts.font_file) font_opts.font_file = locale_to_utf8 (argv[0]), argc--, argv++;
if (argc && !input.text && !input.text_file) input.text = locale_to_utf8 (argv[0]), argc--, argv++;
if (argc && !this->font_file) this->font_file = locale_to_utf8 (argv[0]), argc--, argv++;
if (argc && !this->text && !this->text_file) this->text = locale_to_utf8 (argv[0]), argc--, argv++;
if (argc)
fail (true, "Too many arguments on the command line");
if (!font_opts.font_file)
if (!this->font_file)
options.usage ();
if (!input.text && !input.text_file)
input.text_file = g_strdup ("-");
if (!this->text && !this->text_file)
this->text_file = g_strdup ("-");
consumer.init (&font_opts);
this->init (this);
unsigned int text_len;
const char *text;
while ((text = input.get_line (&text_len)))
consumer.consume_line (text, text_len, input.text_before, input.text_after);
unsigned int line_len;
const char *line;
while ((line = this->get_line (&line_len)))
this->consume_line (line, line_len, this->text_before, this->text_after);
consumer.finish (&font_opts);
this->finish (this);
return consumer.failed ? 1 : 0;
}
return this->failed ? 1 : 0;
}
};
#endif