csvpp 1.2.0
CSV parsing / writing libraries
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Attributes | List of all members
csv::Reader Class Reference

Parses CSV data. More...

#include <csv.hpp>

Collaboration diagram for csv::Reader:
Collaboration graph
[legend]

Classes

struct  input_string_t
 Disambiguation tag type. More...
 
class  Iterator
 Iterates over Rows in CSV data. More...
 
class  Row
 Represents a single row of CSV data. More...
 

Public Member Functions

 Reader (std::istream &input_stream, const char delimiter=',', const char quote='"', const bool lenient = false)
 Use a std::istream for CSV parsing.
 
 Reader (const std::string &filename, const char delimiter=',', const char quote='"', const bool lenient = false)
 Open a file for CSV parsing.
 
 Reader (input_string_t, const std::string &input_data, const char delimiter=',', const char quote='"', const bool lenient = false)
 Parse CSV from memory.
 
 Reader (const Reader &)=delete
 
Readeroperator= (const Reader &)=delete
 
 Reader (Reader &&)=default
 
Readeroperator= (Reader &&)=default
 
bool end_of_row () const
 Check for end of input.
 
bool eof () const
 Check for end of row.
 
 operator bool ()
 
void set_delimiter (const char delimiter)
 Change the delimiter character.
 
void set_quote (const char quote)
 Change the quote character.
 
void set_lenient (const bool lenient)
 Enable / disable lenient parsing.
 
Iterator begin ()
 
auto end ()
 
template<typename T = std::string>
read_field ()
 Read a single field.
 
template<typename T >
Readeroperator>> (T &data)
 Read a single field.
 
template<typename ... Data>
void read_v (Data &... data)
 Reads fields into variadic arguments.
 
Row get_row ()
 Get the current Row.
 
template<typename T = std::string, typename OutputIter >
bool read_row (OutputIter it)
 Reads current row into an output iterator.
 
template<typename T = std::string>
std::optional< std::vector< T > > read_row_vec ()
 Reads current row into a std::vector.
 
template<typename ... Args>
std::optional< std::tuple< Args... > > read_row_tuple ()
 Reads current row into a tuple.
 
template<typename T = std::string>
std::vector< std::vector< T > > read_all ()
 Read entire CSV data into a vector of vectors.
 

Static Public Attributes

static constexpr input_string_t input_string {}
 Disambiguation tag.
 

Detailed Description

Parses CSV data.

By default, parses according to RFC 4180 rules, and throws a Parse_error when given non-conformant input. The field delimiter and quote characters may be changed, and there is a lenient parsing option to ignore violations.

Blank rows are ignored and skipped over.

Most methods operate on rows, but some read field-by-field. Mixing row-wise and field-wise methods is not recommended, but is possible. Row-wise methods will act as if the current position is the start of a row, regardless of any fields that have been read from the current row so far.

Constructor & Destructor Documentation

◆ Reader() [1/3]

csv::Reader::Reader ( std::istream & input_stream,
const char delimiter = ',',
const char quote = '"',
const bool lenient = false )
inlineexplicit

Use a std::istream for CSV parsing.

Parameters
input_streamstd::istream to read from
delimiterDelimiter character
quoteQuote character
lenientEnable lenient parsing (will attempt to read past syntax errors)
Warning
input_stream must not be destroyed or read from during the lifetime of this Reader

◆ Reader() [2/3]

csv::Reader::Reader ( const std::string & filename,
const char delimiter = ',',
const char quote = '"',
const bool lenient = false )
inlineexplicit

Open a file for CSV parsing.

Parameters
filenamePath to a file to parse
delimiterDelimiter character
quoteQuote character
lenientEnable lenient parsing (will attempt to read past syntax errors)
Exceptions
IO_errorif there is an error opening the file

◆ Reader() [3/3]

csv::Reader::Reader ( input_string_t ,
const std::string & input_data,
const char delimiter = ',',
const char quote = '"',
const bool lenient = false )
inline

Parse CSV from memory.

Use Reader::input_string to distinguish this constructor from the constructor accepting a filename

Parameters
input_datastd::string containing CSV to parse
delimiterDelimiter character
quoteQuote character
lenientEnable lenient parsing (will attempt to read past syntax errors)

Member Function Documentation

◆ begin()

Iterator csv::Reader::begin ( )
inline
Returns
Iterator to current row

◆ end()

auto csv::Reader::end ( )
inline
Returns
Iterator to end of CSV data

◆ end_of_row()

bool csv::Reader::end_of_row ( ) const
inline

Check for end of input.

Returns
true if the last field in the row has been read

◆ eof()

bool csv::Reader::eof ( ) const
inline

Check for end of row.

Returns
true if no fields remain in the current row

◆ get_row()

Row csv::Reader::get_row ( )
inline

Get the current Row.

Returns
Row object for the current row

◆ operator bool()

Returns
true if there is more data to be read

◆ operator>>()

template<typename T >
Reader & csv::Reader::operator>> ( T & data)
inline

Read a single field.

Check end_of_row() to see if this is the last field in the current row

Parameters
[out]dataVariable to to write field to
Returns
This Reader object
Exceptions
Parse_errorif error parsing field (only when not parsing in lenient mode)
IO_errorif error reading CSV data
Type_conversion_errorif error converting to type T. Caller may call this again with a different type to try again

◆ read_all()

template<typename T = std::string>
std::vector< std::vector< T > > csv::Reader::read_all ( )
inline

Read entire CSV data into a vector of vectors.

Template Parameters
TType to convert fields to. Defaults to std::string
Returns
2D vector of CSV data.
Exceptions
Parse_errorif error parsing field (only when not parsing in lenient mode)
IO_errorif error reading CSV data
Type_conversion_errorif error converting to type T

◆ read_field()

template<typename T = std::string>
T csv::Reader::read_field ( )
inline

Read a single field.

Check end_of_row() to see if this is the last field in the current row

Template Parameters
TType to convert fields to. Defaults to std::string
Returns
The next field from the row, or a default-initialized object if past the end of the input data
Exceptions
Parse_errorif error parsing field (only when not parsing in lenient mode)
IO_errorif error reading CSV data
Type_conversion_errorif error converting to type T. Caller may call this again with a different type to try again

◆ read_row()

template<typename T = std::string, typename OutputIter >
bool csv::Reader::read_row ( OutputIter it)
inline

Reads current row into an output iterator.

Template Parameters
TType to convert fields to. Defaults to std::string
Parameters
itAn output iterator to receive the row data
Returns
false if this is the last row
Exceptions
Parse_errorif error parsing field (only when not parsing in lenient mode)
IO_errorif error reading CSV data
Type_conversion_errorif error converting to type T

◆ read_row_tuple()

template<typename ... Args>
std::optional< std::tuple< Args... > > csv::Reader::read_row_tuple ( )
inline

Reads current row into a tuple.

Template Parameters
Argstypes to convert fields to
Returns
std::tuple containing the fields from the row or empty optional if no rows remain. If Args contains more elements than there are fields in the row, the remaining elements of the tuple will be default initialized
Exceptions
Parse_errorif error parsing field (only when not parsing in lenient mode)
IO_errorif error reading CSV data
Type_conversion_errorif error converting to specified types

◆ read_row_vec()

template<typename T = std::string>
std::optional< std::vector< T > > csv::Reader::read_row_vec ( )
inline

Reads current row into a std::vector.

Template Parameters
TType to convert fields to. Defaults to std::string
Returns
std::vector containing the fields from the row or empty optional if no rows remain
Exceptions
Parse_errorif error parsing field (only when not parsing in lenient mode)
IO_errorif error reading CSV data
Type_conversion_errorif error converting to type T

◆ read_v()

template<typename ... Data>
void csv::Reader::read_v ( Data &... data)
inline

Reads fields into variadic arguments.

Warning
This may be used to fields from multiple rows at a time. Use with caution if the number of fields per row is not known beforehand.
Parameters
[out]dataVariables to read into. If more parameters are passed than there are fields remaining, the remaining parameters will be default initialized
Exceptions
Parse_errorif error parsing field (only when not parsing in lenient mode)
IO_errorif error reading CSV data
Type_conversion_errorif error converting to specified types

◆ set_delimiter()

void csv::Reader::set_delimiter ( const char delimiter)
inline

Change the delimiter character.

Parameters
delimiterNew delimiter character

◆ set_lenient()

void csv::Reader::set_lenient ( const bool lenient)
inline

Enable / disable lenient parsing.

Lenient parsing will attempt to ignore syntax errors in CSV input.

Parameters
lenienttrue for lenient parsing

◆ set_quote()

void csv::Reader::set_quote ( const char quote)
inline

Change the quote character.

Parameters
quoteNew quote character

Member Data Documentation

◆ input_string

constexpr input_string_t csv::Reader::input_string {}
inlinestaticconstexpr

Disambiguation tag.

Distinguishes opening a Reader with a filename from opening a Reader with a string


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