62 std::istringstream stream(input);
66 return static_cast<bool>(stream);
75 : m_delimiter(delimiter),
77 m_fileStream(filepath),
78 m_stream(&m_fileStream)
84 : m_delimiter(delimiter),
162 return GenericTransform(std::move(input), value);
170 return GenericTransform(std::move(input), value);
176 typedef signed char byte_type;
180 std::istringstream tempStream(input);
182 int16_t tempOutput = 0;
183 tempStream >> tempOutput;
185 if (tempOutput >= std::numeric_limits<byte_type>::min() &&
186 tempOutput <= std::numeric_limits<byte_type>::max())
188 value =
static_cast<byte_type
>(tempOutput);
191 bool success =
static_cast<bool>(tempStream);
193 NS_LOG_DEBUG(
"Input='" << input <<
"', output=" << tempOutput <<
", result=" << success);
203 return GenericTransform(std::move(input), value);
211 return GenericTransform(std::move(input), value);
219 return GenericTransform(std::move(input), value);
227 return GenericTransform(std::move(input), value);
243 typedef unsigned char byte_type;
247 std::istringstream tempStream(input);
249 uint16_t tempOutput = 0;
250 tempStream >> tempOutput;
252 if (tempOutput >= std::numeric_limits<byte_type>::min() &&
253 tempOutput <= std::numeric_limits<byte_type>::max())
255 value =
static_cast<byte_type
>(tempOutput);
258 bool success =
static_cast<bool>(tempStream);
260 NS_LOG_DEBUG(
"Input='" << input <<
"', output=" << tempOutput <<
", result=" << success);
270 return GenericTransform(std::move(input), value);
278 return GenericTransform(std::move(input), value);
286 return GenericTransform(std::move(input), value);
294 return GenericTransform(std::move(input), value);
313 auto start_col = line.begin();
314 auto end_col = line.end();
316 while (start_col != line.end())
318 std::tie(value, end_col) =
ParseColumn(start_col, line.end());
324 if (end_col != line.end())
335std::tuple<std::string, std::string::const_iterator>
350 State state = State::BEGIN;
354 while (state != State::END)
358 NS_LOG_DEBUG(
"Found end iterator, switching to END state");
369 if (state != State::QUOTED_STRING)
373 NS_LOG_DEBUG(
"Found field delimiter, switching to END state");
375 if (state == State::UNQUOTED_STRING)
377 NS_LOG_DEBUG(
"Removing trailing whitespace from unquoted field: '" << buffer
379 auto len = buffer.size();
382 while (!buffer.empty() &&
383 std::isspace(
static_cast<unsigned char>(buffer.back())))
388 auto finalLen = buffer.size();
391 <<
" trailing whitespace characters");
400 NS_LOG_DEBUG(
"Found start of comment, switching to END state");
416 NS_LOG_DEBUG(
"Switching state: BEGIN -> QUOTED_STRING");
418 state = State::QUOTED_STRING;
420 else if (!std::isspace(c))
422 NS_LOG_DEBUG(
"Switching state: BEGIN -> UNQUOTED_STRING");
424 state = State::UNQUOTED_STRING;
429 case State::QUOTED_STRING: {
432 NS_LOG_DEBUG(
"Switching state: QUOTED_STRING -> END_QUOTE");
433 state = State::END_QUOTE;
441 case State::END_QUOTE: {
444 NS_LOG_DEBUG(
"Switching state: END_QUOTE -> QUOTED_STRING");
447 state = State::QUOTED_STRING;
452 NS_LOG_DEBUG(
"Switching state: END_QUOTE -> FIND_DELIMITER");
453 state = State::FIND_DELIMITER;
457 case State::UNQUOTED_STRING: {
461 case State::FIND_DELIMITER:
471 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.