const std::string & current_path,
const char path_separator,
const std::vector<std::unique_ptr<const Patterns::PatternBase>> &patterns,
- ParameterHandler & prm)
+ const bool skip_undefined,
+ ParameterHandler &prm)
{
for (const auto &p : source)
{
// a sub-tree must either be a parameter node or a subsection
- if (p.second.get_optional<std::string>("value"))
+ if (p.second.empty())
{
// set the found parameter in the destination argument
- prm.set(demangle(p.first), p.second.get<std::string>("value"));
+ if (skip_undefined)
+ {
+ try
+ {
+ prm.set(demangle(p.first), p.second.data());
+ }
+ catch (const ParameterHandler::ExcEntryUndeclared &)
+ {
+ // ignore undeclared entry assert
+ }
+ }
+ else
+ prm.set(demangle(p.first), p.second.data());
+ }
+ else if (p.second.get_optional<std::string>("value"))
+ {
+ // set the found parameter in the destination argument
+ if (skip_undefined)
+ {
+ try
+ {
+ prm.set(demangle(p.first),
+ p.second.get<std::string>("value"));
+ }
+ catch (const ParameterHandler::ExcEntryUndeclared &)
+ {
+ // ignore undeclared entry assert
+ }
+ }
+ else
+ prm.set(demangle(p.first), p.second.get<std::string>("value"));
// this node might have sub-nodes in addition to "value", such as
// "default_value", "documentation", etc. we might at some point
current_path + path_separator + p.first),
path_separator,
patterns,
+ skip_undefined,
prm);
prm.leave_subsection();
}
void
-ParameterHandler::parse_input_from_xml(std::istream &in)
+ParameterHandler::parse_input_from_xml(std::istream &in,
+ const bool skip_undefined)
{
AssertThrow(in, ExcIO());
// read the XML tree assuming that (as we
const boost::property_tree::ptree &my_entries =
single_node_tree.get_child("ParameterHandler");
- read_xml_recursively(my_entries, "", path_separator, patterns, *this);
+ read_xml_recursively(
+ my_entries, "", path_separator, patterns, skip_undefined, *this);
}
void
-ParameterHandler::parse_input_from_json(std::istream &in)
+ParameterHandler::parse_input_from_json(std::istream &in,
+ const bool skip_undefined)
{
AssertThrow(in, ExcIO());
// The xml function is reused to read in the xml into the parameter file.
// This means that only mangled files can be read.
- read_xml_recursively(node_tree, "", path_separator, patterns, *this);
+ read_xml_recursively(
+ node_tree, "", path_separator, patterns, skip_undefined, *this);
}
--- /dev/null
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2020 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.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+
+
+// check ParameterHandler::parse_input_from_xml
+
+#include <deal.II/base/parameter_handler.h>
+
+#include "../tests.h"
+
+
+int
+main()
+{
+ initlog();
+
+ // default values
+ int int1 = 0;
+ int int2 = 0;
+ int int3 = 0;
+ int int4 = 0;
+
+ ParameterHandler prm;
+ prm.add_parameter("int1", int1);
+ prm.add_parameter("int2", int2);
+
+ prm.enter_subsection("dummy");
+ prm.add_parameter("int3", int3);
+ prm.add_parameter("int4", int4);
+ prm.leave_subsection();
+
+ // read from json
+ std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json_02.prm");
+ prm.parse_input_from_json(in, true);
+
+ AssertDimension(int1, 1);
+ AssertDimension(int2, 2);
+ AssertDimension(int3, 3);
+ AssertDimension(int4, 4);
+
+ return 0;
+}