CSV::Reader Class Reference

Class to read a CSV file. More...

#include <Reader.h>

Inheritance diagram for CSV::Reader:
Collaboration diagram for CSV::Reader:

Public Member Functions

 Reader (IDataSourceStream *source, const Options &options, const CStringArray &headings=nullptr)
 Construct a CSV reader. More...
 
 Reader (IDataSourceStream *source, char fieldSeparator=',', const CStringArray &headings=nullptr, uint16_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) const
 Get a value from the current row. More...
 
const char * getValue (const char *name) const
 Get a value from the current row. More...
 
int getColumn (const char *name) const
 Get index of column given its name. More...
 
 operator bool () const
 Determine if reader is valid. More...
 
const CStringArraygetHeadings () const
 Get headings. More...
 
bool seek (int offset)
 Set reader to previously noted position. More...
 
bool seek (const Cursor &cursor)
 
const CStringArraygetRow () const
 Get current row. More...
 
const CursorgetCursor () const
 Get cursor position for current row. More...
 
int tell () const
 Get cursor position for current row. More...
 

Detailed Description

Class to read a CSV file.

This class

See also
See Parser for details

Constructor & Destructor Documentation

◆ Reader() [1/2]

CSV::Reader::Reader ( IDataSourceStream source,
const Options options,
const CStringArray headings = nullptr 
)

Construct a CSV reader.

Parameters
sourceStream to read CSV text from, reader takes ownership
options
headingsRequired if source data does not contain field headings as first row

◆ Reader() [2/2]

CSV::Reader::Reader ( IDataSourceStream source,
char  fieldSeparator = ',',
const CStringArray headings = nullptr,
uint16_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 CSV::Reader::count ( ) const
inline

Get number of columns.

◆ getColumn()

int CSV::Reader::getColumn ( const char *  name) const
inline

Get index of column given its name.

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

◆ getCursor()

const Cursor& CSV::Parser::getCursor
inline

Get cursor position for current row.

◆ getHeadings()

const CStringArray& CSV::Reader::getHeadings ( ) const
inline

Get headings.

◆ getRow()

const CStringArray& CSV::Parser::getRow
inline

Get current row.

◆ getValue() [1/2]

const char* CSV::Reader::getValue ( const char *  name) const
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* CSV::Reader::getValue ( unsigned  index) const
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 CSV::Reader::next ( )
inline

Seek to next record.

Return values
booltrue on success, false if there are no more records

◆ operator bool()

CSV::Reader::operator bool ( ) const
inlineexplicit

Determine if reader is valid.

◆ reset()

void CSV::Reader::reset ( )
inline

Reset reader to start of CSV file.

Cursor is set to 'before start'. Call 'next()' to fetch first record.

◆ seek() [1/2]

bool CSV::Reader::seek ( const Cursor cursor)
inline

◆ seek() [2/2]

bool CSV::Reader::seek ( int  offset)

Set reader to previously noted position.

Parameters
offsetValue obtained via tell() or Cursor::start
Return values
booltrue on success, false on failure or end of records
Note
Source stream must support random seeking (seekFrom)

If cursor is BOF then there will be no current record until next() is called. This is the same as if next() were called.

Otherwise the corresponding row will be available via getRow().

◆ tell()

int CSV::Parser::tell
inline

Get cursor position for current row.

The returned value indicates source stream offset for start of current row. After construction cursor is set to -1. This indicates 'Before first record' (BOF).


The documentation for this class was generated from the following file: