Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
core_lib::csv_grid::TCsvGrid< C, T > Class Template Referencefinal

Grid class with CSV file capabilities. More...

#include <CsvGridMain.h>

Public Types

using row_type = TRow< C, T >
 typedef for row type
 
using container_type = C< row_type, std::allocator< row_type > >
 typedef for container type
 

Public Member Functions

 TCsvGrid ()=default
 Default constructor.
 
 TCsvGrid (const TCsvGrid &)=default
 Copy constructor.
 
 TCsvGrid (TCsvGrid &&)=default
 Move constructor.
 
 TCsvGrid (size_t rows, size_t cols)
 Initializing constructor. More...
 
 TCsvGrid (const std::string &filename, eCellFormatOptions options)
 Initializing constructor. More...
 
 TCsvGrid (std::initializer_list< row_type > rows)
 Initializer list constructor. More...
 
 ~TCsvGrid ()=default
 Ddestructor.
 
TCsvGridoperator= (const TCsvGrid &)=default
 Copy assignment operator.
 
TCsvGridoperator= (TCsvGrid &&)=default
 Move assignment operator.
 
row_typeoperator[] (size_t row)
 Subscript operator. More...
 
const row_typeoperator[] (size_t row) const
 Const subscript operator. More...
 
bool Empty () const
 Get empty state of grid. More...
 
size_t GetRowCount () const
 Get the number of rows. More...
 
size_t GetColCount (size_t row) const
 Get the number of columns for a given row. More...
 
void SetRowCount (size_t rows, size_t defaultCols=0)
 Resize the grid. More...
 
void AddRow (size_t cols=0)
 Add a new row. More...
 
void AddColumnToAllRows ()
 Add a column to each row. More...
 
void InsertRow (size_t row, size_t defaultCols=0)
 Insert a new row. More...
 
void InsertColumnInAllRows (size_t col)
 Insert a new column in all rows. More...
 
void ClearCells ()
 Clear the contents of all cells. More...
 
void ResetGrid ()
 Clear the entire grid. More...
 
void LoadFromCSVFile (const std::string &filename, eCellFormatOptions options, size_t firstRowToLoad=0, size_t maxNumRowsToLoad=std::numeric_limits< size_t >::max())
 Load a csv file into the grid. More...
 
void SaveToCsvFile (const std::string &filename, eSaveToFileOptions option=eSaveToFileOptions::truncate) const
 Save the grid to a CSV file. More...
 

Private Member Functions

void OutputCsvGridToStream (std::ostream &os) const
 Output the grid to a stream object. More...
 

Static Private Member Functions

static bool ContainsEndOfRow (const std::string &row)
 Check if row string contains the actual end of the CSV row. More...
 

Private Attributes

container_type m_grid {}
 The grid row data.
 

Detailed Description

template<template< class, class > class C, class T = Cell>
class core_lib::csv_grid::TCsvGrid< C, T >

Grid class with CSV file capabilities.

This class provides an implemtnation of an easy-to-use managed grid that can contain strings, integers or floating point data in any combination. Although in its standard setup data is internally stored as a std::string due to the implict text-based nature of a CSV file.

The grid can optionally be initialised by loading data from a CSV file. The grid is able to save its contents to CSV file. It can also be streamed to any compatible stream object using the << operator.

The grid class handles both rectangular and jagged data and CSV fles. Jagged data contains different number of columns for different rows.

The default behaviour is to be able to handle CSV files that may have their cells contained in double quotes. There is a slight performance overhead associated with this because extra parsing is required to tokenize each line. If it is known that the CSV file contains simple data, e.g. no cells in the CSV file are in double quotes, then the user can disable double quote handling, which gives a slight performance increase. With large data sets the performance increase can be significant.

Note
This class loads the entire CSV file into memory so if the file is particulary large you will crash your program if you do not have enough free memory. However, if you know in advance from what row number you want to start readingdata from a CSV file and how many rows you want to read then you can sepcify these in the LoadFromCSVFile method.

Constructor & Destructor Documentation

◆ TCsvGrid() [1/3]

template<template< class, class > class C, class T = Cell>
core_lib::csv_grid::TCsvGrid< C, T >::TCsvGrid ( size_t  rows,
size_t  cols 
)
inline

Initializing constructor.

Parameters
[in]rows- The number of rows.
[in]cols- The number of columns.

Create the rectangular grid object with a non-zero number of rows and columns. If rows or columns are 0 then std::out_of_range exception is thrown.

◆ TCsvGrid() [2/3]

template<template< class, class > class C, class T = Cell>
core_lib::csv_grid::TCsvGrid< C, T >::TCsvGrid ( const std::string &  filename,
eCellFormatOptions  options 
)
inline

Initializing constructor.

Parameters
[in]filename- The full path name of the CSV file to load.
[in]options- Cell formatting options.

Create a grid object from a CSV file. If cells are wrapped in double quotes in the CSV file then set options = doubleQuotedCells else set options = simpleCells. Throw a std::runtime_error exception if the file cannot be loaded.

◆ TCsvGrid() [3/3]

template<template< class, class > class C, class T = Cell>
core_lib::csv_grid::TCsvGrid< C, T >::TCsvGrid ( std::initializer_list< row_type rows)
inline

Initializer list constructor.

Parameters
[in]rows- The initial list of rows.

Create the CsvGrid from the given list of rows.

Member Function Documentation

◆ AddColumnToAllRows()

template<template< class, class > class C, class T = Cell>
void core_lib::csv_grid::TCsvGrid< C, T >::AddColumnToAllRows ( )
inline

Add a column to each row.

Resize the grid, adding a new column to each row.

◆ AddRow()

template<template< class, class > class C, class T = Cell>
void core_lib::csv_grid::TCsvGrid< C, T >::AddRow ( size_t  cols = 0)
inline

Add a new row.

Parameters
[in]cols- The default number of cells for the new row.

Resize the grid, adding a new row with the given number of cells.

◆ ClearCells()

template<template< class, class > class C, class T = Cell>
void core_lib::csv_grid::TCsvGrid< C, T >::ClearCells ( )
inline

Clear the contents of all cells.

The contents of each cell in the grid is cleared but the row and column counts remain unchanged.

◆ ContainsEndOfRow()

template<template< class, class > class C, class T = Cell>
static bool core_lib::csv_grid::TCsvGrid< C, T >::ContainsEndOfRow ( const std::string &  row)
inlinestaticprivate

Check if row string contains the actual end of the CSV row.

Parameters
[in]row- The row string object.
Returns
True if contains valid row end, false otherwise.

If row contains an odd number of " chars then there must be a new line within a cell on the real row i nthe CSV file. If there are 0 or an even number of " chars in row then it must represent a complete row from the CSV file.

◆ Empty()

template<template< class, class > class C, class T = Cell>
bool core_lib::csv_grid::TCsvGrid< C, T >::Empty ( ) const
inline

Get empty state of grid.

Returns
True if grid is empty.

◆ GetColCount()

template<template< class, class > class C, class T = Cell>
size_t core_lib::csv_grid::TCsvGrid< C, T >::GetColCount ( size_t  row) const
inline

Get the number of columns for a given row.

Parameters
[in]row- A 0-based row index.
Returns
The number of columns for this row.
Note
If the index is out of bounds a std::out_of_range exception is thrown.

◆ GetRowCount()

template<template< class, class > class C, class T = Cell>
size_t core_lib::csv_grid::TCsvGrid< C, T >::GetRowCount ( ) const
inline

Get the number of rows.

Returns
The number of rows for this grid.

◆ InsertColumnInAllRows()

template<template< class, class > class C, class T = Cell>
void core_lib::csv_grid::TCsvGrid< C, T >::InsertColumnInAllRows ( size_t  col)
inline

Insert a new column in all rows.

Parameters
[in]col- The column index at which the new column is to be inserted.

The column is only inserted if the column index is within range of the row in the grid otherwise a column is not added to the row.

◆ InsertRow()

template<template< class, class > class C, class T = Cell>
void core_lib::csv_grid::TCsvGrid< C, T >::InsertRow ( size_t  row,
size_t  defaultCols = 0 
)
inline

Insert a new row.

Parameters
[in]row- The row index at which the new row is to be inserted.
[in]defaultCols- The default number of columns for the new row.

Insert a new row at a given row index in the grid. If the row index is out of range a std::out_of_range exception is thrown.

◆ LoadFromCSVFile()

template<template< class, class > class C, class T = Cell>
void core_lib::csv_grid::TCsvGrid< C, T >::LoadFromCSVFile ( const std::string &  filename,
eCellFormatOptions  options,
size_t  firstRowToLoad = 0,
size_t  maxNumRowsToLoad = std::numeric_limits<size_t>::max() 
)
inline

Load a csv file into the grid.

Parameters
[in]filename- The full path name of the CSV file to load.
[in]options- Cell formating options.
[in]firstRowToLoad- (Optional) First row to load into the grid (zero-based).
[in]maxNumRowsToLoad- (Optional) Limit number of rows read in to grid.

Create a grid object from a CSV file. If cells are wrapped in double quotes in the CSV file then set options = doubleQuotedCells else set options = simpleCells. If the file stream cannot be created or opened the a std::runtime_error exception is thrown.

◆ operator[]() [1/2]

template<template< class, class > class C, class T = Cell>
row_type& core_lib::csv_grid::TCsvGrid< C, T >::operator[] ( size_t  row)
inline

Subscript operator.

Parameters
[in]row- A 0-based row index.
Returns
The row at the given row index.

Retrieve the row at a given row index within a grid.

Note
If the index is out of bounds a std::out_of_range exception is thrown.

◆ operator[]() [2/2]

template<template< class, class > class C, class T = Cell>
const row_type& core_lib::csv_grid::TCsvGrid< C, T >::operator[] ( size_t  row) const
inline

Const subscript operator.

Parameters
[in]row- A 0-based row index.
Returns
The row at the given row index.

Retrieve the row at a given row index within a grid.

Note
If the index is out of bounds a std::out_of_range exception is thrown.

◆ OutputCsvGridToStream()

template<template< class, class > class C, class T = Cell>
void core_lib::csv_grid::TCsvGrid< C, T >::OutputCsvGridToStream ( std::ostream &  os) const
inlineprivate

Output the grid to a stream object.

Parameters
[in]os- The stream object.

Write the grid in CSV format to a stream object.

◆ ResetGrid()

template<template< class, class > class C, class T = Cell>
void core_lib::csv_grid::TCsvGrid< C, T >::ResetGrid ( )
inline

Clear the entire grid.

All rows are removed from the grid leaving a row count of 0 afterwards.

◆ SaveToCsvFile()

template<template< class, class > class C, class T = Cell>
void core_lib::csv_grid::TCsvGrid< C, T >::SaveToCsvFile ( const std::string &  filename,
eSaveToFileOptions  option = eSaveToFileOptions::truncate 
) const
inline

Save the grid to a CSV file.

Parameters
[in]filename- The full path name of the CSV file to load.
[in]option- (Optional) Save to file options: append to or overwrite existing file.

Create a CSV file from a grid object. If the file stream cannot be created or opened the a std::runtime_error exception is thrown.

◆ SetRowCount()

template<template< class, class > class C, class T = Cell>
void core_lib::csv_grid::TCsvGrid< C, T >::SetRowCount ( size_t  rows,
size_t  defaultCols = 0 
)
inline

Resize the grid.

Parameters
[in]rows- The number of rows.
[in]defaultCols- The number of columns for newly created rows.

Resize the grid, adding or dropping rows as necessary.


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