[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); typedef int (*main_func_t) (int argc, char **argv);
template <bool report_status=false> template <typename main_t, bool report_status=false>
int 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")) 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; args[argc++] = p = e;
} }
int result = main_func (argc, args); int result = main_t () (argc, args);
if (report_status) if (report_status)
fprintf (stdout, result == 0 ? "success\n" : "failure\n"); 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 ret;
} }
return main_func (argc, argv); return main_t () (argc, argv);
} }
#endif #endif

View File

@ -114,5 +114,5 @@ struct shape_closure_consumer_t
int int
main (int argc, char **argv) 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 int
main (int argc, char **argv) main (int argc, char **argv)
{ {
auto main_func = main_font_text<shape_consumer_t<output_buffer_t>, font_options_t, text_options_t>; using main_t = main_font_text<shape_consumer_t<output_buffer_t>, font_options_t, text_options_t>;
return batch_main<> (main_func, argc, argv); return batch_main<main_t> (argc, argv);
} }

View File

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

View File

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