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

#include <reader.hpp>

Collaboration diagram for anch::csv::Reader:

Public Member Functions

 Reader (std::istream &input, const anch::csv::Options &options=anch::csv::Options())
 Reader (const Reader &)=delete
bool next ()
std::optional< std::size_t > column (std::string_view name) const noexcept
std::span< const std::string_view > fields () const noexcept
std::size_t size () const noexcept
std::string_view operator[] (std::size_t index) const
bool quoted (std::size_t index) const
std::size_t line () const noexcept
const std::vector< std::string > & header () const noexcept
const anch::csv::Options & options () const noexcept

Detailed Description

CSV records reader (RFC 4180): records are read one by one from a stream, without loading the whole input.
Fields are std::string_view valid until the next call to next (the record buffer is reused: no allocation per field). Quoted fields can contain the separator, doubled quotes and line endings. CRLF and LF line endings are read (CR alone is refused), the last record can have no line ending and an UTF-8 BOM is skipped. An empty field and an empty quoted field ( "" ) are distinguished with quoted .
Errors raise CsvError with the line and the column of the error.

try {
anch::csv::Reader reader(file); // the header is read (Options::header)
const std::size_t name = reader.column("name").value();
while(reader.next()) { // one record at a time
std::cout << reader.line() << ": " << reader[name] << std::endl;
}
} catch(const anch::csv::CsvError& error) {
std::cerr << error.what() << std::endl; // line 3, column 2: quote in an unquoted field
return 1;
}
Author
Vincent Lachenal
Since
0.3

Constructor & Destructor Documentation

◆ Reader() [1/2]

anch::csv::Reader::Reader ( std::istream & input,
const anch::csv::Options & options = anch::csv::Options() )

Reader constructor: the header is read when Options::header is set

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

◆ Reader() [2/2]

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

Forbids Reader copy constructor

Here is the call graph for this function:

Member Function Documentation

◆ column()

std::optional< std::size_t > anch::csv::Reader::column ( std::string_view name) const
noexcept

Get column index of a header name

Parameters
namethe column name
Returns
the first column with this name, empty when it is not in header

◆ fields()

std::span< const std::string_view > anch::csv::Reader::fields ( ) const
noexcept

Current record fields

Returns
the fields, valid until the next call to next

◆ header()

const std::vector< std::string > & anch::csv::Reader::header ( ) const
noexcept

Header (empty without Options::header )

Returns
the column names
Here is the call graph for this function:

◆ line()

std::size_t anch::csv::Reader::line ( ) const
noexcept

Physical line where the current record begins (records can have line endings in quoted fields)

Returns
the line, from 1
Here is the call graph for this function:

◆ next()

bool anch::csv::Reader::next ( )

Read next record

Returns
false at the end of the input
Exceptions
CsvErroron invalid record

◆ operator[]()

std::string_view anch::csv::Reader::operator[] ( std::size_t index) const

Current record field

Parameters
indexthe field index
Returns
the field, valid until the next call to next

◆ options()

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

Options getter

Returns
the options
Here is the call graph for this function:

◆ quoted()

bool anch::csv::Reader::quoted ( std::size_t index) const

Check if a field is quoted: an empty field is an absent value, an empty quoted field ( "" ) an empty string

Parameters
indexthe field index
Returns
true when field is quoted
Here is the call graph for this function:

◆ size()

std::size_t anch::csv::Reader::size ( ) const
noexcept

Current record number of fields

Returns
the number of fields
Here is the call graph for this function:

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