Stride Reference Manual  - generated for commit 9643b11
CSV.h
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, 2018, Kuylen E, Willem L, Broeckhove J
14  */
15 
21 #pragma once
22 
23 #include "CSVRow.h"
24 
25 #include "util/StringUtils.h"
26 #include "util/is_iterator.h"
27 
28 #include <iosfwd>
29 #include <type_traits>
30 #include <vector>
31 
32 #ifdef BOOST_FOUND
33 #include <boost/filesystem.hpp>
34 #include <boost/filesystem/path.hpp>
35 namespace filesys = boost::filesystem;
36 #else
37 #include <filesystem>
38 namespace filesys = std::filesystem;
39 #endif
40 namespace stride {
41 namespace util {
42 
46 class CSV : protected std::vector<CSVRow>
47 {
48 public:
50  explicit CSV(const filesys::path& path, std::initializer_list<std::string> optLabels = {});
51 
53  explicit CSV(std::istream& inputStream);
54 
56  explicit CSV(size_t columnCount);
57 
59  explicit CSV(const std::vector<std::string>& labels);
60 
62  template <typename It>
63  explicit CSV(It labelsBegin, It labelsEnd)
64  : m_labels(ToString(labelsBegin, labelsEnd)), m_column_count(m_labels.size())
65  {
66  }
67 
69  CSV() = default;
70 
72  using std::vector<CSVRow>::begin;
73  using std::vector<CSVRow>::end;
74  using std::vector<CSVRow>::size;
75 
77  bool operator==(const CSV& other) const;
78 
80  template <typename... T>
81  void AddRow(const T&... values);
82 
84  void AddRows(const std::vector<std::vector<std::string>>& rows);
85 
87  void AddRow(const std::vector<std::string>& row);
88 
93  template <typename It>
94  void AddRow(typename std::enable_if<is_iterator<It>::value, It>::type first, It last)
95  {
96  CSVRow csvRow(this, ToString(first, last));
97  this->push_back(csvRow);
98  }
99 
101  size_t GetColumnCount() const { return m_column_count; }
102 
105  size_t GetIndexForLabel(const std::string& label) const;
106 
108  void Write(const filesys::path& path) const;
109 
110  const std::vector<std::string>& GetLabels() const;
111 
112 private:
113  friend std::ofstream& operator<<(std::ofstream& ofs, const CSV& csv);
114 
116  void ReadFromStream(std::istream& inputStream);
117 
119  void WriteLabels(std::ofstream& file) const;
120 
122  void WriteRows(std::ofstream& file) const;
123 
124  std::vector<std::string> m_labels;
125  size_t m_column_count = 0;
126 };
127 
128 template <typename... T>
129 inline void CSV::AddRow(const T&... values)
130 {
131  AddRow({ToString(values)...});
132 }
133 
134 inline std::ofstream& operator<<(std::ofstream& ofs, const CSV& csv)
135 {
136  csv.WriteLabels(ofs);
137  csv.WriteRows(ofs);
138  return ofs;
139 }
140 
141 } // namespace util
142 } // namespace stride
void WriteLabels(std::ofstream &file) const
Write header with labels.
Definition: CSV.cpp:113
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
const std::vector< std::string > & GetLabels() const
Definition: CSV.cpp:163
bool operator==(const CSV &other) const
Comparison operator.
Definition: CSV.cpp:88
void Write(const filesys::path &path) const
Write CSV to file.
Definition: CSV.cpp:102
void ReadFromStream(std::istream &inputStream)
Read data from input stream.
Definition: CSV.cpp:133
size_t m_column_count
Definition: CSV.h:125
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_labels
Definition: CSV.h:124
size_t GetColumnCount() const
Number of columns in the CSV.
Definition: CSV.h:101
void AddRows(const std::vector< std::vector< std::string >> &rows)
Add multiple rows of strings at the same time.
Definition: CSV.cpp:81
When it &#39;s not an iterator.
Definition: is_iterator.h:43
friend std::ofstream & operator<<(std::ofstream &ofs, const CSV &csv)
Definition: CSV.h:134
CSV()=default
Default constructor, used for swig.
void AddRow(typename std::enable_if< is_iterator< It >::value, It >::type first, It last)
Add row of values (ToString handles the case when the iterator points to strings).
Definition: CSV.h:94
Interface/Implementation of is_iterator.
CSV(It labelsBegin, It labelsEnd)
Initialize with header labels only.
Definition: CSV.h:63
void WriteRows(std::ofstream &file) const
Write the body of rows.
Definition: CSV.cpp:126
Miscellaneous string utilities.
A collection of CSVRow elements.
Definition: CSV.h:46
Header file of base class for config that needs to be read from a file.
void AddRow(const T &...values)
Add row of values.
Definition: CSV.h:129
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28