BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
CSVFile Class Reference

Public Member Functions

 CSVFile (std::string path_to_file)
 
 CSVFile (std::string path_to_file, char sep)
 
 CSVFile (std::string path_to_file, char sep, unsigned headRow)
 
std::vector< std::vector< std::string > > asArray ()
 
void EqualizeRowLengths ()
 
CSVRow get_headers ()
 
CSVRow get_row (unsigned i)
 
char get_separator ()
 
void Init ()
 
unsigned long NumberOfColumns () const
 
unsigned long NumberOfRows () const
 
std::vector< std::string > const operator[] (unsigned index_i) const
 
void Read ()
 
void set_separator (char sep)
 

Private Attributes

std::string filepath
 
unsigned headersRow = 0
 
std::vector< std::vector< std::string > > m_data
 
unsigned numberOfColumns = 0
 
std::vector< CSVRowrows
 
char separator = '-'
 

Detailed Description

Definition at line 93 of file CsvReader.h.

Constructor & Destructor Documentation

◆ CSVFile() [1/3]

CSVFile::CSVFile ( std::string  path_to_file)
inline

Definition at line 95 of file CsvReader.h.

95 : filepath(path_to_file) { Init(); }
std::string filepath
Definition: CsvReader.h:116
void Init()
Definition: CsvReader.cpp:65

References Init().

Here is the call graph for this function:

◆ CSVFile() [2/3]

CSVFile::CSVFile ( std::string  path_to_file,
char  sep 
)
inline

Definition at line 96 of file CsvReader.h.

96 : filepath(path_to_file), separator(sep) { Init(); }
char separator
Definition: CsvReader.h:117

References Init().

Here is the call graph for this function:

◆ CSVFile() [3/3]

CSVFile::CSVFile ( std::string  path_to_file,
char  sep,
unsigned  headRow 
)
inline

Definition at line 97 of file CsvReader.h.

98  : filepath(path_to_file), separator(sep), headersRow(headRow)
99  {
100  Init();
101  }
unsigned headersRow
Definition: CsvReader.h:118

References Init().

Here is the call graph for this function:

Member Function Documentation

◆ asArray()

std::vector<std::vector<std::string> > CSVFile::asArray ( )
inline

Definition at line 113 of file CsvReader.h.

113 { return m_data; }
std::vector< std::vector< std::string > > m_data
Definition: CsvReader.h:121

References m_data.

◆ EqualizeRowLengths()

void CSVFile::EqualizeRowLengths ( )

Definition at line 84 of file CsvReader.cpp.

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 }
unsigned long NumberOfColumns() const
Definition: CsvReader.cpp:106
std::vector< CSVRow > rows
Definition: CsvReader.h:120
unsigned long NumberOfRows() const
Definition: CsvReader.cpp:101

References m_data, NumberOfColumns(), NumberOfRows(), and rows.

Referenced by Init().

Here is the call graph for this function:

◆ get_headers()

CSVRow CSVFile::get_headers ( )

Definition at line 122 of file CsvReader.cpp.

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 }
void addCell(std::string str)
Definition: CsvReader.cpp:60
unsigned long size() const
Definition: CsvReader.cpp:24

References CSVRow::addCell(), headersRow, NumberOfColumns(), rows, and CSVRow::size().

Here is the call graph for this function:

◆ get_row()

CSVRow CSVFile::get_row ( unsigned  i)

Definition at line 134 of file CsvReader.cpp.

135 {
136  return this->rows[i];
137 }

References rows.

◆ get_separator()

char CSVFile::get_separator ( )

Definition at line 117 of file CsvReader.cpp.

118 {
119  return this->separator;
120 }

References separator.

◆ Init()

void CSVFile::Init ( )

Definition at line 65 of file CsvReader.cpp.

66 {
67  Read();
69 }
void Read()
Definition: CsvReader.cpp:71
void EqualizeRowLengths()
Definition: CsvReader.cpp:84

References EqualizeRowLengths(), and Read().

Referenced by CSVFile().

Here is the call graph for this function:

◆ NumberOfColumns()

unsigned long CSVFile::NumberOfColumns ( ) const

Definition at line 106 of file CsvReader.cpp.

107 {
108  return this->numberOfColumns;
109 }
unsigned numberOfColumns
Definition: CsvReader.h:119

References numberOfColumns.

Referenced by EqualizeRowLengths(), and get_headers().

◆ NumberOfRows()

unsigned long CSVFile::NumberOfRows ( ) const

Definition at line 101 of file CsvReader.cpp.

102 {
103  return static_cast<unsigned long>(rows.size());
104 }

References rows.

Referenced by EqualizeRowLengths().

◆ operator[]()

std::vector< std::string > const CSVFile::operator[] ( unsigned  index_i) const

Definition at line 96 of file CsvReader.cpp.

97 {
98  return m_data[index_i];
99 }

References m_data.

◆ Read()

void CSVFile::Read ( )

Definition at line 71 of file CsvReader.cpp.

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 }

References filepath, numberOfColumns, rows, and separator.

Referenced by Init().

◆ set_separator()

void CSVFile::set_separator ( char  sep)

Definition at line 111 of file CsvReader.cpp.

112 {
113  this->separator = sep;
114  return;
115 }

References separator.

Member Data Documentation

◆ filepath

std::string CSVFile::filepath
private

Definition at line 116 of file CsvReader.h.

Referenced by Read().

◆ headersRow

unsigned CSVFile::headersRow = 0
private

Definition at line 118 of file CsvReader.h.

Referenced by get_headers().

◆ m_data

std::vector<std::vector<std::string> > CSVFile::m_data
private

Definition at line 121 of file CsvReader.h.

Referenced by asArray(), EqualizeRowLengths(), and operator[]().

◆ numberOfColumns

unsigned CSVFile::numberOfColumns = 0
private

Definition at line 119 of file CsvReader.h.

Referenced by NumberOfColumns(), and Read().

◆ rows

std::vector<CSVRow> CSVFile::rows
private

Definition at line 120 of file CsvReader.h.

Referenced by EqualizeRowLengths(), get_headers(), get_row(), NumberOfRows(), and Read().

◆ separator

char CSVFile::separator = '-'
private

Definition at line 117 of file CsvReader.h.

Referenced by get_separator(), Read(), and set_separator().


The documentation for this class was generated from the following files: