From cb72c13ec7a7764a4539d80a92193423636bae0b Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Wed, 20 Apr 2016 14:59:14 +0200 Subject: [PATCH] add an extra string parameter to terminate the parsing of a file by ParameterHandler --- doc/news/changes.h | 8 ++++++++ include/deal.II/base/parameter_handler.h | 21 ++++++++++++++++++--- source/base/parameter_handler.cc | 19 ++++++++++++++----- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/doc/news/changes.h b/doc/news/changes.h index 17fca73239..6015244161 100644 --- a/doc/news/changes.h +++ b/doc/news/changes.h @@ -201,6 +201,14 @@ inconvenience this causes.

Specific improvements

    +
  1. New: Added an optional string parameter to the ParameterHandler::read_input () + and ParameterHandler::read_input_from_string() functions. + When a line which equals this string is encountered, the parsing of parameters + is terminated. +
    + (Denis Davydov, 2016/04/20) +
  2. +
  3. New: Added move operations to IndexSet.
    diff --git a/include/deal.II/base/parameter_handler.h b/include/deal.II/base/parameter_handler.h index b7be6c18c1..fb1aa1735c 100644 --- a/include/deal.II/base/parameter_handler.h +++ b/include/deal.II/base/parameter_handler.h @@ -1576,10 +1576,15 @@ public: * the file (if that's what the input stream represents) we are reading * from; this is only used when creating output for error messages. * + * If non-empty @p last_line is provided, the ParameterHandler object + * will stop parsing lines after encountering @p last_line . + * This is handy when adding extra data that shall be parsed manually. + * * Return whether the read was successful. */ virtual bool read_input (std::istream &input, - const std::string &filename = "input file"); + const std::string &filename = "input file", + const std::string &last_line = ""); /** * Read input from a file the name of which is given. The PathSearch class @@ -1591,18 +1596,28 @@ public: * automatically generate the requested file with default values if the file * did not exist. This file will not contain additional comments if * write_stripped_file is true. + * + * If non-empty @p last_line is provided, the ParameterHandler object + * will stop parsing lines after encountering @p last_line . + * This is handy when adding extra data that shall be parsed manually. */ virtual bool read_input (const std::string &filename, const bool optional = false, - const bool write_stripped_file = false); + const bool write_stripped_file = false, + const std::string &last_line = ""); /** * Read input from a string in memory. The lines in memory have to be * separated by @\n characters. * + * If non-empty @p last_line is provided, the ParameterHandler object + * will stop parsing lines after encountering @p last_line . + * This is handy when adding extra data that shall be parsed manually. + * * Return whether the read was successful. */ - virtual bool read_input_from_string (const char *s); + virtual bool read_input_from_string (const char *s, + const std::string &last_line = ""); /** * Read a parameter file in XML format. This could be from a file originally diff --git a/source/base/parameter_handler.cc b/source/base/parameter_handler.cc index dab4492046..7aefd994c3 100644 --- a/source/base/parameter_handler.cc +++ b/source/base/parameter_handler.cc @@ -1332,7 +1332,8 @@ ParameterHandler::get_current_full_path (const std::string &name) const bool ParameterHandler::read_input (std::istream &input, - const std::string &filename) + const std::string &filename, + const std::string &last_line) { AssertThrow (input, ExcIO()); @@ -1352,6 +1353,12 @@ bool ParameterHandler::read_input (std::istream &input, // scan_line. This makes the continuation line logic a lot simpler. input_line = Utilities::trim (input_line); + // If we see the line which is the same as @p last_line , + // terminate the parsing. + if (last_line.length() != 0 && + input_line == last_line) + break; + // Check whether or not the current line should be joined with the next // line before calling scan_line. if (input_line.length() != 0 && @@ -1418,7 +1425,8 @@ bool ParameterHandler::read_input (std::istream &input, bool ParameterHandler::read_input (const std::string &filename, const bool optional, - const bool write_compact) + const bool write_compact, + const std::string &last_line) { PathSearch search("PARAMETERS"); @@ -1428,7 +1436,7 @@ bool ParameterHandler::read_input (const std::string &filename, std::ifstream file_stream (openname.c_str()); AssertThrow(file_stream, ExcIO()); - return read_input (file_stream, filename); + return read_input (file_stream, filename, last_line); } catch (const PathSearch::ExcFileNotFound &) { @@ -1448,10 +1456,11 @@ bool ParameterHandler::read_input (const std::string &filename, -bool ParameterHandler::read_input_from_string (const char *s) +bool ParameterHandler::read_input_from_string (const char *s, + const std::string &last_line) { std::istringstream input_stream (s); - return read_input (input_stream, "input string"); + return read_input (input_stream, "input string", last_line); } -- 2.39.5