UTILLib 2.0
Common C++ library with utilities.
Loading...
Searching...
No Matches
util::plot::CsvWriter Class Reference

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.

Detailed Description

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:

CsvWriter csv_file(filename); // Opens the file. The filename shall have full
path. csv_file.AddColumnHeader("Donald","duck");
csv_file.AddColumnHeader("Micky, "mouse");
csv_file.AddRow();
csv_file.AddColumnValue(value1);
csv_file.AddColumnValue(value2);
csv_file.CloseFile(); // Closes the file. Running the destructor will do the
same.
CsvWriter(const std::string &filename)
Constructor that opens a CSV file.

Constructor & Destructor Documentation

◆ CsvWriter() [1/2]

util::plot::CsvWriter::CsvWriter ( const std::string & filename)
explicit

This constructor opens the CSV file. Note that the destructor closes the file.

Parameters
filenameFile name with full path and extension.

◆ CsvWriter() [2/2]

util::plot::CsvWriter::CsvWriter ( )

This constructor uses an internal string stream instead of a physical file.

Member Function Documentation

◆ AddColumnHeader()

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]'.

Parameters
nameThe column name.
unitThe column unit
validTrue if the column is valid.

◆ AddColumnValue() [1/4]

template<>
void util::plot::CsvWriter::AddColumnValue ( const bool & value)
Parameters
valueboolean value

◆ AddColumnValue() [2/4]

template<>
void util::plot::CsvWriter::AddColumnValue ( const double & value)

Adds a double value.

Parameters
valueDouble value

◆ AddColumnValue() [3/4]

template<>
void util::plot::CsvWriter::AddColumnValue ( const float & value)
Parameters
valueFloat value

◆ AddColumnValue() [4/4]

template<typename T>
void util::plot::CsvWriter::AddColumnValue ( const T & value)
Template Parameters
TType of value
Parameters
valueThe value to save to the file.

◆ CloseFile()

void util::plot::CsvWriter::CloseFile ( )

Closes the CSV file.

◆ Columns()

size_t util::plot::CsvWriter::Columns ( ) const
inlinenodiscard

Returns number of columns in the file.

Returns
Number of columns.

◆ FileName()

std::string util::plot::CsvWriter::FileName ( ) const
nodiscard

Returns the file name without path but with it's extension.

Returns
Returns the file name without path.

◆ FullName()

const std::string & util::plot::CsvWriter::FullName ( ) const
inlinenodiscard

Returns the file name with full path

Returns
The file name

◆ GetText()

std::string util::plot::CsvWriter::GetText ( ) const
inlinenodiscard

Returns the CSV file as a string.

Returns
CSV content as a string.

◆ IsColumnValid()

bool util::plot::CsvWriter::IsColumnValid ( size_t column) const
nodiscard

This function returns true if this column have any valid values. Plotting applications typically need this functionality.

Parameters
columnColumn index
Returns
True if column have any valid values.

◆ IsOk()

bool util::plot::CsvWriter::IsOk ( ) const
nodiscard

Checks if the file is open and if so return true.

Returns
True if file is open.

◆ Label()

const std::string util::plot::CsvWriter::Label ( size_t index) const
nodiscard

Returns a suitable label text for a column.

Parameters
indexColumn index.
Returns
Label text.

◆ Name()

const std::string & util::plot::CsvWriter::Name ( size_t index) const
nodiscard

Returns the column name.

Parameters
indexColumn index.
Returns
Column name.

◆ OpenFile()

void util::plot::CsvWriter::OpenFile ( const std::string & filename)

Opens a file for writing. Any existing file will be truncated

Parameters
filenameFull path file name of the CSV file

◆ Reset()

void util::plot::CsvWriter::Reset ( )

Rests the writer object so it can be reused.

◆ Rows()

size_t util::plot::CsvWriter::Rows ( ) const
inlinenodiscard

Returns number of rows in the writer. Note first row is typical value name and unit.

Returns
Number of rows.

◆ SetColumnValid()

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.

Parameters
columnColumn index
validSets the column valid.

◆ Unit()

const std::string & util::plot::CsvWriter::Unit ( size_t index) const
nodiscard

Returns the column unit.

Parameters
indexDColumn index.
Returns
Column unit.

The documentation for this class was generated from the following file: