|
Isis 3 Programmer Reference
|
13 #include "CSVReader.h"
14 #include "CollectorMap.h"
15 #include "IException.h"
35 CSVReader::CSVReader() : _header(false), _skip(0),
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());
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));
CSVAxis getColumn(int index) const
Parse and return a column specified by index order.
Parser::TokenList CSVAxis
Row/Column token list.
int columns() const
Determine the number of columns in the input source.
bool isTableValid(const CSVTable &table) const
Indicates if all rows have the same number of columns.
Reads strings and parses them into tokens separated by a delimiter character.
CSVAxis getRow(int index) const
Parse and return the requested row by index.
T & get(const K &key)
Returns the value associated with the name provided.
CSVReader()
Default constructor for CSV reader.
int size() const
Returns the size of the collection.
CSVAxis getHeader() const
Retrieve the header from the input source if it exists.
TNT::Array1D< CSVAxis > CSVTable
Table of all rows/columns.
bool _ignoreComments
Ignore comments on read.
std::istream & load(std::istream &ifile)
Reads all lines from the input stream until an EOF is encoutered.
TokenList result() const
Returns the list of tokens.
bool exists(const K &key) const
Checks the existance of a particular key in the list.
const K & key(int nth) const
Returns the nth key in the collection.
CSVTable getTable() const
Parse and return all rows and columns in a table array.
int _skip
Number of lines to skip.
CSVList _lines
List of lines from file.
int firstRowIndex() const
Computes the index of the first data.
void read(const QString &fname)
Reads the entire contents of a file for subsequent parsing.
CSVColumnSummary getColumnSummary(const CSVTable &table) const
Computes a row summary of the number of distinct columns in table.
void add(const K &key, const T &value)
Adds the element to the list.
Namespace for the standard library.
Collector/container for arbitrary items.
bool _keepParts
Keep empty parts between delimiter.
bool _header
Indicates presences of header.
CSVParser< QString > Parser
Defines single line parser.
CSV Parser seperates fields (tokens) from a string with a delimeter.
char _delimiter
Separator of values.
int parse(const QString &str, const char &delimiter=',', bool keepEmptyParts=true)
Parser method accepting string, delimiter and multiple token handling.
std::istream & operator>>(std::istream &is, CSVReader &csv)
Input read operator for input stream sources.
int size() const
Returns the number of tokens in the parsed string.
This is free and unencumbered software released into the public domain.
int rows() const
Reports the number of rows in the table.
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....