2 #include <eris/noncopyable.hpp> 4 #include <unordered_set> 5 #include <unordered_map> 41 explicit CSVParser(
const std::string &filename);
49 const std::unordered_set<std::string>&
skip()
const;
52 void skip(
const std::string &name);
55 void dontSkip(
const std::string &name);
62 const std::vector<std::string>&
header()
const;
70 const std::vector<std::string>&
fields()
const;
91 double field(
const std::string &field)
const;
118 const std::vector<double>&
row()
const {
return row_; }
121 const std::unordered_map<std::string, std::string>&
rowSkipped()
const {
return row_skipped_; }
137 static std::vector<std::string> split(
const std::string &csr);
140 std::unordered_set<std::string> skip_;
141 std::vector<std::string> header_;
142 std::vector<std::string> fields_;
143 mutable std::unordered_map<std::string, unsigned> field_pos_;
146 std::vector<double> row_;
147 std::unordered_map<std::string, std::string> row_skipped_;
152 class CSVParser::iterator final :
public std::iterator<std::input_iterator_tag, const std::vector<double>, long> {
171 &csv_ == &(other.csv_)
pointer operator->()
Dereferences the iterator, returning the current CSVParser row pointer.
Definition: CSVParser.hpp:158
Primary namespace for all Creativity library code.
Definition: config.hpp:4
bool operator==(const iterator &other)
Return true if the given object is a reference to the current object, or if the current object is at ...
Definition: CSVParser.hpp:169
CSVParser()=delete
Not default constructible.
reference operator*()
Dereferences the iterator, returning the current CSVParser row.
Definition: CSVParser.hpp:155
const std::vector< std::string > & header() const
Returns the vector of header names read during construction.
bool readRow()
Reads the next line of the CSV file, storing it in row(), replacing what was previously stored there...
double field(const std::string &field) const
Returns the value (in the current row) of the given field.
size_t fieldPosition(const std::string &field) const
Returns the column index of the given field.
const size_t & lineNumber() const
Accesses the most-recently-read line number.
Definition: CSVParser.hpp:124
iterator end()
Returns a past-the-end iterator.
bool eof() const
Returns true if the parser has reached the end of the file.
bool hasField(const std::string &field) const
Returns true if fields() contains the requested field name.
const std::unordered_map< std::string, std::string > & rowSkipped() const
Accesses any skipped fields in the most-recently-read row.
Definition: CSVParser.hpp:121
const std::vector< std::string > & fields() const
Returns the vector of field names corresponding to a readRow() call.
iterator & operator++()
Increments the iterator, reading the next row of the file.
Definition: CSVParser.hpp:163
const std::vector< double > & row() const
Accesses the most-recently-read row of the file.
Definition: CSVParser.hpp:118
Primitive comma-separated-value file parser.
Definition: CSVParser.hpp:32
size_t allow_missing_values
The number of values that may be missing from the end of a data row for subsequent read rows...
Definition: CSVParser.hpp:98
iterator begin()
Returns an iterator that reads through the file.
void dontSkip(const std::string &name)
Removes the given field name from the list of header fields to skip, if present.
Iterator class that allows iterating through the file.
Definition: CSVParser.hpp:152
bool operator!=(const iterator &other)
Returns the negation of the == operator.
Definition: CSVParser.hpp:177
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...