14#include "CollectorMap.h"
15#include "IException.h"
36 _delimiter(
','), _keepParts(true), _lines(),
37 _ignoreComments(true) { }
66 const char &delimiter,
const bool keepEmptyParts,
67 const bool ignoreComments) :
68 _header(header), _skip(skip), _delimiter(delimiter),
69 _keepParts(keepEmptyParts), _lines(),
70 _ignoreComments(ignoreComments) {
119 return ((summary.size() > 0) ? summary.key(0) : 0);
141 ifstream ifile(csvfile.toLatin1().data(), ios::in);
143 QString mess =
"Unable to open file [" + csvfile +
"]";
190 if((index < 0) || (index >=
rows())) {
228 for(
int i = 0 ; i < nrows ; i++) {
230 if(parser.
size() <= index) {
235 column[i] = parser(index);
240 return ((nbad == nrows) ?
CSVAxis(0) : column);
266 QString head = hname.trimmed();
267 for(
int i = 0 ; i < header.dim() ; i++) {
268 if(head.toLower() == header[i].trimmed().toLower()) {
306 for(
int row = 0 ; row < nrows ; row++) {
308 table[row] = parser.
result();
347 for(
int row = 0 ; row < table.dim() ; row++) {
348 int n(table[row].dim());
349 if(summary.exists(n)) {
350 int &count = summary.get(n);
374 return (summary.size() <= 1);
406 while(getline(ifile, iline)) {
409 _lines.push_back(iline.c_str());
417 mess <<
"Error reading line [" << (nlines + 1) <<
"]" << ends;
448 return (csv.load(is));
CSV Parser seperates fields (tokens) from a string with a delimeter.
TokenList result() const
Returns the list of tokens.
int parse(const QString &str, const char &delimiter=',', bool keepEmptyParts=true)
Parser method accepting string, delimiter and multiple token handling.
int size() const
Returns the number of tokens in the parsed string.
Reads strings and parses them into tokens separated by a delimiter character.
void read(const QString &fname)
Reads the entire contents of a file for subsequent parsing.
bool isTableValid(const CSVTable &table) const
Indicates if all rows have the same number of columns.
CSVAxis getRow(int index) const
Parse and return the requested row by index.
CSVTable getTable() const
Parse and return all rows and columns in a table array.
int rows() const
Reports the number of rows in the table.
int firstRowIndex() const
Computes the index of the first data.
CSVColumnSummary getColumnSummary(const CSVTable &table) const
Computes a row summary of the number of distinct columns in table.
CollectorMap< int, int > CSVColumnSummary
Column summary for all rows.
CSVParser< QString > Parser
Defines single line parser.
CSVReader()
Default constructor for CSV reader.
std::istream & load(std::istream &ifile)
Reads all lines from the input stream until an EOF is encoutered.
CSVList _lines
List of lines from file.
char _delimiter
Separator of values.
int _skip
Number of lines to skip.
bool _ignoreComments
Ignore comments on read.
TNT::Array1D< CSVAxis > CSVTable
Table of all rows/columns.
Parser::TokenList CSVAxis
Row/Column token list.
bool _keepParts
Keep empty parts between delimiter.
CSVAxis getColumn(int index) const
Parse and return a column specified by index order.
int columns() const
Determine the number of columns in the input source.
bool _header
Indicates presences of header.
CSVAxis getHeader() const
Retrieve the header from the input source if it exists.
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
This is free and unencumbered software released into the public domain.
std::istream & operator>>(std::istream &is, CSVReader &csv)
Input read operator for input stream sources.
Namespace for the standard library.