Vince's CSV Parser
csv::DelimWriter< OutputStream, Delim, Quote, Flush > Class Template Reference

Class for writing delimiter separated values files. More...

#include <csv_writer.hpp>

Public Member Functions

 DelimWriter (OutputStream &_out, bool _quote_minimal=true)
 Construct a DelimWriter over the specified output stream. More...
 
 DelimWriter (const std::string &filename)
 Construct a DelimWriter over the file. More...
 
 ~DelimWriter ()
 Destructor will flush remaining data.
 
template<typename T , size_t Size>
DelimWriteroperator<< (const std::array< T, Size > &record)
 Format a sequence of strings and write to CSV according to RFC 4180. More...
 
template<typename... T>
DelimWriteroperator<< (const std::tuple< T... > &record)
 Format a sequence of strings and write to CSV according to RFC 4180. More...
 
template<typename T , typename Alloc , template< typename, typename > class Container, csv::enable_if_t< std::is_class< Alloc >::value, int > = 0>
DelimWriteroperator<< (const Container< T, Alloc > &record)
 Format a sequence of strings and write to CSV according to RFC 4180. More...
 
void flush ()
 Flushes the written data.
 

Detailed Description

template<class OutputStream, char Delim, char Quote, bool Flush>
class csv::DelimWriter< OutputStream, Delim, Quote, Flush >

Class for writing delimiter separated values files.

To write formatted strings, one should

  1. Initialize a DelimWriter with respect to some output stream
  2. Call write_row() on std::vector<std::string>s of unformatted text
Template Parameters
OutputStreamThe output stream, e.g. std::ofstream, std::stringstream
DelimThe delimiter character
QuoteThe quote character
FlushTrue: flush after every writing function, false: you need to flush explicitly if needed. In both cases the destructor will flush.
Hint
Use the aliases csv::CSVWriter<OutputStream> to write CSV formatted strings and csv::TSVWriter<OutputStream> to write tab separated strings
Example w/ std::vector, std::deque, std::list
TEMPLATE_TEST_CASE("CSV/TSV Writer - operator <<", "[test_csv_operator<<]",
std::vector<std::string>, std::deque<std::string>, std::list<std::string>) {
std::stringstream output, correct_comma, correct_tab;
// Build correct strings
correct_comma << "A,B,C" << std::endl << "\"1,1\",2,3" << std::endl;
correct_tab << "A\tB\tC" << std::endl << "1,1\t2\t3" << std::endl;
// Test input
auto test_row_1 = TestType({ "A", "B", "C" }),
test_row_2 = TestType({ "1,1", "2", "3" });
SECTION("CSV Writer") {
auto csv_writer = make_csv_writer(output);
csv_writer << test_row_1 << test_row_2;
REQUIRE(output.str() == correct_comma.str());
}
SECTION("TSV Writer") {
auto tsv_writer = make_tsv_writer(output);
tsv_writer << test_row_1 << test_row_2;
REQUIRE(output.str() == correct_tab.str());
}
}
CSVWriter< OutputStream > make_csv_writer(OutputStream &out, bool quote_minimal=true)
Return a csv::CSVWriter over the output stream.
Definition: csv_writer.hpp:390
TSVWriter< OutputStream > make_tsv_writer(OutputStream &out, bool quote_minimal=true)
Return a csv::TSVWriter over the output stream.
Definition: csv_writer.hpp:402
Example w/ std::tuple
struct Time {
std::string hour;
std::string minute;
operator std::string() const {
std::string ret = hour;
ret += ":";
ret += minute;
return ret;
}
};
#ifndef __clang__
TEST_CASE("CSV Tuple", "[test_csv_tuple]") {
#ifdef CSV_HAS_CXX17
Time time = { "5", "30" };
#else
std::string time = "5:30";
#endif
std::stringstream output, correct_output;
auto csv_writer = make_csv_writer(output);
csv_writer << std::make_tuple("One", 2, "Three", 4.0, time)
<< std::make_tuple("One", (short)2, "Three", 4.0f, time)
<< std::make_tuple(-1, -2.0)
<< std::make_tuple(20.2, -20.3, -20.123)
<< std::make_tuple(0.0, 0.0f, 0);
correct_output << "One,2,Three,4.0,5:30" << std::endl
<< "One,2,Three,4.0,5:30" << std::endl
<< "-1,-2.0" << std::endl
<< "20.19999,-20.30000,-20.12300" << std::endl
<< "0.0,0.0,0" << std::endl;
REQUIRE(output.str() == correct_output.str());
}
#endif

Definition at line 193 of file csv_writer.hpp.

Constructor & Destructor Documentation

◆ DelimWriter() [1/2]

template<class OutputStream , char Delim, char Quote, bool Flush>
csv::DelimWriter< OutputStream, Delim, Quote, Flush >::DelimWriter ( OutputStream &  _out,
bool  _quote_minimal = true 
)
inline

Construct a DelimWriter over the specified output stream.

Parameters
_outStream to write to
_quote_minimalLimit field quoting to only when necessary

Definition at line 201 of file csv_writer.hpp.

◆ DelimWriter() [2/2]

template<class OutputStream , char Delim, char Quote, bool Flush>
csv::DelimWriter< OutputStream, Delim, Quote, Flush >::DelimWriter ( const std::string &  filename)
inline

Construct a DelimWriter over the file.

Parameters
[out]filenameFile to write to

Definition at line 208 of file csv_writer.hpp.

Member Function Documentation

◆ operator<<() [1/3]

template<class OutputStream , char Delim, char Quote, bool Flush>
template<typename T , typename Alloc , template< typename, typename > class Container, csv::enable_if_t< std::is_class< Alloc >::value, int > = 0>
DelimWriter& csv::DelimWriter< OutputStream, Delim, Quote, Flush >::operator<< ( const Container< T, Alloc > &  record)
inline

Format a sequence of strings and write to CSV according to RFC 4180.

Template Parameters
TA container such as std::vector, std::deque, or std::list
Warning
This does not check to make sure row lengths are consistent
Parameters
[in]recordSequence of strings to be formatted
Returns
The current DelimWriter instance (allowing for operator chaining)

Definition at line 254 of file csv_writer.hpp.

◆ operator<<() [2/3]

template<class OutputStream , char Delim, char Quote, bool Flush>
template<typename T , size_t Size>
DelimWriter& csv::DelimWriter< OutputStream, Delim, Quote, Flush >::operator<< ( const std::array< T, Size > &  record)
inline

Format a sequence of strings and write to CSV according to RFC 4180.

Warning
This does not check to make sure row lengths are consistent
Parameters
[in]recordSequence of strings to be formatted
Returns
The current DelimWriter instance (allowing for operator chaining)

Definition at line 226 of file csv_writer.hpp.

◆ operator<<() [3/3]

template<class OutputStream , char Delim, char Quote, bool Flush>
template<typename... T>
DelimWriter& csv::DelimWriter< OutputStream, Delim, Quote, Flush >::operator<< ( const std::tuple< T... > &  record)
inline

Format a sequence of strings and write to CSV according to RFC 4180.

Warning
This does not check to make sure row lengths are consistent
Parameters
[in]recordSequence of strings to be formatted
Returns
The current DelimWriter instance (allowing for operator chaining)

Definition at line 238 of file csv_writer.hpp.


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