From aa21dd72de9557c52e5fae2cf04608627e7ee214 Mon Sep 17 00:00:00 2001 From: Luca Heltai Date: Tue, 28 Apr 2020 23:07:33 +0200 Subject: [PATCH] Make parameter_acceptor use the new print_parameter function. --- .../minor/20200424NiklasFehnLucaHeltai | 3 + include/deal.II/base/parameter_acceptor.h | 43 +++++++---- source/base/parameter_acceptor.cc | 77 +++---------------- 3 files changed, 42 insertions(+), 81 deletions(-) create mode 100644 doc/news/changes/minor/20200424NiklasFehnLucaHeltai diff --git a/doc/news/changes/minor/20200424NiklasFehnLucaHeltai b/doc/news/changes/minor/20200424NiklasFehnLucaHeltai new file mode 100644 index 0000000000..d1ba8541f1 --- /dev/null +++ b/doc/news/changes/minor/20200424NiklasFehnLucaHeltai @@ -0,0 +1,3 @@ +New: ParameterAcceptor::initialize() now uses ParameterHandler::print_parameters() internally. +
+(Niklas Fehn, Luca Heltai, 2020/04/24) diff --git a/include/deal.II/base/parameter_acceptor.h b/include/deal.II/base/parameter_acceptor.h index 0a1a20060d..ee6ee8fc69 100644 --- a/include/deal.II/base/parameter_acceptor.h +++ b/include/deal.II/base/parameter_acceptor.h @@ -45,7 +45,7 @@ DEAL_II_NAMESPACE_OPEN * file. * * Such registry is traversed upon invocation of the single function - * ParameterAcceptor::initialize(file.prm) which in turn calls the method + * ParameterAcceptor::initialize("file.prm") which in turn calls the method * ParameterAcceptor::declare_parameters() for each of the registered classes, * reads the file `file.prm` and subsequently calls the method * ParameterAcceptor::parse_parameters(), again for each of the registered @@ -373,29 +373,40 @@ public: * default values, and don't want to read external files to use a class * derived from ParameterAcceptor. * - * If outfilename is not the empty string, then write the content that was - * read in to the outfilename. The format of both input and output files are - * selected using the extensions of the files themselves. This can be either - * `prm` or `xml` for input, and `prm`, `xml`, or `tex/latex` for output. If - * the output format is `prm`, then `output_style_for_prm_format` is used to - * decide whether we write the full documentation as well, or only the - * parameters. + * If @p output_filename is not the empty string, then we write the content + * that was read into the @p output_filename file, using the style specified + * in @p output_style_for_output_filename. The format of both input and output + * files are selected using the extensions of the files themselves. This can + * be either `prm`, `xml`, or `json` for the @p filename, and any of the + * supported formats for the @p output_filename. * * If the input file does not exist, a default one with the same name is - * created for you, and an exception is thrown. + * created for you following the style specified in + * @p output_style_for_filename, and an exception is thrown. + * + * By default, the file format used to write the files is deduced from + * the extension of the file names. If the corresponding + * ParameterHandler::OutputStyle specifies a format specification, this must + * be compatible with the file extension, or an exception will be thrown. + * + * If the extension is not recognized, and you do not specify a format in the + * corresponding ParameterHandler::OutputStyle, an assertion is thrown. * * @param filename Input file name * @param output_filename Output file name - * @param output_style_for_prm_format How to write the output file if format - * is `prm` + * @param output_style_for_output_filename How to write the output file * @param prm The ParameterHandler to use + * @param output_style_for_filename How to write the default input file if it + * does not exist */ static void - initialize(const std::string & filename = "", - const std::string & output_filename = "", - const ParameterHandler::OutputStyle output_style_for_prm_format = - ParameterHandler::ShortText, - ParameterHandler &prm = ParameterAcceptor::prm); + initialize(const std::string &filename = "", + const std::string &output_filename = "", + const ParameterHandler::OutputStyle + output_style_for_output_filename = ParameterHandler::Short, + ParameterHandler & prm = ParameterAcceptor::prm, + const ParameterHandler::OutputStyle output_style_for_filename = + ParameterHandler::DefaultStyle); /** * Call declare_all_parameters(), read the parameters from the `input_stream` diff --git a/source/base/parameter_acceptor.cc b/source/base/parameter_acceptor.cc index 8879238d2e..48230acf65 100644 --- a/source/base/parameter_acceptor.cc +++ b/source/base/parameter_acceptor.cc @@ -56,82 +56,29 @@ void ParameterAcceptor::initialize( const std::string & filename, const std::string & output_filename, - const ParameterHandler::OutputStyle output_style_for_prm_format, - ParameterHandler & prm) + const ParameterHandler::OutputStyle output_style_for_output_filename, + ParameterHandler & prm, + const ParameterHandler::OutputStyle output_style_for_filename) { declare_all_parameters(prm); if (!filename.empty()) { - // check the extension of input file - if (filename.substr(filename.find_last_of('.') + 1) == "prm") + try { - try - { - prm.parse_input(filename); - } - catch (const dealii::PathSearch::ExcFileNotFound &) - { - std::ofstream out(filename); - Assert(out, ExcIO()); - prm.print_parameters(out, ParameterHandler::Text); - out.close(); - AssertThrow(false, - ExcMessage("You specified <" + filename + - "> as input " + - "parameter file, but it does not exist. " + - "We created it for you.")); - } + prm.parse_input(filename); } - else if (filename.substr(filename.find_last_of('.') + 1) == "xml") + catch (const dealii::PathSearch::ExcFileNotFound &) { - std::ifstream is(filename); - if (!is) - { - std::ofstream out(filename); - Assert(out, ExcIO()); - prm.print_parameters(out, ParameterHandler::XML); - out.close(); - AssertThrow(false, - ExcMessage("You specified <" + filename + - "> as input " + - "parameter file, but it does not exist. " + - "We created it for you.")); - } - prm.parse_input_from_xml(is); + prm.print_parameters(filename, output_style_for_filename); + AssertThrow(false, + ExcMessage("You specified <" + filename + "> as input " + + "parameter file, but it does not exist. " + + "We created it for you.")); } - else - AssertThrow( - false, - ExcMessage( - "Invalid extension of parameter file. Please use .prm or .xml")); } if (!output_filename.empty()) - { - std::ofstream outfile(output_filename.c_str()); - Assert(outfile, ExcIO()); - std::string extension = - output_filename.substr(output_filename.find_last_of('.') + 1); - - if (extension == "prm") - { - outfile << "# Parameter file generated with " << std::endl - << "# DEAL_II_PACKAGE_VERSION = " << DEAL_II_PACKAGE_VERSION - << std::endl; - Assert( - output_style_for_prm_format == ParameterHandler::Text || - output_style_for_prm_format == ParameterHandler::ShortText, - ExcMessage( - "Only Text or ShortText can be specified in output_style_for_prm_format.")) - prm.print_parameters(outfile, output_style_for_prm_format); - } - else if (extension == "xml") - prm.print_parameters(outfile, ParameterHandler::XML); - else if (extension == "latex" || extension == "tex") - prm.print_parameters(outfile, ParameterHandler::LaTeX); - else - AssertThrow(false, ExcNotImplemented()); - } + prm.print_parameters(output_filename, output_style_for_output_filename); // Finally do the parsing. parse_all_parameters(prm); -- 2.39.5