61 std::istringstream stream (input);
65 return static_cast<bool> (stream);
73 : m_delimiter (delimiter),
75 m_fileStream (filepath),
76 m_stream (&m_fileStream)
82 : m_delimiter (delimiter),
173 typedef signed char byte_type;
177 std::istringstream tempStream (input);
179 std::int16_t tempOutput = 0;
180 tempStream >> tempOutput;
185 value =
static_cast<byte_type
> (tempOutput);
188 bool success =
static_cast<bool> (tempStream);
191 <<
"', output=" << tempOutput
192 <<
", result=" << success);
244 typedef unsigned char byte_type;
248 std::istringstream tempStream (input);
250 std::uint16_t tempOutput = 0;
251 tempStream >> tempOutput;
256 value =
static_cast<byte_type
> (tempOutput);
259 bool success =
static_cast<bool> (tempStream);
262 <<
"', output=" << tempOutput
263 <<
", result=" << success);
316 auto start_col = line.begin ();
317 auto end_col = line.end ();
319 while ( start_col != line.end () )
321 std::tie (value, end_col) =
ParseColumn (start_col, line.end ());
327 if ( end_col != line.end () )
338 std::tuple<std::string, std::string::const_iterator>
353 State state = State::BEGIN;
361 NS_LOG_DEBUG (
"Found end iterator, switching to END state");
372 if (state != State::QUOTED_STRING)
376 NS_LOG_DEBUG (
"Found field delimiter, switching to END state");
378 if ( state == State::UNQUOTED_STRING )
380 NS_LOG_DEBUG (
"Removing trailing whitespace from unquoted field: '" << buffer <<
"'");
381 auto len = buffer.size ();
384 while ( !buffer.empty ()
385 && std::isspace (static_cast<unsigned char> (buffer.back ())) )
390 auto finalLen = buffer.size ();
392 NS_LOG_DEBUG (
"Removed " << (len - finalLen) <<
" trailing whitespace characters");
401 NS_LOG_DEBUG (
"Found start of comment, switching to END state");
418 NS_LOG_DEBUG (
"Switching state: BEGIN -> QUOTED_STRING");
420 state = State::QUOTED_STRING;
422 else if (!std::isspace (c))
424 NS_LOG_DEBUG (
"Switching state: BEGIN -> UNQUOTED_STRING");
426 state = State::UNQUOTED_STRING;
427 buffer.push_back (c);
431 case State::QUOTED_STRING:
435 NS_LOG_DEBUG (
"Switching state: QUOTED_STRING -> END_QUOTE");
436 state = State::END_QUOTE;
440 buffer.push_back (c);
444 case State::END_QUOTE:
448 NS_LOG_DEBUG (
"Switching state: END_QUOTE -> QUOTED_STRING" );
451 state = State::QUOTED_STRING;
452 buffer.push_back (c);
456 NS_LOG_DEBUG (
"Switching state: END_QUOTE -> FIND_DELIMITER" );
457 state = State::FIND_DELIMITER;
461 case State::UNQUOTED_STRING:
463 buffer.push_back (c);
465 case State::FIND_DELIMITER:
476 return std::make_tuple (buffer, iter);
#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.