SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00009 // Retrieves a file linewise and reports the lines to a handler. 00010 /****************************************************************************/ 00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00013 /****************************************************************************/ 00014 // 00015 // This file is part of SUMO. 00016 // SUMO is free software: you can redistribute it and/or modify 00017 // it under the terms of the GNU General Public License as published by 00018 // the Free Software Foundation, either version 3 of the License, or 00019 // (at your option) any later version. 00020 // 00021 /****************************************************************************/ 00022 00023 00024 // =========================================================================== 00025 // included modules 00026 // =========================================================================== 00027 #ifdef _MSC_VER 00028 #include <windows_config.h> 00029 #else 00030 #include <config.h> 00031 #endif 00032 00033 #include <string> 00034 #include <fstream> 00035 #include <iostream> 00036 #include <algorithm> 00037 #include <sstream> 00038 #include <utils/common/UtilExceptions.h> 00039 #include "LineHandler.h" 00040 #include "LineReader.h" 00041 00042 #ifdef CHECK_MEMORY_LEAKS 00043 #include <foreign/nvwa/debug_new.h> 00044 #endif // CHECK_MEMORY_LEAKS 00045 00046 00047 // =========================================================================== 00048 // method definitions 00049 // =========================================================================== 00050 LineReader::LineReader() {} 00051 00052 00053 LineReader::LineReader(const std::string& file) 00054 : myFileName(file), 00055 myRead(0) { 00056 reinit(); 00057 } 00058 00059 00060 LineReader::~LineReader() {} 00061 00062 00063 bool 00064 LineReader::hasMore() const { 00065 return myRread < myAvailable; 00066 } 00067 00068 00069 void 00070 LineReader::readAll(LineHandler& lh) { 00071 while (myRread < myAvailable) { 00072 if (!readLine(lh)) { 00073 return; 00074 } 00075 } 00076 } 00077 00078 00079 bool 00080 LineReader::readLine(LineHandler& lh) { 00081 std::string toReport; 00082 bool moreAvailable = true; 00083 while (toReport.length() == 0) { 00084 size_t idx = myStrBuffer.find('\n'); 00085 if (idx == 0) { 00086 myStrBuffer = myStrBuffer.substr(1); 00087 myRread++; 00088 return lh.report(""); 00089 } 00090 if (idx != std::string::npos) { 00091 toReport = myStrBuffer.substr(0, idx); 00092 myStrBuffer = myStrBuffer.substr(idx + 1); 00093 myRread += (unsigned int)idx + 1; 00094 } else { 00095 if (myRead < myAvailable) { 00096 myStrm.read(myBuffer, 00097 myAvailable - myRead < 1024 00098 ? myAvailable - myRead 00099 : 1024); 00100 size_t noBytes = myAvailable - myRead; 00101 noBytes = noBytes > 1024 ? 1024 : noBytes; 00102 myStrBuffer += std::string(myBuffer, noBytes); 00103 myRead += 1024; 00104 } else { 00105 toReport = myStrBuffer; 00106 moreAvailable = false; 00107 if (toReport == "") { 00108 return lh.report(toReport); 00109 } 00110 } 00111 } 00112 } 00113 // remove trailing blanks 00114 int idx = (int)toReport.length() - 1; 00115 while (idx >= 0 && toReport[idx] < 32) { 00116 idx--; 00117 } 00118 if (idx >= 0) { 00119 toReport = toReport.substr(0, idx + 1); 00120 } else { 00121 toReport = ""; 00122 } 00123 // give it to the handler 00124 if (!lh.report(toReport)) { 00125 return false; 00126 } 00127 return moreAvailable; 00128 } 00129 00130 00131 std::string 00132 LineReader::readLine() { 00133 std::string toReport; 00134 bool moreAvailable = true; 00135 while (toReport.length() == 0 && myStrm.good()) { 00136 size_t idx = myStrBuffer.find('\n'); 00137 if (idx == 0) { 00138 myStrBuffer = myStrBuffer.substr(1); 00139 myRread++; 00140 return ""; 00141 } 00142 if (idx != std::string::npos) { 00143 toReport = myStrBuffer.substr(0, idx); 00144 myStrBuffer = myStrBuffer.substr(idx + 1); 00145 myRread += (unsigned int) idx + 1; 00146 } else { 00147 if (myRead < myAvailable) { 00148 myStrm.read(myBuffer, 00149 myAvailable - myRead < 1024 00150 ? myAvailable - myRead 00151 : 1024); 00152 size_t noBytes = myAvailable - myRead; 00153 noBytes = noBytes > 1024 ? 1024 : noBytes; 00154 myStrBuffer += std::string(myBuffer, noBytes); 00155 myRead += 1024; 00156 } else { 00157 toReport = myStrBuffer; 00158 myRread += 1024; 00159 moreAvailable = false; 00160 if (toReport == "") { 00161 return toReport; 00162 } 00163 } 00164 } 00165 } 00166 if (!myStrm.good()) { 00167 return ""; 00168 } 00169 // remove trailing blanks 00170 int idx = (int)toReport.length() - 1; 00171 while (idx >= 0 && toReport[idx] < 32) { 00172 idx--; 00173 } 00174 if (idx >= 0) { 00175 toReport = toReport.substr(0, idx + 1); 00176 } else { 00177 toReport = ""; 00178 } 00179 return toReport; 00180 } 00181 00182 00183 00184 std::string 00185 LineReader::getFileName() const { 00186 return myFileName; 00187 } 00188 00189 00190 bool 00191 LineReader::setFile(const std::string& file) { 00192 myFileName = file; 00193 reinit(); 00194 return myStrm.good(); 00195 } 00196 00197 00198 unsigned long 00199 LineReader::getPosition() { 00200 return myRread; 00201 } 00202 00203 00204 void 00205 LineReader::reinit() { 00206 if (myStrm.is_open()) { 00207 myStrm.close(); 00208 } 00209 myStrm.clear(); 00210 myStrm.open(myFileName.c_str(), std::ios::binary); 00211 myStrm.unsetf(std::ios::skipws); 00212 myStrm.seekg(0, std::ios::end); 00213 myAvailable = static_cast<unsigned int>(myStrm.tellg()); 00214 myStrm.seekg(0, std::ios::beg); 00215 myRead = 0; 00216 myRread = 0; 00217 myStrBuffer = ""; 00218 } 00219 00220 00221 void 00222 LineReader::setPos(unsigned long pos) { 00223 myStrm.seekg(pos, std::ios::beg); 00224 myRead = pos; 00225 myRread = pos; 00226 myStrBuffer = ""; 00227 } 00228 00229 00230 bool 00231 LineReader::good() const { 00232 return myStrm.good(); 00233 } 00234 00235 00236 00237 /****************************************************************************/ 00238