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 
194 class TableTextFormatter final : public TableFormatter
195 {
196 public:
197 
204  {}
205 
210  TableTextFormatter( TableLayout const & tableLayout );
211 
216  string toString() const;
217 
224  template< typename DATASOURCE >
225  string toString( DATASOURCE const & tableData ) const;
226 
232  void toStream( std::ostream & outputStream ) const
233  { toStreamImpl( outputStream, toString() ); }
234 
242  template< typename DATASOURCE >
243  void toStream( std::ostream & outputStream, DATASOURCE const & tableData ) const
244  { toStreamImpl( outputStream, toString( tableData ) ); }
245 
246 private:
247 
249  static constexpr char m_verticalLine = '|';
251  static constexpr char m_horizontalLine = '-';
252 
253 
263  void initalizeTableGrids( PreparedTableLayout const & tableLayout,
264  TableData const & tableData,
265  CellLayoutRows & dataCellsLayout,
266  CellLayoutRows & headerCellsLayout,
267  CellLayoutRows & errorCellsLayout,
268  size_t & tableTotalWidth ) const;
269 
279  void outputTable( PreparedTableLayout const & tableLayout,
280  std::ostream & tableOutput,
281  CellLayoutRows const & headerCellsLayout,
282  CellLayoutRows const & dataCellsLayout,
283  CellLayoutRows & errorCellsLayout,
284  size_t tableTotalWidth ) const;
285 
292  void outputLines( PreparedTableLayout const & tableLayout,
293  CellLayoutRows const & cellsLayout,
294  std::ostream & tableOutput ) const;
295 
302  void outputErrors( PreparedTableLayout const & tableLayout,
303  CellLayoutRows & errorCellsLayout,
304  std::ostream & tableOutput ) const;
305 
313  void populateTitleCellsLayout( PreparedTableLayout const & tableLayout,
314  CellLayoutRows & headerCellsLayout,
315  size_t const nbVisibleColumn ) const;
316 
328  void populateHeaderCellsLayout( PreparedTableLayout const & tableLayout,
329  CellLayoutRows & headerCellsLayout,
330  size_t nbVisibleColumn ) const;
337  void populateDataCellsLayout( PreparedTableLayout const & tableLayout,
338  CellLayoutRows & dataCellsLayout,
339  RowsCellInput const & inputDataValues,
340  size_t const nbVisibleColumn ) const;
341 
348  void populateErrorCellsLayout( PreparedTableLayout const & tableLayout,
349  CellLayoutRows & errorCellsLayout,
350  TableErrorListing const & dataErrors ) const;
351 
358  void populateDataCellsLayout( PreparedTableLayout const & tableLayout,
359  CellLayoutRows & dataCellsLayout,
360  RowsCellInput const & inputDataValues ) const;
361 
367  void stretchColumnsByCellsWidth( stdVector< size_t > & columnsWidth,
368  TableFormatter::CellLayoutRows const & tableGrid ) const;
369 
379  void stretchColumnsByMergedCellsWidth( stdVector< size_t > & columnsWidth,
380  TableFormatter::CellLayoutRows & tableGrid,
381  PreparedTableLayout const & tableLayout,
382  bool const compress ) const;
383 
390  void applyColumnsWidth( stdVector< size_t > const & columnsWidth,
391  TableFormatter::CellLayoutRows & tableGrid,
392  PreparedTableLayout const & tableLayout ) const;
393 
394 
402  void formatCell( std::ostream & tableOutput,
403  TableLayout::CellLayout const & cell,
404  size_t idxLine ) const;
405 };
406 
412 template<>
413 string TableTextFormatter::toString< TableData >( TableData const & tableData ) const;
414 }
415 
416 #endif /* GEOS_COMMON_FORMAT_TABLE_TABLEFORMATTER_HPP */
Variation of the TableLayout to store precomputed layout information, ready to be formatted.
class for CSV formatting
~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:45
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:82
Class for setup the table layout.
Definition: TableLayout.hpp:36
class for log formatting
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 toStream(std::ostream &outputStream) const
Output the formatted data to a stream. Adds appropriate messages to the error list when the operation...
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...
string toString(DATASOURCE const &tableData) const
Convert a data source to a table string.
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).