]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Fix ParameterHandler in a different way.
authorWolfgang Bangerth <bangerth@math.tamu.edu>
Sun, 31 Mar 2013 21:48:23 +0000 (21:48 +0000)
committerWolfgang Bangerth <bangerth@math.tamu.edu>
Sun, 31 Mar 2013 21:48:23 +0000 (21:48 +0000)
git-svn-id: https://svn.dealii.org/trunk@29126 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/changes.h
deal.II/include/deal.II/base/parameter_handler.h
deal.II/source/base/parameter_handler.cc
tests/bits/parameter_handler_14.cc [new file with mode: 0644]
tests/bits/parameter_handler_14/cmp/generic [new file with mode: 0644]

index d108c770aa2b20f726aab3691bbbfcf59915eaa3..af36ae7ade44964ba08148fbe0df19a3857ddadc 100644 (file)
@@ -89,6 +89,12 @@ this function.
 
 <ol>
 
+<li> Fixed: The ParameterHandler class could not deal with parameters named
+<code>"value"</code> (and a few other names). This is now fixed.
+<br>
+(Denis Davydov, Matthias Maier, Wolfgang Bangerth, 2013/3/31)
+</li>
+
 <li> Changed: TimerOutput no longer assumes that sections are not nested
 when outputting percentage and total run time.
 <br>
index 7d1a7ba379e1de71061120718d947301e21322a4..bf9aad7c0ec7d1ca39b22b7caeb090ffc09cb269 100644 (file)
@@ -1772,9 +1772,8 @@ namespace Patterns
  *   numbers, every character in their names that is not a letter or number
  *   is replaced
  *   by an underscore followed by its two-digit hexadecimal representation.
- *   In addition, the special names "value", "default_value", "pattern",
- *   "pattern_index", and "documentation" are mangled when used as
- *   names of parameters given that these names are also used to name
+ *   In addition, the special name "value" is mangled when used as the
+ *   name of a parameter, given that this name is also used to name
  *   special files in the hierarchy structure.
  *   Finally, the entire tree is wrapped into a tag
  *   <code>%ParameterHandler</code> to satisfy the XML requirement that there
index 40c512cce1574da779a5a1bb32874ecb3f1cd143..c1846a87cb89d2f5330275676d4f8edc40c9ee23 100644 (file)
@@ -2,7 +2,7 @@
 //    $Id$
 //    Version: $Name$
 //
-//    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2010, 2011, 2012 by the deal.II authors
+//    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2010, 2011, 2012, 2013 by the deal.II authors
 //
 //    This file is subject to QPL and may not be  distributed
 //    without copyright and license information. Please refer
@@ -1067,25 +1067,35 @@ std::string
 ParameterHandler::mangle (const std::string &s)
 {
   std::string u;
+
+  // reserve the minimum number of characters we will need. it may
+  // be more but this is the least we can do
   u.reserve (s.size());
 
-  static const std::string allowed_characters
-  ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
+  // see if the name is special and if so mangle the whole thing
+  const bool mangle_whole_string = (s == "value");
 
   // for all parts of the string, see
   // if it is an allowed character or
   // not
   for (unsigned int i=0; i<s.size(); ++i)
-    if (allowed_characters.find (s[i]) != std::string::npos)
-      u.push_back (s[i]);
-    else
-      {
-        u.push_back ('_');
-        static const char hex[16]
-          = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
-        u.push_back (hex[static_cast<unsigned char>(s[i])/16]);
-        u.push_back (hex[static_cast<unsigned char>(s[i])%16]);
-      }
+    {
+      static const std::string allowed_characters
+       ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
+
+      if (mangle_whole_string
+         ||
+         (allowed_characters.find (s[i]) != std::string::npos))
+       u.push_back (s[i]);
+      else
+       {
+         u.push_back ('_');
+         static const char hex[16]
+           = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
+         u.push_back (hex[static_cast<unsigned char>(s[i])/16]);
+         u.push_back (hex[static_cast<unsigned char>(s[i])%16]);
+       }
+    }
 
   return u;
 }
@@ -1228,14 +1238,7 @@ ParameterHandler::demangle (const std::string &s)
 bool
 ParameterHandler::is_parameter_node (const boost::property_tree::ptree &p)
 {
-  // A parameter node must have a value key
-  if (p.get_optional<std::string>("value"))
-    {
-      // and the associated child ptree must be final, i.e. no children:
-      if(p.get_child("value").size() == 0)
-        return true;
-    }
-  return false;
+  return (p.get_optional<std::string>("value"));
 }
 
 
diff --git a/tests/bits/parameter_handler_14.cc b/tests/bits/parameter_handler_14.cc
new file mode 100644 (file)
index 0000000..74dca55
--- /dev/null
@@ -0,0 +1,60 @@
+//----------------------------  parameter_handler_14.cc  ---------------------------
+//    $Id$
+//    Version: $Name$
+//
+//    Copyright (C) 2002, 2003, 2004, 2005, 2010, 2012, 2013 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.
+//
+//----------------------------  parameter_handler_14.cc  ---------------------------
+
+
+// ParameterHandler could not deal with parameters named "value" as well as a
+// few other names. see the thread on the mailing starting with a post by
+// Denis Davydov on March 30, 2013
+
+#include "../tests.h"
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/parameter_handler.h>
+#include <fstream>
+
+void check ()
+{
+  ParameterHandler foo;
+  foo.enter_subsection("bar");
+  foo.declare_entry("value", "1.0", dealii::Patterns::Double(), "");
+  foo.leave_subsection();
+
+  try
+    {
+      foo.read_input("parameter_handler_14/tmp.prm");
+    }
+  catch (...)
+    {
+      deallog << "Exception caught, but none should happen here!!!"
+             << std::endl;
+    }
+
+  foo.enter_subsection("bar");
+  deallog << foo.get ("value") << std::endl;
+  foo.leave_subsection();
+
+  // delete tmp file again
+  std::remove("parameter_handler_14/tmp.prm");
+}
+
+
+int main ()
+{
+  std::ofstream logfile("parameter_handler_14/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  check ();
+
+  return 0;
+}
diff --git a/tests/bits/parameter_handler_14/cmp/generic b/tests/bits/parameter_handler_14/cmp/generic
new file mode 100644 (file)
index 0000000..3d77c7d
--- /dev/null
@@ -0,0 +1,3 @@
+
+DEAL::Exception caught, but none should happen here!!!
+DEAL::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.