Alexandria 2.31.0
SDC-CH common library for the Euclid project
|
TableReader implementation for reading ASCII tables from streams. More...
#include <AsciiReader.h>
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 | |
AsciiReader & | operator= (AsciiReader &&)=default |
AsciiReader (const AsciiReader &)=delete | |
AsciiReader & | operator= (const AsciiReader &)=delete |
virtual | ~AsciiReader ()=default |
Destructor. | |
AsciiReader & | setCommentIndicator (const std::string &indicator) |
Set the comment indicator. | |
AsciiReader & | fixColumnNames (std::vector< std::string > column_names) |
Overrides the automatically detected column names. | |
AsciiReader & | fixColumnTypes (std::vector< std::type_index > column_types) |
Overrides the automatically detected column types. | |
AsciiReader & | fixColumnTypes (std::vector< std::pair< std::type_index, std::size_t > > column_types) |
const ColumnInfo & | getInfo () 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. | |
![]() | |
TableReader ()=default | |
TableReader (TableReader &&)=default | |
TableReader & | operator= (TableReader &&)=default |
TableReader (const TableReader &)=delete | |
TableReader & | operator= (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::string > | m_column_names {} |
std::shared_ptr< ColumnInfo > | m_column_info |
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.
|
explicit |
Constructs an AsciiReader which reads from the given stream.
Definition at line 53 of file AsciiReader.cpp.
|
explicit |
Constructs an AsciiReader which reads from the given file.
Definition at line 55 of file AsciiReader.cpp.
|
default |
|
delete |
|
virtualdefault |
Destructor.
|
explicitprivate |
Definition at line 57 of file AsciiReader.cpp.
|
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.
StreamType | The type of the stream to use for reading |
Args | The types of arguments to pass to the StreamType constructor |
args | The arguments to use for constructing the StreamType instance |
AsciiReader & Euclid::Table::AsciiReader::fixColumnNames | ( | std::vector< std::string > | column_names | ) |
Overrides the automatically detected column names.
column_names | The names of the columns or empty for auto-detection |
Elements::Exception | if there are duplicate column names |
Elements::Exception | if any of the given column names is empty or contains whitespace characters |
Elements::Exception | if 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().
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().
AsciiReader & Euclid::Table::AsciiReader::fixColumnTypes | ( | std::vector< std::type_index > | column_types | ) |
Overrides the automatically detected column types.
column_types | The types of the columns or empty for auto-detection |
Elements::Exception | if 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().
|
overridevirtual |
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().
|
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.
Elements::Exception | If automatic column type or name detection is overridden and the stream contains a different number of columns |
Elements::Exception | if 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().
|
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.
|
default |
|
delete |
|
private |
Definition at line 133 of file AsciiReader.cpp.
References Euclid::Table::autoDetectColumnDescriptions(), Euclid::Table::autoDetectColumnNames(), Euclid::Table::countColumns(), Euclid::Table::createColumnInfo(), std::vector< T >::emplace_back(), std::vector< T >::empty(), Euclid::Table::firstDataLine(), Euclid::Table::guessColumnType(), m_column_info, m_column_names, m_column_types, m_comment, m_reading_started, m_stream_holder, and std::vector< T >::size().
Referenced by getInfo(), readImpl(), and skip().
|
overrideprotectedvirtual |
Reads the next rows into a Table.
For more info see the TableReader::read() documentation
rows | The number of rows to read |
Elements::Exception | If automatic column type or name detection is overridden and the stream contains a different number of columns |
Elements::Exception | if the stream does not contain any more non comment lines |
Elements::Exception | if 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().
|
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.
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.
Elements::Exception | If 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.
|
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().
|
private |
Definition at line 234 of file AsciiReader.h.
Referenced by getInfo(), readColumnInfo(), and readImpl().
|
private |
Definition at line 233 of file AsciiReader.h.
Referenced by fixColumnNames(), fixColumnTypes(), fixColumnTypes(), and readColumnInfo().
|
private |
Definition at line 232 of file AsciiReader.h.
Referenced by fixColumnNames(), fixColumnTypes(), fixColumnTypes(), and readColumnInfo().
|
private |
Definition at line 231 of file AsciiReader.h.
Referenced by getComment(), hasMoreRows(), readColumnInfo(), readImpl(), rowsLeft(), setCommentIndicator(), and skip().
|
private |
Definition at line 230 of file AsciiReader.h.
Referenced by fixColumnNames(), fixColumnTypes(), fixColumnTypes(), getComment(), readColumnInfo(), and setCommentIndicator().
|
private |
Definition at line 229 of file AsciiReader.h.
Referenced by getComment(), hasMoreRows(), readColumnInfo(), readImpl(), rowsLeft(), and skip().