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 CStringArraygetHeadings () const
 Get headings. More...
 
const CStringArraygetRow () const
 Get current row. More...
 

Detailed Description

Class to parse a CSV file.

Spec: https://www.ietf.org/rfc/rfc4180.txt

  1. Each record is located on a separate line
  2. Line ending for last record in the file is optional
  3. Field headings are provided either in the source data or in constructor (but not both)
  4. Fields separated with ',' and whitespace considered part of field content
  5. Fields may or may not be quoted - if present, will be removed during parsing
  6. Fields may contain line breaks, quotes or commas
  7. 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()

CsvReader::CsvReader ( IDataSourceStream source,
char  fieldSeparator = ',',
const CStringArray headings = nullptr,
size_t  maxLineLength = 2048 
)
inline

Construct a CSV reader.

Parameters
sourceStream to read CSV text from
fieldSeparator
headingsRequired if source data does not contain field headings as first row
maxLineLengthLimit size of buffer to guard against malformed data

Member Function Documentation

◆ count()

unsigned CsvReader::count ( ) const
inline

Get number of columns.

◆ getColumn()

int CsvReader::getColumn ( const char *  name)
inline

Get index of column given its name.

Parameters
nameColumn name to find
Return values
int-1 if name is not found

◆ getHeadings()

const CStringArray& CsvReader::getHeadings ( ) const
inline

Get headings.

◆ getRow()

const CStringArray& CsvReader::getRow ( ) const
inline

Get current row.

◆ getValue() [1/2]

const char* CsvReader::getValue ( const char *  name)
inline

Get a value from the current row.

Parameters
indexColumn name
Return values
constchar* nullptr if name is not found

◆ getValue() [2/2]

const char* CsvReader::getValue ( unsigned  index)
inline

Get a value from the current row.

Parameters
indexColumn index, starts at 0
Return values
constchar* nullptr if index is not valid

◆ next()

bool CsvReader::next ( )
inline

Seek to next record.

◆ operator bool()

CsvReader::operator bool ( ) const
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: