From d4032d4d67f613437c1462f1e0dcf087d5c62844 Mon Sep 17 00:00:00 2001 From: bangerth Date: Sun, 25 Aug 2013 05:53:26 +0000 Subject: [PATCH] Allow including one input file from another. git-svn-id: https://svn.dealii.org/trunk@30477 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/doc/news/changes.h | 7 ++++ .../include/deal.II/base/parameter_handler.h | 18 ++++++++-- deal.II/source/base/parameter_handler.cc | 36 +++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index 9dba3186c6..e4ff71cee2 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -68,6 +68,13 @@ inconvenience this causes.

Specific improvements

    +
  1. + Fixed: The ParameterHandler class can now deal with including one parameter + file from another. +
    + (Wolfgang Bangerth, 2013/08/25) +
  2. +
  3. New: The method VectorTools::compute_normal_flux_constraints can be used to force a vector finite element function to be normal to the boundary. diff --git a/deal.II/include/deal.II/base/parameter_handler.h b/deal.II/include/deal.II/base/parameter_handler.h index e14cc51e98..70712ea751 100644 --- a/deal.II/include/deal.II/base/parameter_handler.h +++ b/deal.II/include/deal.II/base/parameter_handler.h @@ -889,7 +889,7 @@ namespace Patterns * set Geometry = [0,1]x[0,3] * @endcode * Input may be sorted into subsection trees in order to give the input a - * logical structure. + * logical structure, and input files may include other files. * * The ParameterHandler class is discussed in detail in the @ref step_19 * "step-19" example program, and is used in more realistic situations in @@ -1034,9 +1034,23 @@ namespace Patterns * = sign. * * + *

    Including other input files

    + * + * An input file can include other include files using the syntax + * @code + * ... + * include some_other_file.prm + * ... + * @endcode + * The file so referenced is searched for relative to the current + * directory (not relative to the directory in which the including + * parameter file is located, since this is not known to all three + * versions of the read_input() function). + * + * *

    Reading data from input sources

    * - * In order to read input you can use three possibilities: reading from + * In order to read input there are three possibilities: reading from * an std::istream object, reading from a file of which the name * is given and reading from a string in memory in which the lines are * separated by @\n characters. These possibilities are used as diff --git a/deal.II/source/base/parameter_handler.cc b/deal.II/source/base/parameter_handler.cc index e91986060e..a9edbef5e5 100644 --- a/deal.II/source/base/parameter_handler.cc +++ b/deal.II/source/base/parameter_handler.cc @@ -2393,6 +2393,42 @@ ParameterHandler::scan_line (std::string line, } } + // an include statement? + if ((line.find ("INCLUDE ") == 0) || + (line.find ("include ") == 0)) + { + // erase "set" statement and eliminate + // spaces around the '=' + line.erase (0, 7); + while ((line.size() > 0) && (line[0] == ' ')) + line.erase (0, 1); + + // the remainder must then be a filename + if (line.size() == 0) + { + std::cerr << "Line <" << lineno + << "> of file <" << input_filename + << "> is an include statement but does not name a file!" + << std::endl; + + return false; + } + + std::ifstream input (line); + if (!input) + { + std::cerr << "Line <" << lineno + << "> of file <" << input_filename + << "> is an include statement but the file <" + << line << "> could not be opened!" + << std::endl; + + return false; + } + else + return read_input (input); + } + // this line matched nothing known std::cerr << "Line <" << lineno << "> of file <" << input_filename -- 2.39.5