173 {
174 reset();
175 dataVector.clear();
176 if(!transpose)
177 dataVector.resize(cols.size());
178 int nrow=0;
179 bool withinRange=true;
180 if(m_fs>' '&&m_fs<='~'){
181 if(verbose)
182 std::cout << "reading csv file " << m_filename << std::endl;
183 std::string csvRecord;
184 while(getline(m_ifstream,csvRecord)){
185 std::vector<T> sampleVector;
186 withinRange=true;
187 if(nrow<m_minRow)
188 withinRange=false;
189 if(m_maxRow>m_minRow)
190 if(nrow>m_maxRow)
191 withinRange=false;
192 if(withinRange){
193 std::istringstream csvstream(csvRecord);
194 std::string item;
195 int ncol=0;
196 bool isComment=false;
197 while(getline(csvstream,item,m_fs)){
198 if(verbose)
199 std::cout << item << " ";
200 size_t pos=item.find(m_comment);
201 if(pos!=std::string::npos){
202 isComment=true;
203 if(pos>0)
204 item=item.substr(0,pos-1);
205 else
206 break;
207 if(verbose)
208 std::cout << "comment found, string is " << item << std::endl;
209 }
210 for(int icol=0;icol<cols.size();++icol){
211 if(ncol==cols[icol]){
212 T value=scale*string2type<T>(item)+offset;
213
214 if((value>=m_min&&value<=m_max)||m_max<=m_min){
215 if(transpose)
216 sampleVector.push_back(value);
217 else
218 dataVector[icol].push_back(value);
219 }
220 }
221 }
222 ++ncol;
223 if(isComment)
224 break;
225 }
226 if(verbose)
227 std::cout << std::endl;
228
229
230 }
231 if(sampleVector.size()&&transpose)
232 dataVector.push_back(sampleVector);
233 ++nrow;
234 }
235 assert(dataVector.size());
236 }
237 else{
238 if(verbose)
239 std::cout << "space or tab delimited fields" << std::endl;
240 std::string spaceRecord;
241 while(!getline(m_ifstream, spaceRecord).eof()){
242 std::vector<T> sampleVector;
243 withinRange=true;
244 if(nrow<m_minRow)
245 withinRange=false;
246 if(m_maxRow>m_minRow)
247 if(nrow>m_maxRow)
248 withinRange=false;
249 if(withinRange){
250 if(verbose>1)
251 std::cout << spaceRecord << std::endl;
252 std::istringstream lineStream(spaceRecord);
253 std::string item;
254 int ncol=0;
255 bool isComment=false;
256 while(lineStream >> item){
257 if(verbose)
258 std::cout << item << " ";
259
260 size_t pos=item.find(m_comment);
261 if(pos!=std::string::npos){
262 isComment=true;
263 if(pos>0)
264 item=item.substr(0,pos-1);
265 else
266 break;
267 if(verbose)
268 std::cout << "comment found, string is " << item << std::endl;
269 }
270 T value=scale*string2type<T>(item)+offset;
271
272 for(int icol=0;icol<cols.size();++icol){
273 if(ncol==cols[icol]){
274 if((value>=m_min&&value<=m_max)||m_max<=m_min){
275 if(transpose)
276 sampleVector.push_back(value);
277 else
278 dataVector[icol].push_back(value);
279 }
280 }
281 }
282 ++ncol;
283 if(isComment)
284 break;
285 }
286 if(verbose>1)
287 std::cout << std::endl;
288 if(verbose)
289 std::cout << "number of columns: " << ncol << std::endl;
290
291
292 }
293 if(sampleVector.size()&&transpose)
294 dataVector.push_back(sampleVector);
295 ++nrow;
296 }
297 }
298 return dataVector.size();
299}