From: Wolfgang Bangerth Date: Sun, 22 Mar 2015 16:04:20 +0000 (-0500) Subject: Some cleanups. X-Git-Tag: v8.3.0-rc1~358^2~4 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4bcca4b2df21f77d12dbfb9d6ebf741c75de2620;p=dealii.git Some cleanups. Avoid code duplication and use the much better tested conversion functions in namespace Utilities. This also covers a number of corner cases that likely didn't work before. Also provide more information in exceptions. --- diff --git a/include/deal.II/base/parameter_handler.h b/include/deal.II/base/parameter_handler.h index 68ed0b91b2..55b9e56ca2 100644 --- a/include/deal.II/base/parameter_handler.h +++ b/include/deal.II/base/parameter_handler.h @@ -1648,7 +1648,7 @@ public: const std::string &documentation = std::string()); /** - * Enter a subsection; if not yet existent, declare it. + * Enter a subsection. If it does not yet exist, create it. */ void enter_subsection (const std::string &subsection); @@ -1887,12 +1887,6 @@ public: DeclException1 (ExcEntryUndeclared, std::string, << "You can't ask for entry <" << arg1 << "> you have not yet declared"); - /** - * Exception - */ - DeclException1 (ExcConversionError, - std::string, - << "Error when trying to convert the following string: " << arg1); //@} private: /** diff --git a/source/base/parameter_handler.cc b/source/base/parameter_handler.cc index 03f3f35adb..7144a41065 100644 --- a/source/base/parameter_handler.cc +++ b/source/base/parameter_handler.cc @@ -1656,47 +1656,55 @@ ParameterHandler::get (const std::string &entry_string) const long int ParameterHandler::get_integer (const std::string &entry_string) const { - std::string s = get (entry_string); - char *endptr; - const long int i = std::strtol (s.c_str(), &endptr, 10); - - // assert that there was no error. an error would be if - // either there was no string to begin with, or if - // strtol set the endptr to anything but the end of - // the string - AssertThrow ((s.size()>0) && (*endptr == '\0'), - ExcConversionError(s)); - - return i; + try + { + return Utilities::string_to_int (get (entry_string)); + } + catch (...) + { + AssertThrow (false, + ExcMessage("Can't convert the parameter value <" + + get(entry_string) + + "> for entry <" + + entry_string + + " to an integer.")); + return 0; + } } double ParameterHandler::get_double (const std::string &entry_string) const { - std::string s = get (entry_string); - char *endptr; - double d = std::strtod (s.c_str(), &endptr); - - // assert that there was no error. an error would be if - // either there was no string to begin with, or if - // strtol set the endptr to anything but the end of - // the string - AssertThrow ((s.size()>0) && (*endptr == '\0'), - ExcConversionError(s)); - - return d; + try + { + return Utilities::string_to_double (get (entry_string)); + } + catch (...) + { + AssertThrow (false, + ExcMessage("Can't convert the parameter value <" + + get(entry_string) + + "> for entry <" + + entry_string + + " to a double precision variable.")); + return 0; + } } bool ParameterHandler::get_bool (const std::string &entry_string) const { - std::string s = get(entry_string); + const std::string s = get(entry_string); AssertThrow ((s=="true") || (s=="false") || (s=="yes") || (s=="no"), - ExcConversionError(s)); + ExcMessage("Can't convert the parameter value <" + + get(entry_string) + + "> for entry <" + + entry_string + + " to a boolean.")); if (s=="true" || s=="yes") return true; else diff --git a/tests/bits/parameter_handler_17.cc b/tests/bits/parameter_handler_17.cc index 99efd0853e..cabd0996c6 100644 --- a/tests/bits/parameter_handler_17.cc +++ b/tests/bits/parameter_handler_17.cc @@ -1,6 +1,6 @@ // --------------------------------------------------------------------- // -// Copyright (C) 2002 - 2014 by the deal.II authors +// Copyright (C) 2002 - 2015 by the deal.II authors // // This file is part of the deal.II library. // @@ -34,7 +34,7 @@ void check () { prm.get_double ("a"); } - catch (const ParameterHandler::ExcConversionError &) + catch (...) { deallog << "get_double() detected the mistake" << std::endl; } @@ -43,7 +43,7 @@ void check () { prm.get_integer ("a"); } - catch (const ParameterHandler::ExcConversionError &) + catch (...) { deallog << "get_integer() detected the mistake" << std::endl; } @@ -52,7 +52,7 @@ void check () { prm.get_bool ("a"); } - catch (const ParameterHandler::ExcConversionError &) + catch (...) { deallog << "get_bool() detected the mistake" << std::endl; }