]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Improve ParameterHandler continuation handling.
authorDavid Wells <wellsd2@rpi.edu>
Tue, 19 Jan 2016 02:10:39 +0000 (21:10 -0500)
committerDavid Wells <wellsd2@rpi.edu>
Tue, 19 Jan 2016 02:12:18 +0000 (21:12 -0500)
This commit builds on 23f306dfc5. ParameterHandler now correctly handles
continuation lines in both versions of the read_input function, while before it
only handled continuation lines in one version (the file name as a string
version) before.

Closes #333.

12 files changed:
doc/news/changes.h
source/base/parameter_handler.cc
tests/bits/parameter_handler_backslash_01.cc
tests/bits/parameter_handler_backslash_01.output
tests/bits/parameter_handler_backslash_02.cc
tests/bits/parameter_handler_backslash_02.output
tests/bits/parameter_handler_backslash_03.cc
tests/bits/parameter_handler_backslash_03.expect=diff.output
tests/bits/parameter_handler_backslash_04.cc
tests/bits/parameter_handler_backslash_04.expect=diff.output
tests/bits/parameter_handler_backslash_05.cc
tests/bits/parameter_handler_backslash_05.expect=diff.output

index b4b652751f35fc775d4de7a316496e8054aa8556..ec08bf161ce95f4234c44879cf65107c77189fe2 100644 (file)
@@ -433,7 +433,7 @@ inconvenience this causes.
 
   <li> Improved: Allow continuation lines in ParameterHandler.
   <br>
-  (Alberto Sartori, 2015/09/04)
+  (Alberto Sartori, 2015/09/04, David Wells, 2016/01/18)
   </li>
 
   <li> Cleanup: The interface of Tensor<rank,dim,Number> has been cleaned
index 97679b3f06f6523946e9f9c4737cdbc6e3d3831a..fa072ed560eb1d5f43c585f9c260be77a5287eac 100644 (file)
@@ -1337,14 +1337,45 @@ bool ParameterHandler::read_input (std::istream &input,
   // store subsections we are currently in
   std::vector<std::string> saved_path = subsection_path;
 
-  std::string line;
+  std::string input_line;
+  std::string fully_concatenated_line;
+  bool is_concatenated = false;
   unsigned int current_line_n = 0;
   bool status = true;
 
-  while (std::getline (input, line))
+  while (std::getline (input, input_line))
     {
       ++current_line_n;
-      status &= scan_line (line, filename, current_line_n);
+
+      // check whether or not the current line should be joined with the next
+      // line before calling scan_line.
+      if (input_line.length() != 0 &&
+          input_line.find_last_of('\\') == input_line.length() - 1)
+        {
+          input_line.erase(input_line.length() - 1); // remove the last '\'
+          is_concatenated = true;
+
+          fully_concatenated_line += input_line;
+        }
+      // If the previous line ended in a '\' but the current did not, then we
+      // should proceed to scan_line.
+      else if (is_concatenated)
+        {
+          fully_concatenated_line += input_line;
+          is_concatenated = false;
+        }
+      // finally, if neither the previous nor current lines are continuations,
+      // then the current input line is entirely concatenated.
+      else
+        {
+          fully_concatenated_line = input_line;
+        }
+
+      if (!is_concatenated)
+        {
+          status &= scan_line (fully_concatenated_line, filename, current_line_n);
+          fully_concatenated_line.clear();
+        }
     }
 
   if (status && (saved_path != subsection_path))
@@ -1382,19 +1413,10 @@ bool ParameterHandler::read_input (const std::string &filename,
   try
     {
       std::string openname = search.find(filename);
-      std::ifstream file (openname.c_str());
-      AssertThrow(file, ExcIO());
+      std::ifstream file_stream (openname.c_str());
+      AssertThrow(file_stream, ExcIO());
 
-      std::string str;
-      std::string file_contents;
-
-      while (std::getline(file, str))
-        {
-          file_contents += str;
-          file_contents.push_back('\n');
-        }
-
-      return ParameterHandler::read_input_from_string (file_contents.c_str());
+      return read_input (file_stream, filename);
     }
   catch (const PathSearch::ExcFileNotFound &)
     {
@@ -1416,43 +1438,8 @@ bool ParameterHandler::read_input (const std::string &filename,
 
 bool ParameterHandler::read_input_from_string (const char *s)
 {
-  // create an istringstream representation and pass it off
-  // to the other functions doing this work
-
-  // 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");
+  std::istringstream input_stream (s);
+  return read_input (input_stream, "input string");
 }
 
 
@@ -2840,7 +2827,7 @@ bool MultipleParameterLoop::read_input (const std::string &filename,
   return ParameterHandler::read_input (filename, optional, write_compact);
   // don't call init_branches, since this read_input
   // function calls
-  // MultipleParameterLoop::Readinput(std::istream &, std::ostream &)
+  // MultipleParameterLoop::read_input(std::istream &, std::ostream &)
   // which itself calls init_branches.
 }
 
index 689734fd0e0614900bfba099890b4dc66a3bdc92..88f65430a44b160ce1692b6d65f5291f0cc133cd 100644 (file)
@@ -1,6 +1,6 @@
 // ---------------------------------------------------------------------
 //
-// Copyright (C) 2003 - 2014 by the deal.II authors
+// Copyright (C) 2003 - 2016 by the deal.II authors
 //
 // This file is part of the deal.II library.
 //
@@ -26,21 +26,34 @@ int main ()
   deallog.attach(logfile);
   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;
+  for (unsigned int i = 0; i < 2; ++i)
+    {
+      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 ();
+
+      // test both relevant read_input functions
+      if (i == 0)
+        {
+          prm.read_input(SOURCE_DIR "/prm/parameter_handler_backslash_01.prm");
+        }
+      else
+        {
+          std::ifstream input_stream
+          (SOURCE_DIR "/prm/parameter_handler_backslash_01.prm");
+          prm.read_input(input_stream);
+        }
+
+      std::string list;
+      prm.enter_subsection ("Testing");
+      list = prm.get ("Function");
+      prm.leave_subsection ();
+
+      deallog << list << std::endl;
+    }
 
   return 0;
 }
index 9eb091f67dbed915a1bf7cf0ff38baf89a417a6d..5c8ecde2ba9429f56b1ce7ecd1189040b102f7ee 100644 (file)
@@ -1,2 +1,3 @@
 
 DEAL::a, b, c
+DEAL::a, b, c
index 08f7b89675d6d91ae49367b14008e941d7e4bebc..f5e8b216e733f13af736033b2fb45780b32f2116 100644 (file)
@@ -1,6 +1,6 @@
 // ---------------------------------------------------------------------
 //
-// Copyright (C) 2003 - 2014 by the deal.II authors
+// Copyright (C) 2003 - 2016 by the deal.II authors
 //
 // This file is part of the deal.II library.
 //
@@ -26,21 +26,34 @@ int main ()
   deallog.attach(logfile);
   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;
+  for (unsigned int i = 0; i < 2; ++i)
+    {
+      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 ();
+
+      // test both relevant read_input functions
+      if (i == 0)
+        {
+          prm.read_input(SOURCE_DIR "/prm/parameter_handler_backslash_02.prm");
+        }
+      else
+        {
+          std::ifstream input_stream
+          (SOURCE_DIR "/prm/parameter_handler_backslash_02.prm");
+          prm.read_input(input_stream);
+        }
+
+      std::string list;
+      prm.enter_subsection ("Testing");
+      list = prm.get ("Function");
+      prm.leave_subsection ();
+
+      deallog << list << std::endl;
+    }
 
   return 0;
 }
index 7e7203d2cdd575d1609891ef5b00ceb488ae2bad..0c39df8770de98544a7a37f2eff1c810c30bd93d 100644 (file)
@@ -1,2 +1,3 @@
 
 DEAL::a,b,c
+DEAL::a,b,c
index 99e59faa093699915be91401577e7e663486df26..3b205e7be0bce9bf833a71ae91ef5c0a2d76bb0a 100644 (file)
@@ -1,6 +1,6 @@
 // ---------------------------------------------------------------------
 //
-// Copyright (C) 2003 - 2014 by the deal.II authors
+// Copyright (C) 2003 - 2016 by the deal.II authors
 //
 // This file is part of the deal.II library.
 //
@@ -26,21 +26,35 @@ int main ()
   deallog.attach(logfile);
   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;
+  for (unsigned int i = 0; i < 2; ++i)
+    {
+      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 ();
+
+      // test both relevant read_input functions
+      if (i == 0)
+        {
+          prm.read_input(SOURCE_DIR "/prm/parameter_handler_backslash_03.prm");
+        }
+      else
+        {
+          std::ifstream input_stream
+          (SOURCE_DIR "/prm/parameter_handler_backslash_03.prm");
+          prm.read_input(input_stream);
+        }
+
+
+      std::string list;
+      prm.enter_subsection ("Testing");
+      list = prm.get ("Function");
+      prm.leave_subsection ();
+
+      deallog << list << std::endl;
+    }
 
   return 0;
 }
index 9eb091f67dbed915a1bf7cf0ff38baf89a417a6d..5c8ecde2ba9429f56b1ce7ecd1189040b102f7ee 100644 (file)
@@ -1,2 +1,3 @@
 
 DEAL::a, b, c
+DEAL::a, b, c
index 4b8e8f90371f411cf18a968b8133a99e7967caa5..92fe3fe89ac24786c8d967deeca531aee6e206a4 100644 (file)
@@ -1,6 +1,6 @@
 // ---------------------------------------------------------------------
 //
-// Copyright (C) 2003 - 2014 by the deal.II authors
+// Copyright (C) 2003 - 2016 by the deal.II authors
 //
 // This file is part of the deal.II library.
 //
@@ -26,21 +26,35 @@ int main ()
   deallog.attach(logfile);
   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;
+  for (unsigned int i = 0; i < 2; ++i)
+    {
+      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 ();
+
+      // test both relevant read_input functions
+      if (i == 0)
+        {
+          prm.read_input(SOURCE_DIR "/prm/parameter_handler_backslash_04.prm");
+        }
+      else
+        {
+          std::ifstream input_stream
+          (SOURCE_DIR "/prm/parameter_handler_backslash_04.prm");
+          prm.read_input(input_stream);
+        }
+
+
+      std::string list;
+      prm.enter_subsection ("Testing");
+      list = prm.get ("Function");
+      prm.leave_subsection ();
+
+      deallog << list << std::endl;
+    }
 
   return 0;
 }
index 9eb091f67dbed915a1bf7cf0ff38baf89a417a6d..5c8ecde2ba9429f56b1ce7ecd1189040b102f7ee 100644 (file)
@@ -1,2 +1,3 @@
 
 DEAL::a, b, c
+DEAL::a, b, c
index c34a71c2c0ba7b8013ef6672a8f124bc9352c332..cd0b010ff7a1d87b03c01f0e8058956e17e60451 100644 (file)
@@ -1,6 +1,6 @@
 // ---------------------------------------------------------------------
 //
-// Copyright (C) 2003 - 2014 by the deal.II authors
+// Copyright (C) 2003 - 2016 by the deal.II authors
 //
 // This file is part of the deal.II library.
 //
@@ -26,27 +26,41 @@ int main ()
   deallog.attach(logfile);
   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;
+  for (unsigned int i = 0; i < 2; ++i)
+    {
+      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 ();
+
+
+      // test both relevant read_input functions
+      if (i == 0)
+        {
+          prm.read_input(SOURCE_DIR "/prm/parameter_handler_backslash_05.prm");
+        }
+      else
+        {
+          std::ifstream input_stream
+          (SOURCE_DIR "/prm/parameter_handler_backslash_05.prm");
+          prm.read_input(input_stream);
+        }
+
+      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;
 }
index 5902adc93b8af839b187c9448f4a74479d3c42a6..ab2fb7d59e40f1483e17f2c569ecfdf9b172f009 100644 (file)
@@ -1,3 +1,5 @@
 
 DEAL::a, b, c
 DEAL::d, e, f
+DEAL::a, b, c
+DEAL::d, e, f

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.