]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Provide an alternative header format for TableHandler::write_text().
authorbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Sat, 22 Oct 2011 21:20:00 +0000 (21:20 +0000)
committerbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Sat, 22 Oct 2011 21:20:00 +0000 (21:20 +0000)
git-svn-id: https://svn.dealii.org/trunk@24657 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/changes.h
deal.II/include/deal.II/base/table_handler.h
deal.II/source/base/table_handler.cc
tests/base/table_handler_06.cc [new file with mode: 0644]
tests/base/table_handler_06/cmp/generic [new file with mode: 0644]

index 77132af44df19779d9ca7014dbdf002c9101947e..d8460dc40715a80761e5a67530463e0a6a565dfa 100644 (file)
@@ -44,6 +44,12 @@ inconvenience this causes.
 <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>
index 4ada2dbb9e5ec78b806d558e8934d5457fcc9eb8..e50ded1824e56dccf34cbbb94bb1c9f48584ff7f 100644 (file)
@@ -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:
+     * 
+     * - <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.
@@ -408,8 +430,14 @@ class TableHandler
                                       * 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
index 6de9f3e31959af1738f5063616951f8bf7895856..3bb10290f0516c48b62cd9bb462bb5be3bea8de9 100644 (file)
@@ -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<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;
@@ -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_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)
     {
diff --git a/tests/base/table_handler_06.cc b/tests/base/table_handler_06.cc
new file mode 100644 (file)
index 0000000..721b18c
--- /dev/null
@@ -0,0 +1,55 @@
+//-----------------------------------------------------------------------------
+//    $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);
+}
diff --git a/tests/base/table_handler_06/cmp/generic b/tests/base/table_handler_06/cmp/generic
new file mode 100644 (file)
index 0000000..e6a8bb1
--- /dev/null
@@ -0,0 +1,8 @@
+
+# 1: key1
+# 2: key2
+# 3: key3
+0 0  "" 
+1 0  "" 
+2 13 a  
+1 0  "" 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.