1 //==============================================================================
2 //
3 //  This file is part of GPSTk, the GPS Toolkit.
4 //
5 //  The GPSTk is free software; you can redistribute it and/or modify
6 //  it under the terms of the GNU Lesser General Public License as published
7 //  by the Free Software Foundation; either version 3.0 of the License, or
8 //  any later version.
9 //
10 //  The GPSTk is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU Lesser General Public License for more details.
14 //
15 //  You should have received a copy of the GNU Lesser General Public
16 //  License along with GPSTk; if not, write to the Free Software Foundation,
17 //  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 //  This software was developed by Applied Research Laboratories at the
20 //  University of Texas at Austin.
21 //  Copyright 2004-2020, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 //  This software was developed by Applied Research Laboratories at the
28 //  University of Texas at Austin, under contract to an agency or agencies
29 //  within the U.S. Department of Defense. The U.S. Government retains all
30 //  rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 //  Pursuant to DoD Directive 523024
33 //
34 //  DISTRIBUTION STATEMENT A: This software has been approved for public
35 //                            release, distribution is unlimited.
36 //
37 //==============================================================================
38 
39 /**
40  *  @file InOutFramework.hpp
41  *  An extension of the looped framework that takes care of setting up the
42  *  input and output streams.
43  */
44 
45 #ifndef INOUTFRAMEWORK_HPP
46 #define INOUTFRAMEWORK_HPP
47 
48 #include "LoopedFramework.hpp"
49 
50 
51 namespace gpstk
52 {
53       /// @ingroup AppFrame
54       //@{
55 
56       /**
57        * This is a framework for programs that take a single type of
58        * input data and output a single stream of output.
59        *
60        * The end user should define subclasses of this class,
61        * implementing those methods described as being meant to be
62        * overridden.
63        *
64        * In use, the user will construct an object of the class
65        * derived from this, then call the initialize() and run()
66        * methods in that order.
67        */
68    template<class IType, class OType>
69    class InOutFramework : public LoopedFramework
70    {
71    public:
72          /// Exit code used when the output file can't be opened
73       static const int OUTPUT_ERROR = 1;
74 
75          /** Constructor for InOutFramework.
76           *
77           * @param applName   name of the program (argv[0]).
78           * @param applDesc   text description of program's function
79           *                   (used by CommandOption help).
80           */
InOutFramework(const std::string & applName,const std::string & applDesc)81       InOutFramework( const std::string& applName,
82                       const std::string& applDesc )
83             throw()
84             : LoopedFramework(applName, applDesc)
85       {};
86 
87 
88          /// Destructor
~InOutFramework()89       virtual ~InOutFramework() {};
90 
91 
initialize(int argc,char * argv[],bool pretty=true)92       bool initialize( int argc,
93                        char *argv[],
94                        bool pretty = true )
95          throw()
96       {
97          using std::ios;
98 
99          CommandOptionWithAnyArg
100             inputOpt('i', "input",
101                      "A file to take the input from. The default is stdin."),
102             outputOpt('o', "output",
103                       "A file to receive the output. The default is stdout.");
104 
105          if (!LoopedFramework::initialize(argc, argv, pretty))
106             return false;
107 
108          if (inputOpt.getCount())
109             inputFn = inputOpt.getValue()[0];
110 
111 
112          if (inputFn=="-" || inputFn=="")
113          {
114             input.copyfmt(std::cin);
115             input.clear(std::cin.rdstate());
116             input.ios::rdbuf(std::cin.rdbuf());
117             inputFn = "<stdin>";
118          }
119          else
120          {
121             input.open(inputFn.c_str(), std::ios::in);
122          }
123 
124 
125          if (!input)
126          {
127             std::cerr << "Could not open: " << inputFn << std::endl;
128             exitCode = gpstk::BasicFramework::EXIST_ERROR;
129             return false;
130          }
131 
132          if (outputOpt.getCount())
133             outputFn = outputOpt.getValue()[0];
134 
135          if (outputFn=="-" || outputFn=="")
136          {
137             output.copyfmt(std::cout);
138             output.clear(std::cout.rdstate());
139             output.ios::rdbuf(std::cout.rdbuf());
140             outputFn = "<stdout>";
141          }
142          else
143          {
144             output.open(outputFn.c_str(), std::ios::out);
145          }
146 
147          if (!output)
148          {
149             std::cerr << "Could not open: " << outputFn << std::endl;
150             exitCode = OUTPUT_ERROR;
151             return false;
152          }
153 
154          if (debugLevel)
155             std::cerr << "Sending output to " << outputFn << std::endl
156                       << "Reading input from " << inputFn << std::endl;
157 
158          return true;
159       }  // End of method 'InOutFramework::initialize()'
160 
161 
162       IType input;
163 
164       OType output;
165 
166       std::string inputFn, outputFn;
167 
168 
169    private:
170 
171 
172          // Do not allow the use of the default constructor.
173       InOutFramework();
174 
175 
176    }; // End of class 'InOutFramework'
177 
178       //@}
179 
180 }  // End of namespace gpstk
181 #endif   // INOUTFRAMEWORK_HPP
182