]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Print "" in empty entries of a table.
authorbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Tue, 18 Oct 2011 10:14:33 +0000 (10:14 +0000)
committerbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Tue, 18 Oct 2011 10:14:33 +0000 (10:14 +0000)
git-svn-id: https://svn.dealii.org/trunk@24623 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_03.cc [new file with mode: 0644]
tests/base/table_handler_03/cmp/generic [new file with mode: 0644]

index 174cda9d6b663018313b95abcdda5f76093c1037..f09d237f5625c232ea81ab5bbc9375ff585cf787 100644 (file)
@@ -44,6 +44,14 @@ inconvenience this causes.
 <h3>Specific improvements</h3>
 
 <ol>
+<li> Fixed: In TableHandler::print_text, it can happen that the function
+wants to print an empty string as the element of the table to be printed.
+This can confuse machine readers of this table, for example for visualization,
+since they then do not see this column in that row. To prevent this, we now
+print <code>""</code> in such places.
+<br>
+(Wolfgang Bangerth, 2011/10/18)
+
 <li> Fixed: Using Trilinos versions 10.4 and later on Debian failed to
 configure due to a different naming scheme of Trilinos libraries on
 Debian. This is now fixed.
index b985ff47666e7396ea72f4d815c0b2c2d4be47e5..01c91f11d3e16ae294444d84be14d836ea632e23 100644 (file)
@@ -307,8 +307,18 @@ class TableHandler
                          const std::string &format="c");
 
                                      /**
-                                      * Write table as formatted text,
-                                      * e.g.  to the standard output.
+                                      * Write table as formatted text to the
+                                      * given stream. The text is formatted in
+                                      * such as way that it represents data as
+                                      * formatted columns of text. To avoid
+                                      * problems when reading these tables
+                                      * automatically, for example for
+                                      * postprocessing, if an entry in a cell
+                                      * of this table is empty (i.e. it has
+                                      * been created by calling the
+                                      * add_value() function with an empty
+                                      * string), then the entry of the table
+                                      * is printed as <code>""</code>.
                                       */
     void write_text (std::ostream &out) const;
 
index d9bc6c736ccf0299f976cf627e92cd624297ba43..7a3771d9fc43c28d0cdfcb9f00fe231273a53728 100644 (file)
@@ -67,7 +67,7 @@ void TableHandler::add_value (const std::string &key,
       columns.insert(new_column);
       column_order.push_back(key);
     }
-    
+
   const internal::TableEntry entry = { value };
   columns[key].entries.push_back (entry);
 }
@@ -229,10 +229,18 @@ void TableHandler::write_text(std::ostream &out) const
 
        dummy_out << column.entries[i].value;
 
-                                        // get size, note that we are
-                                        // not interested in the
-                                        // trailing \0
-       entry_widths[i][j] = dummy_out.str().length();
+                                        // get size. as a side note, if the
+                                        // text printed is in fact the empty
+                                        // string, then we get into a bit of
+                                        // trouble since readers would skip
+                                        // over the resulting whitespaces. as
+                                        // a consequence, we'll print ""
+                                        // instead in that case.
+       const unsigned int size = dummy_out.str().length();
+       if (size > 0)
+         entry_widths[i][j] = size;
+       else
+         entry_widths[i][j] = 2;
       }
 
                                   // next compute the width each row
@@ -288,7 +296,24 @@ void TableHandler::write_text(std::ostream &out) const
          else
            out.setf(std::ios::fixed, std::ios::floatfield);
          out << std::setw(column_widths[j]);
-         out << column.entries[i].value;
+
+                                          // get the string to write into the
+                                          // table. we could simply << it
+                                          // into the stream but we have to
+                                          // be a bit careful about the case
+                                          // where the string may be empty,
+                                          // in which case we'll print it as
+                                          // ""
+         {
+           std::ostringstream text;
+           text << column.entries[i].value;
+           if (text.str().size() > 0)
+             out << text.str();
+           else
+             out << "\"\"";
+         }
+
+                                          // pad after this column
          out << " ";
        }
       out << std::endl;
diff --git a/tests/base/table_handler_03.cc b/tests/base/table_handler_03.cc
new file mode 100644 (file)
index 0000000..f900841
--- /dev/null
@@ -0,0 +1,45 @@
+//-----------------------------------------------------------------------------
+//    $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.
+//
+//-----------------------------------------------------------------------------
+
+// make sure we print something at all when an entry in the table corresponds
+// to the empty string
+
+
+#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_03/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  TableHandler table;
+
+  std::string keys[3] = { "key1", "key2", "key3" };
+
+  table.add_value(keys[0], 0);
+  table.add_value(keys[1], std::string(""));
+  table.add_value(keys[2], std::string("a"));
+
+  table.write_text(deallog.get_file_stream());
+}
diff --git a/tests/base/table_handler_03/cmp/generic b/tests/base/table_handler_03/cmp/generic
new file mode 100644 (file)
index 0000000..e890002
--- /dev/null
@@ -0,0 +1,3 @@
+
+key1 key2 key3 
+0    ""   a    

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.