A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
csv-reader.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Lawrence Livermore National Laboratory
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathew Bielejeski <bielejeski1@llnl.gov>
18 */
19
20#ifndef NS3_CSV_READER_H_
21#define NS3_CSV_READER_H_
22
23#include <cstddef>
24#include <cstdint>
25#include <fstream>
26#include <istream>
27#include <string>
28#include <vector>
29
38namespace ns3
39{
40
233{
234 public:
244 CsvReader(const std::string& filepath, char delimiter = ',');
245
254 CsvReader(std::istream& stream, char delimiter = ',');
255
259 virtual ~CsvReader();
260
266 std::size_t ColumnCount() const;
267
273 std::size_t RowNumber() const;
274
280 char Delimiter() const;
281
290 bool FetchNextRow();
291
304 template <class T>
305 bool GetValue(std::size_t columnIndex, T& value) const;
306
317 bool IsBlankRow() const;
318
319 private:
331 bool GetValueAs(std::string input, double& value) const;
332
333 bool GetValueAs(std::string input, float& value) const;
334
335 bool GetValueAs(std::string input, signed char& value) const;
336
337 bool GetValueAs(std::string input, short& value) const;
338
339 bool GetValueAs(std::string input, int& value) const;
340
341 bool GetValueAs(std::string input, long& value) const;
342
343 bool GetValueAs(std::string input, long long& value) const;
344
345 bool GetValueAs(std::string input, std::string& value) const;
346
347 bool GetValueAs(std::string input, unsigned char& value) const;
348
349 bool GetValueAs(std::string input, unsigned short& value) const;
350
351 bool GetValueAs(std::string input, unsigned int& value) const;
352
353 bool GetValueAs(std::string input, unsigned long& value) const;
354
355 bool GetValueAs(std::string input, unsigned long long& value) const;
365 bool IsDelimiter(char c) const;
366
372 void ParseLine(const std::string& line);
373
382 std::tuple<std::string, std::string::const_iterator> ParseColumn(
383 std::string::const_iterator begin,
384 std::string::const_iterator end);
385
391 typedef std::vector<std::string> Columns;
392
394 std::size_t m_rowsRead;
397 std::ifstream m_fileStream;
398
402 std::istream* m_stream;
403
404}; // class CsvReader
405
406/****************************************************
407 * Template implementations.
408 ***************************************************/
409
410template <class T>
411bool
412CsvReader::GetValue(std::size_t columnIndex, T& value) const
413{
414 if (columnIndex >= ColumnCount())
415 {
416 return false;
417 }
418
419 std::string cell = m_columns[columnIndex];
420
421 return GetValueAs(std::move(cell), value);
422}
423
424} // namespace ns3
425
426#endif // NS3_CSV_READER_H_
Provides functions for parsing and extracting data from Comma Separated Value (CSV) formatted text fi...
Definition: csv-reader.h:233
virtual ~CsvReader()
Destructor.
Definition: csv-reader.cc:93
bool GetValue(std::size_t columnIndex, T &value) const
Attempt to convert from the string data in the specified column to the specified data type.
Definition: csv-reader.h:412
std::size_t RowNumber() const
The number of lines that have been read.
Definition: csv-reader.cc:106
char Delimiter() const
Returns the delimiter character specified during object construction.
Definition: csv-reader.cc:114
std::istream * m_stream
Pointer to the input stream containing the data.
Definition: csv-reader.h:402
bool IsDelimiter(char c) const
Returns true if the supplied character matches the delimiter.
Definition: csv-reader.cc:299
void ParseLine(const std::string &line)
Scans the string and splits it into individual columns based on the delimiter.
Definition: csv-reader.cc:307
std::size_t ColumnCount() const
Returns the number of columns in the csv data.
Definition: csv-reader.cc:98
std::size_t m_rowsRead
Number of lines processed.
Definition: csv-reader.h:394
std::ifstream m_fileStream
File stream containing the data.
Definition: csv-reader.h:397
bool m_blankRow
Line contains no data (blank line or comment only).
Definition: csv-reader.h:396
bool FetchNextRow()
Reads one line from the input until a new line is encountered.
Definition: csv-reader.cc:122
std::vector< std::string > Columns
Container of CSV data.
Definition: csv-reader.h:391
bool IsBlankRow() const
Check if the current row is blank.
Definition: csv-reader.cc:153
Columns m_columns
Fields extracted from the current line.
Definition: csv-reader.h:395
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.
Definition: csv-reader.cc:159
char m_delimiter
Character used to separate fields.
Definition: csv-reader.h:393
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.
Definition: csv-reader.cc:337
Every class exported by the ns3 library is enclosed in the ns3 namespace.