From 6ba7ddafed4cf80cdb4ff38700d96629d10eed64 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 11 Aug 2021 19:06:57 -0600 Subject: [PATCH] [util] Move text-file opening to post_parse --- util/text-options.hh | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/util/text-options.hh b/util/text-options.hh index 4fce69ce9..8f76612b0 100644 --- a/util/text-options.hh +++ b/util/text-options.hh @@ -1,6 +1,9 @@ struct text_options_t { + text_options_t () + : gs (g_string_new (nullptr)) + {} ~text_options_t () { g_free (text_before); @@ -17,13 +20,29 @@ struct text_options_t void post_parse (GError **error G_GNUC_UNUSED) { - if (!this->text && !this->text_file) - this->text_file = g_strdup ("-"); + if (!text && !text_file) + text_file = g_strdup ("-"); if (text && text_file) + { g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "Only one of text and text-file can be set"); + return; + } + + if (text_file) + { + if (0 != strcmp (text_file, "-")) + fp = fopen (text_file, "r"); + else + fp = stdin; + + if (!fp) + g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, + "Failed opening text file `%s': %s", + text_file, strerror (errno)); + } } const char *get_line (unsigned int *len); @@ -137,22 +156,6 @@ text_options_t::get_line (unsigned int *len) return text; } - if (!fp) - { - assert (text_file); - - if (0 != strcmp (text_file, "-")) - fp = fopen (text_file, "r"); - else - fp = stdin; - - if (!fp) - fail (false, "Failed opening text file `%s': %s", - text_file, strerror (errno)); - - gs = g_string_new (nullptr); - } - g_string_set_size (gs, 0); char buf[BUFSIZ]; while (fgets (buf, sizeof (buf), fp))