AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::csv::Writer Class Reference

#include <writer.hpp>

Collaboration diagram for anch::csv::Writer:

Public Member Functions

 Writer (std::ostream &output, const anch::csv::Options &options=anch::csv::Options())
 Writer (const Writer &)=delete
void write (std::span< const std::string_view > fields)
void write (std::initializer_list< std::string_view > fields)
template<typename... V>
void writeRow (const V &... values)
void write (std::span< const std::string_view > fields, const std::vector< bool > &quoted)
const anch::csv::Options & options () const noexcept

Detailed Description

CSV records writer (RFC 4180): fields which contain the separator, the quote, CR or LF are quoted (quotes are doubled), as a first field which begins with an UTF-8 BOM. Records end with CRLF (or LF , Options::crlf ). The header is written as a record ( Options::header is not used by the writer) ; an UTF-8 BOM is written by the constructor with Options::bom .

anch::csv::Writer writer(file);
writer.write({"id", "name", "comment"});
writer.writeRow(1, "Rex", "good, dog"); // "good, dog" is quoted
writer.writeRow(2, "Médor", "says \"woof\""); // quotes are doubled
Author
Vincent Lachenal
Since
0.3

Constructor & Destructor Documentation

◆ Writer() [1/2]

anch::csv::Writer::Writer ( std::ostream & output,
const anch::csv::Options & options = anch::csv::Options() )

Writer constructor

Parameters
outputthe output stream
optionsthe options
Exceptions
std::invalid_argumentwhen separator and quote are equal, or one of them is CR or LF
Here is the call graph for this function:

◆ Writer() [2/2]

anch::csv::Writer::Writer ( const Writer & )
delete

Forbids Writer copy constructor

Here is the call graph for this function:

Member Function Documentation

◆ options()

const anch::csv::Options & anch::csv::Writer::options ( ) const
noexcept

Options getter

Returns
the options

◆ write() [1/3]

void anch::csv::Writer::write ( std::initializer_list< std::string_view > fields)

Write a record

Parameters
fieldsthe fields

◆ write() [2/3]

void anch::csv::Writer::write ( std::span< const std::string_view > fields)

Write a record

Parameters
fieldsthe fields

◆ write() [3/3]

void anch::csv::Writer::write ( std::span< const std::string_view > fields,
const std::vector< bool > & quoted )

Write a record whose fields can be quoted even without special character: an empty quoted field ( "" ) is an empty string, an empty field an absent value

Parameters
fieldsthe fields
quotedthe fields to quote (same size as fields )

◆ writeRow()

template<typename... V>
void anch::csv::Writer::writeRow ( const V &... values)

Write a record of values: strings as is, bool as true / false , numbers with std::to_chars

Parameters
valuesthe values

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