From d5b2c62ba72f763962b9a6c97091ce7f77e63350 Mon Sep 17 00:00:00 2001 From: bangerth Date: Sat, 22 Oct 2011 21:20:00 +0000 Subject: [PATCH] Provide an alternative header format for TableHandler::write_text(). git-svn-id: https://svn.dealii.org/trunk@24657 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/doc/news/changes.h | 6 ++ deal.II/include/deal.II/base/table_handler.h | 30 ++++++++- deal.II/source/base/table_handler.cc | 69 +++++++++++++------- tests/base/table_handler_06.cc | 55 ++++++++++++++++ tests/base/table_handler_06/cmp/generic | 8 +++ 5 files changed, 143 insertions(+), 25 deletions(-) create mode 100644 tests/base/table_handler_06.cc create mode 100644 tests/base/table_handler_06/cmp/generic diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index 77132af44d..d8460dc407 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -44,6 +44,12 @@ inconvenience this causes.

Specific improvements

    +
  1. New: TableHandler::print_text now supports not only printing column +keys above their own column, but also in a separate header, to make it simpler +for external plotting programs to skip this line. +
    +(Wolfgang Bangerth, 2011/10/22) +
  2. Fixed: Trying to write a TableHandler object that is empty resulted in a segmentation fault. This is now fixed.
    diff --git a/deal.II/include/deal.II/base/table_handler.h b/deal.II/include/deal.II/base/table_handler.h index 4ada2dbb9e..e50ded1824 100644 --- a/deal.II/include/deal.II/base/table_handler.h +++ b/deal.II/include/deal.II/base/table_handler.h @@ -246,6 +246,28 @@ namespace internal class TableHandler { public: + /** + * Set of options how a table should be formatted when output with + * the write_text() function. The following possibilities exist: + * + * - table_with_headers: The table is formatted in such a way + * that the contents are aligned under the key of each column, i.e. the + * key sits atop each column. This is suitable for tables with few columns + * where the entire table can be displayed on the screen. + * - table_with_separate_column_description: This is a better + * format when there are many columns and the table as a whole can not + * be displayed on the screen. Here, the column keys are first listed + * one-by-one on lines of their own, and are numbered for better readability. + * In addition, each of these description lines are prefixed by '#' to mark + * these lines as comments for programs that want to read the following + * table as data and should ignore these descriptive lines. GNUPLOT is + * one such program that will automatically ignore lines so prefixed. + **/ + enum TextOutputFormat + { + table_with_headers, + table_with_separate_column_description + }; /** * Constructor. @@ -408,8 +430,14 @@ class TableHandler * add_value() function with an empty * string), then the entry of the table * is printed as "". + * + * The second argument indicates how + * column keys are to be displayed. See + * the description of TextOutputFormat + * for more information */ - void write_text (std::ostream &out) const; + void write_text (std::ostream &out, + const TextOutputFormat format = table_with_headers) const; /** * Write table as a tex file. If diff --git a/deal.II/source/base/table_handler.cc b/deal.II/source/base/table_handler.cc index 6de9f3e319..3bb10290f0 100644 --- a/deal.II/source/base/table_handler.cc +++ b/deal.II/source/base/table_handler.cc @@ -267,7 +267,8 @@ void TableHandler::set_scientific (const std::string &key, } -void TableHandler::write_text(std::ostream &out) const +void TableHandler::write_text(std::ostream &out, + const TextOutputFormat format) const { AssertThrow (out, ExcIO()); @@ -298,9 +299,9 @@ void TableHandler::write_text(std::ostream &out) const for (unsigned int j=0; j::const_iterator - col_iter=columns.find(key); + col_iter = columns.find(key); Assert(col_iter!=columns.end(), ExcInternalError()); const Column & column = col_iter->second; @@ -341,32 +342,52 @@ void TableHandler::write_text(std::ostream &out) const column_widths[j] = std::max(entry_widths[i][j], column_widths[j]); - // write the caption line + // write the captions for (unsigned int j=0; j column_widths[j]) - column_widths[j] = key.length(); - - // now write key. try to center - // it somehow - const unsigned int front_padding = (column_widths[j]-key.length())/2, - rear_padding = (column_widths[j]-key.length()) - - front_padding; - for (unsigned int i=0; i column_widths[j]) + column_widths[j] = key.length(); + + // now write key. try to center + // it somehow + const unsigned int front_padding = (column_widths[j]-key.length())/2, + rear_padding = (column_widths[j]-key.length()) - + front_padding; + for (unsigned int i=0; i +#include +#include + +#include +#include +#include +#include + + +int main () +{ + std::ofstream logfile("table_handler_06/output"); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + TableHandler table; + table.set_auto_fill_mode (true); + + std::string keys[3] = { "key1", "key2", "key3" }; + + // fill rows 1 and 2 partially + table.add_value(keys[0], 0); + table.add_value(keys[0], 1); + // now fill row 3 completely + table.add_value(keys[0], 2); + table.add_value(keys[1], 13); + table.add_value(keys[2], std::string("a")); + + // now again fill row 4 partially + table.add_value(keys[0], 1); + + // produce output. hope that row 4 is + // completely padded + table.write_text(deallog.get_file_stream(), + TableHandler::table_with_separate_column_description); +} diff --git a/tests/base/table_handler_06/cmp/generic b/tests/base/table_handler_06/cmp/generic new file mode 100644 index 0000000000..e6a8bb1bed --- /dev/null +++ b/tests/base/table_handler_06/cmp/generic @@ -0,0 +1,8 @@ + +# 1: key1 +# 2: key2 +# 3: key3 +0 0 "" +1 0 "" +2 13 a +1 0 "" -- 2.39.5