Class to parse a CSV file. More...
#include <Parser.h>
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 CStringArray & | getRow () const |
| Get current row. More... | |
| int | tell () const |
| Get cursor position for current row. More... | |
| const Cursor & | getCursor () const |
| Get cursor position for current row. More... | |
| unsigned | getStreamPos () const |
| Get stream position where next record will be read from. More... | |
| const Options & | getOptions () 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
- 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 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()
Member Function Documentation
◆ flush()
| bool CSV::Parser::flush | ( | ) |
Call to read additional rows after all data pushed.
- Return values
-
bool true if record available, false otherwise.
- Note
- Call repeatedly until returns false
◆ getCursor()
|
inline |
Get cursor position for current row.
◆ getOptions()
|
inline |
◆ getRow()
|
inline |
Get current row.
◆ getStreamPos()
|
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
-
data Buffer containing data to read length Number of characters in data offset Read offset in buffer, updated on return
- Return values
-
bool true if record available, false otherwise.
- Note
- Call
flush()after all data pushed
◆ push() [2/2]
| bool CSV::Parser::push | ( | Stream & | source | ) |
◆ readRow()
| bool CSV::Parser::readRow | ( | IDataSourceStream & | source | ) |
Read a single data row using data from provided DataSourceStream.
- Return values
-
bool false 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
-
offset Initial location for cursor
- Note
- Used by Reader when seeking
◆ 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).
Member Data Documentation
◆ BOF
|
staticconstexpr |
Indicates 'Before First Record'.
The documentation for this class was generated from the following file:
1.9.1