Provides functions for parsing and extracting data from Comma Separated Value (CSV) formatted text files. More...
#include "csv-reader.h"
Public Member Functions | |
CsvReader (const std::string &filepath, char delimiter=',') | |
Constructor. | |
CsvReader (std::istream &stream, char delimiter=',') | |
Constructor. | |
virtual | ~CsvReader () |
Destructor. | |
std::size_t | ColumnCount () const |
Returns the number of columns in the csv data. | |
char | Delimiter () const |
Returns the delimiter character specified during object construction. | |
bool | FetchNextRow () |
Reads one line from the input until a new line is encountered. | |
template<class T > | |
bool | GetValue (std::size_t columnIndex, T &value) const |
Attempt to convert from the string data in the specified column to the specified data type. | |
bool | IsBlankRow () const |
Check if the current row is blank. | |
std::size_t | RowNumber () const |
The number of lines that have been read. | |
Private Types | |
typedef std::vector< std::string > | Columns |
Container of CSV data. | |
Private Member Functions | |
bool | IsDelimiter (char c) const |
Returns true if the supplied character matches the delimiter. | |
std::tuple< std::string, std::string::const_iterator > | ParseColumn (std::string::const_iterator begin, std::string::const_iterator end) |
Extracts the data for one column in a csv row. | |
void | ParseLine (const std::string &line) |
Scans the string and splits it into individual columns based on the delimiter. | |
bool | GetValueAs (std::string input, double &value) const |
Attempt to convert from the string data stored at the specified column index into the specified type. | |
bool | GetValueAs (std::string input, float &value) const |
Attempt to convert from the string data stored at the specified column index into the specified type. | |
bool | GetValueAs (std::string input, signed char &value) const |
Attempt to convert from the string data stored at the specified column index into the specified type. | |
bool | GetValueAs (std::string input, short &value) const |
Attempt to convert from the string data stored at the specified column index into the specified type. | |
bool | GetValueAs (std::string input, int &value) const |
Attempt to convert from the string data stored at the specified column index into the specified type. | |
bool | GetValueAs (std::string input, long &value) const |
Attempt to convert from the string data stored at the specified column index into the specified type. | |
bool | GetValueAs (std::string input, long long &value) const |
Attempt to convert from the string data stored at the specified column index into the specified type. | |
bool | GetValueAs (std::string input, std::string &value) const |
Attempt to convert from the string data stored at the specified column index into the specified type. | |
bool | GetValueAs (std::string input, unsigned char &value) const |
Attempt to convert from the string data stored at the specified column index into the specified type. | |
bool | GetValueAs (std::string input, unsigned short &value) const |
Attempt to convert from the string data stored at the specified column index into the specified type. | |
bool | GetValueAs (std::string input, unsigned int &value) const |
Attempt to convert from the string data stored at the specified column index into the specified type. | |
bool | GetValueAs (std::string input, unsigned long &value) const |
Attempt to convert from the string data stored at the specified column index into the specified type. | |
bool | GetValueAs (std::string input, unsigned long long &value) const |
Attempt to convert from the string data stored at the specified column index into the specified type. | |
Private Attributes | |
bool | m_blankRow |
Line contains no data (blank line or comment only). | |
Columns | m_columns |
Fields extracted from the current line. | |
char | m_delimiter |
Character used to separate fields. | |
std::ifstream | m_fileStream |
File stream containing the data. | |
std::size_t | m_rowsRead |
Number of lines processed. | |
std::istream * | m_stream |
Pointer to the input stream containing the data. | |
Provides functions for parsing and extracting data from Comma Separated Value (CSV) formatted text files.
This parser is somewhat more relaxed than RFC 4180 ; see below for a list of the differences. In particular it is possible to set the delimiting character at construction, enabling parsing of tab-delimited streams or other formats with delimiters.
To use this facility, construct a CsvReader from either a file path or std::istream
, then FetchNextRow(), and finally GetValue() to extract specific values from the row.
For example:
As another example, supposing we need a vector from each row, the middle of the previous example would become:
This parser implements RFC 4180 , but with several restrictions removed; see below for differences. All the formatting features described next are illustrated in the examples which which follow.
The hash character (#) is used to indicate the start of a comment. Comments are not parsed by the reader. Comments are treated as either an empty column or part of an existing column depending on where the comment is located. Comments that are found at the end of a line containing data are ignored.
1,2 # This comment ignored, leaving two data columns
Lines that contain a comment and no data are treated as rows with a single empty column, meaning that ColumnCount will return 1 and GetValue() will return an empty string.
# This row treated as a single empty column, returning an empty string. "" # So is this
IsBlankRow() will return true
in either of these cases.
Columns with string data which contain the delimiter character or the hash character can be wrapped in double quotes to prevent CsvReader from treating them as special characters.
3,string without delimiter,"String with comma ',' delimiter"
Double quotes can be escaped by doubling up the quotes inside a quoted field. See example 6 below for a demonstration.
Leading and trailing whitespace are ignored by the reader and are not stored in the column data.
4,5 , 6 # Columns contain '4', '5', '6'
If leading or trailing whitespace are important for a column, wrap the column in double quotes as discussed above.
7,"8 "," 9" # Columns contain '7', '8 ', ' 9'
Trailing delimiters are ignored; they do not result in an empty column.
Section 2.1
Section 2.3
Section 2.4
Section 2.6
Definition at line 220 of file csv-reader.h.
|
private |
Container of CSV data.
Each entry represents one field in a row of data. The fields are stored in the same order that they are encountered in the CSV data.
Definition at line 379 of file csv-reader.h.
ns3::CsvReader::CsvReader | ( | const std::string & | filepath, |
char | delimiter = ',' ) |
Constructor.
Opens the file specified in the filepath argument and reads data from it.
filepath | Path to a file containing CSV data. |
delimiter | Character used to separate fields in the data file. |
Definition at line 63 of file csv-reader.cc.
References NS_LOG_FUNCTION.
ns3::CsvReader::CsvReader | ( | std::istream & | stream, |
char | delimiter = ',' ) |
Constructor.
Reads csv data from the supplied input stream.
stream | Input stream containing csv data. |
delimiter | Character used to separate fields in the data stream. |
Definition at line 72 of file csv-reader.cc.
References NS_LOG_FUNCTION.
|
virtual |
Destructor.
Definition at line 81 of file csv-reader.cc.
std::size_t ns3::CsvReader::ColumnCount | ( | ) | const |
Returns the number of columns in the csv data.
Definition at line 86 of file csv-reader.cc.
References m_columns, and NS_LOG_FUNCTION.
Referenced by ns3::ListPositionAllocator::Add(), and GetValue().
char ns3::CsvReader::Delimiter | ( | ) | const |
Returns the delimiter character specified during object construction.
Definition at line 102 of file csv-reader.cc.
References m_delimiter, and NS_LOG_FUNCTION.
bool ns3::CsvReader::FetchNextRow | ( | ) |
Reads one line from the input until a new line is encountered.
The read data is stored in a cache which is accessed by the GetValue functions to extract fields from the data.
true
if a line was read successfully or false
if the read failed or reached the end of the file. Definition at line 110 of file csv-reader.cc.
References m_rowsRead, m_stream, NS_LOG_ERROR, NS_LOG_FUNCTION, NS_LOG_LOGIC, and ParseLine().
Referenced by ns3::ListPositionAllocator::Add().
bool ns3::CsvReader::GetValue | ( | std::size_t | columnIndex, |
T & | value ) const |
Attempt to convert from the string data in the specified column to the specified data type.
T | The data type of the output variable. |
[in] | columnIndex | Index of the column to fetch. |
[out] | value | Location where the converted data will be stored. |
true
if the specified column has data and the data was converted to the specified data type. Definition at line 400 of file csv-reader.h.
References ColumnCount(), GetValueAs(), and m_columns.
Referenced by ns3::ListPositionAllocator::Add().
|
private |
Attempt to convert from the string data stored at the specified column index into the specified type.
input | [in] String value to be converted. |
value | [out] Location where the converted value will be stored. |
true
if the column exists and the conversion succeeded, false
otherwise. Definition at line 147 of file csv-reader.cc.
References NS_LOG_FUNCTION.
Referenced by GetValue().
|
private |
Attempt to convert from the string data stored at the specified column index into the specified type.
input | [in] String value to be converted. |
value | [out] Location where the converted value will be stored. |
true
if the column exists and the conversion succeeded, false
otherwise. Definition at line 155 of file csv-reader.cc.
References NS_LOG_FUNCTION.
|
private |
Attempt to convert from the string data stored at the specified column index into the specified type.
input | [in] String value to be converted. |
value | [out] Location where the converted value will be stored. |
true
if the column exists and the conversion succeeded, false
otherwise. Definition at line 196 of file csv-reader.cc.
References NS_LOG_FUNCTION.
|
private |
Attempt to convert from the string data stored at the specified column index into the specified type.
input | [in] String value to be converted. |
value | [out] Location where the converted value will be stored. |
true
if the column exists and the conversion succeeded, false
otherwise. Definition at line 204 of file csv-reader.cc.
References NS_LOG_FUNCTION.
|
private |
Attempt to convert from the string data stored at the specified column index into the specified type.
input | [in] String value to be converted. |
value | [out] Location where the converted value will be stored. |
true
if the column exists and the conversion succeeded, false
otherwise. Definition at line 212 of file csv-reader.cc.
References NS_LOG_FUNCTION.
|
private |
Attempt to convert from the string data stored at the specified column index into the specified type.
input | [in] String value to be converted. |
value | [out] Location where the converted value will be stored. |
true
if the column exists and the conversion succeeded, false
otherwise. Definition at line 188 of file csv-reader.cc.
References NS_LOG_FUNCTION.
|
private |
Attempt to convert from the string data stored at the specified column index into the specified type.
input | [in] String value to be converted. |
value | [out] Location where the converted value will be stored. |
true
if the column exists and the conversion succeeded, false
otherwise. Definition at line 163 of file csv-reader.cc.
References NS_LOG_DEBUG, and NS_LOG_FUNCTION.
|
private |
Attempt to convert from the string data stored at the specified column index into the specified type.
input | [in] String value to be converted. |
value | [out] Location where the converted value will be stored. |
true
if the column exists and the conversion succeeded, false
otherwise. Definition at line 220 of file csv-reader.cc.
References NS_LOG_FUNCTION.
|
private |
Attempt to convert from the string data stored at the specified column index into the specified type.
input | [in] String value to be converted. |
value | [out] Location where the converted value will be stored. |
true
if the column exists and the conversion succeeded, false
otherwise. Definition at line 230 of file csv-reader.cc.
References NS_LOG_DEBUG, and NS_LOG_FUNCTION.
|
private |
Attempt to convert from the string data stored at the specified column index into the specified type.
input | [in] String value to be converted. |
value | [out] Location where the converted value will be stored. |
true
if the column exists and the conversion succeeded, false
otherwise. Definition at line 263 of file csv-reader.cc.
References NS_LOG_FUNCTION.
|
private |
Attempt to convert from the string data stored at the specified column index into the specified type.
input | [in] String value to be converted. |
value | [out] Location where the converted value will be stored. |
true
if the column exists and the conversion succeeded, false
otherwise. Definition at line 271 of file csv-reader.cc.
References NS_LOG_FUNCTION.
|
private |
Attempt to convert from the string data stored at the specified column index into the specified type.
input | [in] String value to be converted. |
value | [out] Location where the converted value will be stored. |
true
if the column exists and the conversion succeeded, false
otherwise. Definition at line 279 of file csv-reader.cc.
References NS_LOG_FUNCTION.
|
private |
Attempt to convert from the string data stored at the specified column index into the specified type.
input | [in] String value to be converted. |
value | [out] Location where the converted value will be stored. |
true
if the column exists and the conversion succeeded, false
otherwise. Definition at line 255 of file csv-reader.cc.
References NS_LOG_FUNCTION.
bool ns3::CsvReader::IsBlankRow | ( | ) | const |
Check if the current row is blank.
A blank row can consist of any combination of
""
true
if the input row is a blank line. Definition at line 141 of file csv-reader.cc.
References m_blankRow.
|
private |
Returns true
if the supplied character matches the delimiter.
c | Character to check. |
true
if c is the delimiter character, false
otherwise. Definition at line 287 of file csv-reader.cc.
References m_delimiter, and NS_LOG_FUNCTION.
Referenced by ParseColumn().
|
private |
Extracts the data for one column in a csv row.
begin | Iterator to the first character in the row. |
end | Iterator to the last character in the row. |
Definition at line 325 of file csv-reader.cc.
References END, IsDelimiter(), NS_LOG_DEBUG, and NS_LOG_FUNCTION.
Referenced by ParseLine().
|
private |
Scans the string and splits it into individual columns based on the delimiter.
[in] | line | String containing delimiter separated data. |
Definition at line 295 of file csv-reader.cc.
References m_blankRow, m_columns, NS_LOG_DEBUG, NS_LOG_FUNCTION, NS_LOG_LOGIC, and ParseColumn().
Referenced by FetchNextRow().
std::size_t ns3::CsvReader::RowNumber | ( | ) | const |
The number of lines that have been read.
Definition at line 94 of file csv-reader.cc.
References m_rowsRead, and NS_LOG_FUNCTION.
Referenced by ns3::ListPositionAllocator::Add().
|
private |
Line contains no data (blank line or comment only).
Definition at line 384 of file csv-reader.h.
Referenced by IsBlankRow(), and ParseLine().
|
private |
Fields extracted from the current line.
Definition at line 383 of file csv-reader.h.
Referenced by ColumnCount(), GetValue(), and ParseLine().
|
private |
Character used to separate fields.
Definition at line 381 of file csv-reader.h.
Referenced by Delimiter(), and IsDelimiter().
|
private |
File stream containing the data.
Definition at line 385 of file csv-reader.h.
|
private |
Number of lines processed.
Definition at line 382 of file csv-reader.h.
Referenced by FetchNextRow(), and RowNumber().
|
private |
Pointer to the input stream containing the data.
Definition at line 390 of file csv-reader.h.
Referenced by FetchNextRow().