00001 #pragma ident "$Id: FFTextStream.hpp 2559 2011-04-18 21:12:23Z architest $"
00002
00008 #ifndef GPSTK_FFTEXTSTREAM_HPP
00009 #define GPSTK_FFTEXTSTREAM_HPP
00010
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 "FFStream.hpp"
00050
00051 namespace gpstk
00052 {
00053
00056
00066 class FFTextStream : public FFStream
00067 {
00068 public:
00069
00070
00072 virtual ~FFTextStream() {};
00073
00074
00076 FFTextStream()
00077 : lineNumber(0) {};
00078
00079
00085 FFTextStream( const char* fn,
00086 std::ios::openmode mode=std::ios::in )
00087 : FFStream(fn, mode), lineNumber(0)
00088 {};
00089
00090
00096 FFTextStream( const std::string& fn,
00097 std::ios::openmode mode=std::ios::in )
00098 : FFStream( fn.c_str(), mode ), lineNumber(0)
00099 {};
00100
00101
00103 virtual void open( const char* fn,
00104 std::ios::openmode mode )
00105 { FFStream::open(fn, mode); lineNumber = 0; };
00106
00107
00109 virtual void open( const std::string& fn,
00110 std::ios::openmode mode )
00111 { open(fn.c_str(), mode); };
00112
00113
00116 unsigned int lineNumber;
00117
00118
00134 inline void formattedGetLine( std::string& line,
00135 const bool expectEOF = false )
00136 throw(EndOfFile, FFStreamError, gpstk::StringUtils::StringException);
00137
00138
00139 protected:
00140
00141
00143 virtual void tryFFStreamGet(FFData& rec)
00144 throw(FFStreamError, gpstk::StringUtils::StringException)
00145 {
00146
00147 unsigned int initialLineNumber = lineNumber;
00148
00149 try
00150 {
00151 FFStream::tryFFStreamGet(rec);
00152 }
00153 catch(gpstk::Exception& e)
00154 {
00155 e.addText( std::string("Near file line ") +
00156 gpstk::StringUtils::asString(lineNumber) );
00157 lineNumber = initialLineNumber;
00158 mostRecentException = e;
00159 conditionalThrow();
00160 }
00161
00162 };
00163
00164
00166 virtual void tryFFStreamPut(const FFData& rec)
00167 throw(FFStreamError, gpstk::StringUtils::StringException)
00168 {
00169
00170 unsigned int initialLineNumber = lineNumber;
00171
00172 try
00173 {
00174 FFStream::tryFFStreamPut(rec);
00175 }
00176 catch(gpstk::Exception& e)
00177 {
00178 e.addText( std::string("Near file line ") +
00179 gpstk::StringUtils::asString(lineNumber) );
00180 lineNumber = initialLineNumber;
00181 mostRecentException = e;
00182 conditionalThrow();
00183 }
00184
00185 }
00186
00187 };
00188
00189
00190
00191
00192
00193
00194
00195
00196 void FFTextStream::formattedGetLine( std::string& line,
00197 const bool expectEOF )
00198 throw(EndOfFile, FFStreamError, gpstk::StringUtils::StringException)
00199 {
00200
00201 try
00202 {
00203
00204
00205
00206
00207
00208
00209
00210 const int MAX_LINE_LENGTH = 1500;
00211 char templine[MAX_LINE_LENGTH + 1];
00212 getline(templine, MAX_LINE_LENGTH);
00213 lineNumber++;
00214
00215 if(fail() && !eof())
00216 {
00217 FFStreamError err("Line too long");
00218 GPSTK_THROW(err);
00219 }
00220 line = templine;
00221 gpstk::StringUtils::stripTrailing(line, '\r');
00222
00223 if ((gcount() == 0) && eof())
00224 {
00225 if (expectEOF)
00226 {
00227 EndOfFile err("EOF encountered");
00228 GPSTK_THROW(err);
00229 }
00230 else
00231 {
00232 FFStreamError err("Unexpected EOF encountered");
00233 GPSTK_THROW(err);
00234 }
00235 }
00236 }
00237 catch(std::exception &e)
00238 {
00239
00240
00241 if ( (gcount() == 0) && eof())
00242 {
00243 if (expectEOF)
00244 {
00245 EndOfFile err("EOF encountered");
00246 GPSTK_THROW(err);
00247 }
00248 else
00249 {
00250 FFStreamError err("Unexpected EOF");
00251 GPSTK_THROW(err);
00252 }
00253 }
00254 else
00255 {
00256 FFStreamError err("Critical file error: " +
00257 std::string(e.what()));
00258 GPSTK_THROW(err);
00259 }
00260
00261 }
00262
00263 }
00264
00266
00267 }
00268 #endif // GPSTK_FFTEXTSTREAM_HPP