]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Patch by Oleh Krehel: add org-mode as a possibility for TableHandler::write_text().
authorbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Thu, 15 Aug 2013 11:59:44 +0000 (11:59 +0000)
committerbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Thu, 15 Aug 2013 11:59:44 +0000 (11:59 +0000)
git-svn-id: https://svn.dealii.org/trunk@30316 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_13.cc [new file with mode: 0644]
tests/base/table_handler_13/cmp/generic [new file with mode: 0644]

index 0b615ad489ff8e4ec7c4aba05dac8f6588074ca4..d247f75c4bcc445e69e13f26c9d97e28e66f3274 100644 (file)
@@ -56,6 +56,14 @@ inconvenience this causes.
 <h3>Specific improvements</h3>
 
 <ol>
+  <li>
+  New: TableHandler::write_text() now also supports output in
+  org-mode (http://orgmode.org/) format via a new entry in the
+  TableHandler::TextOutputFormat enumeration.
+  <br>
+  (Oleh Krehel, 2013/08/15)
+  </li>
+
   <li>
   New: There are now global functions <code>scalar_product</code>
   that compute the scalar product (double contraction) between
index 0e2aebf0fe664e8448acb3083f66d63f6968545d..314a83a7b9a5c2e6b0d96c3e243d9ef04c539411 100644 (file)
@@ -311,13 +311,23 @@ public:
    *     2 13 a
    *     1 0 ""
    *   @endcode
-   *
-   **/
+   * - <code>org_mode_table</code>: 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
   };
 
   /**
index aed67bee637e93477d67317d8ec70181dcb5b30e..425c09a8421d26bbe9eedad750e43bc6a3179119 100644 (file)
@@ -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<const Column *> cols;
+  std::vector<unsigned int> column_widths (n_cols, 0);
+  for (unsigned int j=0; j<n_cols; ++j)
+    {
+      std::string key=sel_columns[j];
+      const std::map<std::string, Column>::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<n_cols; ++j)
         {
           const std::string &key = sel_columns[j];
-          out << "# " << j+1 << ": " << key << '\n';
+          out << std::setw(column_widths[j]);
+          out << key << " | ";
+        }
+      out << std::endl;
+
+      // write the body
+      for (unsigned int i=0; i<nrows; ++i)
+        {
+          out << "| ";
+          for (unsigned int j=0; j<n_cols; ++j)
+            {
+              const Column &column=*(cols[j]);
+
+              out << std::setw(column_widths[j]);
+              out << column.entries[i].get_cached_string();
+              out << " | ";
+            }
+          out << '\n';
         }
 
-      // cache the columns
-      std::vector<const Column *> cols;
+      out << std::flush;
+      return;
+    }
+    
+    case simple_table_with_separate_column_description:
+    {
+      // write the captions
       for (unsigned int j=0; j<n_cols; ++j)
         {
-          std::string key=sel_columns[j];
-          const std::map<std::string, Column>::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<const Column *> cols;
-  std::vector<unsigned int> column_widths (n_cols, 0);
-  for (unsigned int j=0; j<n_cols; ++j)
-    {
-      std::string key=sel_columns[j];
-      const std::map<std::string, Column>::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<n_cols; ++j)
         {
           std::string key=sel_columns[j];
           out << "# " << j+1 << ": " << key << '\n';
         }
+      break;
     }
-  else if (format == table_with_headers)
+    
+    case table_with_headers:
     {
       // This format output supercolumn headers and aligns them centered
       // over all the columns that belong to it.
@@ -480,12 +503,16 @@ void TableHandler::write_text(std::ostream &out,
           out << ' ';
         }
       out << '\n';
+      break;
+    }
+    
+    default:
+      Assert (false, ExcInternalError());
     }
-  else
-    Assert (false, ExcInternalError());
-
 
-  // finally output the data itself
+  
+  // finally output the data itself for
+  // table_with_headers or table_with_separate_column_description:
   for (unsigned int i=0; i<nrows; ++i)
     {
       for (unsigned int j=0; j<n_cols; ++j)
diff --git a/tests/base/table_handler_13.cc b/tests/base/table_handler_13.cc
new file mode 100644 (file)
index 0000000..1a6a019
--- /dev/null
@@ -0,0 +1,59 @@
+// ---------------------------------------------------------------------
+// $Id$
+//
+// Copyright (C) 2010 - 2013 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+// use the org-mode 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_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 (file)
index 0000000..2255538
--- /dev/null
@@ -0,0 +1,6 @@
+
+| key1 | key2 | key3 | 
+| 0 | 0   | "" | 
+| 1 | 0   | "" | 
+| 2 | 113 | 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.