Isis 3.0 Programmer Reference
Back | Home
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 
80  template <typename TokenStore = QString>
81  class CSVParser {
82  public:
83  typedef TokenStore TokenType;
84  typedef TNT::Array1D<TokenType> TokenList;
85 
87  CSVParser() { }
89  virtual ~CSVParser() { }
90 
104  CSVParser(const QString &str, const char &delimiter = ',',
105  bool keepEmptyParts = true) {
106  parse(str, delimiter, keepEmptyParts);
107  }
108 
110  int size() const {
111  return (_elements.dim());
112  }
113 
125  const TokenType &operator()(const int nth) const {
126  return (_elements[nth]);
127  }
128 
146  int parse(const QString &str, const char &delimiter = ',',
147  bool keepEmptyParts = true) {
148  QStringList tokens =
149  str.split(delimiter, keepEmptyParts? QString::KeepEmptyParts : QString::SkipEmptyParts);
150  TokenList slist(tokens.size());
151  for(int i = 0 ; i < tokens.size() ; i++) {
152  slist[i] = TokenType(tokens[i]);
153  }
154  _elements = slist;
155  return (_elements.dim());
156  }
157 
165  TokenList result() const {
166  return (_elements);
167  }
168 
169  private:
171  };
172 
173 
174 
248  class CSVReader {
249 
250  private:
252 
253  public:
254  friend std::istream &operator>>(std::istream &is, CSVReader &csv);
255 
257  typedef TNT::Array1D<CSVAxis> CSVTable;
259 
260  typedef TNT::Array1D<double> CSVDblVector;
261  typedef TNT::Array1D<int> CSVIntVector;
262 
263  // Constructors and Destructor
264  CSVReader();
265  CSVReader(const QString &csvfile, bool header = false, int skip = 0,
266  const char &delimiter = ',', const bool keepEmptyParts = true,
267  const bool ignoreComments = true);
268 
270  virtual ~CSVReader() { }
271 
276  int size() const {
277  return (_lines.size());
278  }
279 
290  int rows() const {
291  int nrows(_lines.size() - firstRowIndex());
292  return ((nrows < 0) ? 0 : nrows);
293  }
294 
295  int columns() const;
296  int columns(const CSVTable &table) const;
297 
318  void setComment(const bool ignore = true) {
319  _ignoreComments = ignore;
320  }
321 
340  void setSkip(int nskip) {
341  if(nskip >= 0) _skip = nskip;
342  }
343 
352  int getSkip() const {
353  return (_skip);
354  }
355 
363  bool haveHeader() const {
364  return (_header);
365  }
366 
387  void setHeader(const bool gotIt = true) {
388  _header = gotIt;
389  }
390 
391 
404  void setDelimiter(const char &delimiter) {
405  _delimiter = delimiter;
406  }
407 
413  char getDelimiter() const {
414  return (_delimiter);
415  }
416 
426  _keepParts = true;
427  }
428 
440  _keepParts = false;
441  }
442 
448  bool keepEmptyParts() const {
449  return (_keepParts);
450  }
451 
452  void read(const QString &fname);
453 
454  CSVAxis getHeader() const;
455  CSVAxis getRow(int index) const;
456  CSVAxis getColumn(int index) const;
457  CSVAxis getColumn(const QString &hname) const;
458  CSVTable getTable() const;
459  bool isTableValid(const CSVTable &table) const;
460 
461  CSVColumnSummary getColumnSummary(const CSVTable &table) const;
462 
463  template <typename T> TNT::Array1D<T> convert(const CSVAxis &data) const;
464 
471  void clear() {
472  _lines.clear();
473  }
474 
475  private:
476  typedef std::vector<QString> CSVList;
477  bool _header;
478  int _skip;
479  char _delimiter;
480  bool _keepParts;
483 
493  int firstRowIndex() const {
494  return (_skip + ((_header) ? 1 : 0));
495  }
496 
497  std::istream &load(std::istream &ifile);
498  };
499 
500 
534  template <typename T>
535  TNT::Array1D<T> CSVReader::convert(const CSVAxis &data) const {
536  TNT::Array1D<T> out(data.dim());
537  for(int i = 0 ; i < data.dim() ; i++) {
538  Parser::TokenType s = data[i];
539  out[i] = toDouble(s);
540  }
541  return (out);
542  }
543 }
544 
545 
546 #endif
547 
std::istream & load(std::istream &ifile)
Reads all lines from the input stream until an EOF is encoutered.
Definition: CSVReader.cpp:418
CSVList _lines
List of lines from file.
Definition: CSVReader.h:481
CSV Parser seperates fields (tokens) from a string with a delimeter.
Definition: CSVReader.h:81
char getDelimiter() const
Reports the character used to delimit tokens in strings.
Definition: CSVReader.h:413
int rows() const
Reports the number of rows in the table.
Definition: CSVReader.h:290
bool isTableValid(const CSVTable &table) const
Indicates if all rows have the same number of columns.
Definition: CSVReader.cpp:388
void read(const QString &fname)
Reads the entire contents of a file for subsequent parsing.
Definition: CSVReader.cpp:156
CSVAxis getColumn(int index) const
Parse and return a column specified by index order.
Definition: CSVReader.cpp:234
int _skip
Number of lines to skip.
Definition: CSVReader.h:478
void setKeepEmptyParts()
Indicate multiple occurances of delimiters are empty tokens.
Definition: CSVReader.h:425
int size() const
Reports the total number of lines read from the stream.
Definition: CSVReader.h:276
CSVParser< QString > Parser
Defines single line parser.
Definition: CSVReader.h:251
int size() const
Returns the number of tokens in the parsed string.
Definition: CSVReader.h:110
bool keepEmptyParts() const
Returns true when preserving succesive tokens, false when they are treated as one token...
Definition: CSVReader.h:448
CSVAxis getHeader() const
Retrieve the header from the input source if it exists.
Definition: CSVReader.cpp:184
void setSkip(int nskip)
Indicate the number of lines at the top of the source to skip to data.
Definition: CSVReader.h:340
bool _keepParts
Keep empty parts between delimiter.
Definition: CSVReader.h:480
double toDouble(const QString &string)
Global function to convert from a string to a double.
Definition: IString.cpp:164
TNT::Array1D< double > CSVDblVector
Double array def.
Definition: CSVReader.h:260
bool _header
Indicates presences of header.
Definition: CSVReader.h:477
int parse(const QString &str, const char &delimiter= ',', bool keepEmptyParts=true)
Parser method accepting string, delimiter and multiple token handling.
Definition: CSVReader.h:146
TokenStore TokenType
Token storage type.
Definition: CSVReader.h:83
char _delimiter
Separator of values.
Definition: CSVReader.h:479
virtual ~CSVReader()
Destructor (benign)
Definition: CSVReader.h:270
TNT::Array1D< TokenType > TokenList
List of tokens.
Definition: CSVReader.h:84
CSVTable getTable() const
Parse and return all rows and columns in a table array.
Definition: CSVReader.cpp:318
CSVParser()
Default constructor.
Definition: CSVReader.h:87
bool haveHeader() const
Returns true if a header is present in the input source.
Definition: CSVReader.h:363
int getSkip() const
Reports the number of lines to skip.
Definition: CSVReader.h:352
int columns() const
Determine the number of columns in the input source.
Definition: CSVReader.cpp:113
CSVAxis getRow(int index) const
Parse and return the requested row by index.
Definition: CSVReader.cpp:204
TokenList _elements
List of tokens parsed from string.
Definition: CSVReader.h:170
Parser::TokenList CSVAxis
Row/Column token list.
Definition: CSVReader.h:256
void setHeader(const bool gotIt=true)
Allows the user to indicate header disposition.
Definition: CSVReader.h:387
int firstRowIndex() const
Computes the index of the first data.
Definition: CSVReader.h:493
Reads strings and parses them into tokens separated by a delimiter character.
Definition: CSVReader.h:248
Collector/container for arbitrary items.
Definition: CollectorMap.h:433
void setComment(const bool ignore=true)
Allows the user to indicate comment disposition.
Definition: CSVReader.h:318
void clear()
Discards all lines read from an input source.
Definition: CSVReader.h:471
friend std::istream & operator>>(std::istream &is, CSVReader &csv)
Input read operator for input stream sources.
Definition: CSVReader.cpp:463
TNT::Array1D< T > convert(const CSVAxis &data) const
Converts a row or column of data to the specified type.
Definition: CSVReader.h:535
void setSkipEmptyParts()
Indicate multiple occurances of delimiters are one token.
Definition: CSVReader.h:439
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:404
CSVParser(const QString &str, const char &delimiter= ',', bool keepEmptyParts=true)
Constructor that parses strings according to given parameters.
Definition: CSVReader.h:104
std::vector< QString > CSVList
Input source line container.
Definition: CSVReader.h:476
virtual ~CSVParser()
Destructor.
Definition: CSVReader.h:89
CSVColumnSummary getColumnSummary(const CSVTable &table) const
Computes a row summary of the number of distinct columns in table.
Definition: CSVReader.cpp:360
const TokenType & operator()(const int nth) const
Returns the nth token in the parsed string.
Definition: CSVReader.h:125
CollectorMap< int, int > CSVColumnSummary
Column summary for all rows.
Definition: CSVReader.h:258
TokenList result() const
Returns the list of tokens.
Definition: CSVReader.h:165
TNT::Array1D< CSVAxis > CSVTable
Table of all rows/columns.
Definition: CSVReader.h:257
bool _ignoreComments
Ignore comments on read.
Definition: CSVReader.h:482
TNT::Array1D< int > CSVIntVector
Integer array def.
Definition: CSVReader.h:261

U.S. Department of the Interior | U.S. Geological Survey
ISIS | Privacy & Disclaimers | Astrogeology Research Program
To contact us, please post comments and questions on the ISIS Support Center
File Modified: 07/12/2023 23:15:21