Eclipse SUMO - Simulation of Urban MObility
LineReader.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // Retrieves a file linewise and reports the lines to a handler.
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <string>
26 #include <fstream>
27 #include <iostream>
28 #include <algorithm>
29 #include <sstream>
31 #include "LineHandler.h"
32 #include "LineReader.h"
33 
34 
35 // ===========================================================================
36 // method definitions
37 // ===========================================================================
39 
40 
41 LineReader::LineReader(const std::string& file)
42  : myFileName(file),
43  myRead(0) {
44  reinit();
45 }
46 
47 
49 
50 
51 bool
53  return myRread < myAvailable;
54 }
55 
56 
57 void
59  while (myRread < myAvailable) {
60  if (!readLine(lh)) {
61  return;
62  }
63  }
64 }
65 
66 
67 bool
69  std::string toReport;
70  bool moreAvailable = true;
71  while (toReport.length() == 0) {
72  const std::string::size_type idx = myStrBuffer.find('\n');
73  if (idx == 0) {
74  myStrBuffer = myStrBuffer.substr(1);
75  myRread++;
76  return lh.report("");
77  }
78  if (idx != std::string::npos) {
79  toReport = myStrBuffer.substr(0, idx);
80  myStrBuffer = myStrBuffer.substr(idx + 1);
81  myRread += (int)idx + 1;
82  } else {
83  if (myRead < myAvailable) {
84  myStrm.read(myBuffer,
85  myAvailable - myRead < 1024
87  : 1024);
88  int noBytes = myAvailable - myRead;
89  noBytes = noBytes > 1024 ? 1024 : noBytes;
90  myStrBuffer += std::string(myBuffer, noBytes);
91  myRead += 1024;
92  } else {
93  toReport = myStrBuffer;
94  moreAvailable = false;
95  if (toReport == "") {
96  return lh.report(toReport);
97  }
98  }
99  }
100  }
101  // remove trailing blanks
102  int idx = (int)toReport.length() - 1;
103  while (idx >= 0 && toReport[idx] < 32) {
104  idx--;
105  }
106  if (idx >= 0) {
107  toReport = toReport.substr(0, idx + 1);
108  } else {
109  toReport = "";
110  }
111  // give it to the handler
112  if (!lh.report(toReport)) {
113  return false;
114  }
115  return moreAvailable;
116 }
117 
118 
119 std::string
121  std::string toReport;
122  while (toReport.length() == 0 && myStrm.good()) {
123  const std::string::size_type idx = myStrBuffer.find('\n');
124  if (idx == 0) {
125  myStrBuffer = myStrBuffer.substr(1);
126  myRread++;
127  return "";
128  }
129  if (idx != std::string::npos) {
130  toReport = myStrBuffer.substr(0, idx);
131  myStrBuffer = myStrBuffer.substr(idx + 1);
132  myRread += (int) idx + 1;
133  } else {
134  if (myRead < myAvailable) {
135  myStrm.read(myBuffer,
136  myAvailable - myRead < 1024
137  ? myAvailable - myRead
138  : 1024);
139  int noBytes = myAvailable - myRead;
140  noBytes = noBytes > 1024 ? 1024 : noBytes;
141  myStrBuffer += std::string(myBuffer, noBytes);
142  myRead += 1024;
143  } else {
144  toReport = myStrBuffer;
145  myRread += 1024;
146  if (toReport == "") {
147  return toReport;
148  }
149  }
150  }
151  }
152  if (!myStrm.good()) {
153  return "";
154  }
155  // remove trailing blanks
156  int idx = (int)toReport.length() - 1;
157  while (idx >= 0 && toReport[idx] < 32) {
158  idx--;
159  }
160  if (idx >= 0) {
161  toReport = toReport.substr(0, idx + 1);
162  } else {
163  toReport = "";
164  }
165  return toReport;
166 }
167 
168 
169 
170 std::string
172  return myFileName;
173 }
174 
175 
176 bool
177 LineReader::setFile(const std::string& file) {
178  myFileName = file;
179  reinit();
180  return myStrm.good();
181 }
182 
183 
184 unsigned long
186  return myRread;
187 }
188 
189 
190 void
192  if (myStrm.is_open()) {
193  myStrm.close();
194  }
195  myStrm.clear();
196  myStrm.open(myFileName.c_str(), std::ios::binary);
197  myStrm.unsetf(std::ios::skipws);
198  myStrm.seekg(0, std::ios::end);
199  myAvailable = static_cast<int>(myStrm.tellg());
200  myStrm.seekg(0, std::ios::beg);
201  myRead = 0;
202  myRread = 0;
203  myStrBuffer = "";
204 }
205 
206 
207 void
208 LineReader::setPos(unsigned long pos) {
209  myStrm.seekg(pos, std::ios::beg);
210  myRead = pos;
211  myRread = pos;
212  myStrBuffer = "";
213 }
214 
215 
216 bool
218  return myStrm.good();
219 }
220 
221 
222 
223 /****************************************************************************/
224 
LineReader.h
LineReader::myRread
int myRread
Information how many bytes were read by the reader from the file.
Definition: LineReader.h:165
LineReader::reinit
void reinit()
Reinitialises the reading (of the previous file)
Definition: LineReader.cpp:191
LineReader::setFile
bool setFile(const std::string &file)
Reinitialises the reader for reading from the given file.
Definition: LineReader.cpp:177
LineHandler::report
virtual bool report(const std::string &result)=0
Method that obatins a line read by the LineReader.
LineHandler
Interface definition for a class which retrieves lines from a LineHandler.
Definition: LineHandler.h:44
LineReader::myStrm
std::ifstream myStrm
the stream used
Definition: LineReader.h:150
LineReader::setPos
void setPos(unsigned long pos)
Sets the current position within the file to the given value.
Definition: LineReader.cpp:208
LineReader::myAvailable
int myAvailable
Information how many bytes are available within the used file.
Definition: LineReader.h:162
LineReader::readLine
std::string readLine()
Reads a single (the next) line from the file and returns it.
Definition: LineReader.cpp:120
LineReader::myBuffer
char myBuffer[1024]
To override MSVC++-bugs, we use an own getline which uses this buffer.
Definition: LineReader.h:153
LineReader::LineReader
LineReader()
Constructor.
Definition: LineReader.cpp:38
LineReader::getFileName
std::string getFileName() const
Returns the name of the used file.
Definition: LineReader.cpp:171
UtilExceptions.h
LineReader::myFileName
std::string myFileName
the name of the file to read the contents from
Definition: LineReader.h:147
LineReader::getPosition
unsigned long getPosition()
Returns the current position within the file.
Definition: LineReader.cpp:185
LineReader::myStrBuffer
std::string myStrBuffer
a string-buffer
Definition: LineReader.h:156
LineReader::hasMore
bool hasMore() const
Returns whether another line may be read (the file was not read completely)
Definition: LineReader.cpp:52
LineReader::~LineReader
~LineReader()
Destructor.
Definition: LineReader.cpp:48
LineHandler.h
LineReader::good
bool good() const
Returns the information whether the stream is readable.
Definition: LineReader.cpp:217
config.h
LineReader::readAll
void readAll(LineHandler &lh)
Reads the whole file linewise, reporting every line to the given LineHandler.
Definition: LineReader.cpp:58
LineReader::myRead
int myRead
Information about how many characters were supplied to the LineHandler.
Definition: LineReader.h:159