]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Make parameter_acceptor use the new print_parameter function.
authorLuca Heltai <luca.heltai@sissa.it>
Tue, 28 Apr 2020 21:07:33 +0000 (23:07 +0200)
committerLuca Heltai <luca.heltai@sissa.it>
Tue, 28 Apr 2020 21:07:33 +0000 (23:07 +0200)
doc/news/changes/minor/20200424NiklasFehnLucaHeltai [new file with mode: 0644]
include/deal.II/base/parameter_acceptor.h
source/base/parameter_acceptor.cc

diff --git a/doc/news/changes/minor/20200424NiklasFehnLucaHeltai b/doc/news/changes/minor/20200424NiklasFehnLucaHeltai
new file mode 100644 (file)
index 0000000..d1ba854
--- /dev/null
@@ -0,0 +1,3 @@
+New: ParameterAcceptor::initialize() now uses ParameterHandler::print_parameters() internally.
+<br>
+(Niklas Fehn, Luca Heltai, 2020/04/24)
index 0a1a20060d4a90d32212c505980b92981d43df18..ee6ee8fc6909bc8149937f30b90d0c2b3a915206 100644 (file)
@@ -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`
index 8879238d2ed31028001d3cec9c880c7fc326021a..48230acf65441750a94a150aa0aba9ad7dc0b731 100644 (file)
@@ -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);

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.