csvpp 1.2.0
CSV parsing / writing libraries
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
csv::Map_reader_iter< Header, Value > Class Template Reference

Map-based Reader iterator. More...

#include <csv.hpp>

Public Types

using value_type = std::map<Header, Value>
 
using difference_type = std::ptrdiff_t
 
using pointer = const value_type*
 
using reference = const value_type&
 
using iterator_category = std::input_iterator_tag
 

Public Member Functions

 Map_reader_iter ()
 Empty constructor.
 
 Map_reader_iter (std::istream &input_stream, const Value &default_val={}, const std::vector< Header > &headers={}, const char delimiter=',', const char quote='"', const bool lenient = false)
 Open a std::istream for CSV parsing.
 
 Map_reader_iter (const std::string &filename, const Value &default_val={}, const std::vector< Header > &headers={}, const char delimiter=',', const char quote='"', const bool lenient = false)
 Open a file for CSV parsing.
 
 Map_reader_iter (Reader::input_string_t, const std::string &input_data, const Value &default_val={}, const std::vector< Header > &headers={}, const char delimiter=',', const char quote='"', const bool lenient = false)
 Parse CSV from memory.
 
const value_type & operator* () const
 
value_type & operator* ()
 
const value_type * operator-> () const
 
value_type * operator-> ()
 
const value_type::mapped_type & operator[] (const typename value_type::key_type &key) const
 Get a field from the current row.
 
value_type::mapped_type & operator[] (const typename value_type::key_type &key)
 
Map_reader_iteroperator++ ()
 Iterate to next field.
 
template<typename Header2 , typename Value2 >
bool equals (const Map_reader_iter< Header2, Value2 > &rhs) const
 Compare to another Map_reader_iter.
 
std::vector< Header > get_headers () const
 Get the headers.
 

Detailed Description

template<typename Header = std::string, typename Value = std::string>
class csv::Map_reader_iter< Header, Value >

Map-based Reader iterator.

Iterates through a Reader, returning rows as a std::map. Map keys (headers) come from the first row unless specified by the header parameter to the constructor. If a row has more fields than the header, Out_of_range_error error will be thrown

Constructor & Destructor Documentation

◆ Map_reader_iter() [1/4]

template<typename Header = std::string, typename Value = std::string>
csv::Map_reader_iter< Header, Value >::Map_reader_iter ( )
inline

Empty constructor.

Denotes the end of iteration

◆ Map_reader_iter() [2/4]

template<typename Header = std::string, typename Value = std::string>
csv::Map_reader_iter< Header, Value >::Map_reader_iter ( std::istream & input_stream,
const Value & default_val = {},
const std::vector< Header > & headers = {},
const char delimiter = ',',
const char quote = '"',
const bool lenient = false )
inlineexplicit

Open a std::istream for CSV parsing.

Parameters
input_streamstd::istream to read from
default_valValue to use if a row has fewer fields than the header
headersList of field names to use. Pass an empty vector to use first (header) row
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 Map_reader_iter
Exceptions
Parse_errorif error parsing fields (only when not parsing in lenient mode)
IO_errorif error reading CSV data
Type_conversion_errorif error converting 1st row to type Header (if headers param is empty), or 1st row to type Value (if header param is specified)

◆ Map_reader_iter() [3/4]

template<typename Header = std::string, typename Value = std::string>
csv::Map_reader_iter< Header, Value >::Map_reader_iter ( const std::string & filename,
const Value & default_val = {},
const std::vector< Header > & headers = {},
const char delimiter = ',',
const char quote = '"',
const bool lenient = false )
inlineexplicit

Open a file for CSV parsing.

Parameters
filenamePath to a file to parse
default_valValue to use if a row has fewer fields than the header
headersList of field names to use. Pass an empty vector to use first (header) row
delimiterDelimiter character
quoteQuote character
lenientEnable lenient parsing (will attempt to read past syntax errors)
Exceptions
Parse_errorif error parsing fields (only when not parsing in lenient mode)
IO_errorif error reading CSV data
Type_conversion_errorif error converting 1st row to type Header (if headers param is empty), or 1st row to type Value (if header param is specified)

◆ Map_reader_iter() [4/4]

template<typename Header = std::string, typename Value = std::string>
csv::Map_reader_iter< Header, Value >::Map_reader_iter ( Reader::input_string_t ,
const std::string & input_data,
const Value & default_val = {},
const std::vector< Header > & headers = {},
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_dataString containing CSV to parse
default_valValue to use if a row has fewer fields than the header
headersList of field names to use. Pass an empty vector to use first (header) row
delimiterDelimiter character
quoteQuote character
lenientEnable lenient parsing (will attempt to read past syntax errors)
Exceptions
Parse_errorif error parsing fields (only when not parsing in lenient mode)
IO_errorif error reading CSV data
Type_conversion_errorif error converting 1st row to type Header (if headers param not specified), or 1st row to type Value (if header param is specified)

Member Function Documentation

◆ get_headers()

template<typename Header = std::string, typename Value = std::string>
std::vector< Header > csv::Map_reader_iter< Header, Value >::get_headers ( ) const
inline

Get the headers.

Returns
Headers as a vector

◆ operator*()

template<typename Header = std::string, typename Value = std::string>
const value_type & csv::Map_reader_iter< Header, Value >::operator* ( ) const
inline
Returns
Current row as a map

◆ operator++()

template<typename Header = std::string, typename Value = std::string>
Map_reader_iter & csv::Map_reader_iter< Header, Value >::operator++ ( )
inline

Iterate to next field.

Exceptions
Parse_errorif error parsing fields (only when not parsing in lenient mode)
IO_errorif error reading CSV data
Type_conversion_errorif error converting fields to Value type

◆ operator->()

template<typename Header = std::string, typename Value = std::string>
const value_type * csv::Map_reader_iter< Header, Value >::operator-> ( ) const
inline
Returns
Pointer to current row map

◆ operator[]()

template<typename Header = std::string, typename Value = std::string>
const value_type::mapped_type & csv::Map_reader_iter< Header, Value >::operator[] ( const typename value_type::key_type & key) const
inline

Get a field from the current row.

This is a shortcut for map_reader_iter->at(key)

Parameters
keyHeader for the desired field
Returns
The requested field
Exceptions
std::out_or_rangeif the key is not a valid header

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