From d3a9c2f7ade30d8d77be7c66d746c1fa5aa8dce1 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 21 Jan 2003 12:00:44 +0000 Subject: [PATCH] Open the input file in main.cc, not in class Options. --- ChangeLog | 9 +++++++++ src/main.cc | 15 ++++++++++++++- src/options.cc | 13 +++++-------- src/options.h | 6 ++++++ src/options.icc | 7 +++++++ 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index ae6c7e4..4f36570 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,14 @@ 2002-11-10 Bruno Haible + * src/options.h (Options::get_input_file_name): New declaration. + (Options::_input_file_name): New field. + * src/options.icc (Options::get_input_file_name): New method. + * src/options.cc (Options::Options): Initialize _input_file_name. + (Options::parse_options): Don't open input file, only store it in + _input_file_name. + * src/main.cc (main): Open input file here. + Print an error message upon write error on the output file. + Upgrade to autoconf-2.52. * configure.in: Use AC_CONFIG_SUBDIRS instead of AC_OUTPUT_SUBDIRS. * Makefile.devel (configure, lib/configure, src/configure, diff --git a/src/main.cc b/src/main.cc index c55901a..22ebd46 100644 --- a/src/main.cc +++ b/src/main.cc @@ -21,6 +21,7 @@ 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include +#include #include "options.h" #include "input.h" #include "search.h" @@ -51,6 +52,15 @@ main (int argc, char *argv[]) /* Set the Options. Open the input file and assign stdin to it. */ option.parse_options (argc, argv); + /* Open the input file. */ + if (option.get_input_file_name ()) + if (!freopen (option.get_input_file_name (), "r", stdin)) + { + fprintf (stderr, "Cannot open input file '%s'\n", + option.get_input_file_name ()); + exit (1); + } + /* Initialize the keyword list. */ KeywordExt_Factory factory; Input inputter (stdin, &factory); @@ -82,7 +92,10 @@ main (int argc, char *argv[]) /* Check for write error on stdout. */ int status = 0; if (fflush (stdout) || ferror (stdout)) - status = 1; + { + fprintf (stderr, "error while writing output file\n"); + status = 1; + } /* Don't use exit() here, it skips the destructors. */ return status; diff --git a/src/options.cc b/src/options.cc index f207ff9..5a912dd 100644 --- a/src/options.cc +++ b/src/options.cc @@ -416,6 +416,7 @@ PositionStringParser::nextPosition () Options::Options () : _option_word (C), + _input_file_name (NULL), _iterations (0), _jump (DEFAULT_JUMP_VALUE), _initial_asso_value (0), @@ -832,19 +833,15 @@ Options::parse_options (int argc, char *argv[]) } - if (/*getopt*/optind + 1 < argc) + if (/*getopt*/optind < argc) + _input_file_name = argv[/*getopt*/optind++]; + + if (/*getopt*/optind < argc) { fprintf (stderr, "Extra trailing arguments to %s.\n", program_name); short_usage (stderr); exit (1); } - - if (argv[/*getopt*/optind] && ! freopen (argv[/*getopt*/optind], "r", stdin)) - { - fprintf (stderr, "Cannot open keyword file `%s'\n", argv[/*getopt*/optind]); - short_usage (stderr); - exit (1); - } } #ifndef __OPTIMIZE__ diff --git a/src/options.h b/src/options.h index e2e12b9..874ca2a 100644 --- a/src/options.h +++ b/src/options.h @@ -181,6 +181,9 @@ public: /* Tests a given boolean option. Returns true if set, false otherwise. */ bool operator[] (Option_Type option) const; + /* Returns the input file name. */ + const char * get_input_file_name () const; + /* Returns the iterations value. */ int get_iterations () const; @@ -244,6 +247,9 @@ private: /* Holds the boolean options. */ int _option_word; + /* Name of input file. */ + char * _input_file_name; + /* Amount to iterate when a collision occurs. */ int _iterations; diff --git a/src/options.icc b/src/options.icc index 2585570..f053898 100644 --- a/src/options.icc +++ b/src/options.icc @@ -125,6 +125,13 @@ Options::operator[] (Option_Type option) const return _option_word & option; } +/* Returns the input file name. */ +INLINE const char * +Options::get_input_file_name () const +{ + return _input_file_name; +} + /* Returns the iterations value. */ INLINE int Options::get_iterations () const