34 const filesys::path check(
const filesys::path& filename,
const filesys::path& root = filesys::current_path())
36 const filesys::path file_path = canonical(absolute(root / filename));
37 if (!is_regular_file(file_path)) {
38 throw runtime_error(
string(__func__) +
">File " + file_path.string() +
" not present. Aborting.");
48 CSV::CSV(
const filesys::path& path, std::initializer_list<std::string> optLabels) : m_labels(), m_column_count(0)
51 filesys::path full_path = check(path);
53 file.open(full_path.string());
54 if (!file.is_open()) {
55 throw runtime_error(
"Error opening csv file: " + full_path.string());
59 }
catch (std::runtime_error& error) {
61 if (optLabels.size() == 0) {
76 for (
unsigned i = 1U; i < columnCount + 1; ++i) {
83 for (
const vector<string>& row : rows) {
90 return m_labels == other.
m_labels && (
const vector<CSVRow>&)*
this == (
const vector<CSVRow>&)other;
95 for (
unsigned int index = 0; index <
m_labels.size(); ++index) {
99 throw runtime_error(
"Label: " + label +
" not found in CSV");
105 file.open(path.string());
106 if (!file.is_open()) {
107 throw runtime_error(
"Error opening csv file: " + path.string());
115 for (
unsigned int i = 0; i <
m_labels.size(); ++i) {
116 const string& label =
m_labels.at(i);
117 file <<
"\"" << label <<
"\"";
128 for (
const CSVRow& row : *
this) {
138 getline(inputStream, line);
140 std::vector<std::string> headerLabels =
Split(line,
",");
141 for (
const std::string& label : headerLabels) {
147 while (getline(inputStream, line)) {
150 std::vector<std::string> values =
159 CSVRow csvRow(
this, values);
160 this->push_back(csvRow);
void WriteLabels(std::ofstream &file) const
Write header with labels.
std::string ToString(const T &value)
Builds a string representation of a value of type T.
const std::vector< std::string > & GetLabels() const
bool operator==(const CSV &other) const
Comparison operator.
void Write(const filesys::path &path) const
Write CSV to file.
void ReadFromStream(std::istream &inputStream)
Read data from input stream.
size_t GetIndexForLabel(const std::string &label) const
Convert label to index for more user friendly and robust implementation.
std::vector< std::string > m_labels
void AddRows(const std::vector< std::vector< std::string >> &rows)
Add multiple rows of strings at the same time.
std::vector< std::string > Split(const std::string &s, const std::string &delimiters)
Split a string (in order of occurence) by splitting it on the given delimiters.
CSV()=default
Default constructor, used for swig.
std::string Trim(const std::string &source, const std::string &t=" ")
Trim characters at both ends of string.
void WriteRows(std::ofstream &file) const
Write the body of rows.
A collection of CSVRow elements.
void AddRow(const T &...values)
Add row of values.
Namespace for the simulator and related classes.
Header file of base class for config that needs to be read from a file.