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

CSV writer. More...

#include <csv.hpp>

Classes

class  Iterator
 Output iterator for writing CSV data field-by-field. More...
 

Public Member Functions

 Writer (std::ostream &output_stream, const char delimiter=',', const char quote='"')
 Use a std::ostream for CSV output.
 
 Writer (const std::string &filename, const char delimiter=',', const char quote='"')
 Open a file for CSV output.
 
 ~Writer ()
 Destructor.
 
 Writer (const Writer &)=delete
 
 Writer (Writer &&)=default
 
Writeroperator= (const Writer &)=delete
 
Writeroperator= (Writer &&)=default
 
Iterator iterator ()
 Get iterator.
 
void set_delimiter (const char delimiter)
 Change the delimiter character.
 
void set_quote (const char quote)
 Change the quote character.
 
template<typename T >
void write_field (const T &field)
 Writes a field to the CSV output.
 
template<typename T >
Writeroperator<< (const T &field)
 Writes a field to the CSV output.
 
Writeroperator<< (Writer &(*manip)(Writer &))
 Apply a stream manipulator to the CSV output.
 
void end_row ()
 End the current row.
 
template<typename Iter >
void write_fields (Iter first, Iter last)
 
template<typename T >
void write_fields (const std::initializer_list< T > &data)
 
template<typename Range >
void write_fields (const Range &data)
 Write fields from a range, without ending the row.
 
template<typename ... Data>
void write_fields_v (const Data &...data)
 Write fields from the given variadic parameters, without ending the row.
 
template<typename ... Args>
void write_fields (const std::tuple< Args... > &data)
 Write fields from a tuple, without ending the row.
 
template<typename Iter >
void write_row (Iter first, Iter last)
 
template<typename T >
void write_row (const std::initializer_list< T > &data)
 
template<typename Range >
void write_row (const Range &data)
 Write a row from a range.
 
template<typename ... Data>
void write_row_v (const Data &...data)
 Write a row from the given variadic parameters.
 
template<typename ... Args>
void write_row (const std::tuple< Args... > &data)
 Write a row from a tuple.
 

Friends

Writerend_row (Writer &w)
 End row stream manipulator for Writer.
 

Detailed Description

CSV writer.

Writes data in CSV format, with correct escaping as needed, according to RFC 4180 rules. Allows writing by rows or field-by-field. Mixing these is not recommended, but is possible. Row-wise methods will append to the row started by any field-wise methods.

Constructor & Destructor Documentation

◆ Writer() [1/2]

csv::Writer::Writer ( std::ostream & output_stream,
const char delimiter = ',',
const char quote = '"' )
inlineexplicit

Use a std::ostream for CSV output.

Parameters
output_streamstd::ostream to write to
delimiterDelimiter character
quoteQuote character
Warning
output_stream must not be destroyed or written to during the lifetime of this Writer

◆ Writer() [2/2]

csv::Writer::Writer ( const std::string & filename,
const char delimiter = ',',
const char quote = '"' )
inlineexplicit

Open a file for CSV output.

Parameters
filenamePath to file to write to. Any existing file will be overwritten
delimiterDelimiter character
quoteQuote character
Warning
output_stream must not be destroyed or written to during the lifetime of this Writer
Exceptions
IO_errorif there is an error opening the file

◆ ~Writer()

csv::Writer::~Writer ( )
inline

Destructor.

Writes a final newline sequence if needed to close current row

Member Function Documentation

◆ end_row()

void csv::Writer::end_row ( )
inline

End the current row.

Exceptions
IO_errorif there is an error writing

◆ iterator()

Iterator csv::Writer::iterator ( )
inline

Get iterator.

Returns
An iterator on this Writer
Exceptions
IO_errorif there is an error writing

◆ operator<<() [1/2]

template<typename T >
Writer & csv::Writer::operator<< ( const T & field)
inline

Writes a field to the CSV output.

Parameters
fieldData to write. Type must be convertible to std::string either directly, by to_string, or by ostream::operator<<
Exceptions
IO_errorif there is an error writing

◆ operator<<() [2/2]

Writer & csv::Writer::operator<< ( Writer &(*)(Writer &) manip)
inline

Apply a stream manipulator to the CSV output.

Currently only csv::end_row is supported

Parameters
manipStream manipulator to apply

◆ set_delimiter()

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

Change the delimiter character.

Parameters
delimiterNew delimiter character

◆ set_quote()

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

Change the quote character.

Parameters
quoteNew quote character

◆ write_field()

template<typename T >
void csv::Writer::write_field ( const T & field)
inline

Writes a field to the CSV output.

Parameters
fieldData to write. Type must be convertible to std::string either directly, by to_string, or by ostream::operator<<
Exceptions
IO_errorif there is an error writing

◆ write_fields() [1/4]

template<typename Range >
void csv::Writer::write_fields ( const Range & data)
inline

Write fields from a range, without ending the row.

A range in this context must support std::begin and std::end as at least input iterators. Most STL containers, such as std::vector will work. The ranges type must be convertible to std::string either directly, by to_string, or by ostream::operator<<

Parameters
dataRange of fields to write
Exceptions
IO_errorif there is an error writing

◆ write_fields() [2/4]

template<typename T >
void csv::Writer::write_fields ( const std::initializer_list< T > & data)
inline

Write fields from an initializer_list, without ending the row

Template Parameters
TType of data. Must be convertible to std::string either directly, by to_string, or by ostream::operator<<
Parameters
datalist of fields to write
Exceptions
IO_errorif there is an error writing

◆ write_fields() [3/4]

template<typename ... Args>
void csv::Writer::write_fields ( const std::tuple< Args... > & data)
inline

Write fields from a tuple, without ending the row.

Parameters
datatuple of fields to write. Each element must be convertible to std::string either directly, by to_string, or by ostream::operator<<
Exceptions
IO_errorif there is an error writing

◆ write_fields() [4/4]

template<typename Iter >
void csv::Writer::write_fields ( Iter first,
Iter last )
inline

Write fields from iterators, without ending the row

Parameters
firstIterator to start of data to write. Dereferenced type must be convertible to std::string either directly, by to_string, or by ostream::operator<<
lastIterator to end of data to write
Exceptions
IO_errorif there is an error writing

◆ write_fields_v()

template<typename ... Data>
void csv::Writer::write_fields_v ( const Data &... data)
inline

Write fields from the given variadic parameters, without ending the row.

Parameters
dataFields to write. Each must be convertible to std::string either directly, by to_string, or by ostream::operator<<
Exceptions
IO_errorif there is an error writing

◆ write_row() [1/4]

template<typename Range >
void csv::Writer::write_row ( const Range & data)
inline

Write a row from a range.

A range in this context must support std::begin and std::end as at least input iterators. Most STL containers, such as std::vector will work. The ranges type must be convertible to std::string either directly, by to_string, or by ostream::operator<<

Parameters
dataRange of fields to write
Exceptions
IO_errorif there is an error writing

◆ write_row() [2/4]

template<typename T >
void csv::Writer::write_row ( const std::initializer_list< T > & data)
inline

Write a row from an initializer_list

Template Parameters
TType of data. Must be convertible to std::string either directly, by to_string, or by ostream::operator<<
Parameters
datalist of fields to write
Exceptions
IO_errorif there is an error writing

◆ write_row() [3/4]

template<typename ... Args>
void csv::Writer::write_row ( const std::tuple< Args... > & data)
inline

Write a row from a tuple.

Parameters
datatuple of fields to write. Each element must be convertible to std::string either directly, by to_string, or by ostream::operator<<
Exceptions
IO_errorif there is an error writing

◆ write_row() [4/4]

template<typename Iter >
void csv::Writer::write_row ( Iter first,
Iter last )
inline

Write a row from iterators

Parameters
firstIterator to start of data to write. Dereferenced type must be convertible to std::string either directly, by to_string, or by ostream::operator<<
lastIterator to end of data to write
Exceptions
IO_errorif there is an error writing

◆ write_row_v()

template<typename ... Data>
void csv::Writer::write_row_v ( const Data &... data)
inline

Write a row from the given variadic parameters.

Parameters
dataFields to write. Each must be convertible to std::string either directly, by to_string, or by ostream::operator<<
Exceptions
IO_errorif there is an error writing

Friends And Related Symbol Documentation

◆ end_row

Writer & end_row ( Writer & w)
friend

End row stream manipulator for Writer.

Ends the current row, as in: writer << field << csv::end_row;

Exceptions
IO_errorif there is an error writing

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