00001 #pragma ident "$Id: InOutFramework.hpp 1363 2008-08-21 02:55:57Z ocibu $"
00002
00009 #ifndef INOUTFRAMEWORK_HPP
00010 #define INOUTFRAMEWORK_HPP
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #include "LoopedFramework.hpp"
00050
00051
00052 namespace gpstk
00053 {
00054
00057
00070 template<class IType, class OType>
00071 class InOutFramework : public LoopedFramework
00072 {
00073 public:
00074
00075
00082 InOutFramework( const std::string& applName,
00083 const std::string& applDesc )
00084 throw()
00085 : LoopedFramework(applName, applDesc)
00086 {};
00087
00088
00090 virtual ~InOutFramework() {};
00091
00092
00093 bool initialize( int argc,
00094 char *argv[],
00095 bool pretty = true )
00096 throw()
00097 {
00098 using std::ios;
00099
00100 CommandOptionWithAnyArg
00101 inputOpt('i', "input",
00102 "A file to take the input from. The default is stdin."),
00103 outputOpt('o', "output",
00104 "A file to receive the output. The default is stdout.");
00105
00106 if (!LoopedFramework::initialize(argc, argv, pretty))
00107 return false;
00108
00109 if (inputOpt.getCount())
00110 inputFn = inputOpt.getValue()[0];
00111
00112
00113 if (inputFn=="-" || inputFn=="")
00114 {
00115 input.copyfmt(std::cin);
00116 input.clear(std::cin.rdstate());
00117 input.ios::rdbuf(std::cin.rdbuf());
00118 inputFn = "<stdin>";
00119 }
00120 else
00121 {
00122 input.open(inputFn.c_str(), std::ios::in);
00123 }
00124
00125
00126 if (!input)
00127 {
00128 std::cerr << "Could not open: " << inputFn << std::endl;
00129 return false;
00130 }
00131
00132 if (outputOpt.getCount())
00133 outputFn = outputOpt.getValue()[0];
00134
00135 if (outputFn=="-" || outputFn=="")
00136 {
00137 output.copyfmt(std::cout);
00138 output.clear(std::cout.rdstate());
00139 output.ios::rdbuf(std::cout.rdbuf());
00140 outputFn = "<stdout>";
00141 }
00142 else
00143 {
00144 output.open(outputFn.c_str(), std::ios::out);
00145 }
00146
00147 if (!output)
00148 {
00149 std::cerr << "Could not open: " << outputFn << std::endl;
00150 return false;
00151 }
00152
00153 if (debugLevel)
00154 std::cout << "Sending output to " << outputFn << std::endl
00155 << "Reading input from " << inputFn << std::endl;
00156
00157 return true;
00158 }
00159
00160
00161 IType input;
00162
00163 OType output;
00164
00165 std::string inputFn, outputFn;
00166
00167
00168 private:
00169
00170
00171
00172 InOutFramework();
00173
00174
00175 };
00176
00178
00179 }
00180 #endif // INOUTFRAMEWORK_HPP