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