From 23f306dfc5d68801cd56d407287ef3cb5a1345a9 Mon Sep 17 00:00:00 2001 From: alberto sartori Date: Thu, 3 Sep 2015 13:09:21 +0200 Subject: [PATCH] allows continuation lines in ParameterHandler --- doc/news/changes.h | 4 ++ include/deal.II/base/parameter_handler.h | 3 ++ source/base/parameter_handler.cc | 50 +++++++++++++++-- tests/bits/parameter_handler_backslash_01.cc | 47 ++++++++++++++++ .../parameter_handler_backslash_01.output | 2 + tests/bits/parameter_handler_backslash_02.cc | 47 ++++++++++++++++ .../parameter_handler_backslash_02.output | 2 + tests/bits/parameter_handler_backslash_03.cc | 47 ++++++++++++++++ ...er_handler_backslash_03.expect=diff.output | 2 + tests/bits/parameter_handler_backslash_04.cc | 47 ++++++++++++++++ ...er_handler_backslash_04.expect=diff.output | 2 + tests/bits/parameter_handler_backslash_05.cc | 53 +++++++++++++++++++ ...er_handler_backslash_05.expect=diff.output | 3 ++ .../prm/parameter_handler_backslash_01.prm | 7 +++ .../prm/parameter_handler_backslash_02.prm | 9 ++++ .../prm/parameter_handler_backslash_03.prm | 7 +++ .../prm/parameter_handler_backslash_04.prm | 6 +++ .../prm/parameter_handler_backslash_05.prm | 18 +++++++ 18 files changed, 352 insertions(+), 4 deletions(-) create mode 100644 tests/bits/parameter_handler_backslash_01.cc create mode 100644 tests/bits/parameter_handler_backslash_01.output create mode 100644 tests/bits/parameter_handler_backslash_02.cc create mode 100644 tests/bits/parameter_handler_backslash_02.output create mode 100644 tests/bits/parameter_handler_backslash_03.cc create mode 100644 tests/bits/parameter_handler_backslash_03.expect=diff.output create mode 100644 tests/bits/parameter_handler_backslash_04.cc create mode 100644 tests/bits/parameter_handler_backslash_04.expect=diff.output create mode 100644 tests/bits/parameter_handler_backslash_05.cc create mode 100644 tests/bits/parameter_handler_backslash_05.expect=diff.output create mode 100644 tests/bits/prm/parameter_handler_backslash_01.prm create mode 100644 tests/bits/prm/parameter_handler_backslash_02.prm create mode 100644 tests/bits/prm/parameter_handler_backslash_03.prm create mode 100644 tests/bits/prm/parameter_handler_backslash_04.prm create mode 100644 tests/bits/prm/parameter_handler_backslash_05.prm diff --git a/doc/news/changes.h b/doc/news/changes.h index 2ecb7ce9ae..ea479bf185 100644 --- a/doc/news/changes.h +++ b/doc/news/changes.h @@ -114,6 +114,10 @@ inconvenience this causes.
    +
  1. Improved: Allow continuation lines in ParameterHandler. +
    + (Alberto Sartori, 2015/09/04) +
  2. Fixed: VectorTools::integrate_difference for VectorTools::Hdiv_seminorm was computed incorrectly.
    diff --git a/include/deal.II/base/parameter_handler.h b/include/deal.II/base/parameter_handler.h index fd687a6e8a..6042d7b3b4 100644 --- a/include/deal.II/base/parameter_handler.h +++ b/include/deal.II/base/parameter_handler.h @@ -1025,6 +1025,9 @@ namespace Patterns * * Comments starting with \# are skipped. * + * Continuation lines are allowed by means of the character \\, + * which must be the last character of the line. + * * We propose to use the following scheme to name entries: start the first * word with a capital letter and use lowercase letters further on. The same * applies to the possible entry values to the right of the = sign. diff --git a/source/base/parameter_handler.cc b/source/base/parameter_handler.cc index d410fdabf1..5ee5b461a0 100644 --- a/source/base/parameter_handler.cc +++ b/source/base/parameter_handler.cc @@ -1382,10 +1382,19 @@ bool ParameterHandler::read_input (const std::string &filename, try { std::string openname = search.find(filename); - std::ifstream input (openname.c_str()); - AssertThrow(input, ExcIO()); + std::ifstream file (openname.c_str()); + AssertThrow(file, ExcIO()); - return read_input (input, filename); + std::string str; + std::string file_contents; + + while (std::getline(file, str)) + { + file_contents += str; + file_contents.push_back('\n'); + } + + return read_input_from_string (file_contents.c_str()); } catch (const PathSearch::ExcFileNotFound &) { @@ -1409,7 +1418,40 @@ bool ParameterHandler::read_input_from_string (const char *s) { // create an istringstream representation and pass it off // to the other functions doing this work - std::istringstream in (s); + + // concatenate the lines ending with "\" + + std::istringstream file(s); + std::string str; + std::string file_contents; + bool is_concatenated(false); + while (std::getline(file, str)) + { + // if this str must be concatenated with the previous one + // we strip the whitespaces + if (is_concatenated) + { + const std::size_t str_begin = str.find_first_not_of(" \t"); + if (str_begin == std::string::npos) + str = ""; // no content + else + str.erase(0,str_begin); // trim whitespaces from left + } + + if ((std::find(str.begin(),str.end(),'\\') != str.end()) && str.find_last_of('\\') == str.length()-1) + { + str.erase(str.length()-1); // remove '\' + file_contents += str; + is_concatenated = true; // next line must be concatenated = true + } + else + { + is_concatenated = false; // next line must be concatenated = false + file_contents += str; + file_contents.push_back('\n'); + } + } + std::istringstream in (file_contents); return read_input (in, "input string"); } diff --git a/tests/bits/parameter_handler_backslash_01.cc b/tests/bits/parameter_handler_backslash_01.cc new file mode 100644 index 0000000000..36154ff84f --- /dev/null +++ b/tests/bits/parameter_handler_backslash_01.cc @@ -0,0 +1,47 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2003 - 2014 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. +// +// --------------------------------------------------------------------- + + +#include "../tests.h" +#include +#include +#include + + +int main () +{ + std::ofstream logfile("output"); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + ParameterHandler prm; + prm.enter_subsection ("Testing"); + prm.declare_entry ("Function", + "a", + Patterns::List(Patterns::Selection("a|b|c|d|e|f|g|h"))); + prm.leave_subsection (); + + prm.read_input(SOURCE_DIR "/prm/parameter_handler_backslash_01.prm"); + + std::string list; + prm.enter_subsection ("Testing"); + list = prm.get ("Function"); + prm.leave_subsection (); + + deallog << list << std::endl; + + return 0; +} diff --git a/tests/bits/parameter_handler_backslash_01.output b/tests/bits/parameter_handler_backslash_01.output new file mode 100644 index 0000000000..9eb091f67d --- /dev/null +++ b/tests/bits/parameter_handler_backslash_01.output @@ -0,0 +1,2 @@ + +DEAL::a, b, c diff --git a/tests/bits/parameter_handler_backslash_02.cc b/tests/bits/parameter_handler_backslash_02.cc new file mode 100644 index 0000000000..a8556fbfbe --- /dev/null +++ b/tests/bits/parameter_handler_backslash_02.cc @@ -0,0 +1,47 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2003 - 2014 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. +// +// --------------------------------------------------------------------- + + +#include "../tests.h" +#include +#include +#include + + +int main () +{ + std::ofstream logfile("output"); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + ParameterHandler prm; + prm.enter_subsection ("Testing"); + prm.declare_entry ("Function", + "a", + Patterns::List(Patterns::Selection("a|b|c|d|e|f|g|h"))); + prm.leave_subsection (); + + prm.read_input(SOURCE_DIR "/prm/parameter_handler_backslash_02.prm"); + + std::string list; + prm.enter_subsection ("Testing"); + list = prm.get ("Function"); + prm.leave_subsection (); + + deallog << list << std::endl; + + return 0; +} diff --git a/tests/bits/parameter_handler_backslash_02.output b/tests/bits/parameter_handler_backslash_02.output new file mode 100644 index 0000000000..7e7203d2cd --- /dev/null +++ b/tests/bits/parameter_handler_backslash_02.output @@ -0,0 +1,2 @@ + +DEAL::a,b,c diff --git a/tests/bits/parameter_handler_backslash_03.cc b/tests/bits/parameter_handler_backslash_03.cc new file mode 100644 index 0000000000..4d9924e269 --- /dev/null +++ b/tests/bits/parameter_handler_backslash_03.cc @@ -0,0 +1,47 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2003 - 2014 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. +// +// --------------------------------------------------------------------- + + +#include "../tests.h" +#include +#include +#include + + +int main () +{ + std::ofstream logfile("output"); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + ParameterHandler prm; + prm.enter_subsection ("Testing"); + prm.declare_entry ("Function", + "a", + Patterns::List(Patterns::Selection("a|b|c|d|e|f|g|h"))); + prm.leave_subsection (); + + prm.read_input(SOURCE_DIR "/prm/parameter_handler_backslash_03.prm"); + + std::string list; + prm.enter_subsection ("Testing"); + list = prm.get ("Function"); + prm.leave_subsection (); + + deallog << list << std::endl; + + return 0; +} diff --git a/tests/bits/parameter_handler_backslash_03.expect=diff.output b/tests/bits/parameter_handler_backslash_03.expect=diff.output new file mode 100644 index 0000000000..9eb091f67d --- /dev/null +++ b/tests/bits/parameter_handler_backslash_03.expect=diff.output @@ -0,0 +1,2 @@ + +DEAL::a, b, c diff --git a/tests/bits/parameter_handler_backslash_04.cc b/tests/bits/parameter_handler_backslash_04.cc new file mode 100644 index 0000000000..1dcf2236b4 --- /dev/null +++ b/tests/bits/parameter_handler_backslash_04.cc @@ -0,0 +1,47 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2003 - 2014 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. +// +// --------------------------------------------------------------------- + + +#include "../tests.h" +#include +#include +#include + + +int main () +{ + std::ofstream logfile("output"); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + ParameterHandler prm; + prm.enter_subsection ("Testing"); + prm.declare_entry ("Function", + "a", + Patterns::List(Patterns::Selection("a|b|c|d|e|f|g|h"))); + prm.leave_subsection (); + + prm.read_input(SOURCE_DIR "/prm/parameter_handler_backslash_04.prm"); + + std::string list; + prm.enter_subsection ("Testing"); + list = prm.get ("Function"); + prm.leave_subsection (); + + deallog << list << std::endl; + + return 0; +} diff --git a/tests/bits/parameter_handler_backslash_04.expect=diff.output b/tests/bits/parameter_handler_backslash_04.expect=diff.output new file mode 100644 index 0000000000..9eb091f67d --- /dev/null +++ b/tests/bits/parameter_handler_backslash_04.expect=diff.output @@ -0,0 +1,2 @@ + +DEAL::a, b, c diff --git a/tests/bits/parameter_handler_backslash_05.cc b/tests/bits/parameter_handler_backslash_05.cc new file mode 100644 index 0000000000..ae050fdc9f --- /dev/null +++ b/tests/bits/parameter_handler_backslash_05.cc @@ -0,0 +1,53 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2003 - 2014 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. +// +// --------------------------------------------------------------------- + + +#include "../tests.h" +#include +#include +#include + + +int main () +{ + std::ofstream logfile("output"); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + ParameterHandler prm; + prm.enter_subsection ("Testing"); + prm.declare_entry ("Function_1", + "a", + Patterns::List(Patterns::Selection("a|b|c"))); + prm.declare_entry ("Function_2", + "d", + Patterns::List(Patterns::Selection("d|e|f"))); + prm.leave_subsection (); + + prm.read_input(SOURCE_DIR "/prm/parameter_handler_backslash_05.prm"); + + std::string list_1; + std::string list_2; + prm.enter_subsection ("Testing"); + list_1 = prm.get ("Function_1"); + list_2 = prm.get ("Function_2"); + prm.leave_subsection (); + + deallog << list_1 << std::endl; + deallog << list_2 << std::endl; + + return 0; +} diff --git a/tests/bits/parameter_handler_backslash_05.expect=diff.output b/tests/bits/parameter_handler_backslash_05.expect=diff.output new file mode 100644 index 0000000000..5902adc93b --- /dev/null +++ b/tests/bits/parameter_handler_backslash_05.expect=diff.output @@ -0,0 +1,3 @@ + +DEAL::a, b, c +DEAL::d, e, f diff --git a/tests/bits/prm/parameter_handler_backslash_01.prm b/tests/bits/prm/parameter_handler_backslash_01.prm new file mode 100644 index 0000000000..c5a42a192a --- /dev/null +++ b/tests/bits/prm/parameter_handler_backslash_01.prm @@ -0,0 +1,7 @@ +#Listing of Parameters +#--------------------- +subsection Testing + set Function = a, \ + b, \ + c +end diff --git a/tests/bits/prm/parameter_handler_backslash_02.prm b/tests/bits/prm/parameter_handler_backslash_02.prm new file mode 100644 index 0000000000..0e3772c2ae --- /dev/null +++ b/tests/bits/prm/parameter_handler_backslash_02.prm @@ -0,0 +1,9 @@ +#Listing of Parameters +#--------------------- +subsection Testing + set Function = a,\ + \ + b,\ +\ + c +end diff --git a/tests/bits/prm/parameter_handler_backslash_03.prm b/tests/bits/prm/parameter_handler_backslash_03.prm new file mode 100644 index 0000000000..b8191f5225 --- /dev/null +++ b/tests/bits/prm/parameter_handler_backslash_03.prm @@ -0,0 +1,7 @@ +#Listing of Parameters +#--------------------- +subsection Testing + set Function = a,\ # first term + b,\ # second + c # third +end diff --git a/tests/bits/prm/parameter_handler_backslash_04.prm b/tests/bits/prm/parameter_handler_backslash_04.prm new file mode 100644 index 0000000000..59282a3c27 --- /dev/null +++ b/tests/bits/prm/parameter_handler_backslash_04.prm @@ -0,0 +1,6 @@ +#Listing of Parameters +#--------------------- +subsection Testing + set Function = a,\ b, + c +end diff --git a/tests/bits/prm/parameter_handler_backslash_05.prm b/tests/bits/prm/parameter_handler_backslash_05.prm new file mode 100644 index 0000000000..a2b3ce415a --- /dev/null +++ b/tests/bits/prm/parameter_handler_backslash_05.prm @@ -0,0 +1,18 @@ +# a blank line will break the continuation + +# Listing of Parameters +# --------------------- +subsection Testing + set Function_1 = a, \ + + b, \ + c + + set Function_2 = d, e, \ +\ +\ + \ + f +end + + -- 2.39.5