51 std::istringstream stream(input);
55 return static_cast<bool>(stream);
64 : m_delimiter(delimiter),
66 m_fileStream(filepath),
67 m_stream(&m_fileStream)
73 : m_delimiter(delimiter),
151 return GenericTransform(std::move(input), value);
159 return GenericTransform(std::move(input), value);
165 typedef signed char byte_type;
169 std::istringstream tempStream(input);
171 int16_t tempOutput = 0;
172 tempStream >> tempOutput;
174 if (tempOutput >= std::numeric_limits<byte_type>::min() &&
175 tempOutput <= std::numeric_limits<byte_type>::max())
177 value =
static_cast<byte_type
>(tempOutput);
180 bool success =
static_cast<bool>(tempStream);
182 NS_LOG_DEBUG(
"Input='" << input <<
"', output=" << tempOutput <<
", result=" << success);
192 return GenericTransform(std::move(input), value);
200 return GenericTransform(std::move(input), value);
208 return GenericTransform(std::move(input), value);
216 return GenericTransform(std::move(input), value);
232 typedef unsigned char byte_type;
236 std::istringstream tempStream(input);
238 uint16_t tempOutput = 0;
239 tempStream >> tempOutput;
241 if (tempOutput >= std::numeric_limits<byte_type>::min() &&
242 tempOutput <= std::numeric_limits<byte_type>::max())
244 value =
static_cast<byte_type
>(tempOutput);
247 bool success =
static_cast<bool>(tempStream);
249 NS_LOG_DEBUG(
"Input='" << input <<
"', output=" << tempOutput <<
", result=" << success);
259 return GenericTransform(std::move(input), value);
267 return GenericTransform(std::move(input), value);
275 return GenericTransform(std::move(input), value);
283 return GenericTransform(std::move(input), value);
302 auto start_col = line.begin();
303 auto end_col = line.end();
305 while (start_col != line.end())
307 std::tie(value, end_col) =
ParseColumn(start_col, line.end());
313 if (end_col != line.end())
324std::tuple<std::string, std::string::const_iterator>
339 State state = State::BEGIN;
343 while (state != State::END)
347 NS_LOG_DEBUG(
"Found end iterator, switching to END state");
358 if (state != State::QUOTED_STRING)
362 NS_LOG_DEBUG(
"Found field delimiter, switching to END state");
364 if (state == State::UNQUOTED_STRING)
366 NS_LOG_DEBUG(
"Removing trailing whitespace from unquoted field: '" << buffer
368 auto len = buffer.size();
371 while (!buffer.empty() &&
372 std::isspace(
static_cast<unsigned char>(buffer.back())))
377 auto finalLen = buffer.size();
380 <<
" trailing whitespace characters");
389 NS_LOG_DEBUG(
"Found start of comment, switching to END state");
405 NS_LOG_DEBUG(
"Switching state: BEGIN -> QUOTED_STRING");
407 state = State::QUOTED_STRING;
409 else if (!std::isspace(c))
411 NS_LOG_DEBUG(
"Switching state: BEGIN -> UNQUOTED_STRING");
413 state = State::UNQUOTED_STRING;
418 case State::QUOTED_STRING: {
421 NS_LOG_DEBUG(
"Switching state: QUOTED_STRING -> END_QUOTE");
422 state = State::END_QUOTE;
430 case State::END_QUOTE: {
433 NS_LOG_DEBUG(
"Switching state: END_QUOTE -> QUOTED_STRING");
436 state = State::QUOTED_STRING;
441 NS_LOG_DEBUG(
"Switching state: END_QUOTE -> FIND_DELIMITER");
442 state = State::FIND_DELIMITER;
446 case State::UNQUOTED_STRING: {
450 case State::FIND_DELIMITER:
460 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.