Isis Developer Reference
CSVReader.h
Go to the documentation of this file.
1 #ifndef CSVReader_h
2 #define CSVReader_h
3 
8 /* SPDX-License-Identifier: CC0-1.0 */
9 
10 #include <QString>
11 #include <vector>
12 #include <fstream>
13 #include <tnt/tnt_array1d.h>
14 #include "CollectorMap.h"
15 #include "IException.h"
16 #include "IString.h"
17 
18 namespace Isis {
19 
68  template <typename TokenStore = QString>
69  class CSVParser {
70  public:
71  typedef TokenStore TokenType;
72  typedef TNT::Array1D<TokenType> TokenList;
73 
75  CSVParser() { }
77  virtual ~CSVParser() { }
78 
92  CSVParser(const QString &str, const char &delimiter = ',',
93  bool keepEmptyParts = true) {
94  parse(str, delimiter, keepEmptyParts);
95  }
96 
101  int size() const {
102  return (_elements.dim());
103  }
104 
116  const TokenType &operator()(const int nth) const {
117  return (_elements[nth]);
118  }
119 
137  int parse(const QString &str, const char &delimiter = ',',
138  bool keepEmptyParts = true) {
139  QStringList tokens =
140  str.split(delimiter, keepEmptyParts? QString::KeepEmptyParts : QString::SkipEmptyParts);
141  TokenList slist(tokens.size());
142  for(int i = 0 ; i < tokens.size() ; i++) {
143  slist[i] = TokenType(tokens[i]);
144  }
145  _elements = slist;
146  return (_elements.dim());
147  }
148 
156  TokenList result() const {
157  return (_elements);
158  }
159 
160  private:
161  TokenList _elements;
162  };
163 
164 
165 
239  class CSVReader {
240 
241  private:
242  typedef CSVParser<QString> Parser;
243 
244  public:
245  friend std::istream &operator>>(std::istream &is, CSVReader &csv);
246 
248  typedef TNT::Array1D<CSVAxis> CSVTable;
250 
251  typedef TNT::Array1D<double> CSVDblVector;
252  typedef TNT::Array1D<int> CSVIntVector;
253 
254  // Constructors and Destructor
255  CSVReader();
260  CSVReader(const QString &csvfile, bool header = false, int skip = 0,
261  const char &delimiter = ',', const bool keepEmptyParts = true,
262  const bool ignoreComments = true);
263 
265  virtual ~CSVReader() { }
266 
271  int size() const {
272  return (_lines.size());
273  }
274 
285  int rows() const {
286  int nrows(_lines.size() - firstRowIndex());
287  return ((nrows < 0) ? 0 : nrows);
288  }
289 
290  int columns() const;
291  int columns(const CSVTable &table) const;
292 
313  void setComment(const bool ignore = true) {
314  _ignoreComments = ignore;
315  }
316 
335  void setSkip(int nskip) {
336  if(nskip >= 0) _skip = nskip;
337  }
338 
347  int getSkip() const {
348  return (_skip);
349  }
350 
359  bool haveHeader() const {
360  return (_header);
361  }
362 
383  void setHeader(const bool gotIt = true) {
384  _header = gotIt;
385  }
386 
387 
400  void setDelimiter(const char &delimiter) {
401  _delimiter = delimiter;
402  }
403 
409  char getDelimiter() const {
410  return (_delimiter);
411  }
412 
422  _keepParts = true;
423  }
424 
436  _keepParts = false;
437  }
438 
445  bool keepEmptyParts() const {
446  return (_keepParts);
447  }
448 
449  void read(const QString &fname);
450 
451  CSVAxis getHeader() const;
452  CSVAxis getRow(int index) const;
453  CSVAxis getColumn(int index) const;
454  CSVAxis getColumn(const QString &hname) const;
455  CSVTable getTable() const;
456  bool isTableValid(const CSVTable &table) const;
457 
458  CSVColumnSummary getColumnSummary(const CSVTable &table) const;
459 
460  template <typename T> TNT::Array1D<T> convert(const CSVAxis &data) const;
461 
468  void clear() {
469  _lines.clear();
470  }
471 
472  private:
473  typedef std::vector<QString> CSVList;
474  bool _header;
475  int _skip;
476  char _delimiter;
477  bool _keepParts;
478  CSVList _lines;
479  bool _ignoreComments;
480 
490  int firstRowIndex() const {
491  return (_skip + ((_header) ? 1 : 0));
492  }
493 
494  std::istream &load(std::istream &ifile);
495  };
496 
497 
531  template <typename T>
532  TNT::Array1D<T> CSVReader::convert(const CSVAxis &data) const {
533  TNT::Array1D<T> out(data.dim());
534  for(int i = 0 ; i < data.dim() ; i++) {
535  Parser::TokenType s = data[i];
536  out[i] = toDouble(s);
537  }
538  return (out);
539  }
540 }
541 
542 
543 #endif
Isis::CSVReader::getColumn
CSVAxis getColumn(int index) const
Parse and return a column specified by index order.
Definition: CSVReader.cpp:218
Isis::CSVReader::CSVAxis
Parser::TokenList CSVAxis
Row/Column token list.
Definition: CSVReader.h:247
Isis::CSVReader::columns
int columns() const
Determine the number of columns in the input source.
Definition: CSVReader.cpp:97
Isis::CSVReader::isTableValid
bool isTableValid(const CSVTable &table) const
Indicates if all rows have the same number of columns.
Definition: CSVReader.cpp:372
Isis::CSVReader::setComment
void setComment(const bool ignore=true)
Allows the user to indicate comment disposition.
Definition: CSVReader.h:313
Isis::CSVReader
Reads strings and parses them into tokens separated by a delimiter character.
Definition: CSVReader.h:239
Isis::CSVReader::getRow
CSVAxis getRow(int index) const
Parse and return the requested row by index.
Definition: CSVReader.cpp:188
Isis::CSVReader::haveHeader
bool haveHeader() const
Returns true if a header is present in the input source.
Definition: CSVReader.h:359
Isis::CSVReader::setHeader
void setHeader(const bool gotIt=true)
Allows the user to indicate header disposition.
Definition: CSVReader.h:383
Isis::CSVReader::clear
void clear()
Discards all lines read from an input source.
Definition: CSVReader.h:468
Isis::CSVParser::TokenList
TNT::Array1D< TokenType > TokenList
List of tokens.
Definition: CSVReader.h:72
CSVReader.h
Isis::CSVParser::~CSVParser
virtual ~CSVParser()
Destructor.
Definition: CSVReader.h:77
Isis::CSVReader::setDelimiter
void setDelimiter(const char &delimiter)
Set the delimiter character that separate tokens in the strings.
Definition: CSVReader.h:400
Isis::CollectorMap::get
T & get(const K &key)
Returns the value associated with the name provided.
Definition: CollectorMap.h:567
Isis::CSVParser::operator()
const TokenType & operator()(const int nth) const
Returns the nth token in the parsed string.
Definition: CSVReader.h:116
Isis::CSVReader::setSkipEmptyParts
void setSkipEmptyParts()
Indicate multiple occurances of delimiters are one token.
Definition: CSVReader.h:435
Isis::CSVReader::CSVReader
CSVReader()
Default constructor for CSV reader.
Definition: CSVReader.cpp:35
Isis::CSVReader::operator>>
friend std::istream & operator>>(std::istream &is, CSVReader &csv)
Input read operator for input stream sources.
Definition: CSVReader.cpp:447
Isis::CollectorMap::size
int size() const
Returns the size of the collection.
Definition: CollectorMap.h:512
Isis::CSVReader::getHeader
CSVAxis getHeader() const
Retrieve the header from the input source if it exists.
Definition: CSVReader.cpp:168
QStringList
Isis::CSVReader::convert
TNT::Array1D< T > convert(const CSVAxis &data) const
Converts a row or column of data to the specified type.
Definition: CSVReader.h:532
Isis::CSVReader::CSVColumnSummary
CollectorMap< int, int > CSVColumnSummary
Column summary for all rows.
Definition: CSVReader.h:249
Isis::CSVReader::CSVTable
TNT::Array1D< CSVAxis > CSVTable
Table of all rows/columns.
Definition: CSVReader.h:248
IString.h
Isis::CSVReader::keepEmptyParts
bool keepEmptyParts() const
Returns true when preserving succesive tokens, false when they are treated as one token.
Definition: CSVReader.h:445
Isis::CSVParser::CSVParser
CSVParser(const QString &str, const char &delimiter=',', bool keepEmptyParts=true)
Constructor that parses strings according to given parameters.
Definition: CSVReader.h:92
Isis::CSVParser::result
TokenList result() const
Returns the list of tokens.
Definition: CSVReader.h:156
Isis::CollectorMap::exists
bool exists(const K &key) const
Checks the existance of a particular key in the list.
Definition: CollectorMap.h:551
Isis::CollectorMap::key
const K & key(int nth) const
Returns the nth key in the collection.
Definition: CollectorMap.h:673
Isis::CSVReader::getTable
CSVTable getTable() const
Parse and return all rows and columns in a table array.
Definition: CSVReader.cpp:302
_FILEINFO_
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:24
Isis::CSVReader::CSVIntVector
TNT::Array1D< int > CSVIntVector
Integer array def.
Definition: CSVReader.h:252
Isis::CSVReader::read
void read(const QString &fname)
Reads the entire contents of a file for subsequent parsing.
Definition: CSVReader.cpp:140
Isis::CSVReader::getColumnSummary
CSVColumnSummary getColumnSummary(const CSVTable &table) const
Computes a row summary of the number of distinct columns in table.
Definition: CSVReader.cpp:344
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::CSVReader::setKeepEmptyParts
void setKeepEmptyParts()
Indicate multiple occurances of delimiters are empty tokens.
Definition: CSVReader.h:421
Isis::CollectorMap::add
void add(const K &key, const T &value)
Adds the element to the list.
Definition: CollectorMap.h:540
Isis::CSVReader::size
int size() const
Reports the total number of lines read from the stream.
Definition: CSVReader.h:271
IException.h
Isis::toDouble
double toDouble(const QString &string)
Global function to convert from a string to a double.
Definition: IString.cpp:149
std
Namespace for the standard library.
CollectorMap.h
Isis::CSVReader::setSkip
void setSkip(int nskip)
Indicate the number of lines at the top of the source to skip to data.
Definition: CSVReader.h:335
Isis::CollectorMap
Collector/container for arbitrary items.
Definition: CollectorMap.h:419
Isis::CSVReader::getDelimiter
char getDelimiter() const
Reports the character used to delimit tokens in strings.
Definition: CSVReader.h:409
Isis::CSVParser::CSVParser
CSVParser()
Default constructor
Definition: CSVReader.h:75
Isis::CSVParser
CSV Parser seperates fields (tokens) from a string with a delimeter.
Definition: CSVReader.h:69
Isis::CSVReader::CSVDblVector
TNT::Array1D< double > CSVDblVector
Double array def.
Definition: CSVReader.h:251
Isis::CSVParser::parse
int parse(const QString &str, const char &delimiter=',', bool keepEmptyParts=true)
Parser method accepting string, delimiter and multiple token handling.
Definition: CSVReader.h:137
Isis::operator>>
std::istream & operator>>(std::istream &is, CSVReader &csv)
Input read operator for input stream sources.
Definition: CSVReader.cpp:447
Isis::CSVParser::size
int size() const
Returns the number of tokens in the parsed string.
Definition: CSVReader.h:101
Isis::CSVReader::~CSVReader
virtual ~CSVReader()
Destructor (benign)
Definition: CSVReader.h:265
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::CSVReader::rows
int rows() const
Reports the number of rows in the table.
Definition: CSVReader.h:285
Isis::IException::User
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition: IException.h:126
Isis::CSVParser::TokenType
TokenStore TokenType
Token storage type.
Definition: CSVReader.h:71
Isis::CSVReader::getSkip
int getSkip() const
Reports the number of lines to skip.
Definition: CSVReader.h:347