USGS

Isis 3.0 Object Programmers' Reference

Home

CSVReader.cpp
Go to the documentation of this file.
1 
23 #include <string>
24 #include <vector>
25 #include <numeric>
26 #include <iostream>
27 #include <sstream>
28 #include "IString.h"
29 #include "CSVReader.h"
30 #include "CollectorMap.h"
31 #include "IException.h"
32 
33 using namespace std;
34 
35 namespace Isis {
36 
51  CSVReader::CSVReader() : _header(false), _skip(0),
52  _delimiter(','), _keepParts(true), _lines(),
53  _ignoreComments(true) { }
54 
81  CSVReader::CSVReader(const QString &csvfile, bool header, int skip,
82  const char &delimiter, const bool keepEmptyParts,
83  const bool ignoreComments) :
84  _header(header), _skip(skip), _delimiter(delimiter),
85  _keepParts(keepEmptyParts), _lines(),
86  _ignoreComments(ignoreComments) {
87 
88  read(csvfile);
89  }
90 
91 
113  int CSVReader::columns() const {
114  return ((rows() > 0) ? columns(getTable()) : 0);
115  }
116 
133  int CSVReader::columns(const CSVReader::CSVTable &table) const {
134  CSVColumnSummary summary = getColumnSummary(table);
135  return ((summary.size() > 0) ? summary.key(0) : 0);
136  }
137 
138 
156  void CSVReader::read(const QString &csvfile) {
157  ifstream ifile(csvfile.toAscii().data(), ios::in);
158  if(!ifile) {
159  QString mess = "Unable to open file " + csvfile;
160  throw IException(IException::User, mess, _FILEINFO_);
161  }
162 
163  _lines.clear();
164  load(ifile);
165  ifile.close();
166  }
167 
185  // Return an empty header if we don't have one
186  if((!_header) || (_skip >= rows())) {
187  return (CSVAxis(0));
188  }
189  return (Parser(_lines[_skip], _delimiter, _keepParts).result());
190  }
191 
205  // Return an empty header if we don't have one
206  if((index < 0) || (index >= rows())) {
207  return (CSVAxis(0));
208  }
209  return (Parser(_lines[index+firstRowIndex()], _delimiter, _keepParts).result());
210  }
211 
212 
235  // Return an empty header if we don't have one
236  if(index < 0) {
237  return (CSVAxis(0));
238  }
239 
240  int nrows(rows());
241  int nbad(0);
242  CSVAxis column(nrows);
243  Parser parser;
244  for(int i = 0 ; i < nrows ; i++) {
246  if(parser.size() <= index) {
247 // column[i] = Parser::TokenType("");
248  nbad++;
249  }
250  else {
251  column[i] = parser(index);
252  }
253  }
254 
255  // If we had no good columns (index is invalid) return an empty column
256  return ((nbad == nrows) ? CSVAxis(0) : column);
257  }
258 
279  CSVReader::CSVAxis CSVReader::getColumn(const QString &hname) const {
280  // Get the header
281  CSVAxis header(getHeader());
282  QString head = hname.trimmed();
283  for(int i = 0 ; i < header.dim() ; i++) {
284  if(head.toLower() == header[i].trimmed().toLower()) {
285  return (getColumn(i));
286  }
287  }
288 
289  // If we reach here, we did not find the column name
290  return (CSVAxis(0));
291  }
292 
293 
319  CSVTable table(rows());
320  int nrows(rows());
321  Parser parser;
322  for(int row = 0 ; row < nrows ; row++) {
324  table[row] = parser.result();
325  }
326  return (table);
327  }
328 
361  const {
362  CSVColumnSummary summary;
363  for(int row = 0 ; row < table.dim() ; row++) {
364  int n(table[row].dim());
365  if(summary.exists(n)) {
366  int &count = summary.get(n);
367  count++;
368  }
369  else {
370  summary.add(n, 1);
371  }
372  }
373 
374  return (summary);
375  }
376 
388  bool CSVReader::isTableValid(const CSVReader::CSVTable &table) const {
389  CSVColumnSummary summary = getColumnSummary(table);
390  return (summary.size() <= 1);
391  }
392 
418  std::istream &CSVReader::load(std::istream &ifile) {
419 
420  std::string iline;
421  int nlines(0);
422  while(getline(ifile, iline)) {
423  if(!iline.empty()) {
424  if(!(_ignoreComments && (iline[0] == '#'))) {
425  _lines.push_back(iline.c_str());
426  nlines++;
427  }
428  }
429  }
430 
431  if(!ifile.eof()) {
432  ostringstream mess;
433  mess << "Error reading line " << (nlines + 1) << ends;
434  throw IException(IException::User, mess.str(), _FILEINFO_);
435  }
436 
437  return (ifile);
438  }
439 
463  std::istream &operator>>(std::istream &is, CSVReader &csv) {
464  return (csv.load(is));
465  }
466 
467 }