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