From: bangerth Date: Tue, 18 Oct 2011 10:14:33 +0000 (+0000) Subject: Print "" in empty entries of a table. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4fb49e6ffc45c295561cd8c83789fb442ec04d35;p=dealii-svn.git Print "" in empty entries of a table. git-svn-id: https://svn.dealii.org/trunk@24623 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index 174cda9d6b..f09d237f56 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -44,6 +44,14 @@ inconvenience this causes.

Specific improvements

    +
  1. 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 "" in such places. +
    +(Wolfgang Bangerth, 2011/10/18) +
  2. 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. diff --git a/deal.II/include/deal.II/base/table_handler.h b/deal.II/include/deal.II/base/table_handler.h index b985ff4766..01c91f11d3 100644 --- a/deal.II/include/deal.II/base/table_handler.h +++ b/deal.II/include/deal.II/base/table_handler.h @@ -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 "". */ void write_text (std::ostream &out) const; diff --git a/deal.II/source/base/table_handler.cc b/deal.II/source/base/table_handler.cc index d9bc6c736c..7a3771d9fc 100644 --- a/deal.II/source/base/table_handler.cc +++ b/deal.II/source/base/table_handler.cc @@ -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 index 0000000000..f900841ec8 --- /dev/null +++ b/tests/base/table_handler_03.cc @@ -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 +#include +#include + +#include +#include +#include +#include + + +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 index 0000000000..e890002dd3 --- /dev/null +++ b/tests/base/table_handler_03/cmp/generic @@ -0,0 +1,3 @@ + +key1 key2 key3 +0 "" a