GEOS
TableFormatter.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 TotalEnergies
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_COMMON_FORMAT_TABLE_TABLEFORMATTER_HPP
21 #define GEOS_COMMON_FORMAT_TABLE_TABLEFORMATTER_HPP
22 
23 #include "TableData.hpp"
24 #include "TableLayout.hpp"
25 #include "TableTypes.hpp"
26 
27 namespace geos
28 {
29 
34 {
35 
36 public:
37 
40 
43  {
46 
48  size_t sublinesCount;
49  };
50 
53 
58  { return *m_errors; }
59 
60 protected:
61 
65  std::unique_ptr< geos::TableErrorListing > m_errors = std::make_unique< geos::TableErrorListing >();
66 
72 
77  TableFormatter( TableLayout const & tableLayout );
78 
85  void toStreamImpl( std::ostream & outputStream, string_view content ) const;
86 };
87 
91 class TableCSVFormatter final : public TableFormatter
92 {
93 public:
94 
98  static constexpr string_view m_separator = ",";
99 
106  {}
107 
112  TableCSVFormatter( TableLayout const & tableLayout );
113 
119 
123  string headerToString() const;
124 
130  string dataToString( TableData const & tableData ) const;
131 
138  template< typename DATASOURCE >
139  string toString( DATASOURCE const & tableData ) const;
140 
146  void headerToStream( std::ostream & outputStream ) const
147  { toStreamImpl( outputStream, headerToString() ); }
148 
155  void dataToStream( std::ostream & outputStream, TableData const & tableData ) const
156  { toStreamImpl( outputStream, dataToString( tableData ) ); }
157 
165  template< typename DATASOURCE >
166  void toStream( std::ostream & outputStream, DATASOURCE const & tableData ) const
167  { toStreamImpl( outputStream, toString( tableData ) ); }
168 
173  void showErrors( bool cond )
174  { m_showErrors = cond; }
175 
176 private:
178  bool m_showErrors = true;
179 
180 };
181 
187 template<>
188 string TableCSVFormatter::toString< TableData >( TableData const & tableData ) const;
189 
190 
196 {
197 public:
198 
205  {}
206 
211  TableTextFormatter( TableLayout const & tableLayout );
212 
217  string toString() const;
218 
225  template< typename DATASOURCE >
226  string toString( DATASOURCE const & tableData ) const;
227 
233  void toStream( std::ostream & outputStream ) const
234  { toStreamImpl( outputStream, toString() ); }
235 
243  template< typename DATASOURCE >
244  void toStream( std::ostream & outputStream, DATASOURCE const & tableData ) const
245  { toStreamImpl( outputStream, toString( tableData ) ); }
246 
247 protected:
248 
250  static constexpr char m_verticalLine = '|';
252  static constexpr char m_horizontalLine = '-';
253 
255  using ColumnWidthModifier = std::function< void ( stdVector< size_t > & ) >;
256 
267  void initalizeTableGrids( PreparedTableLayout const & tableLayout,
268  TableData const & tableData,
269  CellLayoutRows & dataCellsLayout,
270  CellLayoutRows & headerCellsLayout,
271  CellLayoutRows & errorCellsLayout,
272  size_t & tableTotalWidth,
273  ColumnWidthModifier columnWidthModifier ) const;
274 
282  void outputTableHeader( std::ostream & tableOutput,
283  PreparedTableLayout const & tableLayout,
284  CellLayoutRows const & headerCellsLayout,
285  string_view separatorLine ) const;
286 
293  void outputTableData( std::ostream & tableOutput,
294  PreparedTableLayout const & tableLayout,
295  CellLayoutRows const & dataCellsLayout ) const;
296 
305  void outputTableFooter( std::ostream & tableOutput,
306  PreparedTableLayout const & tableLayout,
307  CellLayoutRows & errorCellsLayout,
308  string_view separatorLine,
309  bool hasData ) const;
310 
311 private:
312 
319  void outputLines( PreparedTableLayout const & tableLayout,
320  CellLayoutRows const & cellsLayout,
321  std::ostream & tableOutput ) const;
322 
329  void outputErrors( PreparedTableLayout const & tableLayout,
330  CellLayoutRows & errorCellsLayout,
331  std::ostream & tableOutput ) const;
332 
340  void populateTitleCellsLayout( PreparedTableLayout const & tableLayout,
341  CellLayoutRows & headerCellsLayout,
342  size_t nbVisibleColumn ) const;
343 
355  void populateHeaderCellsLayout( PreparedTableLayout const & tableLayout,
356  CellLayoutRows & headerCellsLayout,
357  size_t nbVisibleColumn ) const;
364  void populateDataCellsLayout( PreparedTableLayout const & tableLayout,
365  CellLayoutRows & dataCellsLayout,
366  RowsCellInput const & inputDataValues,
367  size_t const nbVisibleColumn ) const;
368 
375  void populateErrorCellsLayout( PreparedTableLayout const & tableLayout,
376  CellLayoutRows & errorCellsLayout,
377  TableErrorListing const & dataErrors ) const;
378 
385  void populateDataCellsLayout( PreparedTableLayout const & tableLayout,
386  CellLayoutRows & dataCellsLayout,
387  RowsCellInput const & inputDataValues ) const;
388 
394  void stretchColumnsByCellsWidth( stdVector< size_t > & columnsWidth,
395  TableFormatter::CellLayoutRows const & tableGrid ) const;
396 
406  void stretchColumnsByMergedCellsWidth( stdVector< size_t > & columnsWidth,
407  TableFormatter::CellLayoutRows & tableGrid,
408  PreparedTableLayout const & tableLayout,
409  bool const compress ) const;
410 
417  void applyColumnsWidth( stdVector< size_t > const & columnsWidth,
418  TableFormatter::CellLayoutRows & tableGrid,
419  PreparedTableLayout const & tableLayout ) const;
420 
421 
429  void formatCell( std::ostream & tableOutput,
430  TableLayout::CellLayout const & cell,
431  size_t idxLine ) const;
432 
433 };
434 
440 template<>
441 string TableTextFormatter::toString< TableData >( TableData const & tableData ) const;
442 }
443 
444 #endif /* GEOS_COMMON_FORMAT_TABLE_TABLEFORMATTER_HPP */
Variation of the TableLayout to store precomputed layout information, ready to be formatted.
Class to format data in a formatted CSV format.
~TableCSVFormatter()
Destroy the Table CSV Formatter object We launch GEOS_WARNING if we have encountered any errors.
void dataToStream(std::ostream &outputStream, TableData const &tableData) const
Output the formatted data to a stream. Adds appropriate messages to the error list when the operation...
string toString(DATASOURCE const &tableData) const
Convert a data source to a CSV string.
void toStream(std::ostream &outputStream, DATASOURCE const &tableData) const
Output the formatted data to a stream. Adds appropriate messages to the error list when the operation...
TableCSVFormatter()
Construct a default Table Formatter without layout specification (to only insert data in it,...
void headerToStream(std::ostream &outputStream) const
Output the formatted data to a stream. Adds appropriate messages to the error list when the operation...
string headerToString() const
string dataToString(TableData const &tableData) const
Convert the table data to a CSV string..
TableCSVFormatter(TableLayout const &tableLayout)
Construct a new Table Formatter from a tableLayout.
void showErrors(bool cond)
Indicate if we print the encountered errors on destruction. Enabled by default.
static constexpr string_view m_separator
The column separator for the CSV output.
Class for managing table data.
Definition: TableData.hpp:35
Class for retrieving errors in the table classes.
Definition: TableTypes.hpp:47
abstract class for formatting table data
stdVector< CellLayoutRow > CellLayoutRows
Represent a table section (title + header or values) layout: view on the data and its layout settings...
PreparedTableLayout const m_tableLayout
Layout for a table.
TableFormatter()
Construct a default Table Formatter without layout specification (to only insert data in it,...
TableFormatter(TableLayout const &tableLayout)
Construct a new Table Formatter from a tableLayout.
void toStreamImpl(std::ostream &outputStream, string_view content) const
Implements the actual writing of content to an output stream. Adds appropriate messages to the error ...
std::unique_ptr< geos::TableErrorListing > m_errors
Class used for listing all errors that may have occured during table generation.
TableErrorListing & getErrorsList() const
stdVector< stdVector< TableData::CellData > > RowsCellInput
Represent the TableData values.
View on cell data with information to display it in a table (content, type, alignment,...
Definition: TableLayout.hpp:74
Class for setup the table layout.
Definition: TableLayout.hpp:34
Class to format data in a formatted text format (for log output typically, expecting fixed character ...
void outputTableFooter(std::ostream &tableOutput, PreparedTableLayout const &tableLayout, CellLayoutRows &errorCellsLayout, string_view separatorLine, bool hasData) const
Outputs the bottom part of the formatted table to the provided output stream.
TableTextFormatter()
Construct a default Table Formatter without layout specification (to only insert data in it,...
TableTextFormatter(TableLayout const &tableLayout)
Construct a new TableFormatter from a tableLayout.
void initalizeTableGrids(PreparedTableLayout const &tableLayout, TableData const &tableData, CellLayoutRows &dataCellsLayout, CellLayoutRows &headerCellsLayout, CellLayoutRows &errorCellsLayout, size_t &tableTotalWidth, ColumnWidthModifier columnWidthModifier) const
Initializes the table layout with the given table data and prepares necessary layouts for headers and...
void toStream(std::ostream &outputStream) const
Output the formatted data to a stream. Adds appropriate messages to the error list when the operation...
static constexpr char m_horizontalLine
for the extremity of a row
void outputTableData(std::ostream &tableOutput, PreparedTableLayout const &tableLayout, CellLayoutRows const &dataCellsLayout) const
Outputs the data part of the formatted table to the provided output stream.
static constexpr char m_verticalLine
symbol for separator construction
void toStream(std::ostream &outputStream, DATASOURCE const &tableData) const
Output the formatted data to a stream. Adds appropriate messages to the error list when the operation...
void outputTableHeader(std::ostream &tableOutput, PreparedTableLayout const &tableLayout, CellLayoutRows const &headerCellsLayout, string_view separatorLine) const
Outputs the top part of the formatted table to the provided output stream.
string toString(DATASOURCE const &tableData) const
Convert a data source to a table string.
std::function< void(stdVector< size_t > &) > ColumnWidthModifier
A functor which allow to customize the columns width after their computation.
string toString() const
std::string_view string_view
String type.
Definition: DataTypes.hpp:93
internal::StdVectorWrapper< T, Allocator, USE_STD_CONTAINER_BOUNDS_CHECKING > stdVector
Represent a row of the Table (header or values) when structured for formatting.
stdVector< TableLayout::CellLayout > cells
The cell list of the row instance.
size_t sublinesCount
The maximum number of lines in the cells texts (no text is considered as one line).