From 5ea7bd8085a95987e09eb16906058f17c293db73 Mon Sep 17 00:00:00 2001 From: bangerth Date: Thu, 15 Aug 2013 11:59:44 +0000 Subject: [PATCH] Patch by Oleh Krehel: add org-mode as a possibility for TableHandler::write_text(). git-svn-id: https://svn.dealii.org/trunk@30316 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/doc/news/changes.h | 8 ++ deal.II/include/deal.II/base/table_handler.h | 16 +++- deal.II/source/base/table_handler.cc | 95 +++++++++++++------- tests/base/table_handler_13.cc | 59 ++++++++++++ tests/base/table_handler_13/cmp/generic | 6 ++ 5 files changed, 147 insertions(+), 37 deletions(-) create mode 100644 tests/base/table_handler_13.cc create mode 100644 tests/base/table_handler_13/cmp/generic diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index 0b615ad489..d247f75c4b 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -56,6 +56,14 @@ inconvenience this causes.

Specific improvements

    +
  1. + New: TableHandler::write_text() now also supports output in + org-mode (http://orgmode.org/) format via a new entry in the + TableHandler::TextOutputFormat enumeration. +
    + (Oleh Krehel, 2013/08/15) +
  2. +
  3. New: There are now global functions scalar_product that compute the scalar product (double contraction) between diff --git a/deal.II/include/deal.II/base/table_handler.h b/deal.II/include/deal.II/base/table_handler.h index 0e2aebf0fe..314a83a7b9 100644 --- a/deal.II/include/deal.II/base/table_handler.h +++ b/deal.II/include/deal.II/base/table_handler.h @@ -311,13 +311,23 @@ public: * 2 13 a * 1 0 "" * @endcode - * - **/ + * - org_mode_table: Outputs to org-mode (http://orgmode.org/) table + * format. It is easy to convert org-mode tables to HTML/LaTeX/csv. + * Example output: + * @code + * | key1 | key2 | key3 | + * | 0 | 0 | "" | + * | 1 | 0 | "" | + * | 2 | 13 | a | + * | 1 | 0 | "" | + * @endcode + */ enum TextOutputFormat { table_with_headers, table_with_separate_column_description, - simple_table_with_separate_column_description + simple_table_with_separate_column_description, + org_mode_table }; /** diff --git a/deal.II/source/base/table_handler.cc b/deal.II/source/base/table_handler.cc index aed67bee63..425c09a842 100644 --- a/deal.II/source/base/table_handler.cc +++ b/deal.II/source/base/table_handler.cc @@ -343,25 +343,60 @@ void TableHandler::write_text(std::ostream &out, const unsigned int nrows = n_rows(); const unsigned int n_cols = sel_columns.size(); - // handle the simple format separately first - if (format==simple_table_with_separate_column_description) + // cache the columns and compute the widths of each column for alignment + std::vector cols; + std::vector column_widths (n_cols, 0); + for (unsigned int j=0; j::const_iterator + col_iter=columns.find(key); + Assert(col_iter!=columns.end(), ExcInternalError()); + cols.push_back(&(col_iter->second)); + + column_widths[j] = col_iter->second.max_length; + } + + switch (format) + { + case org_mode_table: { // write the captions + out << "| " << std::left; for (unsigned int j=0; j cols; + out << std::flush; + return; + } + + case simple_table_with_separate_column_description: + { + // write the captions for (unsigned int j=0; j::const_iterator - col_iter=columns.find(key); - Assert(col_iter!=columns.end(), ExcInternalError()); - cols.push_back(&(col_iter->second)); + const std::string &key = sel_columns[j]; + out << "# " << j+1 << ": " << key << '\n'; } // write the body @@ -380,33 +415,21 @@ void TableHandler::write_text(std::ostream &out, out << std::flush; return; } - - // cache the columns and compute the widths of each column for alignment - std::vector cols; - std::vector column_widths (n_cols, 0); - for (unsigned int j=0; j::const_iterator - col_iter=columns.find(key); - Assert(col_iter!=columns.end(), ExcInternalError()); - cols.push_back(&(col_iter->second)); - - column_widths[j] = col_iter->second.max_length; - } - - // writing the captions for table_with_separate_column_description - // means that we ignore supercolumns and output the column - // header for each column. enumerate columns starting with 1 - if (format == table_with_separate_column_description) + + case table_with_separate_column_description: { + // writing the captions for table_with_separate_column_description + // means that we ignore supercolumns and output the column + // header for each column. enumerate columns starting with 1 for (unsigned int j=0; j +#include +#include + +#include +#include +#include +#include + + +int main () +{ + std::ofstream logfile("table_handler_13/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], 113); + 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::org_mode_table); +} diff --git a/tests/base/table_handler_13/cmp/generic b/tests/base/table_handler_13/cmp/generic new file mode 100644 index 0000000000..22555388a3 --- /dev/null +++ b/tests/base/table_handler_13/cmp/generic @@ -0,0 +1,6 @@ + +| key1 | key2 | key3 | +| 0 | 0 | "" | +| 1 | 0 | "" | +| 2 | 113 | a | +| 1 | 0 | "" | -- 2.39.5