62 std::istringstream stream (input);
66 return static_cast<bool> (stream);
74 : m_delimiter (delimiter),
76 m_fileStream (filepath),
77 m_stream (&m_fileStream)
83 : m_delimiter (delimiter),
174 typedef signed char byte_type;
178 std::istringstream tempStream (input);
180 std::int16_t tempOutput = 0;
181 tempStream >> tempOutput;
186 value =
static_cast<byte_type
> (tempOutput);
189 bool success =
static_cast<bool> (tempStream);
192 <<
"', output=" << tempOutput
193 <<
", result=" << success);
245 typedef unsigned char byte_type;
249 std::istringstream tempStream (input);
251 std::uint16_t tempOutput = 0;
252 tempStream >> tempOutput;
257 value =
static_cast<byte_type
> (tempOutput);
260 bool success =
static_cast<bool> (tempStream);
263 <<
"', output=" << tempOutput
264 <<
", result=" << success);
317 auto start_col = line.begin ();
318 auto end_col = line.end ();
320 while ( start_col != line.end () )
322 std::tie (value, end_col) =
ParseColumn (start_col, line.end ());
328 if ( end_col != line.end () )
339 std::tuple<std::string, std::string::const_iterator>
354 State state = State::BEGIN;
362 NS_LOG_DEBUG (
"Found end iterator, switching to END state");
373 if (state != State::QUOTED_STRING)
377 NS_LOG_DEBUG (
"Found field delimiter, switching to END state");
379 if ( state == State::UNQUOTED_STRING )
381 NS_LOG_DEBUG (
"Removing trailing whitespace from unquoted field: '" << buffer <<
"'");
382 auto len = buffer.size ();
385 while ( !buffer.empty ()
386 && std::isspace (static_cast<unsigned char> (buffer.back ())) )
391 auto finalLen = buffer.size ();
393 NS_LOG_DEBUG (
"Removed " << (len - finalLen) <<
" trailing whitespace characters");
402 NS_LOG_DEBUG (
"Found start of comment, switching to END state");
419 NS_LOG_DEBUG (
"Switching state: BEGIN -> QUOTED_STRING");
421 state = State::QUOTED_STRING;
423 else if (!std::isspace (c))
425 NS_LOG_DEBUG (
"Switching state: BEGIN -> UNQUOTED_STRING");
427 state = State::UNQUOTED_STRING;
428 buffer.push_back (c);
432 case State::QUOTED_STRING:
436 NS_LOG_DEBUG (
"Switching state: QUOTED_STRING -> END_QUOTE");
437 state = State::END_QUOTE;
441 buffer.push_back (c);
445 case State::END_QUOTE:
449 NS_LOG_DEBUG (
"Switching state: END_QUOTE -> QUOTED_STRING" );
452 state = State::QUOTED_STRING;
453 buffer.push_back (c);
457 NS_LOG_DEBUG (
"Switching state: END_QUOTE -> FIND_DELIMITER" );
458 state = State::FIND_DELIMITER;
462 case State::UNQUOTED_STRING:
464 buffer.push_back (c);
466 case State::FIND_DELIMITER:
477 return std::make_tuple (buffer, iter);
#define END
End of a line.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Columns m_columns
Fields extracted from the current line.
void ParseLine(const std::string &line)
Scans the string and splits it into individual columns based on the delimiter.
char Delimiter() const
Returns the delimiter character specified during object construction.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
std::size_t m_rowsRead
Number of lines processed.
virtual ~CsvReader()
Destructor.
CsvReader(const std::string &filepath, char delimiter=',')
Constructor.
bool GenericTransform(std::string input, T &output)
Convert a string into another type.
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 IsDelimiter(char c) const
Returns true if the supplied character matches the delimiter.
std::istream * m_stream
Pointer to the input stream containing the data.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::size_t ColumnCount() const
Returns the number of columns in the csv data.
ns3::CsvReader declaration
bool FetchNextRow()
Reads one line from the input until a new line is encountered.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
std::size_t RowNumber() const
The number of lines that have been read.
bool m_blankRow
Line contains no data (blank line or comment only).
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
char m_delimiter
Character used to separate fields.
bool IsBlankRow() const
Check if the current row is blank.
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.