00001 #pragma ident "$Id: FFStream.cpp 2975 2011-11-14 08:16:30Z yanweignss $"
00002
00008
00009
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 #include "FFStream.hpp"
00047
00048 namespace gpstk
00049 {
00050
00051
00052
00053
00054
00055 void FFStream::open( const char* fn,
00056 std::ios::openmode mode )
00057 {
00058
00059 #ifdef _MSC_VER
00060 fstream::open(fn, mode);
00061 #else
00062 std::fstream::open(fn, mode);
00063 #endif
00064 filename = std::string(fn);
00065 recordNumber = 0;
00066 clear();
00067
00068 }
00069
00070
00071
00072
00073 void FFStream::dumpState(std::ostream& s) const
00074 {
00075
00076 s << "filename:" << filename
00077 << ", recordNumber:" << recordNumber;
00078 s << ", exceptions:";
00079
00080 if (exceptions() & std::ios::badbit) s << "bad ";
00081 if (exceptions() & std::ios::failbit) s << "fail ";
00082 if (exceptions() & std::ios::eofbit) s << "eof ";
00083 if (exceptions() == 0) s << "none";
00084
00085 s << ", rdstate:";
00086
00087 if (rdstate() & std::ios::badbit) s << "bad ";
00088 if (rdstate() & std::ios::failbit) s << "fail ";
00089 if (rdstate() & std::ios::eofbit) s << "eof ";
00090 if (rdstate() == 0) s << "none";
00091 s << std::endl;
00092
00093 }
00094
00095
00096
00097
00098
00099
00100 void FFStream::tryFFStreamGet(FFData& rec)
00101 throw(FFStreamError, gpstk::StringUtils::StringException)
00102 {
00103
00104
00105 long initialPosition = tellg();
00106 unsigned long initialRecordNumber = recordNumber;
00107 clear();
00108
00109 try
00110 {
00111 try
00112 {
00113 rec.reallyGetRecord(*this);
00114 recordNumber++;
00115 }
00116 catch (EndOfFile& e)
00117 {
00118
00119
00120 e.addText("In record " +
00121 gpstk::StringUtils::asString(recordNumber));
00122 e.addText("In file " + filename);
00123 e.addLocation(FILE_LOCATION);
00124 mostRecentException = e;
00125 }
00126 catch (std::exception &e)
00127 {
00128 mostRecentException = FFStreamError("std::exception thrown: " +
00129 std::string(e.what()));
00130 mostRecentException.addText("In record " +
00131 gpstk::StringUtils::asString(recordNumber));
00132 mostRecentException.addText("In file " + filename);
00133 mostRecentException.addLocation(FILE_LOCATION);
00134 clear();
00135 seekg(initialPosition);
00136 recordNumber = initialRecordNumber;
00137 setstate(std::ios::failbit);
00138 conditionalThrow();
00139 }
00140 catch (gpstk::StringUtils::StringException& e)
00141 {
00142 e.addText("In record " +
00143 gpstk::StringUtils::asString(recordNumber));
00144 e.addText("In file " + filename);
00145 e.addLocation(FILE_LOCATION);
00146 mostRecentException = e;
00147 clear();
00148 seekg(initialPosition);
00149 recordNumber = initialRecordNumber;
00150 setstate(std::ios::failbit);
00151 conditionalThrow();
00152 }
00153
00154 catch (FFStreamError& e)
00155 {
00156 e.addText("In record " +
00157 gpstk::StringUtils::asString(recordNumber));
00158 e.addText("In file " + filename);
00159 e.addLocation(FILE_LOCATION);
00160 mostRecentException = e;
00161 clear();
00162 seekg(initialPosition);
00163 recordNumber = initialRecordNumber;
00164 setstate(std::ios::failbit);
00165 conditionalThrow();
00166 }
00167 }
00168
00169
00170
00171 catch (gpstk::Exception &e)
00172 {
00173 GPSTK_RETHROW(e);
00174 }
00175 catch (std::ifstream::failure &e)
00176 {
00177
00178
00179
00180
00181 if (!fail())
00182 {
00183 mostRecentException = FFStreamError("ifstream::failure thrown: " +
00184 std::string(e.what()));
00185 mostRecentException.addText("In file " + filename);
00186 mostRecentException.addLocation(FILE_LOCATION);
00187 }
00188 conditionalThrow();
00189 }
00190 catch (std::exception &e)
00191 {
00192 mostRecentException = FFStreamError("std::exception thrown: " +
00193 std::string(e.what()));
00194 mostRecentException.addText("In file " + filename);
00195 mostRecentException.addLocation(FILE_LOCATION);
00196 setstate(std::ios::failbit);
00197 conditionalThrow();
00198 }
00199 catch (...)
00200 {
00201 mostRecentException = FFStreamError("Unknown exception thrown");
00202 mostRecentException.addText("In file " + filename);
00203 mostRecentException.addLocation(FILE_LOCATION);
00204 setstate(std::ios::failbit);
00205 conditionalThrow();
00206 }
00207
00208 }
00209
00210
00211
00212
00213
00214
00215 void FFStream::tryFFStreamPut(const FFData& rec)
00216 throw(FFStreamError, gpstk::StringUtils::StringException)
00217 {
00218
00219 long initialPosition = tellg();
00220 unsigned long initialRecordNumber = recordNumber;
00221 clear();
00222
00223 try
00224 {
00225 try
00226 {
00227 rec.reallyPutRecord(*this);
00228 recordNumber++;
00229 }
00230 catch (std::exception &e)
00231 {
00232
00233
00234 if (dynamic_cast<std::ifstream::failure*>(&e))
00235 throw;
00236
00237
00238
00239 mostRecentException = FFStreamError("std::exception thrown: " +
00240 std::string(e.what()));
00241 mostRecentException.addLocation(FILE_LOCATION);
00242 setstate(std::ios::failbit);
00243 conditionalThrow();
00244 }
00245 catch (gpstk::StringUtils::StringException& e)
00246 {
00247 e.addText("In record " +
00248 gpstk::StringUtils::asString(recordNumber));
00249 e.addText("In file " + filename);
00250 e.addLocation(FILE_LOCATION);
00251 mostRecentException = e;
00252 seekg(initialPosition);
00253 recordNumber = initialRecordNumber;
00254 setstate(std::ios::failbit);
00255 conditionalThrow();
00256 }
00257
00258 catch (FFStreamError& e)
00259 {
00260 e.addText("In record " +
00261 gpstk::StringUtils::asString(recordNumber));
00262 e.addText("In file " + filename);
00263 e.addLocation(FILE_LOCATION);
00264 mostRecentException = e;
00265 seekg(initialPosition);
00266 recordNumber = initialRecordNumber;
00267 setstate(std::ios::failbit);
00268 conditionalThrow();
00269 }
00270 }
00271
00272
00273
00274 catch (gpstk::Exception &e)
00275 {
00276 GPSTK_RETHROW(e);
00277 }
00278 catch (std::ifstream::failure &e)
00279 {
00280
00281
00282
00283
00284 if (!fail())
00285 {
00286 mostRecentException = FFStreamError("ifstream::failure thrown: " +
00287 std::string(e.what()));
00288 mostRecentException.addText("In file " + filename);
00289 mostRecentException.addLocation(FILE_LOCATION);
00290 }
00291 conditionalThrow();
00292 }
00293 catch (std::exception &e)
00294 {
00295 mostRecentException = FFStreamError("std::exception thrown: " +
00296 std::string(e.what()));
00297 mostRecentException.addText("In file " + filename);
00298 mostRecentException.addLocation(FILE_LOCATION);
00299 setstate(std::ios::failbit);
00300 conditionalThrow();
00301 }
00302 catch (...)
00303 {
00304 mostRecentException = FFStreamError("Unknown exception thrown");
00305 mostRecentException.addText("In file " + filename);
00306 mostRecentException.addLocation(FILE_LOCATION);
00307 setstate(std::ios::failbit);
00308 conditionalThrow();
00309 }
00310
00311 }
00312
00313
00314
00315 }
00316