[util] Move text-file opening to post_parse

This commit is contained in:
Behdad Esfahbod 2021-08-11 19:06:57 -06:00
parent 23298bc846
commit 6ba7ddafed
1 changed files with 21 additions and 18 deletions

View File

@ -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))