<h3>Specific improvements</h3>
<ol>
+<li> 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.
+<br>
+(Wolfgang Bangerth, 2011/10/22)
+
<li> Fixed: Trying to write a TableHandler object that is empty resulted
in a segmentation fault. This is now fixed.
<br>
class TableHandler
{
public:
+ /**
+ * Set of options how a table should be formatted when output with
+ * the write_text() function. The following possibilities exist:
+ *
+ * - <code>table_with_headers</code>: 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.
+ * - <code>table_with_separate_column_description</code>: 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.
* add_value() function with an empty
* string), then the entry of the table
* is printed as <code>""</code>.
+ *
+ * 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
}
-void TableHandler::write_text(std::ostream &out) const
+void TableHandler::write_text(std::ostream &out,
+ const TextOutputFormat format) const
{
AssertThrow (out, ExcIO());
for (unsigned int j=0; j<n_cols; ++j)
{
// get key and entry here
- std::string key=sel_columns[j];
+ std::string key = sel_columns[j];
const std::map<std::string, Column>::const_iterator
- col_iter=columns.find(key);
+ col_iter = columns.find(key);
Assert(col_iter!=columns.end(), ExcInternalError());
const Column & column = col_iter->second;
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_order.size(); ++j)
{
const std::string & key = column_order[j];
- // if the key of this column is
- // wider than the widest entry,
- // then adjust
- if (key.length() > 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<front_padding; ++i)
- out << ' ';
- out << key;
- for (unsigned int i=0; i<rear_padding; ++i)
- out << ' ';
-
- // finally column break
- out << ' ';
+ switch (format)
+ {
+ case table_with_headers:
+ {
+ // if the key of this column is
+ // wider than the widest entry,
+ // then adjust
+ if (key.length() > 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<front_padding; ++i)
+ out << ' ';
+ out << key;
+ for (unsigned int i=0; i<rear_padding; ++i)
+ out << ' ';
+
+ // finally column break
+ out << ' ';
+
+ break;
+ }
+
+ case table_with_separate_column_description:
+ {
+ // print column key with column number. enumerate
+ // columns starting with 1
+ out << "# " << j+1 << ": " << key << std::endl;
+ break;
+ }
+
+ default:
+ Assert (false, ExcInternalError());
+ }
}
- out << std::endl;
+ if (format == table_with_headers)
+ out << std::endl;
for (unsigned int i=0; i<nrows; ++i)
{
--- /dev/null
+//-----------------------------------------------------------------------------
+// $Id$
+// Version: $Name$
+//
+// Copyright (C) 2010, 2011 by the deal.II authors
+//
+// This file is subject to QPL and may not be distributed
+// without copyright and license information. Please refer
+// to the file deal.II/doc/license.html for the text and
+// further information on this license.
+//
+//-----------------------------------------------------------------------------
+
+// use the alternative format for the headers in TableHandler::write_text
+
+
+#include "../tests.h"
+#include <deal.II/base/data_out_base.h>
+#include <deal.II/base/table_handler.h>
+#include <deal.II/base/logstream.h>
+
+#include <vector>
+#include <iomanip>
+#include <fstream>
+#include <string>
+
+
+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);
+}
--- /dev/null
+
+# 1: key1
+# 2: key2
+# 3: key3
+0 0 ""
+1 0 ""
+2 13 a
+1 0 ""