|
creativity
v1.3.0
Agent-based model of creativity and piracy
|
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... | |
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.
|
explicit |
Opens a csv file for parsing.
| filename | the file to read |
| std::ios_base::failure | for underlying IO errors |
| void creativity::data::CSVParser::dontSkip | ( | const std::string & | name | ) |
Removes the given field name from the list of header fields to skip, if present.
| bool creativity::data::CSVParser::eof | ( | ) | const |
Returns true if the parser has reached the end of the file.
| double creativity::data::CSVParser::field | ( | const std::string & | field | ) | const |
| size_t creativity::data::CSVParser::fieldPosition | ( | const std::string & | field | ) | const |
Returns the column index of the given field.
| std::out_of_range | if the given field doesn't exist. |
| const std::vector<std::string>& creativity::data::CSVParser::fields | ( | ) | const |
| 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.
| const std::vector<std::string>& creativity::data::CSVParser::header | ( | ) | const |
|
inline |
Accesses the most-recently-read line number.
| 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.
| std::ios_base::failure | if a read error occurs |
| std::logic_error | if readRow() is called when eof() is true (i.e. the previous readRow() already hit the end of the file and returned false). |
| std::invalid_argument | if one of the fields to parse (i.e. all of those not in skip()) cannot be converted to a double value. |
|
inline |
Accesses the most-recently-read row of the file.
If no row has yet been read, this will be an empty vector.
|
inline |
Accesses any skipped fields in the most-recently-read row.
| 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.
| 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.
| 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.
1.8.12