Lumiera  0.pre.03
»edit your freedom«
csv.hpp File Reference

Go to the source code of this file.

Description

Encoding and decoding of data into CSV format.

The sequence of values transformed here is part of a data table, with columns holding data of various primitive value types; persisted CSV data is human readable, can be checked into Git and loaded into various spreadsheet and statistics applications.

CSV Format

Even while there is a standard defined in RFC 4180, a plethora of format variations can be found in the wild. Since the primary purpose of this implementation is to read back our own data, by deliberate choice only one single form of CSV is accepted.

  • first line is a header line and used to verify the number of columns
  • one record per line, embedded line breaks prohibited
  • fields separated by comma, semicolon tolerated
  • fields are trimmed and may be empty
  • a field may be double quoted
  • only quoted fields may contain whitespace or comma
  • no escaping of quotes, i.e. no quotes within quotes
See also
lib::stat::DataTable

Definition in file csv.hpp.

#include "lib/error.hpp"
#include "lib/null-value.hpp"
#include "lib/meta/tuple-helper.hpp"
#include "lib/format-string.hpp"
#include "lib/regex.hpp"
#include <limits>
#include <string>
#include <vector>

Classes

struct  CSVData
 Wrapper to simplify notation in tests. More...
 
struct  CSVLine
 A string with the ability to construct or append the CSV-rendering of data fields. More...
 
class  CsvParser
 Parser to split one line of CSV data into fields. More...
 

Functions

template<typename VAL >
void appendCsvField (string &csv, VAL const &val)
 Format and append a data value to a CSV string representation.
 
template<typename VAL >
string format4Csv (VAL const &val)
 
template<typename TAR >
TAR parseAs (string const &encodedVal)
 parse string representation into typed value
 
template<>
bool parseAs (string const &encodedBool)
 
template<>
string parseAs (string const &string)
 

Variables

const regex ACCEPT_FIELD
 
const regex FIND_DELIMITER_TOKEN {"[,;]"}
 
const string MATCH_DELIMITER { R"~((?:^|,|;)\s*)~" }
 
const string MATCH_QUOTED_TOKEN { R"~("([^"]*)"\s*)~" }
 
const string MATCH_SINGLE_TOKEN { R"~(([^,;"\s]*)\s*)~"}
 

Namespaces

 lib
 Implementation namespace for support and library code.