63 std::istringstream stream(input);
67 return static_cast<bool>(stream);
76 : m_delimiter(delimiter),
78 m_fileStream(filepath),
79 m_stream(&m_fileStream)
85 : m_delimiter(delimiter),
177 typedef signed char byte_type;
181 std::istringstream tempStream(input);
183 int16_t tempOutput = 0;
184 tempStream >> tempOutput;
189 value =
static_cast<byte_type
>(tempOutput);
192 bool success =
static_cast<bool>(tempStream);
194 NS_LOG_DEBUG(
"Input='" << input <<
"', output=" << tempOutput <<
", result=" << success);
244 typedef unsigned char byte_type;
248 std::istringstream tempStream(input);
250 uint16_t tempOutput = 0;
251 tempStream >> tempOutput;
256 value =
static_cast<byte_type
>(tempOutput);
259 bool success =
static_cast<bool>(tempStream);
261 NS_LOG_DEBUG(
"Input='" << input <<
"', output=" << tempOutput <<
", result=" << success);
314 auto start_col = line.begin();
315 auto end_col = line.end();
317 while (start_col != line.end())
319 std::tie(value, end_col) =
ParseColumn(start_col, line.end());
325 if (end_col != line.end())
336 std::tuple<std::string, std::string::const_iterator>
351 State state = State::BEGIN;
359 NS_LOG_DEBUG(
"Found end iterator, switching to END state");
370 if (state != State::QUOTED_STRING)
374 NS_LOG_DEBUG(
"Found field delimiter, switching to END state");
376 if (state == State::UNQUOTED_STRING)
378 NS_LOG_DEBUG(
"Removing trailing whitespace from unquoted field: '" << buffer
380 auto len = buffer.size();
383 while (!buffer.empty() &&
384 std::isspace(
static_cast<unsigned char>(buffer.back())))
389 auto finalLen = buffer.size();
392 <<
" trailing whitespace characters");
401 NS_LOG_DEBUG(
"Found start of comment, switching to END state");
417 NS_LOG_DEBUG(
"Switching state: BEGIN -> QUOTED_STRING");
419 state = State::QUOTED_STRING;
421 else if (!std::isspace(c))
423 NS_LOG_DEBUG(
"Switching state: BEGIN -> UNQUOTED_STRING");
425 state = State::UNQUOTED_STRING;
430 case State::QUOTED_STRING: {
433 NS_LOG_DEBUG(
"Switching state: QUOTED_STRING -> END_QUOTE");
434 state = State::END_QUOTE;
442 case State::END_QUOTE: {
445 NS_LOG_DEBUG(
"Switching state: END_QUOTE -> QUOTED_STRING");
448 state = State::QUOTED_STRING;
453 NS_LOG_DEBUG(
"Switching state: END_QUOTE -> FIND_DELIMITER");
454 state = State::FIND_DELIMITER;
458 case State::UNQUOTED_STRING: {
462 case State::FIND_DELIMITER:
473 return std::make_tuple(buffer, iter);
virtual ~CsvReader()
Destructor.
std::size_t RowNumber() const
The number of lines that have been read.
char Delimiter() const
Returns the delimiter character specified during object construction.
std::istream * m_stream
Pointer to the input stream containing the data.
bool IsDelimiter(char c) const
Returns true if the supplied character matches the delimiter.
CsvReader(const std::string &filepath, char delimiter=',')
Constructor.
void ParseLine(const std::string &line)
Scans the string and splits it into individual columns based on the delimiter.
std::size_t ColumnCount() const
Returns the number of columns in the csv data.
std::size_t m_rowsRead
Number of lines processed.
bool m_blankRow
Line contains no data (blank line or comment only).
bool FetchNextRow()
Reads one line from the input until a new line is encountered.
bool IsBlankRow() const
Check if the current row is blank.
Columns m_columns
Fields extracted from the current line.
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.
char m_delimiter
Character used to separate fields.
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.
ns3::CsvReader declaration
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
bool GenericTransform(std::string input, T &output)
Convert a string into another type.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define END
End of a line.