FFTextStream.hpp

Go to the documentation of this file.
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 //  This file is part of GPSTk, the GPS Toolkit.
00014 //
00015 //  The GPSTk is free software; you can redistribute it and/or modify
00016 //  it under the terms of the GNU Lesser General Public License as published
00017 //  by the Free Software Foundation; either version 2.1 of the License, or
00018 //  any later version.
00019 //
00020 //  The GPSTk is distributed in the hope that it will be useful,
00021 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 //  GNU Lesser General Public License for more details.
00024 //
00025 //  You should have received a copy of the GNU Lesser General Public
00026 //  License along with GPSTk; if not, write to the Free Software Foundation,
00027 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028 //
00029 //  Copyright 2004, The University of Texas at Austin
00030 //
00031 //============================================================================
00032 
00033 //============================================================================
00034 //
00035 //This software developed by Applied Research Laboratories at the University of
00036 //Texas at Austin, under contract to an agency or agencies within the U.S.
00037 //Department of Defense. The U.S. Government retains all rights to use,
00038 //duplicate, distribute, disclose, or release this software.
00039 //
00040 //Pursuant to DoD Directive 523024
00041 //
00042 // DISTRIBUTION STATEMENT A: This software has been approved for public
00043 //                           release, distribution is unlimited.
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    }; // End of class 'FFTextStream'
00188 
00189 
00190 
00191       // the reason for checking ffs.eof() in the try AND catch block is
00192       // because if the user enabled exceptions on the stream with exceptions()
00193       // then eof could throw an exception, in which case we need to catch it
00194       // and rethrow an EOF or FFStream exception.  In any event, EndOfFile
00195       // gets thrown whenever there's an EOF and expectEOF is true
00196    void FFTextStream::formattedGetLine( std::string& line,
00197                                         const bool expectEOF )
00198          throw(EndOfFile, FFStreamError, gpstk::StringUtils::StringException)
00199    {
00200 
00201       try
00202       {
00203             // The following constant used to be 256, but with the change to
00204             // RINEX3 formats the possible length of a line increased
00205             // considerably. A RINEX3 observation file line for Galileo may
00206             // be 1277 characters long (taking into account all the possible
00207             // types of observations available, plus the end of line
00208             // characters), so this constant was conservatively set to
00209             // 1500 characters. Dagoberto Salazar.
00210          const int MAX_LINE_LENGTH = 1500;
00211          char templine[MAX_LINE_LENGTH + 1];
00212          getline(templine, MAX_LINE_LENGTH);
00213          lineNumber++;
00214             //check if line was longer than 256 characters, if so error
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             // catch EOF when stream exceptions are disabled
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             // catch EOF when exceptions are enabled
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          }  // End of 'if ( (gcount() == 0) && eof())'
00260 
00261       }  // End of 'try-catch' block
00262 
00263    }  // End of method 'FFTextStream::formattedGetLine()'
00264 
00266 
00267 }  // End of namespace gpstk
00268 #endif   // GPSTK_FFTEXTSTREAM_HPP

Generated on Thu Feb 9 03:30:56 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1