]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Clarify error handling with PathSearch/read_input.
authorDavid Wells <wellsd2@rpi.edu>
Fri, 16 Sep 2016 00:02:23 +0000 (20:02 -0400)
committerDavid Wells <wellsd2@rpi.edu>
Wed, 28 Sep 2016 13:54:12 +0000 (09:54 -0400)
This commit keeps the same control flow if one tries to use this
function to write out default values to a file but gets rid of the
std::cerr messages (like the recent changes to read_input).

include/deal.II/base/parameter_handler.h
source/base/parameter_handler.cc
tests/parameter_handler/parameter_handler_18.cc

index 3134e84db7075c06c739051f9f6a8b42c42e9042..f395c5e463bc851f13936d9fe85b8a36c18caec3 100644 (file)
@@ -1655,8 +1655,6 @@ public:
    * Read input from a file the name of which is given. The PathSearch class
    * "PARAMETERS" is used to find the file.
    *
-   * Return whether the read was successful.
-   *
    * Unless <tt>optional</tt> is <tt>true</tt>, this function will
    * automatically generate the requested file with default values if the file
    * did not exist. This file will not contain additional comments if
@@ -1665,11 +1663,30 @@ public:
    * 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.
+   *
+   * @deprecated This function has been deprecated in favor of the replacement
+   * ParameterHandler::parse_input, which raises exceptions to indicate errors
+   * instead of returning an error code. ParameterHandler::parse_input does
+   * not have the capability to write default values to a file on failure: if
+   * you wish to duplicate that old behavior then you should catch the
+   * PathSearch::ExcFileNotFound exception and then call
+   * ParameterHandler::print_parameters.
    */
   virtual bool read_input (const std::string &filename,
                            const bool optional = false,
                            const bool write_stripped_file = false,
-                           const std::string &last_line = "");
+                           const std::string &last_line = "") DEAL_II_DEPRECATED;
+
+  /**
+   * Parse the given file to provide values for known parameter fields. The
+   * PathSearch class "PARAMETERS" is used to find the file.
+   *
+   * 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 void parse_input (const std::string &filename,
+                            const std::string &last_line = "");
 
   /**
    * Read input from a string in memory. The lines in memory have to be
index 2dc356d68c9a6f0582d89e5b1f670329d5861d57..2f8e38bedb40b99d78c232737b98824f3fbbe6a4 100644 (file)
@@ -1730,7 +1730,7 @@ bool ParameterHandler::read_input (const std::string &filename,
       std::ifstream file_stream (openname.c_str());
       AssertThrow(file_stream, ExcIO());
 
-      parse_input (file_stream, filename, last_line);
+      return read_input (file_stream, filename, last_line);
     }
   catch (const PathSearch::ExcFileNotFound &)
     {
@@ -1750,6 +1750,18 @@ bool ParameterHandler::read_input (const std::string &filename,
 
 
 
+void ParameterHandler::parse_input (const std::string &filename,
+                                    const std::string &last_line)
+{
+  PathSearch search("PARAMETERS");
+
+  std::string openname = search.find(filename);
+  std::ifstream file_stream (openname.c_str());
+  parse_input (file_stream, filename, last_line);
+}
+
+
+
 bool
 ParameterHandler::read_input_from_string (const char *s,
                                           const std::string &last_line)
index 1b56b842b91b272ef380cd35cd0725efbe39d333..a07c739439a412e29b1e9a2ac9ec68537d9781a1 100644 (file)
@@ -34,8 +34,7 @@ void test ()
   foo.declare_entry("val", "1.0", dealii::Patterns::Double(), "");
   foo.leave_subsection();
 
-  bool okay = foo.read_input(SOURCE_DIR "/parameter_handler_18.prm");
-  Assert(okay, ExcMessage("read_input failed"));
+  foo.parse_input(SOURCE_DIR "/parameter_handler_18.prm");
 
   foo.enter_subsection("bar");
   deallog << foo.get ("val") << std::endl;

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.