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.dev/sumo
3 // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
21 // Retrieves a file linewise and reports the lines to a handler.
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  myLinesRead++;
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 += (int)myStrBuffer.size();
147  if (toReport == "") {
148  myLinesRead++;
149  return toReport;
150  }
151  }
152  }
153  }
154  if (!myStrm.good()) {
155  return "";
156  }
157  // remove trailing blanks
158  int idx = (int)toReport.length() - 1;
159  while (idx >= 0 && toReport[idx] < 32) {
160  idx--;
161  }
162  if (idx >= 0) {
163  toReport = toReport.substr(0, idx + 1);
164  } else {
165  toReport = "";
166  }
167  myLinesRead++;
168  return toReport;
169 }
170 
171 
172 
173 std::string
175  return myFileName;
176 }
177 
178 
179 bool
180 LineReader::setFile(const std::string& file) {
181  myFileName = file;
182  reinit();
183  return myStrm.good();
184 }
185 
186 
187 unsigned long
189  return myRread;
190 }
191 
192 
193 void
195  if (myStrm.is_open()) {
196  myStrm.close();
197  }
198  myStrm.clear();
199  myStrm.open(myFileName.c_str(), std::ios::binary);
200  myStrm.unsetf(std::ios::skipws);
201  myStrm.seekg(0, std::ios::end);
202  myAvailable = static_cast<int>(myStrm.tellg());
203  myStrm.seekg(0, std::ios::beg);
204  if (myAvailable >= 3) {
205  // check for BOM
206  myStrm.read(myBuffer, 3);
207  if (myBuffer[0] == '\xef' && myBuffer[1] == '\xbb' && myBuffer[2] == '\xbf') {
208  mySkipBOM = 3;
210  } else {
211  mySkipBOM = 0;
212  myStrm.seekg(0, std::ios::beg);
213  }
214  }
215  myRead = 0;
216  myRread = 0;
217  myStrBuffer = "";
218  myLinesRead = 0;
219 }
220 
221 
222 void
223 LineReader::setPos(unsigned long pos) {
224  myStrm.seekg(pos + mySkipBOM, std::ios::beg);
225  myRead = pos;
226  myRread = pos;
227  myStrBuffer = "";
228 }
229 
230 
231 bool
233  return myStrm.good();
234 }
235 
236 
237 /****************************************************************************/
Interface definition for a class which retrieves lines from a LineHandler.
Definition: LineHandler.h:42
virtual bool report(const std::string &result)=0
Method that obatins a line read by the LineReader.
void setPos(unsigned long pos)
Sets the current position within the file to the given value.
Definition: LineReader.cpp:223
~LineReader()
Destructor.
Definition: LineReader.cpp:48
unsigned long getPosition()
Returns the current position within the file.
Definition: LineReader.cpp:188
LineReader()
Constructor.
Definition: LineReader.cpp:38
int mySkipBOM
Number of skipped characters at the file begin (UTF-8 BOM)
Definition: LineReader.h:173
bool good() const
Returns the information whether the stream is readable.
Definition: LineReader.cpp:232
int myRead
Information about how many characters were supplied to the LineHandler.
Definition: LineReader.h:161
int myRread
Information how many bytes were read by the reader from the file.
Definition: LineReader.h:167
int myAvailable
Information how many bytes are available within the used file.
Definition: LineReader.h:164
std::string readLine()
Reads a single (the next) line from the file and returns it.
Definition: LineReader.cpp:120
bool setFile(const std::string &file)
Reinitialises the reader for reading from the given file.
Definition: LineReader.cpp:180
void reinit()
Reinitialises the reading (of the previous file)
Definition: LineReader.cpp:194
int myLinesRead
Information how many lines were read for meaningful error messages.
Definition: LineReader.h:170
bool hasMore() const
Returns whether another line may be read (the file was not read completely)
Definition: LineReader.cpp:52
std::string getFileName() const
Returns the name of the used file.
Definition: LineReader.cpp:174
std::ifstream myStrm
the stream used
Definition: LineReader.h:152
std::string myFileName
the name of the file to read the contents from
Definition: LineReader.h:149
char myBuffer[1024]
To override MSVC++-bugs, we use an own getline which uses this buffer.
Definition: LineReader.h:155
std::string myStrBuffer
a string-buffer
Definition: LineReader.h:158
void readAll(LineHandler &lh)
Reads the whole file linewise, reporting every line to the given LineHandler.
Definition: LineReader.cpp:58