]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Make the separator in Patterns::List user defined.
authorWolfgang Bangerth <bangerth@math.tamu.edu>
Thu, 22 Aug 2013 03:12:47 +0000 (03:12 +0000)
committerWolfgang Bangerth <bangerth@math.tamu.edu>
Thu, 22 Aug 2013 03:12:47 +0000 (03:12 +0000)
git-svn-id: https://svn.dealii.org/trunk@30404 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_1a.cc [new file with mode: 0644]
tests/bits/parameter_handler_1a/cmp/generic [new file with mode: 0644]
tests/bits/parameter_handler_1a/prm [new file with mode: 0644]

index c3781bca612388b00f8ca51c19f33eef26216588..529e7e523f08ffdceb07b904b1f729821168eb79 100644 (file)
@@ -56,6 +56,13 @@ inconvenience this causes.
 <h3>Specific improvements</h3>
 
 <ol>
+  <li>
+  New: Patterns::List now accepts a string that denotes the separator
+  between entries of the list.
+  <br>
+  (Wolfgang Bangerth, 2013/08/21)
+  </li>
+
   <li>
   Fixed: Some operations in the MappingQ class are now done in higher
   precision arithmetic to mitigate the ill-conditioning that appears
index f6bfc184de8ec1befeb10d0a42438fd1f6612ebf..0443f57a478f45b84ee5fceaf1a74d919230e484 100644 (file)
@@ -369,7 +369,8 @@ namespace Patterns
 
 
   /**
-   * This pattern matches a list of comma-separated values each of which
+   * This pattern matches a list of values separated by commas (or another
+   * string), each of which
    * have to match a pattern given to the constructor. With two additional
    * parameters, the number of elements this list has to have can be
    * specified. If none is specified, the list may have zero or more
@@ -389,12 +390,14 @@ namespace Patterns
      * Constructor. Take the given parameter as the specification of valid
      * elements of the list.
      *
-     * The two other arguments can be used to denote minimal and maximal
-     * allowable lengths of the list.
+     * The three other arguments can be used to denote minimal and maximal
+     * allowable lengths of the list, and the string that is used as a
+     * separator between elements of the list.
      */
     List (const PatternBase  &base_pattern,
           const unsigned int  min_elements = 0,
-          const unsigned int  max_elements = max_int_value);
+          const unsigned int  max_elements = max_int_value,
+          const std::string  &separator = ",");
 
     /**
      * Destructor.
@@ -461,6 +464,11 @@ namespace Patterns
      */
     const unsigned int max_elements;
 
+    /**
+     * Separator between elements of the list.
+     */
+    const std::string separator;
+
     /**
      * Initial part of description
      */
index 6a8c426d4e86bbdbebcd228a45499f102bd5efcb..c5cb9c1c67528149dca175e7be9e2f6e143789c2 100644 (file)
@@ -433,14 +433,18 @@ namespace Patterns
 
   List::List (const PatternBase  &p,
               const unsigned int  min_elements,
-              const unsigned int  max_elements)
+              const unsigned int  max_elements,
+              const std::string  &separator)
     :
     pattern (p.clone()),
     min_elements (min_elements),
-    max_elements (max_elements)
+    max_elements (max_elements),
+    separator (separator)
   {
     Assert (min_elements <= max_elements,
             ExcInvalidRange (min_elements, max_elements));
+    Assert (separator.size() > 0,
+            ExcMessage ("The separator must have a non-zero length."));
   }
 
 
@@ -457,7 +461,6 @@ namespace Patterns
   {
     std::string tmp = test_string_list;
     std::vector<std::string> split_list;
-    split_list.reserve (std::count (tmp.begin(), tmp.end(), ',')+1);
 
     // first split the input list
     while (tmp.length() != 0)
@@ -465,10 +468,10 @@ namespace Patterns
         std::string name;
         name = tmp;
 
-        if (name.find(",") != std::string::npos)
+        if (name.find(separator) != std::string::npos)
           {
-            name.erase (name.find(","), std::string::npos);
-            tmp.erase (0, tmp.find(",")+1);
+            name.erase (name.find(separator), std::string::npos);
+            tmp.erase (0, tmp.find(separator)+separator.size());
           }
         else
           tmp = "";
@@ -481,7 +484,7 @@ namespace Patterns
           name.erase (name.length()-1, 1);
 
         split_list.push_back (name);
-      };
+      }
 
     if ((split_list.size() < min_elements) ||
         (split_list.size() > max_elements))
@@ -506,8 +509,10 @@ namespace Patterns
     description << description_init
                 << " list of <" << pattern->description() << ">"
                 << " of length " << min_elements << "..." << max_elements
-                << " (inclusive)"
-                << "]";
+                << " (inclusive)";
+    if (separator != ",")
+      description << " separated by <" << separator << ">";
+    description << "]";
 
     return description.str();
   }
@@ -517,7 +522,7 @@ namespace Patterns
   PatternBase *
   List::clone () const
   {
-    return new List(*pattern, min_elements, max_elements);
+    return new List(*pattern, min_elements, max_elements, separator);
   }
 
 
@@ -525,7 +530,8 @@ namespace Patterns
   List::memory_consumption () const
   {
     return (sizeof(*this) +
-            MemoryConsumption::memory_consumption(*pattern));
+            MemoryConsumption::memory_consumption(*pattern) +
+            MemoryConsumption::memory_consumption(separator));
   }
 
 
@@ -552,7 +558,12 @@ namespace Patterns
         if (!(is >> max_elements))
           return new List(*base_pattern, min_elements);
 
-        return new List(*base_pattern, min_elements, max_elements);
+        is.ignore(strlen(" separated by <"));
+        std::string separator;
+        if (!is)
+          std::getline(is, separator, '>');
+
+        return new List(*base_pattern, min_elements, max_elements, separator);
       }
     else
       return 0;
diff --git a/tests/bits/parameter_handler_1a.cc b/tests/bits/parameter_handler_1a.cc
new file mode 100644 (file)
index 0000000..6c94b87
--- /dev/null
@@ -0,0 +1,49 @@
+// ---------------------------------------------------------------------
+// $Id$
+//
+// Copyright (C) 2002 - 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.
+//
+// ---------------------------------------------------------------------
+
+
+
+// check the Patterns::List pattern
+
+#include "../tests.h"
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/parameter_handler.h>
+#include <fstream>
+
+void check (const char *p)
+{
+  ParameterHandler prm;
+  prm.declare_entry ("test_1", "-1,0",
+                     Patterns::List(Patterns::Integer(-1,1),2,3,"xyz"));
+
+  std::ifstream in(p);
+  prm.read_input (in);
+
+  deallog << "test_1=" << prm.get ("test_1") << std::endl;
+}
+
+
+int main ()
+{
+  std::ofstream logfile("parameter_handler_1a/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  check ("parameter_handler_1a/prm");
+
+  return 0;
+}
diff --git a/tests/bits/parameter_handler_1a/cmp/generic b/tests/bits/parameter_handler_1a/cmp/generic
new file mode 100644 (file)
index 0000000..4dd3ceb
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::test_1=-1,0
diff --git a/tests/bits/parameter_handler_1a/prm b/tests/bits/parameter_handler_1a/prm
new file mode 100644 (file)
index 0000000..8927009
--- /dev/null
@@ -0,0 +1 @@
+set test_1 = 1,0,1xyz1,0,-1

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.