From: Wolfgang Bangerth Date: Thu, 22 Aug 2013 03:12:47 +0000 (+0000) Subject: Make the separator in Patterns::List user defined. X-Git-Tag: v8.1.0~1012 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a700ce06069b81cd89e263305146118cf32859b1;p=dealii.git Make the separator in Patterns::List user defined. git-svn-id: https://svn.dealii.org/trunk@30404 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index c3781bca61..529e7e523f 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -56,6 +56,13 @@ inconvenience this causes.

Specific improvements

    +
  1. + New: Patterns::List now accepts a string that denotes the separator + between entries of the list. +
    + (Wolfgang Bangerth, 2013/08/21) +
  2. +
  3. Fixed: Some operations in the MappingQ class are now done in higher precision arithmetic to mitigate the ill-conditioning that appears diff --git a/deal.II/include/deal.II/base/parameter_handler.h b/deal.II/include/deal.II/base/parameter_handler.h index f6bfc184de..0443f57a47 100644 --- a/deal.II/include/deal.II/base/parameter_handler.h +++ b/deal.II/include/deal.II/base/parameter_handler.h @@ -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 */ diff --git a/deal.II/source/base/parameter_handler.cc b/deal.II/source/base/parameter_handler.cc index 6a8c426d4e..c5cb9c1c67 100644 --- a/deal.II/source/base/parameter_handler.cc +++ b/deal.II/source/base/parameter_handler.cc @@ -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 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 index 0000000000..6c94b872d3 --- /dev/null +++ b/tests/bits/parameter_handler_1a.cc @@ -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 +#include +#include + +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 index 0000000000..4dd3ceb053 --- /dev/null +++ b/tests/bits/parameter_handler_1a/cmp/generic @@ -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 index 0000000000..8927009d62 --- /dev/null +++ b/tests/bits/parameter_handler_1a/prm @@ -0,0 +1 @@ +set test_1 = 1,0,1xyz1,0,-1