UTILLib 2.0
Common C++ library with utilities.
Loading...
Searching...
No Matches
csvwriter.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 Ingemar Hedvall
3 * SPDX-License-Identifier: MIT
4 */
8#pragma once
9#include <cstdio>
10#include <fstream>
11#include <sstream>
12#include <string>
13#include <utility>
14#include <vector>
15
16namespace util::plot {
17
36 */
37class CsvWriter {
38 public:
45 explicit CsvWriter(const std::string& filename);
46
52 CsvWriter();
55 virtual ~CsvWriter();
56
60 void CloseFile();
61
66 [[nodiscard]] std::string FileName() const;
67
72 [[nodiscard]] const std::string& FullName() const { return filename_; }
73
78 void OpenFile(const std::string& filename);
79
85 [[nodiscard]] bool IsOk() const;
86
95 void AddColumnHeader(const std::string& name, const std::string& unit,
96 bool valid = true);
97
106 void SetColumnValid(size_t column, bool valid = true);
107
115 [[nodiscard]] bool IsColumnValid(size_t column) const;
116
120 void AddRow();
121
127 template <typename T>
128 void AddColumnValue(const T& value);
129
135 [[nodiscard]] std::string GetText() const { return text_.str(); }
136
141 void Reset();
142
148 [[nodiscard]] size_t Columns() const { return max_columns_; }
149
156 [[nodiscard]] size_t Rows() const { return row_count_; }
157
164 [[nodiscard]] const std::string Label(size_t index) const;
165
172 [[nodiscard]] const std::string& Name(size_t index) const;
173
180 [[nodiscard]] const std::string& Unit(size_t index) const;
181
182 private:
183 std::ofstream file_;
184 std::ostringstream text_;
185 std::ostream* output_ = nullptr;
186
187 std::string filename_;
188 size_t column_count_ = 0;
190 size_t max_columns_ =
191 0;
192 size_t row_count_ = 0;
193
194 struct Header {
195 std::string name;
196 std::string unit;
197 bool valid = false;
198 };
199 using NameUnitList = std::vector<Header>;
200 NameUnitList header_list_;
201
206 void SaveText(const std::string& text);
207};
208
209template <typename T>
210void CsvWriter::AddColumnValue(const T& value) {
211 auto temp = std::to_string(value);
212 SaveText(temp);
213 if (row_count_ == 0) {
214 max_columns_ = std::max(column_count_, max_columns_);
215 }
216}
217
223template <>
224void CsvWriter::AddColumnValue<std::string>(const std::string& value);
225
230template <>
231void CsvWriter::AddColumnValue<float>(const float& value);
232
238template <>
239void CsvWriter::AddColumnValue<double>(const double& value);
240
245template <>
246void CsvWriter::AddColumnValue<bool>(const bool& value);
247} // namespace util::plot
bool IsOk() const
Returns true if the file is successfully opened.
bool IsColumnValid(size_t column) const
Indicate if there is any valid values in this column.
const std::string Label(size_t index) const
Returns suitable label text.
CsvWriter(const std::string &filename)
Constructor that opens a CSV file.
const std::string & Name(size_t index) const
Returns column name.
CsvWriter()
Constructor that opens a string stream instead of a physical file .
void OpenFile(const std::string &filename)
void AddRow()
Starts a new row. S.
void AddColumnHeader(const std::string &name, const std::string &unit, bool valid=true)
Adds a column header string.
const std::string & FullName() const
Definition csvwriter.h:71
size_t Columns() const
Returns number of columns.
Definition csvwriter.h:147
void Reset()
Reset the writer.
void SetColumnValid(size_t column, bool valid=true)
Sets the column valid.
std::string GetText() const
Returns the CSV 'file' as a string.
Definition csvwriter.h:134
void AddColumnValue(const T &value)
Adds a column value. Adds a column value to the file.
Definition csvwriter.h:209
const std::string & Unit(size_t index) const
Returns the column unit.
size_t Rows() const
Number of rows in the CSV file.
Definition csvwriter.h:155
virtual ~CsvWriter()
Destructor that closes the CSV file.
std::string FileName() const
The plot namespace is used for plotting related classes and functions.
Definition csvwriter.h:16