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

index 529e7e523f08ffdceb07b904b1f729821168eb79..5a70443040823ca53c348f351af459123980e5c6 100644 (file)
@@ -57,8 +57,9 @@ inconvenience this causes.
 
 <ol>
   <li>
-  New: Patterns::List now accepts a string that denotes the separator
-  between entries of the list.
+  New: Patterns::List and Patterns::Map now accept a string
+  different than the default comma that denotes the separator
+  between entries of the list or map.
   <br>
   (Wolfgang Bangerth, 2013/08/21)
   </li>
index 0443f57a478f45b84ee5fceaf1a74d919230e484..92868e2f37a11727001808a9434fc9d9536bdb72 100644 (file)
@@ -482,7 +482,8 @@ namespace Patterns
    * pattern given to the constructor. For each entry of the map,
    * parameters have to be entered in the form <code>key: value</code>. In
    * other words, a map is described in the form
-   * <code>key1: value1, key2: value2, key3: value3, ...</code>.
+   * <code>key1: value1, key2: value2, key3: value3, ...</code>. A constructor
+   * argument allows to choose a delimiter between pairs other than the comma.
    *
    * With two additional parameters, the number of elements this list has
    * to have can be specified. If none is specified, the map may have zero
@@ -502,13 +503,15 @@ 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 as well as the separator used to delimit
+     * pairs of the map.
      */
     Map (const PatternBase  &key_pattern,
          const PatternBase  &value_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.
@@ -576,6 +579,11 @@ namespace Patterns
      */
     const unsigned int max_elements;
 
+    /**
+     * Separator between elements of the list.
+     */
+    const std::string separator;
+
     /**
      * Initial part of description
      */
index c5cb9c1c67528149dca175e7be9e2f6e143789c2..e59d518c0afb04ff1dd8f1ad7e08129904d18e6e 100644 (file)
@@ -562,6 +562,8 @@ namespace Patterns
         std::string separator;
         if (!is)
           std::getline(is, separator, '>');
+        else
+          separator = ",";
 
         return new List(*base_pattern, min_elements, max_elements, separator);
       }
@@ -580,15 +582,22 @@ namespace Patterns
   Map::Map (const PatternBase  &p_key,
             const PatternBase  &p_value,
             const unsigned int  min_elements,
-            const unsigned int  max_elements)
+            const unsigned int  max_elements,
+            const std::string  &separator)
     :
     key_pattern (p_key.clone()),
     value_pattern (p_value.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."));
+    Assert (separator != ":",
+            ExcMessage ("The separator can not be a colon ':' sicne that "
+                        "is the separator between the two elements of <key:value> pairs"));
   }
 
 
@@ -608,7 +617,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 at comma sites
     while (tmp.length() != 0)
@@ -616,10 +624,10 @@ namespace Patterns
         std::string map_entry;
         map_entry = tmp;
 
-        if (map_entry.find(",") != std::string::npos)
+        if (map_entry.find(separator) != std::string::npos)
           {
-            map_entry.erase (map_entry.find(","), std::string::npos);
-            tmp.erase (0, tmp.find(",")+1);
+            map_entry.erase (map_entry.find(separator), std::string::npos);
+            tmp.erase (0, tmp.find(separator)+separator.size());
           }
         else
           tmp = "";
@@ -632,7 +640,7 @@ namespace Patterns
           map_entry.erase (map_entry.length()-1, 1);
 
         split_list.push_back (map_entry);
-      };
+      }
 
     if ((split_list.size() < min_elements) ||
         (split_list.size() > max_elements))
@@ -680,8 +688,10 @@ namespace Patterns
                 << key_pattern->description() << ":"
                 << value_pattern->description() << ">"
                 << " of length " << min_elements << "..." << max_elements
-                << " (inclusive)"
-                << "]";
+                << " (inclusive)";
+    if (separator != ",")
+      description << " separated by <" << separator << ">";
+    description << "]";
 
     return description.str();
   }
@@ -691,7 +701,9 @@ namespace Patterns
   PatternBase *
   Map::clone () const
   {
-    return new Map(*key_pattern, *value_pattern, min_elements, max_elements);
+    return new Map(*key_pattern, *value_pattern,
+                   min_elements, max_elements,
+                   separator);
   }
 
 
@@ -700,7 +712,8 @@ namespace Patterns
   {
     return (sizeof(*this) +
             MemoryConsumption::memory_consumption (*key_pattern) +
-            MemoryConsumption::memory_consumption (*value_pattern));
+            MemoryConsumption::memory_consumption (*value_pattern) +
+            MemoryConsumption::memory_consumption (separator));
   }
 
 
@@ -735,7 +748,16 @@ namespace Patterns
         if (!(is >> max_elements))
           return new Map(*key_pattern, *value_pattern, min_elements);
 
-        return new Map(*key_pattern, *value_pattern, min_elements, max_elements);
+        is.ignore(strlen(" separated by <"));
+        std::string separator;
+        if (!is)
+          std::getline(is, separator, '>');
+        else
+          separator = ",";
+
+        return new Map(*key_pattern, *value_pattern,
+                       min_elements, max_elements,
+                       separator);
       }
     else
       return 0;
diff --git a/tests/bits/parameter_handler_13a.cc b/tests/bits/parameter_handler_13a.cc
new file mode 100644 (file)
index 0000000..8ade8c2
--- /dev/null
@@ -0,0 +1,51 @@
+// ---------------------------------------------------------------------
+// $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::Map pattern with a separator other than the default ','
+
+#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_13", "-1:a, 0:b, 1:c",
+                     Patterns::Map(Patterns::Integer(-1,1),
+                                   Patterns::Selection("a|b|c"),
+                                   2,3, "xyz"));
+
+  std::ifstream in(p);
+  prm.read_input (in);
+
+  deallog << "test_13=" << prm.get ("test_13") << std::endl;
+}
+
+
+int main ()
+{
+  std::ofstream logfile("parameter_handler_13a/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  check ("parameter_handler_13a/prm");
+
+  return 0;
+}
diff --git a/tests/bits/parameter_handler_13a/cmp/generic b/tests/bits/parameter_handler_13a/cmp/generic
new file mode 100644 (file)
index 0000000..dca4df2
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::test_13=1 : b xyz 0: a xyz 1 : c
diff --git a/tests/bits/parameter_handler_13a/prm b/tests/bits/parameter_handler_13a/prm
new file mode 100644 (file)
index 0000000..f2af6d7
--- /dev/null
@@ -0,0 +1 @@
+set test_13 = 1 : b xyz 0: a xyz 1 : c
index 6c94b872d3cf98ee07cda2669a3fe57b73a6201f..b26f8bcec192cb2907ee94559bac28d332b3e5c8 100644 (file)
@@ -16,7 +16,8 @@
 
 
 
-// check the Patterns::List pattern
+// check the Patterns::List pattern with a separator different from the
+// default ','
 
 #include "../tests.h"
 #include <deal.II/base/logstream.h>

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.