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-sim.org/
12 // Copyright (C) 2001-2013 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  while (toReport.length() == 0 && myStrm.good()) {
135  size_t idx = myStrBuffer.find('\n');
136  if (idx == 0) {
137  myStrBuffer = myStrBuffer.substr(1);
138  myRread++;
139  return "";
140  }
141  if (idx != std::string::npos) {
142  toReport = myStrBuffer.substr(0, idx);
143  myStrBuffer = myStrBuffer.substr(idx + 1);
144  myRread += (unsigned int) idx + 1;
145  } else {
146  if (myRead < myAvailable) {
147  myStrm.read(myBuffer,
148  myAvailable - myRead < 1024
149  ? myAvailable - myRead
150  : 1024);
151  size_t noBytes = myAvailable - myRead;
152  noBytes = noBytes > 1024 ? 1024 : noBytes;
153  myStrBuffer += std::string(myBuffer, noBytes);
154  myRead += 1024;
155  } else {
156  toReport = myStrBuffer;
157  myRread += 1024;
158  if (toReport == "") {
159  return toReport;
160  }
161  }
162  }
163  }
164  if (!myStrm.good()) {
165  return "";
166  }
167  // remove trailing blanks
168  int idx = (int)toReport.length() - 1;
169  while (idx >= 0 && toReport[idx] < 32) {
170  idx--;
171  }
172  if (idx >= 0) {
173  toReport = toReport.substr(0, idx + 1);
174  } else {
175  toReport = "";
176  }
177  return toReport;
178 }
179 
180 
181 
182 std::string
184  return myFileName;
185 }
186 
187 
188 bool
189 LineReader::setFile(const std::string& file) {
190  myFileName = file;
191  reinit();
192  return myStrm.good();
193 }
194 
195 
196 unsigned long
198  return myRread;
199 }
200 
201 
202 void
204  if (myStrm.is_open()) {
205  myStrm.close();
206  }
207  myStrm.clear();
208  myStrm.open(myFileName.c_str(), std::ios::binary);
209  myStrm.unsetf(std::ios::skipws);
210  myStrm.seekg(0, std::ios::end);
211  myAvailable = static_cast<unsigned int>(myStrm.tellg());
212  myStrm.seekg(0, std::ios::beg);
213  myRead = 0;
214  myRread = 0;
215  myStrBuffer = "";
216 }
217 
218 
219 void
220 LineReader::setPos(unsigned long pos) {
221  myStrm.seekg(pos, std::ios::beg);
222  myRead = pos;
223  myRread = pos;
224  myStrBuffer = "";
225 }
226 
227 
228 bool
230  return myStrm.good();
231 }
232 
233 
234 
235 /****************************************************************************/
236