Isis 3 Developer Reference
CSVReader.h
Go to the documentation of this file.
1 #ifndef CSVReader_h
2 #define CSVReader_h
3 
26 #include <QString>
27 #include <vector>
28 #include <fstream>
29 #include <tnt/tnt_array1d.h>
30 #include "CollectorMap.h"
31 #include "IException.h"
32 #include "IString.h"
33 
34 namespace Isis {
35 
84  template <typename TokenStore = QString>
85  class CSVParser {
86  public:
87  typedef TokenStore TokenType;
88  typedef TNT::Array1D<TokenType> TokenList;
89 
91  CSVParser() { }
93  virtual ~CSVParser() { }
94 
108  CSVParser(const QString &str, const char &delimiter = ',',
109  bool keepEmptyParts = true) {
110  parse(str, delimiter, keepEmptyParts);
111  }
112 
117  int size() const {
118  return (_elements.dim());
119  }
120 
132  const TokenType &operator()(const int nth) const {
133  return (_elements[nth]);
134  }
135 
153  int parse(const QString &str, const char &delimiter = ',',
154  bool keepEmptyParts = true) {
155  QStringList tokens =
156  str.split(delimiter, keepEmptyParts? QString::KeepEmptyParts : QString::SkipEmptyParts);
157  TokenList slist(tokens.size());
158  for(int i = 0 ; i < tokens.size() ; i++) {
159  slist[i] = TokenType(tokens[i]);
160  }
161  _elements = slist;
162  return (_elements.dim());
163  }
164 
172  TokenList result() const {
173  return (_elements);
174  }
175 
176  private:
177  TokenList _elements;
178  };
179 
180 
181 
255  class CSVReader {
256 
257  private:
258  typedef CSVParser<QString> Parser;
259 
260  public:
261  friend std::istream &operator>>(std::istream &is, CSVReader &csv);
262 
264  typedef TNT::Array1D<CSVAxis> CSVTable;
266 
267  typedef TNT::Array1D<double> CSVDblVector;
268  typedef TNT::Array1D<int> CSVIntVector;
269 
270  // Constructors and Destructor
271  CSVReader();
276  CSVReader(const QString &csvfile, bool header = false, int skip = 0,
277  const char &delimiter = ',', const bool keepEmptyParts = true,
278  const bool ignoreComments = true);
279 
281  virtual ~CSVReader() { }
282 
287  int size() const {
288  return (_lines.size());
289  }
290 
301  int rows() const {
302  int nrows(_lines.size() - firstRowIndex());
303  return ((nrows < 0) ? 0 : nrows);
304  }
305 
306  int columns() const;
307  int columns(const CSVTable &table) const;
308 
329  void setComment(const bool ignore = true) {
330  _ignoreComments = ignore;
331  }
332 
351  void setSkip(int nskip) {
352  if(nskip >= 0) _skip = nskip;
353  }
354 
363  int getSkip() const {
364  return (_skip);
365  }
366 
375  bool haveHeader() const {
376  return (_header);
377  }
378 
399  void setHeader(const bool gotIt = true) {
400  _header = gotIt;
401  }
402 
403 
416  void setDelimiter(const char &delimiter) {
417  _delimiter = delimiter;
418  }
419 
425  char getDelimiter() const {
426  return (_delimiter);
427  }
428 
438  _keepParts = true;
439  }
440 
452  _keepParts = false;
453  }
454 
461  bool keepEmptyParts() const {
462  return (_keepParts);
463  }
464 
465  void read(const QString &fname);
466 
467  CSVAxis getHeader() const;
468  CSVAxis getRow(int index) const;
469  CSVAxis getColumn(int index) const;
470  CSVAxis getColumn(const QString &hname) const;
471  CSVTable getTable() const;
472  bool isTableValid(const CSVTable &table) const;
473 
474  CSVColumnSummary getColumnSummary(const CSVTable &table) const;
475 
476  template <typename T> TNT::Array1D<T> convert(const CSVAxis &data) const;
477 
484  void clear() {
485  _lines.clear();
486  }
487 
488  private:
489  typedef std::vector<QString> CSVList;
490  bool _header;
491  int _skip;
492  char _delimiter;
493  bool _keepParts;
494  CSVList _lines;
495  bool _ignoreComments;
496 
506  int firstRowIndex() const {
507  return (_skip + ((_header) ? 1 : 0));
508  }
509 
510  std::istream &load(std::istream &ifile);
511  };
512 
513 
547  template <typename T>
548  TNT::Array1D<T> CSVReader::convert(const CSVAxis &data) const {
549  TNT::Array1D<T> out(data.dim());
550  for(int i = 0 ; i < data.dim() ; i++) {
551  Parser::TokenType s = data[i];
552  out[i] = toDouble(s);
553  }
554  return (out);
555  }
556 }
557 
558 
559 #endif
CSVTable getTable() const
Parse and return all rows and columns in a table array.
Definition: CSVReader.cpp:318
CSV Parser seperates fields (tokens) from a string with a delimeter.
Definition: CSVReader.h:85
CSVColumnSummary getColumnSummary(const CSVTable &table) const
Computes a row summary of the number of distinct columns in table.
Definition: CSVReader.cpp:360
void read(const QString &fname)
Reads the entire contents of a file for subsequent parsing.
Definition: CSVReader.cpp:156
TokenList result() const
Returns the list of tokens.
Definition: CSVReader.h:172
int parse(const QString &str, const char &delimiter=',', bool keepEmptyParts=true)
Parser method accepting string, delimiter and multiple token handling.
Definition: CSVReader.h:153
void setKeepEmptyParts()
Indicate multiple occurances of delimiters are empty tokens.
Definition: CSVReader.h:437
int size() const
Reports the total number of lines read from the stream.
Definition: CSVReader.h:287
void setSkip(int nskip)
Indicate the number of lines at the top of the source to skip to data.
Definition: CSVReader.h:351
double toDouble(const QString &string)
Global function to convert from a string to a double.
Definition: IString.cpp:164
char getDelimiter() const
Reports the character used to delimit tokens in strings.
Definition: CSVReader.h:425
TNT::Array1D< double > CSVDblVector
Double array def.
Definition: CSVReader.h:267
TokenStore TokenType
Token storage type.
Definition: CSVReader.h:87
virtual ~CSVReader()
Destructor (benign)
Definition: CSVReader.h:281
TNT::Array1D< TokenType > TokenList
List of tokens.
Definition: CSVReader.h:88
int getSkip() const
Reports the number of lines to skip.
Definition: CSVReader.h:363
CSVParser()
Default constructor.
Definition: CSVReader.h:91
int rows() const
Reports the number of rows in the table.
Definition: CSVReader.h:301
int size() const
Returns the number of tokens in the parsed string.
Definition: CSVReader.h:117
CSVAxis getRow(int index) const
Parse and return the requested row by index.
Definition: CSVReader.cpp:204
CSVAxis getColumn(int index) const
Parse and return a column specified by index order.
Definition: CSVReader.cpp:234
Parser::TokenList CSVAxis
Row/Column token list.
Definition: CSVReader.h:263
void setHeader(const bool gotIt=true)
Allows the user to indicate header disposition.
Definition: CSVReader.h:399
Reads strings and parses them into tokens separated by a delimiter character.
Definition: CSVReader.h:255
int columns() const
Determine the number of columns in the input source.
Definition: CSVReader.cpp:113
Collector/container for arbitrary items.
Definition: CollectorMap.h:435
bool isTableValid(const CSVTable &table) const
Indicates if all rows have the same number of columns.
Definition: CSVReader.cpp:388
void setComment(const bool ignore=true)
Allows the user to indicate comment disposition.
Definition: CSVReader.h:329
CSVParser(const QString &str, const char &delimiter=',', bool keepEmptyParts=true)
Constructor that parses strings according to given parameters.
Definition: CSVReader.h:108
bool haveHeader() const
Returns true if a header is present in the input source.
Definition: CSVReader.h:375
void clear()
Discards all lines read from an input source.
Definition: CSVReader.h:484
friend std::istream & operator>>(std::istream &is, CSVReader &csv)
Input read operator for input stream sources.
Definition: CSVReader.cpp:463
void setSkipEmptyParts()
Indicate multiple occurances of delimiters are one token.
Definition: CSVReader.h:451
CSVReader()
Default constructor for CSV reader.
Definition: CSVReader.cpp:51
void setDelimiter(const char &delimiter)
Set the delimiter character that separate tokens in the strings.
Definition: CSVReader.h:416
virtual ~CSVParser()
Destructor.
Definition: CSVReader.h:93
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
CollectorMap< int, int > CSVColumnSummary
Column summary for all rows.
Definition: CSVReader.h:265
TNT::Array1D< CSVAxis > CSVTable
Table of all rows/columns.
Definition: CSVReader.h:264
bool keepEmptyParts() const
Returns true when preserving succesive tokens, false when they are treated as one token...
Definition: CSVReader.h:461
const TokenType & operator()(const int nth) const
Returns the nth token in the parsed string.
Definition: CSVReader.h:132
CSVAxis getHeader() const
Retrieve the header from the input source if it exists.
Definition: CSVReader.cpp:184
TNT::Array1D< T > convert(const CSVAxis &data) const
Converts a row or column of data to the specified type.
Definition: CSVReader.h:548
TNT::Array1D< int > CSVIntVector
Integer array def.
Definition: CSVReader.h:268