Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
Euclid::Table::AsciiReader Class Reference

TableReader implementation for reading ASCII tables from streams. More...

#include <AsciiReader.h>

Inheritance diagram for Euclid::Table::AsciiReader:
Inheritance graph
[legend]
Collaboration diagram for Euclid::Table::AsciiReader:
Collaboration graph
[legend]

Public Member Functions

 AsciiReader (std::istream &stream)
 Constructs an AsciiReader which reads from the given stream.
 
 AsciiReader (const std::string &filename)
 Constructs an AsciiReader which reads from the given file.
 
 AsciiReader (AsciiReader &&)=default
 
AsciiReaderoperator= (AsciiReader &&)=default
 
 AsciiReader (const AsciiReader &)=delete
 
AsciiReaderoperator= (const AsciiReader &)=delete
 
virtual ~AsciiReader ()=default
 Destructor.
 
AsciiReadersetCommentIndicator (const std::string &indicator)
 Set the comment indicator.
 
AsciiReaderfixColumnNames (std::vector< std::string > column_names)
 Overrides the automatically detected column names.
 
AsciiReaderfixColumnTypes (std::vector< std::type_index > column_types)
 Overrides the automatically detected column types.
 
AsciiReaderfixColumnTypes (std::vector< std::pair< std::type_index, std::size_t > > column_types)
 
const ColumnInfogetInfo () override
 Returns the column information of the table.
 
std::string getComment () override
 
void skip (long rows) override
 Implements the TableReader::skip() contract.
 
bool hasMoreRows () override
 Implements the TableReader::hasMoreRows() contract.
 
std::size_t rowsLeft () override
 Implements the TableReader::rowsLeft() contract.
 
- Public Member Functions inherited from Euclid::Table::TableReader
 TableReader ()=default
 
 TableReader (TableReader &&)=default
 
TableReaderoperator= (TableReader &&)=default
 
 TableReader (const TableReader &)=delete
 
TableReaderoperator= (const TableReader &)=delete
 
virtual ~TableReader ()=default
 
Table read (long rows=-1)
 Reads next rows as a table.
 

Static Public Member Functions

template<typename StreamType , typename... Args>
static AsciiReader create (Args &&... args)
 Constructs an AsciiReader which contains an object of type StreamType.
 

Protected Member Functions

Table readImpl (long rows) override
 Reads the next rows into a Table.
 

Private Member Functions

 AsciiReader (std::unique_ptr< InstOrRefHolder< std::istream > > stream_holder)
 
void readColumnInfo ()
 

Private Attributes

std::unique_ptr< InstOrRefHolder< std::istream > > m_stream_holder
 
bool m_reading_started = false
 
std::string m_comment = "#"
 
std::vector< std::pair< std::type_index, std::size_t > > m_column_types {}
 
std::vector< std::stringm_column_names {}
 
std::shared_ptr< ColumnInfom_column_info
 

Detailed Description

TableReader implementation for reading ASCII tables from streams.

The format of the ASCII tables this class can read is the following:

Comment are supported and by default the comment character is the #. This can be modified using the setCommentIndicator() method.

The table can contain in its comments (before the data starts) the descriptions of the columns. This is done by comment lines following the format: "Column: NAME TYPE (UNIT) - DESCRIPTION", where the type, unit and description parts are optional. Note that if the type is missing, the column is read as a string column.

The strings which can be used as the column types are:

The last non empty comment line (before the data) can repeat the column names and, if the column description comments are missing, is used for detecting the column names. If both the column descriptions and the line with the column names are missing, the columns are named with an increasing counter as "col1", "col2", etc (starting from 1).

If the column names comment is present, the column descriptions can be missing and be given in any order.

The above automatic detection of the names and types of the columns can be overridden by used defined values, by calling the fixColumnNames() and fixColumnTypes() methods.

Note that the vector entries are values separated by "," (no spaces).

The stream is red line by line and one row is created for each line which, after comments are removed, does not contain only whitespace characters. The columns are separated by one or more whitespace characters and all rows must have the same number of columns.

Definition at line 87 of file AsciiReader.h.

Constructor & Destructor Documentation

◆ AsciiReader() [1/5]

Euclid::Table::AsciiReader::AsciiReader ( std::istream stream)
explicit

Constructs an AsciiReader which reads from the given stream.

Definition at line 53 of file AsciiReader.cpp.

◆ AsciiReader() [2/5]

Euclid::Table::AsciiReader::AsciiReader ( const std::string filename)
explicit

Constructs an AsciiReader which reads from the given file.

Definition at line 55 of file AsciiReader.cpp.

◆ AsciiReader() [3/5]

Euclid::Table::AsciiReader::AsciiReader ( AsciiReader &&  )
default

◆ AsciiReader() [4/5]

Euclid::Table::AsciiReader::AsciiReader ( const AsciiReader )
delete

◆ ~AsciiReader()

virtual Euclid::Table::AsciiReader::~AsciiReader ( )
virtualdefault

Destructor.

◆ AsciiReader() [5/5]

Euclid::Table::AsciiReader::AsciiReader ( std::unique_ptr< InstOrRefHolder< std::istream > >  stream_holder)
explicitprivate

Definition at line 57 of file AsciiReader.cpp.

Member Function Documentation

◆ create()

template<typename StreamType , typename... Args>
static AsciiReader Euclid::Table::AsciiReader::create ( Args &&...  args)
static

Constructs an AsciiReader which contains an object of type StreamType.

This is the most generic construction of AsciiReader, which can be used with any type which inherits from std::istream. It is public to provide full flexibility, but the other constructors should cover most of the use cases of this class. When this constructor is used, an object of type StreamType is constructed using the given arguments, which will be deleted when the AsciiReader goes out of scope. If you want to not bound the lifetime of the stream with the AsciiReader you should use the AsciiReader(std::istream&) constructor instead.

Note that when this method is called, only the StreamType template parameter has to be specified. The argument types will be inferred.

Template Parameters
StreamTypeThe type of the stream to use for reading
ArgsThe types of arguments to pass to the StreamType constructor
Parameters
argsThe arguments to use for constructing the StreamType instance
Returns
The newly constructed AsciiReader

◆ fixColumnNames()

AsciiReader & Euclid::Table::AsciiReader::fixColumnNames ( std::vector< std::string column_names)

Overrides the automatically detected column names.

Parameters
column_namesThe names of the columns or empty for auto-detection
Returns
A reference to the AsciiReader instance
Exceptions
Elements::Exceptionif there are duplicate column names
Elements::Exceptionif any of the given column names is empty or contains whitespace characters
Elements::Exceptionif automatic column type detection is overridden and the length of the vectors does not match

Definition at line 72 of file AsciiReader.cpp.

References std::vector< T >::empty(), m_column_names, m_column_types, m_reading_started, std::move(), and std::vector< T >::size().

Here is the call graph for this function:

◆ fixColumnTypes() [1/2]

AsciiReader & Euclid::Table::AsciiReader::fixColumnTypes ( std::vector< std::pair< std::type_index, std::size_t > >  column_types)

Definition at line 118 of file AsciiReader.cpp.

References std::vector< T >::empty(), m_column_names, m_column_types, m_reading_started, std::move(), and std::vector< T >::size().

Here is the call graph for this function:

◆ fixColumnTypes() [2/2]

AsciiReader & Euclid::Table::AsciiReader::fixColumnTypes ( std::vector< std::type_index column_types)

Overrides the automatically detected column types.

Parameters
column_typesThe types of the columns or empty for auto-detection
Returns
A reference to the AsciiReader instance
Exceptions
Elements::Exceptionif automatic column name detection is overridden and the length of the vectors does not match

Definition at line 102 of file AsciiReader.cpp.

References std::back_inserter(), std::vector< T >::begin(), std::vector< T >::empty(), std::vector< T >::end(), m_column_names, m_column_types, m_reading_started, std::vector< T >::size(), and std::transform().

Here is the call graph for this function:

◆ getComment()

std::string Euclid::Table::AsciiReader::getComment ( )
overridevirtual
Returns
Returns the comment associated to the table

Implements Euclid::Table::TableReader.

Definition at line 195 of file AsciiReader.cpp.

References Euclid::Table::_peekLine(), m_comment, m_reading_started, m_stream_holder, std::string::size(), std::ostringstream::str(), and std::string::substr().

Here is the call graph for this function:

◆ getInfo()

const ColumnInfo & Euclid::Table::AsciiReader::getInfo ( )
overridevirtual

Returns the column information of the table.

For more details of the column info definition in the stream, see the documentation of the class.

Returns
The description of the table columns
Exceptions
Elements::ExceptionIf automatic column type or name detection is overridden and the stream contains a different number of columns
Elements::Exceptionif column name auto-detection is enabled and there are duplicate names

Implements Euclid::Table::TableReader.

Definition at line 190 of file AsciiReader.cpp.

References m_column_info, and readColumnInfo().

Here is the call graph for this function:

◆ hasMoreRows()

bool Euclid::Table::AsciiReader::hasMoreRows ( )
overridevirtual

Implements the TableReader::hasMoreRows() contract.

Implements Euclid::Table::TableReader.

Definition at line 262 of file AsciiReader.cpp.

References Euclid::Table::hasNextRow(), m_comment, and m_stream_holder.

Here is the call graph for this function:

◆ operator=() [1/2]

AsciiReader & Euclid::Table::AsciiReader::operator= ( AsciiReader &&  )
default

◆ operator=() [2/2]

AsciiReader & Euclid::Table::AsciiReader::operator= ( const AsciiReader )
delete

◆ readColumnInfo()

void Euclid::Table::AsciiReader::readColumnInfo ( )
private

◆ readImpl()

Table Euclid::Table::AsciiReader::readImpl ( long  rows)
overrideprotectedvirtual

Reads the next rows into a Table.

For more info see the TableReader::read() documentation

Parameters
rowsThe number of rows to read
Returns
The table containing the row data
Exceptions
Elements::ExceptionIf automatic column type or name detection is overridden and the stream contains a different number of columns
Elements::Exceptionif the stream does not contain any more non comment lines
Elements::Exceptionif any cell cannot be converted to the correct type

Implements Euclid::Table::TableReader.

Definition at line 213 of file AsciiReader.cpp.

References std::back_inserter(), std::vector< T >::empty(), m_column_info, m_comment, m_stream_holder, std::move(), std::vector< T >::push_back(), readColumnInfo(), std::vector< T >::reserve(), Euclid::Table::splitLine(), and std::transform().

Here is the call graph for this function:

◆ rowsLeft()

std::size_t Euclid::Table::AsciiReader::rowsLeft ( )
overridevirtual

Implements the TableReader::rowsLeft() contract.

Implements Euclid::Table::TableReader.

Definition at line 266 of file AsciiReader.cpp.

References Euclid::Table::countRemainingRows(), m_comment, and m_stream_holder.

Here is the call graph for this function:

◆ setCommentIndicator()

AsciiReader & Euclid::Table::AsciiReader::setCommentIndicator ( const std::string indicator)

Set the comment indicator.

This method can be used to change the comment indicator to something different than the default (#). It returns a reference to the AsciiReader so it can be chained with other calls in the same line.

Exceptions
Elements::ExceptionIf the given indicator is the empty string

Definition at line 60 of file AsciiReader.cpp.

References std::string::empty(), m_comment, and m_reading_started.

Here is the call graph for this function:

◆ skip()

void Euclid::Table::AsciiReader::skip ( long  rows)
overridevirtual

Implements the TableReader::skip() contract.

Implements Euclid::Table::TableReader.

Definition at line 244 of file AsciiReader.cpp.

References std::string::empty(), std::string::find(), m_comment, m_stream_holder, readColumnInfo(), and std::string::substr().

Here is the call graph for this function:

Member Data Documentation

◆ m_column_info

std::shared_ptr<ColumnInfo> Euclid::Table::AsciiReader::m_column_info
private

Definition at line 234 of file AsciiReader.h.

Referenced by getInfo(), readColumnInfo(), and readImpl().

◆ m_column_names

std::vector<std::string> Euclid::Table::AsciiReader::m_column_names {}
private

Definition at line 233 of file AsciiReader.h.

Referenced by fixColumnNames(), fixColumnTypes(), fixColumnTypes(), and readColumnInfo().

◆ m_column_types

std::vector<std::pair<std::type_index, std::size_t> > Euclid::Table::AsciiReader::m_column_types {}
private

Definition at line 232 of file AsciiReader.h.

Referenced by fixColumnNames(), fixColumnTypes(), fixColumnTypes(), and readColumnInfo().

◆ m_comment

std::string Euclid::Table::AsciiReader::m_comment = "#"
private

◆ m_reading_started

bool Euclid::Table::AsciiReader::m_reading_started = false
private

◆ m_stream_holder

std::unique_ptr<InstOrRefHolder<std::istream> > Euclid::Table::AsciiReader::m_stream_holder
private

Definition at line 229 of file AsciiReader.h.

Referenced by getComment(), hasMoreRows(), readColumnInfo(), readImpl(), rowsLeft(), and skip().


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