ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
AtCSVReader.h
Go to the documentation of this file.
1 #ifndef ATCSVREADER_H
2 #define ATCSVREADER_H
3 
22 #include <cstddef>
23 #include <sstream> // IWYU pragma: keep
24 #include <string>
25 #include <vector>
26 
28 template <typename T>
29 class CSVRow {
30 public:
31  T operator[](std::size_t index) const { return fData.at(index); }
32  std::size_t size() const { return fData.size(); }
33  const std::string &getLine() const { return fLine; }
34  void readNextLine(std::istream &stream)
35  {
36  fData.clear(); // Clear old data
37 
38  std::getline(stream, fLine);
39  std::istringstream str(fLine); // Get next line
40  for (std::string elem; std::getline(str, elem, ',');) {
41  std::istringstream ss(elem);
42  T obj;
43  ss >> obj;
44  if (!ss.fail())
45  fData.emplace_back(obj);
46  }
47  }
48 
49 private:
50  std::string fLine;
51  std::vector<T> fData;
52 };
53 
55 template <typename T>
56 std::istream &operator>>(std::istream &stream, CSVRow<T> &data)
57 {
58  data.readNextLine(stream);
59  return stream;
60 }
61 
63 template <typename T>
64 class CSVIterator {
65 private:
66  std::istream *fStream;
67  CSVRow<T> fRow;
68 
69 public:
70  CSVIterator(std::istream &stream) : fStream(stream.good() ? &stream : nullptr) { ++(*this); }
71  CSVIterator() : fStream(nullptr) {}
72 
73  // Pre increment
75  {
76  if (fStream) {
77  if (!((*fStream) >> fRow)) {
78  fStream = nullptr;
79  }
80  }
81  return *this;
82  }
83  // Post increment
85  {
86  CSVIterator tmp(*this);
87  ++(*this);
88  return tmp;
89  }
90  CSVRow<T> const &operator*() const { return fRow; }
91  CSVRow<T> const *operator->() const { return &fRow; }
92 
93  bool operator==(CSVIterator const &rhs)
94  {
95  return ((this == &rhs) || ((this->fStream == nullptr) && (rhs.fStream == nullptr)));
96  }
97  bool operator!=(CSVIterator const &rhs) { return !((*this) == rhs); }
98 };
99 
101 template <typename T>
102 class CSVRange {
103 private:
104  std::istream &fStream;
105 
106 public:
107  CSVRange(std::istream &str) : fStream(str) {}
108  CSVIterator<T> begin() const { return CSVIterator<T>{fStream}; }
109  CSVIterator<T> end() const { return CSVIterator<T>{}; }
110 };
111 
112 #endif //#ifndef ATCSVREADER_H
CSVIterator::CSVIterator
CSVIterator()
Definition: AtCSVReader.h:71
CSVRange
Range class for CSVIterator.
Definition: AtCSVReader.h:102
CSVIterator::operator->
CSVRow< T > const * operator->() const
Definition: AtCSVReader.h:91
CSVIterator::operator==
bool operator==(CSVIterator const &rhs)
Definition: AtCSVReader.h:93
CSVIterator::CSVIterator
CSVIterator(std::istream &stream)
Definition: AtCSVReader.h:70
CSVRow::getLine
const std::string & getLine() const
Definition: AtCSVReader.h:33
CSVRange::begin
CSVIterator< T > begin() const
Definition: AtCSVReader.h:108
CSVRow
A simple CSV reader w/ modern cpp features.
Definition: AtCSVReader.h:29
CSVRow::operator[]
T operator[](std::size_t index) const
Definition: AtCSVReader.h:31
CSVIterator::operator++
CSVIterator & operator++()
Definition: AtCSVReader.h:74
CSVIterator
Iterator for CSVRow.
Definition: AtCSVReader.h:64
CSVIterator::operator++
CSVIterator & operator++(int)
Definition: AtCSVReader.h:84
CSVRow::readNextLine
void readNextLine(std::istream &stream)
Definition: AtCSVReader.h:34
CSVRange::CSVRange
CSVRange(std::istream &str)
Definition: AtCSVReader.h:107
operator>>
std::istream & operator>>(std::istream &stream, CSVRow< T > &data)
Method to stream into this type of object.
Definition: AtCSVReader.h:56
CSVIterator::operator*
CSVRow< T > const & operator*() const
Definition: AtCSVReader.h:90
CSVIterator::operator!=
bool operator!=(CSVIterator const &rhs)
Definition: AtCSVReader.h:97
CSVRow::size
std::size_t size() const
Definition: AtCSVReader.h:32
CSVRange::end
CSVIterator< T > end() const
Definition: AtCSVReader.h:109