SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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.sourceforge.net/
12 // Copyright (C) 2001-2012 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 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
51 
52 
53 LineReader::LineReader(const std::string& file)
54  : myFileName(file),
55  myRead(0) {
56  reinit();
57 }
58 
59 
61 
62 
63 bool
65  return myRread < myAvailable;
66 }
67 
68 
69 void
71  while (myRread < myAvailable) {
72  if (!readLine(lh)) {
73  return;
74  }
75  }
76 }
77 
78 
79 bool
81  std::string toReport;
82  bool moreAvailable = true;
83  while (toReport.length() == 0) {
84  size_t idx = myStrBuffer.find('\n');
85  if (idx == 0) {
86  myStrBuffer = myStrBuffer.substr(1);
87  myRread++;
88  return lh.report("");
89  }
90  if (idx != std::string::npos) {
91  toReport = myStrBuffer.substr(0, idx);
92  myStrBuffer = myStrBuffer.substr(idx + 1);
93  myRread += (unsigned int)idx + 1;
94  } else {
95  if (myRead < myAvailable) {
96  myStrm.read(myBuffer,
97  myAvailable - myRead < 1024
99  : 1024);
100  size_t noBytes = myAvailable - myRead;
101  noBytes = noBytes > 1024 ? 1024 : noBytes;
102  myStrBuffer += std::string(myBuffer, noBytes);
103  myRead += 1024;
104  } else {
105  toReport = myStrBuffer;
106  moreAvailable = false;
107  if (toReport == "") {
108  return lh.report(toReport);
109  }
110  }
111  }
112  }
113  // remove trailing blanks
114  int idx = (int)toReport.length() - 1;
115  while (idx >= 0 && toReport[idx] < 32) {
116  idx--;
117  }
118  if (idx >= 0) {
119  toReport = toReport.substr(0, idx + 1);
120  } else {
121  toReport = "";
122  }
123  // give it to the handler
124  if (!lh.report(toReport)) {
125  return false;
126  }
127  return moreAvailable;
128 }
129 
130 
131 std::string
133  std::string toReport;
134  bool moreAvailable = true;
135  while (toReport.length() == 0 && myStrm.good()) {
136  size_t idx = myStrBuffer.find('\n');
137  if (idx == 0) {
138  myStrBuffer = myStrBuffer.substr(1);
139  myRread++;
140  return "";
141  }
142  if (idx != std::string::npos) {
143  toReport = myStrBuffer.substr(0, idx);
144  myStrBuffer = myStrBuffer.substr(idx + 1);
145  myRread += (unsigned int) idx + 1;
146  } else {
147  if (myRead < myAvailable) {
148  myStrm.read(myBuffer,
149  myAvailable - myRead < 1024
150  ? myAvailable - myRead
151  : 1024);
152  size_t noBytes = myAvailable - myRead;
153  noBytes = noBytes > 1024 ? 1024 : noBytes;
154  myStrBuffer += std::string(myBuffer, noBytes);
155  myRead += 1024;
156  } else {
157  toReport = myStrBuffer;
158  myRread += 1024;
159  moreAvailable = false;
160  if (toReport == "") {
161  return toReport;
162  }
163  }
164  }
165  }
166  if (!myStrm.good()) {
167  return "";
168  }
169  // remove trailing blanks
170  int idx = (int)toReport.length() - 1;
171  while (idx >= 0 && toReport[idx] < 32) {
172  idx--;
173  }
174  if (idx >= 0) {
175  toReport = toReport.substr(0, idx + 1);
176  } else {
177  toReport = "";
178  }
179  return toReport;
180 }
181 
182 
183 
184 std::string
186  return myFileName;
187 }
188 
189 
190 bool
191 LineReader::setFile(const std::string& file) {
192  myFileName = file;
193  reinit();
194  return myStrm.good();
195 }
196 
197 
198 unsigned long
200  return myRread;
201 }
202 
203 
204 void
206  if (myStrm.is_open()) {
207  myStrm.close();
208  }
209  myStrm.clear();
210  myStrm.open(myFileName.c_str(), std::ios::binary);
211  myStrm.unsetf(std::ios::skipws);
212  myStrm.seekg(0, std::ios::end);
213  myAvailable = static_cast<unsigned int>(myStrm.tellg());
214  myStrm.seekg(0, std::ios::beg);
215  myRead = 0;
216  myRread = 0;
217  myStrBuffer = "";
218 }
219 
220 
221 void
222 LineReader::setPos(unsigned long pos) {
223  myStrm.seekg(pos, std::ios::beg);
224  myRead = pos;
225  myRread = pos;
226  myStrBuffer = "";
227 }
228 
229 
230 bool
232  return myStrm.good();
233 }
234 
235 
236 
237 /****************************************************************************/
238