A Discrete-Event Network Simulator
API
csv-reader.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2019 Lawrence Livermore National Laboratory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathew Bielejeski <bielejeski1@llnl.gov>
19  */
20 
21 #ifndef NS3_CSV_READER_H_
22 #define NS3_CSV_READER_H_
23 
24 #include <cstddef>
25 #include <cstdint>
26 #include <fstream>
27 #include <istream>
28 #include <string>
29 #include <vector>
30 
39 namespace ns3 {
40 
48 // *NS_CHECK_STYLE_OFF* Style checker trims blank lines in code blocks
49 
234 // *NS_CHECK_STYLE_ON*
236 {
237 public:
247  CsvReader (const std::string& filepath, char delimiter = ',');
248 
257  CsvReader (std::istream& stream, char delimiter = ',');
258 
262  virtual ~CsvReader ();
263 
269  std::size_t ColumnCount () const;
270 
276  std::size_t RowNumber () const;
277 
283  char Delimiter () const;
284 
293  bool FetchNextRow ();
294 
307  template<class T>
308  bool GetValue (std::size_t columnIndex, T& value) const;
309 
320  bool IsBlankRow () const;
321 
322 private:
334  bool GetValueAs (std::string input, double& value) const;
335 
336  bool GetValueAs (std::string input, float& value) const;
337 
338  bool GetValueAs (std::string input, signed char& value) const;
339 
340  bool GetValueAs (std::string input, short& value) const;
341 
342  bool GetValueAs (std::string input, int& value) const;
343 
344  bool GetValueAs (std::string input, long& value) const;
345 
346  bool GetValueAs (std::string input, long long& value) const;
347 
348  bool GetValueAs (std::string input, std::string& value) const;
349 
350  bool GetValueAs (std::string input, unsigned char& value) const;
351 
352  bool GetValueAs (std::string input, unsigned short& value) const;
353 
354  bool GetValueAs (std::string input, unsigned int& value) const;
355 
356  bool GetValueAs (std::string input, unsigned long& value) const;
357 
358  bool GetValueAs (std::string input, unsigned long long& value) const;
368  bool IsDelimiter (char c) const;
369 
375  void ParseLine (const std::string& line);
376 
385  std::tuple<std::string, std::string::const_iterator>
386  ParseColumn (std::string::const_iterator begin, std::string::const_iterator end);
387 
393  typedef std::vector<std::string> Columns;
394 
395  char m_delimiter;
396  std::size_t m_rowsRead;
398  bool m_blankRow;
399  std::ifstream m_fileStream;
400 
404  std::istream* m_stream;
405 
406 }; // class CsvReader
407 
408 
409 /****************************************************
410  * Template implementations.
411  ***************************************************/
412 
413 template<class T>
414 bool
415 CsvReader::GetValue (std::size_t columnIndex, T& value) const
416 {
417  if ( columnIndex >= ColumnCount () )
418  {
419  return false;
420  }
421 
422  std::string cell = m_columns[columnIndex];
423 
424  return GetValueAs (std::move (cell), value);
425 }
426 
427 } // namespace ns3
428 
429 #endif // NS3_CSV_READER_H_
Provides functions for parsing and extracting data from Comma Separated Value (CSV) formatted text fi...
Definition: csv-reader.h:236
virtual ~CsvReader()
Destructor.
Definition: csv-reader.cc:91
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:415
std::size_t RowNumber() const
The number of lines that have been read.
Definition: csv-reader.cc:103
char Delimiter() const
Returns the delimiter character specified during object construction.
Definition: csv-reader.cc:111
std::istream * m_stream
Pointer to the input stream containing the data.
Definition: csv-reader.h:404
bool IsDelimiter(char c) const
Returns true if the supplied character matches the delimiter.
Definition: csv-reader.cc:302
CsvReader(const std::string &filepath, char delimiter=',')
Constructor.
Definition: csv-reader.cc:73
void ParseLine(const std::string &line)
Scans the string and splits it into individual columns based on the delimiter.
Definition: csv-reader.cc:310
std::size_t ColumnCount() const
Returns the number of columns in the csv data.
Definition: csv-reader.cc:95
std::size_t m_rowsRead
Number of lines processed.
Definition: csv-reader.h:396
std::ifstream m_fileStream
File stream containing the data.
Definition: csv-reader.h:399
bool m_blankRow
Line contains no data (blank line or comment only).
Definition: csv-reader.h:398
bool FetchNextRow()
Reads one line from the input until a new line is encountered.
Definition: csv-reader.cc:119
std::vector< std::string > Columns
Container of CSV data.
Definition: csv-reader.h:393
bool IsBlankRow() const
Check if the current row is blank.
Definition: csv-reader.cc:150
Columns m_columns
Fields extracted from the current line.
Definition: csv-reader.h:397
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:156
char m_delimiter
Character used to separate fields.
Definition: csv-reader.h:395
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:340
Every class exported by the ns3 library is enclosed in the ns3 namespace.