]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Detect errors in Utilities::string_to_int.
authorbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Sat, 19 Jul 2014 12:43:59 +0000 (12:43 +0000)
committerbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Sat, 19 Jul 2014 12:43:59 +0000 (12:43 +0000)
git-svn-id: https://svn.dealii.org/trunk@33201 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/changes.h
deal.II/source/base/utilities.cc

index 4969a345292bbb8fd3689caf2d00d66815606e74..4d033f8846c545346d02b70bcf08c48c6c5cf89a 100644 (file)
@@ -190,6 +190,13 @@ inconvenience this causes.
 <h3>Specific improvements</h3>
 
 <ol>
+  <li> Fixed: Utilities::string_to_int() did not catch if the
+  string given started with an integer but contained additional
+  text. It now throws an exception if this happens.
+  <br>
+  (Wolfgang Bangerth, 2014/07/14)
+  </li>
+
   <li> New: The function GridOut::write, can now be used also in 
   the codimension one case. 
   <br>
@@ -203,7 +210,6 @@ inconvenience this causes.
   (Luca Heltai, 2014/07/18)
   </li>
 
-
   <li> New: The GridReordering::reorder_cells() function used a
   numbering format for the vertices in a cell that was last used in
   deal.II version 5.2. This format is still used internally, but
index 16c8d1ab31a06b239fa209b65df09b250c0e4c1c..b014ee6ff7d85c2f33a861282f7d9d2d8eb943e4 100644 (file)
@@ -149,18 +149,10 @@ namespace Utilities
   int
   string_to_int (const std::string &s)
   {
-    std::istringstream ss(s);
-
-    int i = std::numeric_limits<int>::max();
-    ss >> i;
-    // check for errors
-    AssertThrow (i != std::numeric_limits<int>::max(),
-                 ExcCantConvertString (s));
-
-//TODO: The test for errors above doesn't work, as can easily be
-//verified. furthermore, it doesn't catch cases like when calling
-//string_to_int("1.23.4") since it just reads in however much it can, without
-//realizing that there is more
+    char *p;
+    const int i = std::strtol(s.c_str(), &p, 10);
+    AssertThrow ( !((errno != 0) || (s.size() == 0) || ((s.size()>0) && (*p != '\0'))),
+                  ExcMessage ("Can't convert <" + s + "> to an integer."));
 
     return i;
   }

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.