From 51135ac6048bc2d256b89225cbf739a7ea832efd Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Sun, 23 Sep 2018 09:26:12 +0200 Subject: [PATCH] switch to an additional parameter in parse_input() --- include/deal.II/base/parameter_handler.h | 52 ++++++++++++------- source/base/parameter_handler.cc | 29 ++++++----- .../parameter_handler/parameter_handler_24.cc | 4 +- 3 files changed, 52 insertions(+), 33 deletions(-) diff --git a/include/deal.II/base/parameter_handler.h b/include/deal.II/base/parameter_handler.h index f6ca4506ce..6d088bd13c 100644 --- a/include/deal.II/base/parameter_handler.h +++ b/include/deal.II/base/parameter_handler.h @@ -902,14 +902,8 @@ public: /** * Constructor. - * - * If @p skip_undefined is true, the parameter handler - * will skip undefined sections and entries. This is useful for partially - * parsing a parameter file, for example to obtain only the spatial dimension - * of the problem. By default all entries and subsections are expected to be - * declared. */ - ParameterHandler(const bool skip_undefined = false); + ParameterHandler(); /** * Destructor. Declare this only to have a virtual destructor, which is @@ -929,6 +923,12 @@ public: * will stop parsing lines after encountering @p last_line . * This is handy when adding extra data that shall be parsed manually. * + * If @p skip_undefined is true, the parameter handler + * will skip undefined sections and entries. This is useful for partially + * parsing a parameter file, for example to obtain only the spatial dimension + * of the problem. By default all entries and subsections are expected to be + * declared. + * * The function sets the value of all parameters it encounters in the * input file to the provided value. Parameters not explicitly listed * in the input file are left at the value they previously held, which @@ -948,8 +948,9 @@ public: */ virtual void parse_input(std::istream & input, - const std::string &filename = "input file", - const std::string &last_line = ""); + const std::string &filename = "input file", + const std::string &last_line = "", + const bool skip_undefined = false); /** * Parse the given file to provide values for known parameter fields. The @@ -995,7 +996,9 @@ public: * @endcode */ virtual void - parse_input(const std::string &filename, const std::string &last_line = ""); + parse_input(const std::string &filename, + const std::string &last_line = "", + const bool skip_undefined = false); /** * Parse input from a string to populate known parameter fields. The lines @@ -1006,7 +1009,9 @@ public: * there for more information. */ virtual void - parse_input_from_string(const char *s, const std::string &last_line = ""); + parse_input_from_string(const char * s, + const std::string &last_line = "", + const bool skip_undefined = false); /** * Parse input from an XML stream to populate known parameter fields. This @@ -1605,11 +1610,6 @@ private: */ static const char path_separator = '.'; - /** - * A flag to skip undefined entries. - */ - const bool skip_undefined; - /** * Path of presently selected subsections; empty list means top level */ @@ -1686,11 +1686,18 @@ private: * * The function modifies its argument, but also takes it by value, so the * caller's variable is not changed. + * + * If @p skip_undefined is true, the parser + * will skip undefined sections and entries. This is useful for partially + * parsing a parameter file, for example to obtain only the spatial dimension + * of the problem. By default all entries and subsections are expected to be + * declared. */ void scan_line(std::string line, const std::string &input_filename, - const unsigned int current_line_n); + const unsigned int current_line_n, + const bool skip_undefined); /** * Print out the parameters of the subsection given by the @@ -1982,6 +1989,12 @@ public: * will stop parsing lines after encountering @p last_line . * This is handy when adding extra data that shall be parsed manually. * + * If @p skip_undefined is true, the parameter handler + * will skip undefined sections and entries. This is useful for partially + * parsing a parameter file, for example to obtain only the spatial dimension + * of the problem. By default all entries and subsections are expected to be + * declared. + * * @note This is the only overload of the three parse_input * functions implemented by ParameterHandler overridden with new behavior by * this class. This is because the other two parse_input functions @@ -1989,8 +2002,9 @@ public: */ virtual void parse_input(std::istream & input, - const std::string &filename = "input file", - const std::string &last_line = "") override; + const std::string &filename = "input file", + const std::string &last_line = "", + const bool skip_undefined = false) override; /** * Overriding virtual functions which are overloaded (like diff --git a/source/base/parameter_handler.cc b/source/base/parameter_handler.cc index 9c25aabeff..8dd7878596 100644 --- a/source/base/parameter_handler.cc +++ b/source/base/parameter_handler.cc @@ -39,9 +39,8 @@ DEAL_II_NAMESPACE_OPEN -ParameterHandler::ParameterHandler(const bool skip_undefined) - : skip_undefined(skip_undefined) - , entries(new boost::property_tree::ptree()) +ParameterHandler::ParameterHandler() + : entries(new boost::property_tree::ptree()) {} @@ -317,7 +316,8 @@ ParameterHandler::get_current_full_path( void ParameterHandler::parse_input(std::istream & input, const std::string &filename, - const std::string &last_line) + const std::string &last_line, + const bool skip_undefined) { AssertThrow(input, ExcIO()); @@ -346,12 +346,13 @@ ParameterHandler::parse_input(std::istream & input, // // after unwinding the subsection stack, just re-throw the exception auto scan_line_or_cleanup = [this, + &skip_undefined, &saved_path](const std::string &line, const std::string &filename, const unsigned int line_number) { try { - scan_line(line, filename, line_number); + scan_line(line, filename, line_number, skip_undefined); } catch (...) { @@ -447,23 +448,25 @@ ParameterHandler::parse_input(std::istream & input, void ParameterHandler::parse_input(const std::string &filename, - const std::string &last_line) + const std::string &last_line, + const bool skip_undefined) { PathSearch search("PARAMETERS"); std::string openname = search.find(filename); std::ifstream file_stream(openname.c_str()); - parse_input(file_stream, filename, last_line); + parse_input(file_stream, filename, last_line, skip_undefined); } void ParameterHandler::parse_input_from_string(const char * s, - const std::string &last_line) + const std::string &last_line, + const bool skip_undefined) { std::istringstream input_stream(s); - parse_input(input_stream, "input string", last_line); + parse_input(input_stream, "input string", last_line, skip_undefined); } @@ -2079,7 +2082,8 @@ ParameterHandler::log_parameters_section(LogStream &out) void ParameterHandler::scan_line(std::string line, const std::string &input_filename, - const unsigned int current_line_n) + const unsigned int current_line_n, + const bool skip_undefined) { // save a copy for some error messages const std::string original_line = line; @@ -2313,14 +2317,15 @@ MultipleParameterLoop::MultipleParameterLoop() void MultipleParameterLoop::parse_input(std::istream & input, const std::string &filename, - const std::string &last_line) + const std::string &last_line, + const bool skip_undefined) { AssertThrow(input, ExcIO()); // Note that (to avoid infinite recursion) we have to explicitly call the // base class version of parse_input and *not* a wrapper (which may be // virtual and lead us back here) - ParameterHandler::parse_input(input, filename, last_line); + ParameterHandler::parse_input(input, filename, last_line, skip_undefined); init_branches(); } diff --git a/tests/parameter_handler/parameter_handler_24.cc b/tests/parameter_handler/parameter_handler_24.cc index f81f144afb..80abdcfdb8 100644 --- a/tests/parameter_handler/parameter_handler_24.cc +++ b/tests/parameter_handler/parameter_handler_24.cc @@ -24,7 +24,7 @@ void check() { - ParameterHandler prm(true); + ParameterHandler prm; prm.enter_subsection("Geometry"); prm.declare_entry("dim", "1", Patterns::Integer(1, 3)); prm.leave_subsection(); @@ -32,7 +32,7 @@ check() prm.declare_entry("Dimension", "1", Patterns::Integer(1, 3)); std::ifstream in(SOURCE_DIR "/parameter_handler_24.prm"); - prm.parse_input(in, "input file"); + prm.parse_input(in, "input file", "", true); prm.print_parameters(deallog.get_file_stream(), ParameterHandler::Text); } -- 2.39.5