]> https://gitweb.dealii.org/ - dealii.git/commitdiff
allows continuation lines in ParameterHandler 1520/head
authoralberto sartori <alberto.sartori86@gmail.com>
Thu, 3 Sep 2015 11:09:21 +0000 (13:09 +0200)
committeralberto sartori <alberto.sartori86@gmail.com>
Fri, 4 Sep 2015 19:11:21 +0000 (21:11 +0200)
18 files changed:
doc/news/changes.h
include/deal.II/base/parameter_handler.h
source/base/parameter_handler.cc
tests/bits/parameter_handler_backslash_01.cc [new file with mode: 0644]
tests/bits/parameter_handler_backslash_01.output [new file with mode: 0644]
tests/bits/parameter_handler_backslash_02.cc [new file with mode: 0644]
tests/bits/parameter_handler_backslash_02.output [new file with mode: 0644]
tests/bits/parameter_handler_backslash_03.cc [new file with mode: 0644]
tests/bits/parameter_handler_backslash_03.expect=diff.output [new file with mode: 0644]
tests/bits/parameter_handler_backslash_04.cc [new file with mode: 0644]
tests/bits/parameter_handler_backslash_04.expect=diff.output [new file with mode: 0644]
tests/bits/parameter_handler_backslash_05.cc [new file with mode: 0644]
tests/bits/parameter_handler_backslash_05.expect=diff.output [new file with mode: 0644]
tests/bits/prm/parameter_handler_backslash_01.prm [new file with mode: 0644]
tests/bits/prm/parameter_handler_backslash_02.prm [new file with mode: 0644]
tests/bits/prm/parameter_handler_backslash_03.prm [new file with mode: 0644]
tests/bits/prm/parameter_handler_backslash_04.prm [new file with mode: 0644]
tests/bits/prm/parameter_handler_backslash_05.prm [new file with mode: 0644]

index 2ecb7ce9aefa84256bba059150951a4d26f79327..ea479bf18574326ead61c96451142df3d4bc1d5a 100644 (file)
@@ -114,6 +114,10 @@ inconvenience this causes.
 
 
 <ol>
+  <li> Improved: Allow continuation lines in ParameterHandler.
+  <br>
+  (Alberto Sartori, 2015/09/04)
+
   <li> Fixed: VectorTools::integrate_difference for VectorTools::Hdiv_seminorm
   was computed incorrectly.
   <br>
index fd687a6e8a59d2956e209be36e06621cba38ec09..6042d7b3b43f59688a9d2b429fa496950430cdff 100644 (file)
@@ -1025,6 +1025,9 @@ namespace Patterns
  *
  * Comments starting with \# are skipped.
  *
+ * Continuation lines are allowed by means of the character <tt>\\</tt>,
+ * which must be the last character of the line.
+ *
  * We propose to use the following scheme to name entries: start the first
  * word with a capital letter and use lowercase letters further on. The same
  * applies to the possible entry values to the right of the <tt>=</tt> sign.
index d410fdabf135b12c64a84aba8c9035c1024581a4..5ee5b461a03df5392f71c17fc2ffd32ad2a3b197 100644 (file)
@@ -1382,10 +1382,19 @@ bool ParameterHandler::read_input (const std::string &filename,
   try
     {
       std::string openname = search.find(filename);
-      std::ifstream input (openname.c_str());
-      AssertThrow(input, ExcIO());
+      std::ifstream file (openname.c_str());
+      AssertThrow(file, ExcIO());
 
-      return read_input (input, filename);
+      std::string str;
+      std::string file_contents;
+
+      while (std::getline(file, str))
+        {
+          file_contents += str;
+          file_contents.push_back('\n');
+        }
+
+      return read_input_from_string (file_contents.c_str());
     }
   catch (const PathSearch::ExcFileNotFound &)
     {
@@ -1409,7 +1418,40 @@ bool ParameterHandler::read_input_from_string (const char *s)
 {
   // create an istringstream representation and pass it off
   // to the other functions doing this work
-  std::istringstream in (s);
+
+  // concatenate the lines ending with "\"
+
+  std::istringstream file(s);
+  std::string str;
+  std::string file_contents;
+  bool is_concatenated(false);
+  while (std::getline(file, str))
+    {
+      // if this str must be concatenated with the previous one
+      // we strip the whitespaces
+      if (is_concatenated)
+        {
+          const std::size_t str_begin = str.find_first_not_of(" \t");
+          if (str_begin == std::string::npos)
+            str = ""; // no content
+          else
+            str.erase(0,str_begin); // trim whitespaces from left
+        }
+
+      if ((std::find(str.begin(),str.end(),'\\') != str.end()) && str.find_last_of('\\') == str.length()-1)
+        {
+          str.erase(str.length()-1); // remove '\'
+          file_contents += str;
+          is_concatenated = true; // next line must be concatenated = true
+        }
+      else
+        {
+          is_concatenated = false; // next line must be concatenated = false
+          file_contents += str;
+          file_contents.push_back('\n');
+        }
+    }
+  std::istringstream in (file_contents);
   return read_input (in, "input string");
 }
 
diff --git a/tests/bits/parameter_handler_backslash_01.cc b/tests/bits/parameter_handler_backslash_01.cc
new file mode 100644 (file)
index 0000000..36154ff
--- /dev/null
@@ -0,0 +1,47 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2003 - 2014 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.
+//
+// ---------------------------------------------------------------------
+
+
+#include "../tests.h"
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/parameter_handler.h>
+#include <fstream>
+
+
+int main ()
+{
+  std::ofstream logfile("output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  ParameterHandler prm;
+  prm.enter_subsection ("Testing");
+  prm.declare_entry ("Function",
+                     "a",
+                     Patterns::List(Patterns::Selection("a|b|c|d|e|f|g|h")));
+  prm.leave_subsection ();
+
+  prm.read_input(SOURCE_DIR "/prm/parameter_handler_backslash_01.prm");
+
+  std::string list;
+  prm.enter_subsection ("Testing");
+  list = prm.get ("Function");
+  prm.leave_subsection ();
+
+  deallog << list << std::endl;
+
+  return 0;
+}
diff --git a/tests/bits/parameter_handler_backslash_01.output b/tests/bits/parameter_handler_backslash_01.output
new file mode 100644 (file)
index 0000000..9eb091f
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::a, b, c
diff --git a/tests/bits/parameter_handler_backslash_02.cc b/tests/bits/parameter_handler_backslash_02.cc
new file mode 100644 (file)
index 0000000..a8556fb
--- /dev/null
@@ -0,0 +1,47 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2003 - 2014 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.
+//
+// ---------------------------------------------------------------------
+
+
+#include "../tests.h"
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/parameter_handler.h>
+#include <fstream>
+
+
+int main ()
+{
+  std::ofstream logfile("output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  ParameterHandler prm;
+  prm.enter_subsection ("Testing");
+  prm.declare_entry ("Function",
+                     "a",
+                     Patterns::List(Patterns::Selection("a|b|c|d|e|f|g|h")));
+  prm.leave_subsection ();
+
+  prm.read_input(SOURCE_DIR "/prm/parameter_handler_backslash_02.prm");
+
+  std::string list;
+  prm.enter_subsection ("Testing");
+  list = prm.get ("Function");
+  prm.leave_subsection ();
+
+  deallog << list << std::endl;
+
+  return 0;
+}
diff --git a/tests/bits/parameter_handler_backslash_02.output b/tests/bits/parameter_handler_backslash_02.output
new file mode 100644 (file)
index 0000000..7e7203d
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::a,b,c
diff --git a/tests/bits/parameter_handler_backslash_03.cc b/tests/bits/parameter_handler_backslash_03.cc
new file mode 100644 (file)
index 0000000..4d9924e
--- /dev/null
@@ -0,0 +1,47 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2003 - 2014 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.
+//
+// ---------------------------------------------------------------------
+
+
+#include "../tests.h"
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/parameter_handler.h>
+#include <fstream>
+
+
+int main ()
+{
+  std::ofstream logfile("output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  ParameterHandler prm;
+  prm.enter_subsection ("Testing");
+  prm.declare_entry ("Function",
+                     "a",
+                     Patterns::List(Patterns::Selection("a|b|c|d|e|f|g|h")));
+  prm.leave_subsection ();
+
+  prm.read_input(SOURCE_DIR "/prm/parameter_handler_backslash_03.prm");
+
+  std::string list;
+  prm.enter_subsection ("Testing");
+  list = prm.get ("Function");
+  prm.leave_subsection ();
+
+  deallog << list << std::endl;
+
+  return 0;
+}
diff --git a/tests/bits/parameter_handler_backslash_03.expect=diff.output b/tests/bits/parameter_handler_backslash_03.expect=diff.output
new file mode 100644 (file)
index 0000000..9eb091f
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::a, b, c
diff --git a/tests/bits/parameter_handler_backslash_04.cc b/tests/bits/parameter_handler_backslash_04.cc
new file mode 100644 (file)
index 0000000..1dcf223
--- /dev/null
@@ -0,0 +1,47 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2003 - 2014 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.
+//
+// ---------------------------------------------------------------------
+
+
+#include "../tests.h"
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/parameter_handler.h>
+#include <fstream>
+
+
+int main ()
+{
+  std::ofstream logfile("output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  ParameterHandler prm;
+  prm.enter_subsection ("Testing");
+  prm.declare_entry ("Function",
+                     "a",
+                     Patterns::List(Patterns::Selection("a|b|c|d|e|f|g|h")));
+  prm.leave_subsection ();
+
+  prm.read_input(SOURCE_DIR "/prm/parameter_handler_backslash_04.prm");
+
+  std::string list;
+  prm.enter_subsection ("Testing");
+  list = prm.get ("Function");
+  prm.leave_subsection ();
+
+  deallog << list << std::endl;
+
+  return 0;
+}
diff --git a/tests/bits/parameter_handler_backslash_04.expect=diff.output b/tests/bits/parameter_handler_backslash_04.expect=diff.output
new file mode 100644 (file)
index 0000000..9eb091f
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::a, b, c
diff --git a/tests/bits/parameter_handler_backslash_05.cc b/tests/bits/parameter_handler_backslash_05.cc
new file mode 100644 (file)
index 0000000..ae050fd
--- /dev/null
@@ -0,0 +1,53 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2003 - 2014 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.
+//
+// ---------------------------------------------------------------------
+
+
+#include "../tests.h"
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/parameter_handler.h>
+#include <fstream>
+
+
+int main ()
+{
+  std::ofstream logfile("output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  ParameterHandler prm;
+  prm.enter_subsection ("Testing");
+  prm.declare_entry ("Function_1",
+                     "a",
+                     Patterns::List(Patterns::Selection("a|b|c")));
+  prm.declare_entry ("Function_2",
+                     "d",
+                     Patterns::List(Patterns::Selection("d|e|f")));
+  prm.leave_subsection ();
+
+  prm.read_input(SOURCE_DIR "/prm/parameter_handler_backslash_05.prm");
+
+  std::string list_1;
+  std::string list_2;
+  prm.enter_subsection ("Testing");
+  list_1 = prm.get ("Function_1");
+  list_2 = prm.get ("Function_2");
+  prm.leave_subsection ();
+
+  deallog << list_1 << std::endl;
+  deallog << list_2 << std::endl;
+
+  return 0;
+}
diff --git a/tests/bits/parameter_handler_backslash_05.expect=diff.output b/tests/bits/parameter_handler_backslash_05.expect=diff.output
new file mode 100644 (file)
index 0000000..5902adc
--- /dev/null
@@ -0,0 +1,3 @@
+
+DEAL::a, b, c
+DEAL::d, e, f
diff --git a/tests/bits/prm/parameter_handler_backslash_01.prm b/tests/bits/prm/parameter_handler_backslash_01.prm
new file mode 100644 (file)
index 0000000..c5a42a1
--- /dev/null
@@ -0,0 +1,7 @@
+#Listing of Parameters
+#---------------------
+subsection Testing
+  set Function = a, \
+                 b, \
+                 c
+end
diff --git a/tests/bits/prm/parameter_handler_backslash_02.prm b/tests/bits/prm/parameter_handler_backslash_02.prm
new file mode 100644 (file)
index 0000000..0e3772c
--- /dev/null
@@ -0,0 +1,9 @@
+#Listing of Parameters
+#---------------------
+subsection Testing
+  set Function = a,\
+                 \
+                 b,\
+\
+                 c
+end
diff --git a/tests/bits/prm/parameter_handler_backslash_03.prm b/tests/bits/prm/parameter_handler_backslash_03.prm
new file mode 100644 (file)
index 0000000..b8191f5
--- /dev/null
@@ -0,0 +1,7 @@
+#Listing of Parameters
+#---------------------
+subsection Testing
+  set Function = a,\ # first term
+                 b,\ # second
+                 c   # third
+end
diff --git a/tests/bits/prm/parameter_handler_backslash_04.prm b/tests/bits/prm/parameter_handler_backslash_04.prm
new file mode 100644 (file)
index 0000000..59282a3
--- /dev/null
@@ -0,0 +1,6 @@
+#Listing of Parameters
+#---------------------
+subsection Testing
+  set Function = a,\ b,
+                 c
+end
diff --git a/tests/bits/prm/parameter_handler_backslash_05.prm b/tests/bits/prm/parameter_handler_backslash_05.prm
new file mode 100644 (file)
index 0000000..a2b3ce4
--- /dev/null
@@ -0,0 +1,18 @@
+# a blank line will break the continuation
+# Listing of Parameters
+# ---------------------
+subsection Testing
+  set Function_1 = a, \
+
+                   b, \
+                   c
+
+  set Function_2 = d, e, \
+\
+\
+                   \
+                   f
+end
+
+

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.