CSV Reader
-
class CsvReader
Class to parse a CSV file.
Spec: https://www.ietf.org/rfc/rfc4180.txt
Each record is located on a separate line
Line ending for last record in the file is optional
Field headings are provided either in the source data or in constructor (but not both)
Fields separated with ‘,’ and whitespace considered part of field content
Fields may or may not be quoted - if present, will be removed during parsing
Fields may contain line breaks, quotes or commas
Quotes may be escaped thus “” if field itself is quoted
Additional features:
Line breaks can be
or \r
Escapes codes within fields will be converted:
\r \t “, \
Field separator can be changed in constructor
Public Functions
-
inline CsvReader(IDataSourceStream *source, char fieldSeparator = ',', const CStringArray &headings = nullptr, size_t maxLineLength = 2048)
Construct a CSV reader.
- Parameters:
source – Stream to read CSV text from
fieldSeparator –
headings – Required if source data does not contain field headings as first row
maxLineLength – Limit size of buffer to guard against malformed data
-
void reset()
Reset reader to start of CSV file.
Cursor is set to ‘before start’. Call ‘next()’ to fetch first record.
-
inline bool next()
Seek to next record.
-
inline unsigned count() const
Get number of columns.
-
inline const char *getValue(unsigned index)
Get a value from the current row.
- Parameters:
index – Column index, starts at 0
- Return values:
const – char* nullptr if index is not valid
-
inline const char *getValue(const char *name)
Get a value from the current row.
- Parameters:
index – Column name
- Return values:
const – char* nullptr if name is not found
-
inline int getColumn(const char *name)
Get index of column given its name.
- Parameters:
name – Column name to find
- Return values:
int – -1 if name is not found
-
inline explicit operator bool() const
Determine if row is valid.
-
inline const CStringArray &getHeadings() const
Get headings.
-
inline const CStringArray &getRow() const
Get current row.