Documentation 6b54b78464
Loading...
Searching...
No Matches
printing_toolbox.hpp
Go to the documentation of this file.
1/*!
2 * \file printing_toolbox.hpp
3 * \brief Header file for the printing toolbox.
4 * \author T. Albring
5 * \version 8.5.0 "Harrier"
6 *
7 * SU2 Project Website: https://su2code.github.io
8 *
9 * The SU2 Project is maintained by the SU2 Foundation
10 * (http://su2foundation.org)
11 *
12 * Copyright 2012-2026, SU2 Contributors (cf. AUTHORS.md)
13 *
14 * SU2 is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * SU2 is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with SU2. If not, see <http://www.gnu.org/licenses/>.
26 */
27
28#pragma once
29
30#include <iostream>
31#include <iomanip>
32#include <cstdint>
33#include <vector>
34#include <string>
35#include <sstream>
36#include <cmath>
37#include <algorithm>
38#include "../basic_types/datatype_structure.hpp"
39
40namespace PrintingToolbox {
41
42/*!
43 * \class CTablePrinter
44 * \brief Class for writing output in a table.
45 *
46 * For example the output
47 *
48 * +-------------------------------------------+
49 * | MG Level| Presmooth|PostSmooth|CorrectSmo|
50 * +-------------------------------------------+
51 * | 0| 1| 0| 0|
52 * | 1| 1| 0| 0|
53 * | 2| 1| 0| 0|
54 * | 3| 1| 0| 0|
55 * +-------------------------------------------+
56 *
57 *
58 * can be generated with the code
59 *
60 * CTablePrinter MGTable(&std::cout);
61 * MGTable.AddColumn("MG Level", 10);
62 * MGTable.AddColumn("Presmooth", 10);
63 * MGTable.AddColumn("PostSmooth", 10);
64 * MGTable.AddColumn("CorrectSmooth", 10);
65 * MGTable.PrintHeader();
66 * for (unsigned short iLevel = 0; iLevel < nMGLevels+1; iLevel++) {
67 * MGTable << iLevel << MG_PreSmooth[iLevel] << MG_PostSmooth[iLevel] << MG_CorrecSmooth[iLevel];
68 * }
69 * MGTable.PrintFooter();
70 *
71 *
72 * \author T. Albring
73 */
75 public:
76 CTablePrinter(std::ostream* output, const std::string& separator = "|");
77
79
80 /*!
81 * \brief Get number of columns of the table
82 * \return Number of columns.
83 */
84 int GetNumColumns() const;
85
86 /*!
87 * \brief Get total width of the table.
88 * \return Total width of the table.
89 */
90 int GetTableWidth() const;
91
92 /*!
93 * \brief Set the separator between columns (outer decoration)
94 * \param[in] separator - The separation character.
95 */
96 void SetSeparator(const std::string& separator);
97
98 /*!
99 * \brief Set the separator between columns (inner decoration)
100 * \param[in] separator - The separation character.
101 */
102 void SetInnerSeparator(const std::string& inner_separator);
103
104 /*!
105 * \brief Set the alignment of the table entries (CENTER only works for the header at the moment).
106 * \param[in] align_ - The alignment (CENTER, LEFT, RIGHT).
107 */
108 void SetAlign(int align_);
109
110 /*!
111 * \brief Set whether to print the line at the bottom of the table.
112 * \param[in] print - If TRUE, the bottom line is printed.
113 */
114 void SetPrintHeaderBottomLine(bool print);
115
116 /*!
117 * \brief Set whether to print the line at the top of the table.
118 * \param[in] print - If TRUE, the top line is printed.
119 */
120 void SetPrintHeaderTopLine(bool print);
121
122 /*!
123 * \brief Add a column to the table by specifiying the header name and the width.
124 * \param[in] header_name - The name printed in the header.
125 * \param[in] column_width - The width of the column.
126 */
127 void AddColumn(const std::string& header_name, int column_width);
128
129 /*!
130 * \brief Print the header.
131 */
132 void PrintHeader();
133
134 /*!
135 * \brief Print the footer.
136 */
137 void PrintFooter();
138
139 /*!
140 * \brief Set the floating point precision.
141 */
142 void SetPrecision(int precision);
143
144 template <typename T>
146 int indent = 0;
147
148 /* --- Set the left separator --- */
149 if (j_ == 0) *out_stream_ << separator_;
150
151 /* --- Determine and set the current alignment in the stream --- */
152 if (align_ == LEFT)
153 *out_stream_ << std::left;
154 else if (align_ == RIGHT || align_ == CENTER)
155 *out_stream_ << std::right;
156
157 /*--- Print the current column value to the stream --- */
158 *out_stream_ << std::setw(column_widths_.at(j_) - indent) << std::setprecision(precision_) << input;
159
160 /*--- Reset the column counter and if it is the last column,
161 * add also a line break ---*/
162 if (j_ == GetNumColumns() - 1) {
163 *out_stream_ << std::setw(indent + 1 + (int)separator_.size()) << separator_ + "\n";
164 i_ = i_ + 1;
165 j_ = 0;
166 } else {
167 *out_stream_ << std::setw(indent + (int)inner_separator_.size()) << inner_separator_;
168 j_ = j_ + 1;
169 }
170
171 return *this;
172 }
173
174 private:
175 /*!
176 * \brief Print a horizontal line.
177 */
178 void PrintHorizontalLine();
179
180 std::ostream* out_stream_; /*< \brief The output stream. */
181 std::vector<std::string> column_headers_; /*< \brief Vector of column header names. */
182 std::vector<int> column_widths_; /*< \brief Vector of column widths. */
183 std::string separator_; /*< \brief Column separator char. */
184 std::string inner_separator_; /*< \brief Inner column separator char. */
185
186 int precision_; /*< \brief Floating point precision */
187
188 int i_; /*< \brief Index of the current row. */
189 int j_; /*< \brief Index of the current column. */
190
191 int table_width_; /*< \brief The total width of the table. */
192 int align_; /*< \brief The current alignment. */
193 bool print_header_top_line_, /*< \brief Printing the header top line. */
194 print_header_bottom_line_; /*< \brief Printing the header bottom line. */
195};
196
197inline void PrintScreenFixed(std::ostream& stream, su2double val, unsigned short field_width) {
198 stream.precision(6);
199 stream.setf(std::ios::fixed, std::ios::floatfield);
200 stream.width(field_width);
201 stream << std::right << val;
202 stream.unsetf(std::ios::fixed);
203}
204
205inline void PrintScreenScientific(std::ostream& stream, su2double val, unsigned short field_width) {
206 stream.precision(4);
207 stream.setf(std::ios::scientific, std::ios::floatfield);
208 stream.width(field_width);
209 stream << std::right << val;
210 stream.unsetf(std::ios::scientific);
211}
212
213inline void PrintScreenInteger(std::ostream& stream, unsigned long val, unsigned short field_width) {
214 stream.width(field_width);
215 stream << std::right << val;
216}
217
218inline void PrintScreenPercent(std::ostream& stream, su2double val, unsigned short field_width) {
219 stream.precision(2);
220 stream.setf(std::ios::fixed, std::ios::floatfield);
221 stream.width(field_width - 1);
222 stream << std::right << val << "%";
223 stream.unsetf(std::ios::fixed);
224}
225
226inline std::vector<std::string> split(const std::string& s, char delimiter) {
227 std::vector<std::string> tokens;
228 std::string token;
229 std::istringstream tokenStream(s);
230 while (std::getline(tokenStream, token, delimiter)) {
231 tokens.push_back(token);
232 }
233 return tokens;
234}
235
236inline int stoi(const std::string s) {
237 std::istringstream ss(s);
238 int number;
239 ss >> number;
240 return number;
241}
242
243inline su2double stod(const std::string s) {
244 std::istringstream ss(s);
245 su2double number;
246 ss >> number;
247 return number;
248}
249
250inline std::string to_string(const su2double number) {
251 std::stringstream ss;
252
253 ss << number;
254
255 return ss.str();
256}
257
258const static char* ws = " \t\n\r\f\v";
259
260// trim from end of string (right)
261inline std::string& rtrim(std::string& s, const char* t = ws) {
262 s.erase(s.find_last_not_of(t) + 1);
263 return s;
264}
265
266// trim from beginning of string (left)
267inline std::string& ltrim(std::string& s, const char* t = ws) {
268 s.erase(0, s.find_first_not_of(t));
269 return s;
270}
271
272// trim from both ends of string (right then left)
273inline std::string& trim(std::string& s, const char* t = ws) { return ltrim(rtrim(s, t), t); }
274
275/*!
276 * \brief utility function for converting strings to uppercase
277 * \param[in,out] str - string we want to convert
278 */
279inline void StringToUpperCase(std::string& str) { std::transform(str.begin(), str.end(), str.begin(), ::toupper); }
280
281/*!
282 * \brief utility function for converting strings to uppercase
283 * \param[in] str - string we want a copy of converted to uppercase
284 * \return a copy of str in uppercase
285 */
286inline std::string StringToUpperCase(const std::string& str) {
287 std::string upp_str(str);
288 std::transform(upp_str.begin(), upp_str.end(), upp_str.begin(), ::toupper);
289 return upp_str;
290}
291
292/*!
293 * \brief Trim an extension from a name.
294 * \param[in] - extension we want to remove.
295 * \param[in,out] - string we want to trim.
296 */
297inline void TrimExtension(const std::string& ext, std::string& name) {
298 const auto extIndex = name.rfind(ext);
299 if (extIndex != std::string::npos && extIndex + ext.size() == name.size()) {
300 name.resize(extIndex);
301 }
302}
303
304} // namespace PrintingToolbox
Class for writing output in a table.
Definition printing_toolbox.hpp:74
int GetTableWidth() const
Get total width of the table.
Definition printing_toolbox.cpp:47
void SetPrintHeaderTopLine(bool print)
Set whether to print the line at the top of the table.
Definition printing_toolbox.cpp:57
void PrintHeader()
Print the header.
Definition printing_toolbox.cpp:85
void SetPrintHeaderBottomLine(bool print)
Set whether to print the line at the bottom of the table.
Definition printing_toolbox.cpp:55
void SetPrecision(int precision)
Set the floating point precision.
Definition printing_toolbox.cpp:61
void SetSeparator(const std::string &separator)
Set the separator between columns (outer decoration)
Definition printing_toolbox.cpp:49
CTablePrinter & operator<<(T input)
Definition printing_toolbox.hpp:145
void SetInnerSeparator(const std::string &inner_separator)
Set the separator between columns (inner decoration)
Definition printing_toolbox.cpp:51
void PrintFooter()
Print the footer.
Definition printing_toolbox.cpp:118
int GetNumColumns() const
Get number of columns of the table.
Definition printing_toolbox.cpp:45
void SetAlign(int align_)
Set the alignment of the table entries (CENTER only works for the header at the moment).
Definition printing_toolbox.cpp:59
void AddColumn(const std::string &header_name, int column_width)
Add a column to the table by specifiying the header name and the width.
Definition printing_toolbox.cpp:66
alignment
Definition printing_toolbox.hpp:78
@ LEFT
Definition printing_toolbox.hpp:78
@ RIGHT
Definition printing_toolbox.hpp:78
@ CENTER
Definition printing_toolbox.hpp:78
passivedouble su2double
Definition code_config.hpp:181
Definition printing_toolbox.hpp:40
std::string & trim(std::string &s, const char *t=ws)
Definition printing_toolbox.hpp:273
std::vector< std::string > split(const std::string &s, char delimiter)
Definition printing_toolbox.hpp:226
void PrintScreenPercent(std::ostream &stream, su2double val, unsigned short field_width)
Definition printing_toolbox.hpp:218
void PrintScreenInteger(std::ostream &stream, unsigned long val, unsigned short field_width)
Definition printing_toolbox.hpp:213
std::string to_string(const su2double number)
Definition printing_toolbox.hpp:250
int stoi(const std::string s)
Definition printing_toolbox.hpp:236
void StringToUpperCase(std::string &str)
utility function for converting strings to uppercase
Definition printing_toolbox.hpp:279
std::string & ltrim(std::string &s, const char *t=ws)
Definition printing_toolbox.hpp:267
su2double stod(const std::string s)
Definition printing_toolbox.hpp:243
void PrintScreenFixed(std::ostream &stream, su2double val, unsigned short field_width)
Definition printing_toolbox.hpp:197
void TrimExtension(const std::string &ext, std::string &name)
Trim an extension from a name.
Definition printing_toolbox.hpp:297
void PrintScreenScientific(std::ostream &stream, su2double val, unsigned short field_width)
Definition printing_toolbox.hpp:205
std::string & rtrim(std::string &s, const char *t=ws)
Definition printing_toolbox.hpp:261