AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
AnCH CSV library documentation

Introduction

AnCH CSV library reads and writes CSV records (RFC 4180) as streams, without loading the whole input, and maps records to objects with their description shared by formats ( anch::Mapping , anch-utils), one by one (range) or in vectors.

Installation

CMake target anch::csv (option ANCH_BUILD_CSV , ON):

find_package(Anch REQUIRED)
target_link_libraries(my-app PRIVATE anch::csv)

See installation.

Format

  • RFC 4180 by default: , separator, fields quoted with " when they contain the separator, a quote, CR or LF, quotes doubled in quoted fields, records written with \c CRLF . - Reading accepts \c CRLF and \c LF line endings ( \c CR alone is refused), a last record without line ending, and skips an UTF-8 BOM. An empty field and an empty quoted field ( \c "" ) are distinguished ( \ref anch::csv::Reader::quoted "quoted"). - Bytes are kept as is (UTF-8 is expected), without conversion. - Limits: record size ( \c Options::maxRecordSize , 1 MiB) and number of columns ( \c Options::maxColumns , 1024) ; every record has the number of columns of the header or of the first record ( \c Options::sameColumnCount ). @section csv_write_sec Writing \ref anch::csv::Writer "Writer" quotes fields when needed. \c writeRow writes strings as is, booleans as \c true / \c false and numbers with \c std::to_chars (independent of the locale): \snippet{trimleft} anch-csv/examples/csv.cpp writer @section csv_read_sec Reading \ref anch::csv::Reader "Reader" reads one record at a time: fields are \c std::string_view valid until the next record (the record buffer is reused). Errors raise \ref anch::csv::CsvError "CsvError" with a code, the line and the column: \snippet{trimleft} anch-csv/examples/csv.cpp reader @section csv_objects_sec Objects Objects are described once for every format ( \c anch::Mapping ): fields are columns. CSV objects are flat: fields are strings, booleans, numbers, \c anch::UUID , dates (RFC 3339) or \c std::optional of them ; nested objects and collections are refused at compile time. \snippet{trimleft} anch-csv/examples/csv.cpp mapping \ref anch::csv::ObjectWriter "ObjectWriter" writes the header, then one record per object. An absent value ( \c std::nullopt ) is an empty field, an empty string an empty quoted field ( \c "" ): \snippet{trimleft} anch-csv/examples/csv.cpp objects writer \ref anch::csv::ObjectReader "ObjectReader" associates columns with fields by name (or by position without header: each registered field is a column, in registration order, for reading and writing). Columns which are not fields are ignored ( \c Options::ignoreUnknownColumns ), required fields ( \c anch::val::required ) columns have to be present. Conversion errors and anch-val constraints violations ( \c Options::readCheckConstraints ) raise \ref anch::csv::CsvError "CsvError" with the line and the column: \snippet{trimleft} anch-csv/examples/csv.cpp objects reader @section csv_records_sec Records ranges and vectors \ref anch::csv::records "records" reads objects one by one, without loading the whole input (input range, read once: errors are raised while iterating) ; \ref anch::csv::deserialize "deserialize" and \ref anch::csv::serialize "serialize" read and write vectors (and any range) for small volumes: \snippet{trimleft} anch-csv/examples/csv.cpp records @section csv_options_sec Options \ref anch::csv::Options "Options" set separator, quote, line ending, header, BOM, limits and objects mapping (unknown columns, constraints checks, booleans texts):
    options.separator = ';'; // spreadsheets with a comma as decimal separator
    options.crlf = false;
    options.bom = true; // UTF-8 BOM for spreadsheets
    options.header = false; // reader: no header, the first record is data
    std::ostringstream out;
    anch::csv::Writer(out, options).writeRow("1,5", true);