csv_reader.h
Go to the documentation of this file.
1 //
2 // Created by julian on 09.10.16.
3 //
4 
5 #ifndef ECHELLESIMULATOR_CSV_READER_H
6 #define ECHELLESIMULATOR_CSV_READER_H
7 
8 #include <string>
9 #include <sstream>
10 #include <vector>
11 
12 class CSVRow
13 {
14 public:
15  std::string const& operator[](std::size_t index) const
16  {
17  return m_data[index];
18  }
19  std::size_t size() const
20  {
21  return m_data.size();
22  }
23  void readNextRow(std::istream& str)
24  {
25  std::string line;
26  std::getline(str, line);
27 
28  std::stringstream lineStream(line);
29  std::string cell;
30 
31  m_data.clear();
32  while(std::getline(lineStream, cell, ';'))
33  {
34  m_data.push_back(cell);
35  }
36  }
37 
38 private:
39  std::vector<std::string> m_data;
40 };
41 
42 inline std::istream& operator>>(std::istream& str, CSVRow& data)
43 {
44  data.readNextRow(str);
45  return str;
46 }
47 
48 
50 {
51 public:
52  typedef std::input_iterator_tag iterator_category;
53  typedef CSVRow value_type;
54  typedef std::size_t difference_type;
55  typedef CSVRow* pointer;
56  typedef CSVRow& reference;
57 
58  CSVIterator(std::istream& str) :m_str(str.good()?&str:NULL) { ++(*this); }
59  CSVIterator() :m_str(NULL) {}
60 
61  // Pre Increment
62  CSVIterator& operator++() {if (m_str) { if (!((*m_str) >> m_row)){m_str = NULL;}}return *this;}
63  // Post increment
64  CSVIterator operator++(int) {CSVIterator tmp(*this);++(*this);return tmp;}
65  CSVRow const& operator*() const {return m_row;}
66  CSVRow const* operator->() const {return &m_row;}
67 
68  bool operator==(CSVIterator const& rhs) {return ((this == &rhs) || ((this->m_str == NULL) && (rhs.m_str == NULL)));}
69  bool operator!=(CSVIterator const& rhs) {return !((*this) == rhs);}
70 
71 
72 private:
73  std::istream* m_str;
75 };
76 
77 
78 #endif //ECHELLESIMULATOR_CSV_READER_H
CSVRow m_row
Definition: csv_reader.h:74
std::istream & operator>>(std::istream &str, CSVRow &data)
Definition: csv_reader.h:42
std::string const & operator[](std::size_t index) const
Definition: csv_reader.h:15
CSVRow & reference
Definition: csv_reader.h:56
CSVRow const & operator*() const
Definition: csv_reader.h:65
bool operator!=(CSVIterator const &rhs)
Definition: csv_reader.h:69
std::input_iterator_tag iterator_category
Definition: csv_reader.h:52
std::size_t size() const
Definition: csv_reader.h:19
Definition: csv_reader.h:12
std::size_t difference_type
Definition: csv_reader.h:54
CSVRow * pointer
Definition: csv_reader.h:55
std::istream * m_str
Definition: csv_reader.h:73
CSVRow const * operator->() const
Definition: csv_reader.h:66
Definition: csv_reader.h:49
CSVIterator operator++(int)
Definition: csv_reader.h:64
data
Definition: show.py:9
CSVIterator & operator++()
Definition: csv_reader.h:62
std::vector< std::string > m_data
Definition: csv_reader.h:39
void readNextRow(std::istream &str)
Definition: csv_reader.h:23
CSVRow value_type
Definition: csv_reader.h:53
CSVIterator(std::istream &str)
Definition: csv_reader.h:58
CSVIterator()
Definition: csv_reader.h:59
bool operator==(CSVIterator const &rhs)
Definition: csv_reader.h:68