Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/importio/LineReader.cpp
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 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
/****************************************************************************/
14
/// @file LineReader.cpp
15
/// @author Daniel Krajzewicz
16
/// @author Laura Bieker
17
/// @author Michael Behrisch
18
/// @author Mirko Barthauer
19
/// @date Fri, 19 Jul 2002
20
///
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>
30
#include <utils/common/UtilExceptions.h>
31
#include "LineHandler.h"
32
#include "LineReader.h"
33
34
35
// ===========================================================================
36
// method definitions
37
// ===========================================================================
38
LineReader::LineReader() {}
39
40
41
LineReader::LineReader(const std::string& file)
42
: myFileName(file),
43
myRead(0) {
44
reinit();
45
}
46
47
48
LineReader::~LineReader() {}
49
50
51
bool
52
LineReader::hasMore() const {
53
return myRread < myAvailable;
54
}
55
56
57
void
58
LineReader::readAll(LineHandler& lh) {
59
while (myRread < myAvailable) {
60
if (!readLine(lh)) {
61
return;
62
}
63
}
64
}
65
66
67
bool
68
LineReader::readLine(LineHandler& lh) {
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
86
? myAvailable - myRead
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
120
LineReader::readLine() {
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
174
LineReader::getFileName() const {
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
188
LineReader::getPosition() {
189
return myRread;
190
}
191
192
193
void
194
LineReader::reinit() {
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;
209
myAvailable -= mySkipBOM;
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
232
LineReader::good() const {
233
return myStrm.good();
234
}
235
236
237
/****************************************************************************/
238
239