LineReader Class Reference

#include <LineReader.h>


Detailed Description

Retrieves a file linewise and reports the lines to a handler.

This class reads the contents from a file line by line and report them to a LineHandler-derivate.

See also:
LineHandler
Todo:
No checks are done so far during reading/setting position etc.
Todo:
Should not IOError be thrown if something fails?

Definition at line 56 of file LineReader.h.


Public Member Functions

void close () throw ()
 Closes the reading.
std::string getFileName () const throw ()
 Returns the name of the used file.
unsigned long getPosition () throw ()
 Returns the current position within the file.
bool good () const throw ()
 Returns the information whether the stream is readable.
bool hasMore () const throw ()
 Returns whether another line may be read (the file was not read completely).
 LineReader (const std::string &file) throw ()
 Constructor.
 LineReader () throw ()
 Constructor.
void readAll (LineHandler &lh) throw (ProcessError)
 Reads the whole file linewise, reporting every line to the given LineHandler.
std::string readLine () throw ()
 Reads a single (the next) line from the file and returns it.
bool readLine (LineHandler &lh) throw (ProcessError)
 Reads a single (the next) line from the file and reports it to the given LineHandler.
void reinit () throw ()
 Reinitialises the reading (of the previous file).
bool setFile (const std::string &file) throw ()
 Reinitialises the reader for reading from the given file.
void setPos (unsigned long pos) throw ()
 Sets the current position within the file to the given value.
 ~LineReader () throw ()
 Destructor.

Private Attributes

unsigned int myAvailable
 Information how many bytes are available within the used file.
char myBuffer [1024]
 To override MSVC++-bugs, we use an own getline which uses this buffer.
std::string myFileName
 the name of the file to read the contents from
unsigned int myRead
 Information about how many characters were supplied to the LineHandler.
unsigned int myRread
 Information how many bytes were read by the reader from the file.
std::string myStrBuffer
 a string-buffer
std::ifstream myStrm
 the stream used

Constructor & Destructor Documentation

LineReader::LineReader (  )  throw ()

Constructor.

Definition at line 47 of file LineReader.cpp.

00047 {}

LineReader::LineReader ( const std::string &  file  )  throw ()

Constructor.

Initialises reading from the file with the given name using setFile.

Parameters:
[in] file The name of the file to open
See also:
setFile

Definition at line 50 of file LineReader.cpp.

References reinit().

00051         : myFileName(file),
00052         myRead(0) {
00053     reinit();
00054 }

LineReader::~LineReader (  )  throw ()

Destructor.

Definition at line 57 of file LineReader.cpp.

00057 {}


Member Function Documentation

void LineReader::close (  )  throw ()

Closes the reading.

std::string LineReader::getFileName (  )  const throw ()

Returns the name of the used file.

Returns:
The name of the opened file

Definition at line 182 of file LineReader.cpp.

References myFileName.

Referenced by RODFDetFlowLoader::read(), readO(), and readV().

00182                                       {
00183     return myFileName;
00184 }

unsigned long LineReader::getPosition (  )  throw ()

Returns the current position within the file.

Returns:
The current position within the opened file

Definition at line 196 of file LineReader.cpp.

References myRread.

Referenced by NIImporter_VISUM::load().

00196                                 {
00197     return myRread;
00198 }

bool LineReader::good (  )  const throw ()

Returns the information whether the stream is readable.

Returns:
Whether the file is usable (good())

Definition at line 228 of file LineReader.cpp.

References myStrm.

Referenced by getNextNonCommentLine(), and loadMatrix().

00228                                {
00229     return myStrm.good();
00230 }

bool LineReader::hasMore (  )  const throw ()

Returns whether another line may be read (the file was not read completely).

Returns:
Whether further reading is possible

Definition at line 61 of file LineReader.cpp.

References myAvailable, and myRread.

Referenced by getNextNonCommentLine(), PCLoaderVisum::load(), NIImporter_VISUM::load(), PCLoaderDlrNavteq::loadPOIFile(), PCLoaderDlrNavteq::loadPolyFile(), RODFDetFlowLoader::read(), readO(), and readV().

00061                                   {
00062     return myRread<myAvailable;
00063 }

void LineReader::readAll ( LineHandler lh  )  throw (ProcessError)

Reads the whole file linewise, reporting every line to the given LineHandler.

When the LineHandler returns false, the reading will be aborted

Parameters:
[in] lh The LineHandler to report read lines to

Definition at line 67 of file LineReader.cpp.

References myAvailable, myRread, and readLine().

Referenced by NIImporter_DlrNavteq::loadNetwork().

00067                                                        {
00068     while (myRread<myAvailable) {
00069         if (!readLine(lh)) {
00070             return;
00071         }
00072     }
00073 }

std::string LineReader::readLine (  )  throw ()

Reads a single (the next) line from the file and returns it.

Returns:
The next line in the file

Definition at line 129 of file LineReader.cpp.

References myAvailable, myBuffer, myRead, myRread, myStrBuffer, and myStrm.

Referenced by readAll().

00129                              {
00130     std::string toReport;
00131     bool moreAvailable = true;
00132     while (toReport.length()==0&&myStrm.good()) {
00133         size_t idx = myStrBuffer.find('\n');
00134         if (idx==0) {
00135             myStrBuffer = myStrBuffer.substr(1);
00136             myRread++;
00137             return "";
00138         }
00139         if (idx!=std::string::npos) {
00140             toReport = myStrBuffer.substr(0, idx);
00141             myStrBuffer = myStrBuffer.substr(idx+1);
00142             myRread += idx+1;
00143         } else {
00144             if (myRead<myAvailable) {
00145                 myStrm.read(myBuffer,
00146                             myAvailable - myRead<1024
00147                             ? myAvailable - myRead
00148                             : 1024);
00149                 size_t noBytes = myAvailable - myRead;
00150                 noBytes = noBytes > 1024 ? 1024 : noBytes;
00151                 myStrBuffer += std::string(myBuffer, noBytes);
00152                 myRead += 1024;
00153             } else {
00154                 toReport = myStrBuffer;
00155                 myRread += 1024;
00156                 moreAvailable = false;
00157                 if (toReport=="") {
00158                     return toReport;
00159                 }
00160             }
00161         }
00162     }
00163     if (!myStrm.good()) {
00164         return "";
00165     }
00166     // remove trailing blanks
00167     int idx = (int)toReport.length()-1;
00168     while (idx>=0&&toReport[idx]<32) {
00169         idx--;
00170     }
00171     if (idx>=0) {
00172         toReport = toReport.substr(0, idx+1);
00173     } else {
00174         toReport = "";
00175     }
00176     return toReport;
00177 }

bool LineReader::readLine ( LineHandler lh  )  throw (ProcessError)

Reads a single (the next) line from the file and reports it to the given LineHandler.

When the LineHandler returns false, the reading will be aborted

Parameters:
[in] lh The LineHandler to report read lines to
Returns:
Whether a further line exists

Definition at line 77 of file LineReader.cpp.

References myAvailable, myBuffer, myRead, myRread, myStrBuffer, and myStrm.

Referenced by getNextNonCommentLine(), PCLoaderVisum::load(), NIImporter_VISUM::load(), loadMatrix(), PCLoaderDlrNavteq::loadPOIFile(), PCLoaderDlrNavteq::loadPolyFile(), and RODFDetFlowLoader::read().

00077                                                         {
00078     std::string toReport;
00079     bool moreAvailable = true;
00080     while (toReport.length()==0) {
00081         size_t idx = myStrBuffer.find('\n');
00082         if (idx==0) {
00083             myStrBuffer = myStrBuffer.substr(1);
00084             myRread++;
00085             return lh.report("");
00086         }
00087         if (idx!=std::string::npos) {
00088             toReport = myStrBuffer.substr(0, idx);
00089             myStrBuffer = myStrBuffer.substr(idx+1);
00090             myRread += idx+1;
00091         } else {
00092             if (myRead<myAvailable) {
00093                 myStrm.read(myBuffer,
00094                             myAvailable - myRead<1024
00095                             ? myAvailable - myRead
00096                             : 1024);
00097                 size_t noBytes = myAvailable - myRead;
00098                 noBytes = noBytes > 1024 ? 1024 : noBytes;
00099                 myStrBuffer += std::string(myBuffer, noBytes);
00100                 myRead += 1024;
00101             } else {
00102                 toReport = myStrBuffer;
00103                 moreAvailable = false;
00104                 if (toReport=="") {
00105                     return lh.report(toReport);
00106                 }
00107             }
00108         }
00109     }
00110     // remove trailing blanks
00111     int idx = (int)toReport.length()-1;
00112     while (idx>=0&&toReport[idx]<32) {
00113         idx--;
00114     }
00115     if (idx>=0) {
00116         toReport = toReport.substr(0, idx+1);
00117     } else {
00118         toReport = "";
00119     }
00120     // give it to the handler
00121     if (!lh.report(toReport)) {
00122         return false;
00123     }
00124     return moreAvailable;
00125 }

void LineReader::reinit (  )  throw ()

Reinitialises the reading (of the previous file).

Definition at line 202 of file LineReader.cpp.

References myAvailable, myFileName, myRead, myRread, myStrBuffer, and myStrm.

Referenced by LineReader(), PCLoaderVisum::load(), NIImporter_VISUM::load(), and setFile().

00202                            {
00203     if (myStrm.is_open()) {
00204         myStrm.close();
00205     }
00206     myStrm.clear();
00207     myStrm.open(myFileName.c_str(), std::ios::binary);
00208     myStrm.unsetf(std::ios::skipws);
00209     myStrm.seekg(0, std::ios::end);
00210     myAvailable = myStrm.tellg();
00211     myStrm.seekg(0, std::ios::beg);
00212     myRead = 0;
00213     myRread = 0;
00214     myStrBuffer = "";
00215 }

bool LineReader::setFile ( const std::string &  file  )  throw ()

Reinitialises the reader for reading from the given file.

Returns false when the file is not readable

Parameters:
[in] file The name of the file to open
Returns:
Whether the file could be opened

Definition at line 188 of file LineReader.cpp.

References myFileName, myStrm, and reinit().

Referenced by NIImporter_VISUM::load(), and NIImporter_DlrNavteq::loadNetwork().

00188                                                  {
00189     myFileName = file;
00190     reinit();
00191     return myStrm.good();
00192 }

void LineReader::setPos ( unsigned long  pos  )  throw ()

Sets the current position within the file to the given value.

Parameters:
[in] pos The new position within the file

Definition at line 219 of file LineReader.cpp.

References myRead, myRread, myStrBuffer, and myStrm.

Referenced by NIImporter_VISUM::load().

00219                                             {
00220     myStrm.seekg(pos, std::ios::beg);
00221     myRead = pos;
00222     myRread = pos;
00223     myStrBuffer = "";
00224 }


Field Documentation

unsigned int LineReader::myAvailable [private]

Information how many bytes are available within the used file.

Definition at line 168 of file LineReader.h.

Referenced by hasMore(), readAll(), readLine(), and reinit().

char LineReader::myBuffer[1024] [private]

To override MSVC++-bugs, we use an own getline which uses this buffer.

Definition at line 159 of file LineReader.h.

Referenced by readLine().

std::string LineReader::myFileName [private]

the name of the file to read the contents from

Definition at line 153 of file LineReader.h.

Referenced by getFileName(), reinit(), and setFile().

unsigned int LineReader::myRead [private]

Information about how many characters were supplied to the LineHandler.

Definition at line 165 of file LineReader.h.

Referenced by readLine(), reinit(), and setPos().

unsigned int LineReader::myRread [private]

Information how many bytes were read by the reader from the file.

Definition at line 171 of file LineReader.h.

Referenced by getPosition(), hasMore(), readAll(), readLine(), reinit(), and setPos().

std::string LineReader::myStrBuffer [private]

a string-buffer

Definition at line 162 of file LineReader.h.

Referenced by readLine(), reinit(), and setPos().

std::ifstream LineReader::myStrm [private]

the stream used

Definition at line 156 of file LineReader.h.

Referenced by good(), readLine(), reinit(), setFile(), and setPos().


The documentation for this class was generated from the following files:

Generated on Wed May 5 00:06:46 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6