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