CsvReader Class Reference
Class to parse a CSV file. More...
#include <CsvReader.h>
Public Member Functions | |
CsvReader (IDataSourceStream *source, char fieldSeparator=',', const CStringArray &headings=nullptr, size_t maxLineLength=2048) | |
Construct a CSV reader. More... | |
void | reset () |
Reset reader to start of CSV file. More... | |
bool | next () |
Seek to next record. More... | |
unsigned | count () const |
Get number of columns. More... | |
const char * | getValue (unsigned index) |
Get a value from the current row. More... | |
const char * | getValue (const char *name) |
Get a value from the current row. More... | |
int | getColumn (const char *name) |
Get index of column given its name. More... | |
operator bool () const | |
Determine if row is valid. More... | |
const CStringArray & | getHeadings () const |
Get headings. More... | |
const CStringArray & | getRow () const |
Get current row. More... | |
Detailed Description
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
Constructor & Destructor Documentation
◆ CsvReader()
|
inline |
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
Member Function Documentation
◆ count()
|
inline |
Get number of columns.
◆ getColumn()
|
inline |
Get index of column given its name.
- Parameters
-
name Column name to find
- Return values
-
int -1 if name is not found
◆ getHeadings()
|
inline |
Get headings.
◆ getRow()
|
inline |
Get current row.
◆ getValue() [1/2]
|
inline |
Get a value from the current row.
- Parameters
-
index Column name
- Return values
-
const char* nullptr if name is not found
◆ getValue() [2/2]
|
inline |
Get a value from the current row.
- Parameters
-
index Column index, starts at 0
- Return values
-
const char* nullptr if index is not valid
◆ next()
|
inline |
Seek to next record.
◆ operator bool()
|
inlineexplicit |
Determine if row is valid.
◆ reset()
void CsvReader::reset | ( | ) |
Reset reader to start of CSV file.
Cursor is set to 'before start'. Call 'next()' to fetch first record.
The documentation for this class was generated from the following file: