BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
CsvReader.cpp
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.cpp
6 //! @brief Implements 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 
16 #include <fstream>
17 #include <iostream>
18 
19 std::string const& CSVRow::operator[](unsigned index) const
20 {
21  return m_data[index];
22 }
23 
24 unsigned long CSVRow::size() const
25 {
26  return static_cast<unsigned long>(m_data.size());
27 }
28 
29 void CSVRow::readNextRow(std::istream& str)
30 {
31  std::string line;
32  std::getline(str, line);
33  std::replace(std::begin(line), std::end(line), '\t', ' ');
34  std::stringstream lineStream(line);
35  std::string cell;
36 
37  m_data.clear();
38 
39  while (std::getline(lineStream, cell, separator)) {
40  addCell(cell);
41  }
42  // This checks for a trailing comma with no data after it.
43  if (!lineStream && cell.empty()) {
44  // If there was a trailing comma then add an empty element.
45  addCell("");
46  }
47 }
48 
49 void CSVRow::setSeparator(char sep)
50 {
51  this->separator = sep;
52  return;
53 }
54 
56 {
57  return this->separator;
58 }
59 
60 void CSVRow::addCell(std::string str)
61 {
62  m_data.push_back(str);
63 }
64 
66 {
67  Read();
69 }
70 
72 {
73  std::ifstream file(filepath);
74  if (!file.is_open()) {
75  throw std::ios_base::failure("Unable to open file \"" + filepath + "\"");
76  }
77  for (CSVIterator loop(file, separator); loop != CSVIterator(); ++loop) {
78  rows.push_back((*loop));
80  (*loop).size() > numberOfColumns ? unsigned((*loop).size()) : numberOfColumns;
81  }
82 }
83 
85 {
86  for (unsigned i = 0; i < NumberOfRows(); i++) {
87  while (rows[i].size() < NumberOfColumns()) {
88  rows[i].addCell("");
89  }
90  }
91  for (unsigned i = 0; i < NumberOfRows(); i++) {
92  m_data.push_back(rows[i].dataVector());
93  }
94 }
95 
96 std::vector<std::string> const CSVFile::operator[](unsigned index_i) const
97 {
98  return m_data[index_i];
99 }
100 
101 unsigned long CSVFile::NumberOfRows() const
102 {
103  return static_cast<unsigned long>(rows.size());
104 }
105 
106 unsigned long CSVFile::NumberOfColumns() const
107 {
108  return this->numberOfColumns;
109 }
110 
112 {
113  this->separator = sep;
114  return;
115 }
116 
118 {
119  return this->separator;
120 }
121 
123 {
124  if (headersRow > 0) {
125  return this->rows[headersRow - 1];
126  } else {
127  CSVRow dummy;
128  while (dummy.size() < NumberOfColumns())
129  dummy.addCell("");
130  return dummy;
131  }
132 }
133 
135 {
136  return this->rows[i];
137 }
Defines class CsvReader.
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
unsigned long NumberOfColumns() const
Definition: CsvReader.cpp:106
unsigned headersRow
Definition: CsvReader.h:118
std::string filepath
Definition: CsvReader.h:116
unsigned numberOfColumns
Definition: CsvReader.h:119
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
char getSeparator()
Definition: CsvReader.cpp:55
char separator
Definition: CsvReader.h:37
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