Class to parse a CSV file. More...

#include <Parser.h>

Inheritance diagram for CSV::Parser:
Collaboration diagram for CSV::Parser:

Classes

struct  Options
 Parsing options. More...
 

Public Member Functions

 Parser (const Options &options)
 Construct a CSV parser. More...
 
bool push (Stream &source)
 Read a single data row, taking data if required from provided Stream. More...
 
bool push (const char *data, size_t length, size_t &offset)
 Read a single data row, taking data if required from provided buffer. More...
 
bool flush ()
 Call to read additional rows after all data pushed. More...
 
bool readRow (IDataSourceStream &source)
 Read a single data row using data from provided DataSourceStream. More...
 
void reset (int offset=BOF)
 Reset parser to initial conditions. More...
 
const CStringArraygetRow () const
 Get current row. More...
 
int tell () const
 Get cursor position for current row. More...
 
const CursorgetCursor () const
 Get cursor position for current row. More...
 
unsigned getStreamPos () const
 Get stream position where next record will be read from. More...
 
const OptionsgetOptions () const
 

Static Public Attributes

static constexpr int BOF {-1}
 Indicates 'Before First Record'. 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 quoted fields can be converted:
    \r \t ", \
  • Field separator can be changed in constructor
  • Comment lines can be read and returned or discarded

This is a 'push' parser so can handle source data of indefinite size.

Constructor & Destructor Documentation

◆ Parser()

CSV::Parser::Parser ( const Options options)
inline

Construct a CSV parser.

Parameters
options

Member Function Documentation

◆ flush()

bool CSV::Parser::flush ( )

Call to read additional rows after all data pushed.

Return values
booltrue if record available, false otherwise.
Note
Call repeatedly until returns false

◆ getCursor()

const Cursor& CSV::Parser::getCursor ( ) const
inline

Get cursor position for current row.

◆ getOptions()

const Options& CSV::Parser::getOptions ( ) const
inline

◆ getRow()

const CStringArray& CSV::Parser::getRow ( ) const
inline

Get current row.

◆ getStreamPos()

unsigned CSV::Parser::getStreamPos ( ) const
inline

Get stream position where next record will be read from.

◆ push() [1/2]

bool CSV::Parser::push ( const char *  data,
size_t  length,
size_t &  offset 
)

Read a single data row, taking data if required from provided buffer.

Parameters
dataBuffer containing data to read
lengthNumber of characters in data
offsetRead offset in buffer, updated on return
Return values
booltrue if record available, false otherwise.
Note
Call flush() after all data pushed

◆ push() [2/2]

bool CSV::Parser::push ( Stream source)

Read a single data row, taking data if required from provided Stream.

Parameters
source
Return values
booltrue if record available, false otherwise
Note
Call flush() after all data pushed

◆ readRow()

bool CSV::Parser::readRow ( IDataSourceStream source)

Read a single data row using data from provided DataSourceStream.

Return values
boolfalse when there are no more rows
Note
Returns false only on error or when source.isFinished() returns true.

◆ reset()

void CSV::Parser::reset ( int  offset = BOF)

Reset parser to initial conditions.

Parameters
offsetInitial location for cursor
Note
Used by Reader when seeking

◆ tell()

int CSV::Parser::tell ( ) const
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).

Member Data Documentation

◆ BOF

constexpr int CSV::Parser::BOF {-1}
staticconstexpr

Indicates 'Before First Record'.


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