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 
64  { return m_tableLayout; }
65 
66 protected:
67 
71  std::unique_ptr< geos::TableErrorListing > m_errors = std::make_unique< geos::TableErrorListing >();
72 
78 
83  TableFormatter( TableLayout const & tableLayout );
84 
91  void toStreamImpl( std::ostream & outputStream, string_view content ) const;
92 };
93 
97 class TableCSVFormatter final : public TableFormatter
98 {
99 public:
100 
104  static constexpr string_view m_separator = ",";
105 
112  {}
113 
118  TableCSVFormatter( TableLayout const & tableLayout );
119 
125 
129  string headerToString() const;
130 
136  string dataToString( TableData const & tableData ) const;
137 
144  template< typename DATASOURCE >
145  string toString( DATASOURCE const & tableData ) const;
146 
152  void headerToStream( std::ostream & outputStream ) const
153  { toStreamImpl( outputStream, headerToString() ); }
154 
161  void dataToStream( std::ostream & outputStream, TableData const & tableData ) const
162  { toStreamImpl( outputStream, dataToString( tableData ) ); }
163 
171  template< typename DATASOURCE >
172  void toStream( std::ostream & outputStream, DATASOURCE const & tableData ) const
173  { toStreamImpl( outputStream, toString( tableData ) ); }
174 
179  void showErrors( bool cond )
180  { m_showErrors = cond; }
181 
182 private:
184  bool m_showErrors = true;
185 
186 };
187 
193 template<>
194 string TableCSVFormatter::toString< TableData >( TableData const & tableData ) const;
195 
196 
202 {
203 public:
204 
211  {}
212 
217  TableTextFormatter( TableLayout const & tableLayout );
218 
223  string toString() const;
224 
231  template< typename DATASOURCE >
232  string toString( DATASOURCE const & tableData ) const;
233 
239  void toStream( std::ostream & outputStream ) const
240  { toStreamImpl( outputStream, toString() ); }
241 
249  template< typename DATASOURCE >
250  void toStream( std::ostream & outputStream, DATASOURCE const & tableData ) const
251  { toStreamImpl( outputStream, toString( tableData ) ); }
252 
253 protected:
254 
256  static constexpr char m_verticalLine = '|';
258  static constexpr char m_horizontalLine = '-';
259 
261  using ColumnWidthModifier = std::function< void ( stdVector< size_t > & ) >;
262 
273  void initalizeTableGrids( PreparedTableLayout const & tableLayout,
274  TableData const & tableData,
275  CellLayoutRows & dataCellsLayout,
276  CellLayoutRows & headerCellsLayout,
277  CellLayoutRows & errorCellsLayout,
278  size_t & tableTotalWidth,
279  ColumnWidthModifier columnWidthModifier ) const;
280 
288  void outputTableHeader( std::ostream & tableOutput,
289  PreparedTableLayout const & tableLayout,
290  CellLayoutRows const & headerCellsLayout,
291  string_view separatorLine ) const;
292 
299  void outputTableData( std::ostream & tableOutput,
300  PreparedTableLayout const & tableLayout,
301  CellLayoutRows const & dataCellsLayout ) const;
302 
311  void outputTableFooter( std::ostream & tableOutput,
312  PreparedTableLayout const & tableLayout,
313  CellLayoutRows & errorCellsLayout,
314  string_view separatorLine,
315  bool hasData ) const;
316 
317 private:
318 
325  void outputLines( PreparedTableLayout const & tableLayout,
326  CellLayoutRows const & cellsLayout,
327  std::ostream & tableOutput ) const;
328 
335  void outputErrors( PreparedTableLayout const & tableLayout,
336  CellLayoutRows & errorCellsLayout,
337  std::ostream & tableOutput ) const;
338 
346  void populateTitleCellsLayout( PreparedTableLayout const & tableLayout,
347  CellLayoutRows & headerCellsLayout,
348  size_t nbVisibleColumn ) const;
349 
361  void populateHeaderCellsLayout( PreparedTableLayout const & tableLayout,
362  CellLayoutRows & headerCellsLayout,
363  size_t nbVisibleColumn ) const;
370  void populateDataCellsLayout( PreparedTableLayout const & tableLayout,
371  CellLayoutRows & dataCellsLayout,
372  RowsCellInput const & inputDataValues,
373  size_t const nbVisibleColumn ) const;
374 
381  void populateErrorCellsLayout( PreparedTableLayout const & tableLayout,
382  CellLayoutRows & errorCellsLayout,
383  TableErrorListing const & dataErrors ) const;
384 
391  void populateDataCellsLayout( PreparedTableLayout const & tableLayout,
392  CellLayoutRows & dataCellsLayout,
393  RowsCellInput const & inputDataValues ) const;
394 
400  void stretchColumnsByCellsWidth( stdVector< size_t > & columnsWidth,
401  TableFormatter::CellLayoutRows const & tableGrid ) const;
402 
412  void stretchColumnsByMergedCellsWidth( stdVector< size_t > & columnsWidth,
413  TableFormatter::CellLayoutRows & tableGrid,
414  PreparedTableLayout const & tableLayout,
415  bool const compress ) const;
416 
423  void applyColumnsWidth( stdVector< size_t > const & columnsWidth,
424  TableFormatter::CellLayoutRows & tableGrid,
425  PreparedTableLayout const & tableLayout ) const;
426 
427 
435  void formatCell( std::ostream & tableOutput,
436  TableLayout::CellLayout const & cell,
437  size_t idxLine ) const;
438 
439 };
440 
446 template<>
447 string TableTextFormatter::toString< TableData >( TableData const & tableData ) const;
448 }
449 
450 #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.
PreparedTableLayout const & getLayout() const
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).