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