UTILLib 2.0
Common C++ library with utilities.
|
Simple writer interface when creating a CSV file (comma separated value). More...
#include "util/csvwriter.h"
Public Member Functions | |
CsvWriter (const std::string &filename) | |
Constructor that opens a CSV file. | |
CsvWriter () | |
Constructor that opens a string stream instead of a physical file . | |
virtual | ~CsvWriter () |
Destructor that closes the CSV file. | |
void | CloseFile () |
std::string | FileName () const |
const std::string & | FullName () const |
void | OpenFile (const std::string &filename) |
bool | IsOk () const |
Returns true if the file is successfully opened. | |
void | AddColumnHeader (const std::string &name, const std::string &unit, bool valid=true) |
Adds a column header string. | |
void | SetColumnValid (size_t column, bool valid=true) |
Sets the column valid. | |
bool | IsColumnValid (size_t column) const |
Indicate if there is any valid values in this column. | |
void | AddRow () |
Starts a new row. S. | |
template<typename T> | |
void | AddColumnValue (const T &value) |
Adds a column value. Adds a column value to the file. | |
std::string | GetText () const |
Returns the CSV 'file' as a string. | |
void | Reset () |
Reset the writer. | |
size_t | Columns () const |
Returns number of columns. | |
size_t | Rows () const |
Number of rows in the CSV file. | |
const std::string | Label (size_t index) const |
Returns suitable label text. | |
const std::string & | Name (size_t index) const |
Returns column name. | |
const std::string & | Unit (size_t index) const |
Returns the column unit. | |
template<> | |
void | AddColumnValue (const float &value) |
Adds a column float value. | |
template<> | |
void | AddColumnValue (const double &value) |
Adds a column double value. | |
template<> | |
void | AddColumnValue (const bool &value) |
Adds a boolean column value. |
The class simplifies writing of CSV files. It fixes all tricks about floating point dots which are comma in some countries. The first row in a CSV file may be the values name wnd unit, typically formatted as 'name [unit]'.
Usage:
|
explicit |
This constructor opens the CSV file. Note that the destructor closes the file.
filename | File name with full path and extension. |
util::plot::CsvWriter::CsvWriter | ( | ) |
This constructor uses an internal string stream instead of a physical file.
void util::plot::CsvWriter::AddColumnHeader | ( | const std::string & | name, |
const std::string & | unit, | ||
bool | valid = true ) |
Adds a header string to the first row in the file. Normally is the header text 'name [unit]'.
name | The column name. |
unit | The column unit |
valid | True if the column is valid. |
void util::plot::CsvWriter::AddColumnValue | ( | const bool & | value | ) |
value | boolean value |
void util::plot::CsvWriter::AddColumnValue | ( | const double & | value | ) |
Adds a double value.
value | Double value |
void util::plot::CsvWriter::AddColumnValue | ( | const float & | value | ) |
value | Float value |
void util::plot::CsvWriter::AddColumnValue | ( | const T & | value | ) |
T | Type of value |
value | The value to save to the file. |
void util::plot::CsvWriter::CloseFile | ( | ) |
Closes the CSV file.
|
inlinenodiscard |
Returns number of columns in the file.
|
nodiscard |
Returns the file name without path but with it's extension.
|
inlinenodiscard |
Returns the file name with full path
|
inlinenodiscard |
Returns the CSV file as a string.
|
nodiscard |
This function returns true if this column have any valid values. Plotting applications typically need this functionality.
column | Column index |
|
nodiscard |
Checks if the file is open and if so return true.
|
nodiscard |
Returns a suitable label text for a column.
index | Column index. |
|
nodiscard |
Returns the column name.
index | Column index. |
void util::plot::CsvWriter::OpenFile | ( | const std::string & | filename | ) |
Opens a file for writing. Any existing file will be truncated
filename | Full path file name of the CSV file |
void util::plot::CsvWriter::Reset | ( | ) |
Rests the writer object so it can be reused.
|
inlinenodiscard |
Returns number of rows in the writer. Note first row is typical value name and unit.
void util::plot::CsvWriter::SetColumnValid | ( | size_t | column, |
bool | valid = true ) |
This function is somewhat strange but it indicate if all values are invalid or at least one or more value are valid. This is used when plotting the columns.
column | Column index |
valid | Sets the column valid. |
|
nodiscard |
Returns the column unit.
index | DColumn index. |