pacemaker 3.0.3
COM automation for people with deadlines and a hatred of GUIs
 
Loading...
Searching...
No Matches
CSV Class Reference

Stateless utility for parsing CSV files into in-memory records. More...

#include <CSV.hpp>

Static Public Member Functions

static auto parse (const std::filesystem::path &filename) -> std::vector< std::vector< std::string > >
 Parses a CSV file from disk into a vector of records.
 

Detailed Description

Stateless utility for parsing CSV files into in-memory records.

CSV exposes a single static entry point, parse(), which reads a file from disk and returns its contents as std::vector<std::vector<std::string>>: one inner vector per record (row), each containing the unescaped field values of that record in order.

The underlying grammar (see csv.l / csv.y) follows the RFC 4180 conventions:

  • Records are separated by CRLF, CR or LF.
  • Fields within a record are separated by a comma.
  • A field may be unescaped (printable ASCII excluding comma and double-quote) or escaped (enclosed in double quotes, in which case it may contain commas, CR, LF, and double-quote characters represented as a double "").
  • Escaped fields are unescaped during parsing: a doubled quote "" becomes a single '' in the returned string, and embedded CR/LF tokens become literal \r/\n characters in the field.

CSV has no instance state; both parse() and the private parse_helper() are static. The class exists purely to namespace the parsing entry point and is never instantiated.

Typical usage

auto rows = pacemaker::io::CSV::parse("measurements.csv");
for(const auto &row : rows)
{
for (const auto &field : row)
std::cout << field << '\t';
std::cout << '\n';
}
static auto parse(const std::filesystem::path &filename) -> std::vector< std::vector< std::string > >
Parses a CSV file from disk into a vector of records.
See also
pacemaker::io::CSVLexer

Definition at line 41 of file CSV.hpp.

Member Function Documentation

◆ parse()

static auto parse ( const std::filesystem::path & filename) -> std::vector< std::vector< std::string > >
static

Parses a CSV file from disk into a vector of records.

Validates that filename exists and refers to a regular file, opens it for reading, and delegates to parse_helper() to run the lexer/parser over the resulting stream.

Parameters
filenamePath to the CSV file to parse.
Returns
A vector of records, each a vector of field strings in document order. An empty file yields an empty (or grammar-dependent minimal) result; no record is syntesised for input that contains no data.
Exceptions
std::invalid_argumentif filename does not exist or does not refer to a regular file
std::invalid_argumentif the file cannot be opened for reading (e.g. permissions)
(parse-defined)if the file content does not conform to the CSV grammar; parse errors are thrown.

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