creativity  v1.3.0
Agent-based model of creativity and piracy
Classes | Public Member Functions | Public Attributes | List of all members
creativity::data::CSVParser Class Reference

Primitive comma-separated-value file parser. More...

#include <creativity/data/CSVParser.hpp>

Classes

class  iterator
 Iterator class that allows iterating through the file. More...
 

Public Member Functions

 CSVParser ()=delete
 Not default constructible.
 
 CSVParser (const std::string &filename)
 Opens a csv file for parsing. More...
 
const std::unordered_set< std::string > & skip () const
 The set of fields to skip; all non-numeric fields must be added here before attempting to read a data row. More...
 
void skip (const std::string &name)
 Adds the given field name to the list of header fields to skip, if not already present. More...
 
void dontSkip (const std::string &name)
 Removes the given field name from the list of header fields to skip, if present. More...
 
const std::vector< std::string > & header () const
 Returns the vector of header names read during construction. More...
 
const std::vector< std::string > & fields () const
 Returns the vector of field names corresponding to a readRow() call. More...
 
bool hasField (const std::string &field) const
 Returns true if fields() contains the requested field name. More...
 
size_t fieldPosition (const std::string &field) const
 Returns the column index of the given field. More...
 
double field (const std::string &field) const
 Returns the value (in the current row) of the given field. More...
 
bool readRow ()
 Reads the next line of the CSV file, storing it in row(), replacing what was previously stored there. More...
 
bool eof () const
 Returns true if the parser has reached the end of the file. More...
 
const std::vector< double > & row () const
 Accesses the most-recently-read row of the file. More...
 
const std::unordered_map< std::string, std::string > & rowSkipped () const
 Accesses any skipped fields in the most-recently-read row. More...
 
const size_t & lineNumber () const
 Accesses the most-recently-read line number. More...
 
iterator begin ()
 Returns an iterator that reads through the file.
 
iterator end ()
 Returns a past-the-end iterator.
 

Public Attributes

size_t allow_missing_values = 0
 The number of values that may be missing from the end of a data row for subsequent read rows. More...
 

Detailed Description

Primitive comma-separated-value file parser.

This is far from a complete CSV parser: it doesn't handle non-numeric values at all, for example; it is simply intended to parse the numeric CSV output produced by data.cpp.

The intended basic usage is:

CSVParser csv("filename.csv");
for (auto &row : csv) {
    // Do something with row (which is a `const std::vector<double>`)
}

or, equivalently:

CSVParser csv("filename.csv");
while (csv.readRow()) {
    // Do something with csv.row()
}

One non-standard extension is that this allows comment lines beginning with # in the file: any such line will be skipped.

Constructor & Destructor Documentation

§ CSVParser()

creativity::data::CSVParser::CSVParser ( const std::string &  filename)
explicit

Opens a csv file for parsing.

Parameters
filenamethe file to read
Exceptions
std::ios_base::failurefor underlying IO errors

Member Function Documentation

§ dontSkip()

void creativity::data::CSVParser::dontSkip ( const std::string &  name)

Removes the given field name from the list of header fields to skip, if present.

§ eof()

bool creativity::data::CSVParser::eof ( ) const

Returns true if the parser has reached the end of the file.

§ field()

double creativity::data::CSVParser::field ( const std::string &  field) const

Returns the value (in the current row) of the given field.

Shortcut for row()[fieldPosition(field)].

Exceptions
std::out_of_rangeif the given field doesn't exist.
std::logic_errorif there is no current row (either no row has been read yet, or eof() is true).

§ fieldPosition()

size_t creativity::data::CSVParser::fieldPosition ( const std::string &  field) const

Returns the column index of the given field.

Exceptions
std::out_of_rangeif the given field doesn't exist.

§ fields()

const std::vector<std::string>& creativity::data::CSVParser::fields ( ) const

Returns the vector of field names corresponding to a readRow() call.

This is essentially the same as header(), but skips any fields in skip().

See also
header()
skip()

§ hasField()

bool creativity::data::CSVParser::hasField ( const std::string &  field) const

Returns true if fields() contains the requested field name.

This is optimized for multiple calls: the first call puts all fields into a set; subsequent calls reuse the set.

§ header()

const std::vector<std::string>& creativity::data::CSVParser::header ( ) const

Returns the vector of header names read during construction.

This includes fields that will be skipped; if you don't want them, call fields() instead.

See also
fields()

§ lineNumber()

const size_t& creativity::data::CSVParser::lineNumber ( ) const
inline

Accesses the most-recently-read line number.

§ readRow()

bool creativity::data::CSVParser::readRow ( )

Reads the next line of the CSV file, storing it in row(), replacing what was previously stored there.

Returns true if a row was read, false if the end of the file was hit, throws an exception if something goes wrong.

Exceptions
std::ios_base::failureif a read error occurs
std::logic_errorif readRow() is called when eof() is true (i.e. the previous readRow() already hit the end of the file and returned false).
std::invalid_argumentif one of the fields to parse (i.e. all of those not in skip()) cannot be converted to a double value.

§ row()

const std::vector<double>& creativity::data::CSVParser::row ( ) const
inline

Accesses the most-recently-read row of the file.

If no row has yet been read, this will be an empty vector.

§ rowSkipped()

const std::unordered_map<std::string, std::string>& creativity::data::CSVParser::rowSkipped ( ) const
inline

Accesses any skipped fields in the most-recently-read row.

§ skip() [1/2]

const std::unordered_set<std::string>& creativity::data::CSVParser::skip ( ) const

The set of fields to skip; all non-numeric fields must be added here before attempting to read a data row.

See also
skip(const std::string &name)
dontSkip()

§ skip() [2/2]

void creativity::data::CSVParser::skip ( const std::string &  name)

Adds the given field name to the list of header fields to skip, if not already present.

Member Data Documentation

§ allow_missing_values

size_t creativity::data::CSVParser::allow_missing_values = 0

The number of values that may be missing from the end of a data row for subsequent read rows.

If left at the default value, 0, data rows must contain exactly the same number of values as the file's header; if set to a value n, up to n values may be missing from the end of data rows.

Inheritance diagram for creativity::data::CSVParser:
[legend]
Collaboration diagram for creativity::data::CSVParser:
[legend]

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