Stride Reference Manual  - generated for commit 9643b11
CSVRow.cpp
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2017, Kuylen E, Willem L, Broeckhove J
14  */
20 #include "CSVRow.h"
21 
22 #include "CSV.h"
23 
24 #include <iostream>
25 #include <ostream>
26 
27 namespace {
28 
29 inline bool IsFloat(const std::string& s)
30 {
31  float a;
32  std::stringstream ss(s);
33  return (ss >> a) ? true : false;
34 }
35 
36 } // namespace
37 
38 namespace stride {
39 namespace util {
40 
41 CSVRow::CSVRow(const CSV* parent, const std::vector<std::string>& values) : m_parent(parent), m_values()
42 {
43  if (values.size() != parent->GetColumnCount()) {
44  throw std::runtime_error("Tried adding row with " + ToString(values.size()) + " value(s) to CSV with " +
45  ToString(parent->GetColumnCount()) + " columns.");
46  }
47  for (const std::string& value : values) {
48  this->m_values.push_back(Trim(value, "\""));
49  }
50 }
51 
53 template <>
54 std::string CSVRow::GetValue<std::string>(size_t index) const
55 {
56  if (index >= m_parent->GetColumnCount()) {
57  throw std::runtime_error("Index out of range for CSV: " + ToString(index));
58  }
59  return m_values.at(index);
60 }
61 
63 template <>
64 std::string CSVRow::GetValue<std::string>(const std::string& label) const
65 {
66  size_t index = m_parent->GetIndexForLabel(label);
67  return GetValue(index);
68 }
69 
70 bool CSVRow::operator==(const CSVRow& other) const { return m_values == other.m_values; }
71 
72 std::ostream& operator<<(std::ostream& os, const CSVRow& row)
73 {
74  for (unsigned int i = 0; i < row.m_values.size(); ++i) {
75  const std::string& value = row.m_values.at(i);
76  if (!IsFloat(value)) {
77  os << "\"";
78  }
79  os << value;
80  if (!IsFloat(value)) {
81  os << "\"";
82  }
83  if (i != row.m_values.size() - 1) {
84  os << ",";
85  }
86  }
87  return os;
88 }
89 
90 } // namespace util
91 } // namespace stride
Row in CSV file.
Definition: CSVRow.h:36
std::string ToString(const T &value)
Builds a string representation of a value of type T.
Definition: StringUtils.h:90
bool operator==(const CSVRow &other) const
Compare operator.
Definition: CSVRow.cpp:70
size_t GetIndexForLabel(const std::string &label) const
Convert label to index for more user friendly and robust implementation.
Definition: CSV.cpp:93
std::vector< std::string > m_values
Definition: CSVRow.h:62
size_t GetColumnCount() const
Number of columns in the CSV.
Definition: CSV.h:101
T GetValue(size_t index) const
Get value at index. When T is specified, StringUtils are used to try to convert the value to type T...
Definition: CSVRow.h:105
friend std::ostream & operator<<(std::ostream &os, const CSVRow &row)
Print to stream.
Definition: CSVRow.cpp:72
CSVRow(const CSV *parent, const std::vector< std::string > &values)
CSVRow initialized with values. Should no be called by user code. CSV has convenience functions...
Definition: CSVRow.cpp:41
const CSV * m_parent
Definition: CSVRow.h:61
std::string Trim(const std::string &source, const std::string &t=" ")
Trim characters at both ends of string.
Definition: StringUtils.h:167
A collection of CSVRow elements.
Definition: CSV.h:46
Header file of base class for config that needs to be read from a file.
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
Header file of base class for config that needs to be read from a file.