29 #include <tnt/tnt_array1d.h>
80 template <
typename TokenStore = QString>
104 CSVParser(
const QString &str,
const char &delimiter =
',',
105 bool keepEmptyParts =
true) {
106 parse(str, delimiter, keepEmptyParts);
111 return (_elements.dim());
126 return (_elements[nth]);
146 int parse(
const QString &str,
const char &delimiter =
',',
147 bool keepEmptyParts =
true) {
149 str.split(delimiter, keepEmptyParts? QString::KeepEmptyParts : QString::SkipEmptyParts);
151 for(
int i = 0 ; i < tokens.size() ; i++) {
155 return (_elements.dim());
265 CSVReader(
const QString &csvfile,
bool header =
false,
int skip = 0,
267 const bool ignoreComments =
true);
277 return (_lines.size());
291 int nrows(_lines.size() - firstRowIndex());
292 return ((nrows < 0) ? 0 : nrows);
319 _ignoreComments = ignore;
341 if(nskip >= 0) _skip = nskip;
405 _delimiter = delimiter;
452 void read(
const QString &fname);
463 template <
typename T> TNT::Array1D<T>
convert(
const CSVAxis &data)
const;
476 typedef std::vector<QString> CSVList;
482 bool _ignoreComments;
493 int firstRowIndex()
const {
494 return (_skip + ((_header) ? 1 : 0));
497 std::istream &load(std::istream &ifile);
534 template <
typename T>
536 TNT::Array1D<T> out(data.dim());
537 for(
int i = 0 ; i < data.dim() ; i++) {
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
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
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
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
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
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
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
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
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
TNT::Array1D< int > CSVIntVector
Integer array def.
Definition: CSVReader.h:261