FFStream.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: FFStream.cpp 2975 2011-11-14 08:16:30Z yanweignss $"
00002 
00008 //============================================================================
00009 //
00010 //  This file is part of GPSTk, the GPS Toolkit.
00011 //
00012 //  The GPSTk is free software; you can redistribute it and/or modify
00013 //  it under the terms of the GNU Lesser General Public License as published
00014 //  by the Free Software Foundation; either version 2.1 of the License, or
00015 //  any later version.
00016 //
00017 //  The GPSTk is distributed in the hope that it will be useful,
00018 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //  GNU Lesser General Public License for more details.
00021 //
00022 //  You should have received a copy of the GNU Lesser General Public
00023 //  License along with GPSTk; if not, write to the Free Software Foundation,
00024 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 //
00026 //  Copyright 2004, The University of Texas at Austin
00027 //
00028 //============================================================================
00029 
00030 //============================================================================
00031 //
00032 //This software developed by Applied Research Laboratories at the University of
00033 //Texas at Austin, under contract to an agency or agencies within the U.S.
00034 //Department of Defense. The U.S. Government retains all rights to use,
00035 //duplicate, distribute, disclose, or release this software.
00036 //
00037 //Pursuant to DoD Directive 523024
00038 //
00039 // DISTRIBUTION STATEMENT A: This software has been approved for public
00040 //                           release, distribution is unlimited.
00041 //
00042 //=============================================================================
00043 
00044 
00045 
00046 #include "FFStream.hpp"
00047 
00048 namespace gpstk
00049 {
00050 
00051       /*
00052        * Overrides fstream:open so derived classes can make appropriate
00053        * internal changes (line count, header info, etc).
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    }  // End of method 'FFStream::open()'
00069 
00070 
00071 
00072       // A function to help debug FFStreams
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    }  // End of method 'FFStream::dumpState()'
00094 
00095 
00096 
00097       // the crazy double try block is so that no gpstk::Exception throws
00098       // get masked, allowing all exception information (line numbers, text,
00099       // etc) to be retained.
00100    void FFStream::tryFFStreamGet(FFData& rec)
00101       throw(FFStreamError, gpstk::StringUtils::StringException)
00102    {
00103 
00104          // Mark where we start in case there is an error.
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             // EOF - do nothing - eof causes fail() to be set which
00119             // is handled by std::fstream
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             // catches some errors we can encounter
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          // this is if you throw an FFStream error in the above catch
00169          // block because the catch(...) below will mask it otherwise.
00170          // This also takes care of catching StringExceptions
00171       catch (gpstk::Exception &e)
00172       {
00173          GPSTK_RETHROW(e);
00174       }
00175       catch (std::ifstream::failure &e)
00176       {
00177             // setting failbit when catching FFStreamError can cause
00178             // this exception to be thrown. in this case, we don't want
00179             // to lose the exception info so only make a new exception
00180             // if this isn't a fail() case
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    }  // End of method 'FFStream::tryFFStreamGet()'
00209 
00210 
00211 
00212       // the crazy double try block is so that no gpstk::Exception throws 
00213       // get masked, allowing all exception information (line numbers, text,
00214       // etc) to be retained.
00215    void FFStream::tryFFStreamPut(const FFData& rec)
00216       throw(FFStreamError, gpstk::StringUtils::StringException)
00217    {
00218          // Mark where we start in case there is an error.
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                // if this is a stream failure, don't mask it and let the
00233                // later catch block handle it
00234             if (dynamic_cast<std::ifstream::failure*>(&e))
00235                throw;
00236 
00237                // the catch(FFStreamError) below will add file information
00238                // to this exception
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             // catches some errors we can encounter
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          // this is if you throw an FFStream error in the above catch
00272          // block because the catch(...) below will mask it otherwise.
00273          // This also takes care of catching StringExceptions
00274       catch (gpstk::Exception &e)
00275       {
00276          GPSTK_RETHROW(e);
00277       }
00278       catch (std::ifstream::failure &e)
00279       {
00280             // setting failbit when catching FFStreamError can cause
00281             // this exception to be thrown. in this case, we don't want
00282             // to lose the exception info so only make a new exception
00283             // if this isn't a fail() case
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    }  // End of method 'FFStream::tryFFStreamPut()'
00312 
00313 
00314 
00315 }  // End of namespace gpstk
00316 

Generated on Tue May 22 03:30:58 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1