BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
CsvReader.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Views/ImportDataWidgets/CsvImportAssistant/CsvReader.h
6 //! @brief Defines class CsvReader
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #ifndef BORNAGAIN_GUI_COREGUI_VIEWS_IMPORTDATAWIDGETS_CSVIMPORTASSISTANT_CSVREADER_H
16 #define BORNAGAIN_GUI_COREGUI_VIEWS_IMPORTDATAWIDGETS_CSVIMPORTASSISTANT_CSVREADER_H
17 
18 #include <algorithm>
19 #include <fstream>
20 #include <iostream>
21 #include <iterator>
22 #include <sstream>
23 #include <vector>
24 
25 class CSVRow {
26 public:
27  std::string const& operator[](unsigned index) const;
28  unsigned long size() const;
29  void readNextRow(std::istream& str);
30  void setSeparator(char sep);
31  char getSeparator();
32  void addCell(std::string str);
33  std::vector<std::string> dataVector() { return m_data; }
34 
35 private:
36  std::vector<std::string> m_data;
37  char separator = '-';
38 };
39 
40 inline std::istream& operator>>(std::istream& str, CSVRow& data)
41 {
42  data.readNextRow(str);
43  return str;
44 }
45 
46 class CSVIterator {
47 public:
48  typedef std::input_iterator_tag iterator_category;
49  typedef CSVRow value_type;
50  typedef unsigned long difference_type;
51  typedef CSVRow* pointer;
52  typedef CSVRow& reference;
53 
54  CSVIterator(std::istream& str, char sep) : m_str(str.good() ? &str : nullptr)
55  {
56  m_sep = sep;
57  ++(*this);
58  }
59  CSVIterator() : m_str(nullptr) {}
60 
61  // Pre Increment
63  {
64  if (m_str) {
66  if (!((*m_str) >> m_row)) {
67  m_str = nullptr;
68  }
69  }
70  return *this;
71  }
72  // Post increment
74  {
75  CSVIterator tmp(*this);
76  ++(*this);
77  return tmp;
78  }
79  CSVRow const& operator*() const { return m_row; }
80  CSVRow const* operator->() const { return &m_row; }
81  bool operator==(CSVIterator const& rhs)
82  {
83  return ((this == &rhs) || ((this->m_str == nullptr) && (rhs.m_str == nullptr)));
84  }
85  bool operator!=(CSVIterator const& rhs) { return !((*this) == rhs); }
86 
87 private:
88  std::istream* m_str;
90  char m_sep;
91 };
92 
93 class CSVFile {
94 public:
95  CSVFile(std::string path_to_file) : filepath(path_to_file) { Init(); }
96  CSVFile(std::string path_to_file, char sep) : filepath(path_to_file), separator(sep) { Init(); }
97  CSVFile(std::string path_to_file, char sep, unsigned headRow)
98  : filepath(path_to_file), separator(sep), headersRow(headRow)
99  {
100  Init();
101  }
102 
103  void Init();
104  void Read();
105  void EqualizeRowLengths();
106  std::vector<std::string> const operator[](unsigned index_i) const;
107  unsigned long NumberOfRows() const;
108  unsigned long NumberOfColumns() const;
109  void set_separator(char sep);
110  char get_separator();
112  CSVRow get_row(unsigned i);
113  std::vector<std::vector<std::string>> asArray() { return m_data; }
114 
115 private:
116  std::string filepath;
117  char separator = '-';
118  unsigned headersRow = 0;
119  unsigned numberOfColumns = 0;
120  std::vector<CSVRow> rows;
121  std::vector<std::vector<std::string>> m_data;
122 };
123 
124 #endif // BORNAGAIN_GUI_COREGUI_VIEWS_IMPORTDATAWIDGETS_CSVIMPORTASSISTANT_CSVREADER_H
std::istream & operator>>(std::istream &str, CSVRow &data)
Definition: CsvReader.h:40
std::vector< std::vector< std::string > > m_data
Definition: CsvReader.h:121
CSVRow get_row(unsigned i)
Definition: CsvReader.cpp:134
char separator
Definition: CsvReader.h:117
CSVFile(std::string path_to_file, char sep)
Definition: CsvReader.h:96
unsigned long NumberOfColumns() const
Definition: CsvReader.cpp:106
CSVFile(std::string path_to_file, char sep, unsigned headRow)
Definition: CsvReader.h:97
unsigned headersRow
Definition: CsvReader.h:118
std::vector< std::vector< std::string > > asArray()
Definition: CsvReader.h:113
std::string filepath
Definition: CsvReader.h:116
unsigned numberOfColumns
Definition: CsvReader.h:119
CSVFile(std::string path_to_file)
Definition: CsvReader.h:95
void Read()
Definition: CsvReader.cpp:71
void EqualizeRowLengths()
Definition: CsvReader.cpp:84
CSVRow get_headers()
Definition: CsvReader.cpp:122
std::vector< CSVRow > rows
Definition: CsvReader.h:120
void Init()
Definition: CsvReader.cpp:65
char get_separator()
Definition: CsvReader.cpp:117
unsigned long NumberOfRows() const
Definition: CsvReader.cpp:101
std::vector< std::string > const operator[](unsigned index_i) const
Definition: CsvReader.cpp:96
void set_separator(char sep)
Definition: CsvReader.cpp:111
CSVRow m_row
Definition: CsvReader.h:89
CSVRow const & operator*() const
Definition: CsvReader.h:79
std::input_iterator_tag iterator_category
Definition: CsvReader.h:48
CSVRow * pointer
Definition: CsvReader.h:51
bool operator!=(CSVIterator const &rhs)
Definition: CsvReader.h:85
CSVRow & reference
Definition: CsvReader.h:52
CSVIterator & operator++()
Definition: CsvReader.h:62
CSVRow const * operator->() const
Definition: CsvReader.h:80
char m_sep
Definition: CsvReader.h:90
bool operator==(CSVIterator const &rhs)
Definition: CsvReader.h:81
CSVIterator(std::istream &str, char sep)
Definition: CsvReader.h:54
CSVRow value_type
Definition: CsvReader.h:49
CSVIterator operator++(int)
Definition: CsvReader.h:73
std::istream * m_str
Definition: CsvReader.h:88
unsigned long difference_type
Definition: CsvReader.h:50
char getSeparator()
Definition: CsvReader.cpp:55
char separator
Definition: CsvReader.h:37
std::vector< std::string > dataVector()
Definition: CsvReader.h:33
void readNextRow(std::istream &str)
Definition: CsvReader.cpp:29
void addCell(std::string str)
Definition: CsvReader.cpp:60
std::string const & operator[](unsigned index) const
Definition: CsvReader.cpp:19
std::vector< std::string > m_data
Definition: CsvReader.h:36
void setSeparator(char sep)
Definition: CsvReader.cpp:49
unsigned long size() const
Definition: CsvReader.cpp:24