[util/hb-subset] Move output-file validation code into post_parse

This commit is contained in:
Behdad Esfahbod 2021-08-11 20:22:03 -06:00
parent 58bfe40794
commit c98773ebb0
5 changed files with 71 additions and 72 deletions

View File

@ -36,7 +36,7 @@
const unsigned DEFAULT_FONT_SIZE = FONT_SIZE_UPEM; const unsigned DEFAULT_FONT_SIZE = FONT_SIZE_UPEM;
const unsigned SUBPIXEL_BITS = 0; const unsigned SUBPIXEL_BITS = 0;
struct output_buffer_t : output_options_t struct output_buffer_t : output_options_t<>
{ {
void add_options (option_parser_t *parser) void add_options (option_parser_t *parser)
{ {

View File

@ -38,7 +38,7 @@
* Command line interface to the harfbuzz font subsetter. * Command line interface to the harfbuzz font subsetter.
*/ */
struct subset_main_t : option_parser_t, face_options_t, text_options_t, subset_options_t, output_options_t struct subset_main_t : option_parser_t, face_options_t, text_options_t, subset_options_t, output_options_t<false>
{ {
int operator () (int argc, char **argv) int operator () (int argc, char **argv)
{ {
@ -92,8 +92,7 @@ struct subset_main_t : option_parser_t, face_options_t, text_options_t, subset_o
bool bool
write_file (const char *output_file, hb_blob_t *blob) write_file (const char *output_file, hb_blob_t *blob)
{ {
if (!output_file) assert (out_fp);
fail (true, "No output file was specified");
unsigned int size; unsigned int size;
const char* data = hb_blob_get_data (blob, &size); const char* data = hb_blob_get_data (blob, &size);

View File

@ -426,6 +426,8 @@ static const char *helper_cairo_supported_formats[] =
nullptr nullptr
}; };
template <typename view_options_t,
typename output_options_t>
static inline cairo_t * static inline cairo_t *
helper_cairo_create_context (double w, double h, helper_cairo_create_context (double w, double h,
view_options_t *view_opts, view_options_t *view_opts,

View File

@ -29,6 +29,7 @@
#include "options.hh" #include "options.hh"
template <bool default_stdout = true>
struct output_options_t struct output_options_t
{ {
~output_options_t () ~output_options_t ()
@ -40,58 +41,8 @@ struct output_options_t
} }
void add_options (option_parser_t *parser, void add_options (option_parser_t *parser,
const char **supported_formats = nullptr); const char **supported_formats = nullptr)
void post_parse (GError **error G_GNUC_UNUSED);
char *output_file = nullptr;
char *output_format = nullptr;
bool explicit_output_format = false;
FILE *out_fp = nullptr;
};
void
output_options_t::post_parse (GError **error)
{
if (output_format)
explicit_output_format = true;
if (output_file && !output_format)
{ {
output_format = strrchr (output_file, '.');
if (output_format)
{
output_format++; /* skip the dot */
output_format = g_strdup (output_format);
}
}
if (output_file && 0 == strcmp (output_file, "-"))
output_file = nullptr; /* STDOUT */
if (output_file)
out_fp = fopen (output_file, "wb");
else
{
#if defined(_WIN32) || defined(__CYGWIN__)
setmode (fileno (stdout), O_BINARY);
#endif
out_fp = stdout;
}
if (!out_fp)
{
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
"Cannot open output file `%s': %s",
g_filename_display_name (output_file), strerror (errno));
return;
}
}
void
output_options_t::add_options (option_parser_t *parser,
const char **supported_formats)
{
const char *text = nullptr; const char *text = nullptr;
if (supported_formats) if (supported_formats)
@ -114,6 +65,53 @@ output_options_t::add_options (option_parser_t *parser,
"Output destination & format options:", "Output destination & format options:",
"Options for the destination & form of the output", "Options for the destination & form of the output",
this); this);
} }
void post_parse (GError **error)
{
if (output_format)
explicit_output_format = true;
if (output_file && !output_format)
{
output_format = strrchr (output_file, '.');
if (output_format)
{
output_format++; /* skip the dot */
output_format = g_strdup (output_format);
}
}
if (output_file && 0 != strcmp (output_file, "-"))
out_fp = fopen (output_file, "wb");
else
{
if (!default_stdout && !output_file)
{
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
"No output file was specified");
return;
}
#if defined(_WIN32) || defined(__CYGWIN__)
setmode (fileno (stdout), O_BINARY);
#endif
out_fp = stdout;
}
if (!out_fp)
{
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
"Cannot open output file `%s': %s",
g_filename_display_name (output_file), strerror (errno));
return;
}
}
char *output_file = nullptr;
char *output_format = nullptr;
bool explicit_output_format = false;
FILE *out_fp = nullptr;
};
#endif #endif

View File

@ -31,7 +31,7 @@
#include "output-options.hh" #include "output-options.hh"
#include "helper-cairo.hh" #include "helper-cairo.hh"
struct view_cairo_t : view_options_t, output_options_t struct view_cairo_t : view_options_t, output_options_t<>
{ {
~view_cairo_t () ~view_cairo_t ()
{ {