51 CSVReader::CSVReader() : _header(false), _skip(0),
52 _delimiter(
','), _keepParts(true), _lines(),
53 _ignoreComments(true) { }
82 const char &delimiter,
const bool keepEmptyParts,
83 const bool ignoreComments) :
84 _header(header), _skip(skip), _delimiter(delimiter),
85 _keepParts(keepEmptyParts), _lines(),
86 _ignoreComments(ignoreComments) {
135 return ((summary.
size() > 0) ? summary.
key(0) : 0);
157 ifstream ifile(csvfile.toLatin1().data(), ios::in);
159 QString mess =
"Unable to open file " + csvfile;
206 if((index < 0) || (index >=
rows())) {
244 for(
int i = 0 ; i < nrows ; i++) {
246 if(parser.
size() <= index) {
251 column[i] = parser(index);
256 return ((nbad == nrows) ?
CSVAxis(0) : column);
282 QString head = hname.trimmed();
283 for(
int i = 0 ; i < header.dim() ; i++) {
284 if(head.toLower() == header[i].trimmed().toLower()) {
322 for(
int row = 0 ; row < nrows ; row++) {
324 table[row] = parser.
result();
363 for(
int row = 0 ; row < table.dim() ; row++) {
364 int n(table[row].dim());
366 int &count = summary.
get(n);
390 return (summary.
size() <= 1);
422 while(getline(ifile, iline)) {
425 _lines.push_back(iline.c_str());
433 mess <<
"Error reading line " << (nlines + 1) << ends;
464 return (csv.
load(is));
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.
const K & key(int nth) const
Returns the nth key in the collection.
CSV Parser seperates fields (tokens) from a string with a delimeter.
bool exists(const K &key) const
Checks the existance of a particular key in the list.
int rows() const
Reports the number of rows in the table.
T & get(const K &key)
Returns the value associated with the name provided.
bool isTableValid(const CSVTable &table) const
Indicates if all rows have the same number of columns.
void read(const QString &fname)
Reads the entire contents of a file for subsequent parsing.
CSVAxis getColumn(int index) const
Parse and return a column specified by index order.
int _skip
Number of lines to skip.
CSVParser< QString > Parser
Defines single line parser.
int size() const
Returns the number of tokens in the parsed string.
CSVAxis getHeader() const
Retrieve the header from the input source if it exists.
bool _keepParts
Keep empty parts between delimiter.
bool _header
Indicates presences of header.
int parse(const QString &str, const char &delimiter= ',', bool keepEmptyParts=true)
Parser method accepting string, delimiter and multiple token handling.
char _delimiter
Separator of values.
CSVTable getTable() const
Parse and return all rows and columns in a table array.
int columns() const
Determine the number of columns in the input source.
CSVAxis getRow(int index) const
Parse and return the requested row by index.
#define _FILEINFO_
Macro for the filename and line number.
void add(const K &key, const T &value)
Adds the element to the list.
std::istream & operator>>(std::istream &is, CSVReader &csv)
Input read operator for input stream sources.
A type of error that could only have occurred due to a mistake on the user's part (e...
Parser::TokenList CSVAxis
Row/Column token list.
int firstRowIndex() const
Computes the index of the first data.
Reads strings and parses them into tokens separated by a delimiter character.
Collector/container for arbitrary items.
CSVReader()
Default constructor for CSV reader.
int size() const
Returns the size of the collection.
CSVColumnSummary getColumnSummary(const CSVTable &table) const
Computes a row summary of the number of distinct columns in table.
TokenList result() const
Returns the list of tokens.
TNT::Array1D< CSVAxis > CSVTable
Table of all rows/columns.
bool _ignoreComments
Ignore comments on read.