From 59bdc08c9fc3a188b6a13dfc6a71075722901a61 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Thu, 12 Oct 2017 12:15:56 +0200
Subject: [PATCH] STYLE: remove FULLDEBUG output on readLabel, readScalar

- used in various places to test if the input can be parsed as a
  label/scalar, so warnings tend to flood the output.

- be more explicit when encountering range errors
---
 .../test/primitives/Test-primitives.C         | 16 +++++++--
 src/OpenFOAM/primitives/Scalar/Scalar.C       | 13 +------
 src/OpenFOAM/primitives/ints/int/intIO.C      | 36 +++++++------------
 src/OpenFOAM/primitives/ints/int32/int32IO.C  | 36 +++++++------------
 src/OpenFOAM/primitives/ints/int64/int64IO.C  | 36 +++++++------------
 .../primitives/ints/uint32/uint32IO.C         | 36 +++++++------------
 .../primitives/ints/uint64/uint64IO.C         | 36 +++++++------------
 .../primitives/strings/parsing/parsing.C      |  3 +-
 .../primitives/strings/parsing/parsing.H      |  3 +-
 .../primitives/strings/parsing/parsingI.H     |  2 +-
 10 files changed, 79 insertions(+), 138 deletions(-)

diff --git a/applications/test/primitives/Test-primitives.C b/applications/test/primitives/Test-primitives.C
index 72017bfee9..219b1ef5ec 100644
--- a/applications/test/primitives/Test-primitives.C
+++ b/applications/test/primitives/Test-primitives.C
@@ -55,6 +55,7 @@ unsigned testParsing
 )
 {
     unsigned nFail = 0;
+    string errMsg;
 
     // Expect some failures
     const bool prev = FatalIOError.throwExceptions();
@@ -74,6 +75,7 @@ unsigned testParsing
         catch (Foam::error& err)
         {
             parsed = false;
+            errMsg = err.message();
         }
 
         if (parsed)
@@ -93,12 +95,15 @@ unsigned testParsing
             if (expected)
             {
                 ++nFail;
-                Info<< "(fail) unexpected failure " << str << nl;
+                Info<< "(fail) unexpected";
             }
             else
             {
-                Info<< "(pass) expected failure " << str << nl;
+                Info<< "(pass) expected";
             }
+
+            Info<< " failure " << str
+                << "  >> " << errMsg.c_str() << nl;
         }
     }
 
@@ -125,6 +130,7 @@ int main(int argc, char *argv[])
                 { " 1234E junk", false },
                 { " 3.14159 ", true },
                 { " 31.4159E-1 " , true },
+                { " 100E1000 " , false },
             }
         );
     }
@@ -165,6 +171,7 @@ int main(int argc, char *argv[])
             &readInt32,
             {
                 { " 3.14159 ", false },
+                { " 31E1 ", false },
                 { " 31.4159E-1 " , false },
                 { "100" , true },
                 { "	2147483644" , true },
@@ -174,13 +181,16 @@ int main(int argc, char *argv[])
     }
 
     {
-        Info<< nl << "Test readUint32 (max= " << INT32_MAX << "):" << nl;
+        Info<< nl << "Test readUint32 (max= "
+            << unsigned(UINT32_MAX) << "):" << nl;
         nFail += testParsing
         (
             &readUint32,
             {
                 { "	2147483644" , true },
                 { "   2147483700  " , true },
+                { "   4294967295  " , true },
+                { "   4294968000  " , false },
             }
         );
     }
diff --git a/src/OpenFOAM/primitives/Scalar/Scalar.C b/src/OpenFOAM/primitives/Scalar/Scalar.C
index d8eb7e06fc..644e9abf12 100644
--- a/src/OpenFOAM/primitives/Scalar/Scalar.C
+++ b/src/OpenFOAM/primitives/Scalar/Scalar.C
@@ -102,18 +102,7 @@ bool readScalar(const char* buf, Scalar& val)
 
     val = ScalarConvert(buf, &endptr);
 
-    const parsing::errorType err = parsing::checkConversion(buf, endptr);
-    if (err != parsing::errorType::NONE)
-    {
-        #ifdef FULLDEBUG
-        IOWarningInFunction("unknown")
-            << parsing::errorNames[err] << " '" << buf << "'"
-            << endl;
-        #endif
-        return false;
-    }
-
-    return true;
+    return (parsing::checkConversion(buf, endptr) == parsing::errorType::NONE);
 }
 
 
diff --git a/src/OpenFOAM/primitives/ints/int/intIO.C b/src/OpenFOAM/primitives/ints/int/intIO.C
index 6a2901aefb..f9a8e2a58b 100644
--- a/src/OpenFOAM/primitives/ints/int/intIO.C
+++ b/src/OpenFOAM/primitives/ints/int/intIO.C
@@ -39,13 +39,13 @@ int Foam::readInt(const char* buf)
 
     const int val = int(parsed);
 
-    if (parsed < INT_MIN || parsed > INT_MAX)
-    {
-        // Range error
-        errno = ERANGE;
-    }
+    const parsing::errorType err =
+    (
+        (parsed < INT_MIN || parsed > INT_MAX)
+      ? parsing::errorType::RANGE
+      : parsing::checkConversion(buf, endptr)
+    );
 
-    const parsing::errorType err = parsing::checkConversion(buf, endptr);
     if (err != parsing::errorType::NONE)
     {
         FatalIOErrorInFunction("unknown")
@@ -65,24 +65,12 @@ bool Foam::readInt(const char* buf, int& val)
 
     val = int(parsed);
 
-    if (parsed < INT_MIN || parsed > INT_MAX)
-    {
-        // Range error
-        errno = ERANGE;
-    }
-
-    const parsing::errorType err = parsing::checkConversion(buf, endptr);
-    if (err != parsing::errorType::NONE)
-    {
-        #ifdef FULLDEBUG
-        IOWarningInFunction("unknown")
-            << parsing::errorNames[err] << " '" << buf << "'"
-            << endl;
-        #endif
-        return false;
-    }
-
-    return true;
+    return
+    (
+        (parsed < INT_MIN || parsed > INT_MAX)
+      ? false
+      : (parsing::checkConversion(buf, endptr) == parsing::errorType::NONE)
+    );
 }
 
 
diff --git a/src/OpenFOAM/primitives/ints/int32/int32IO.C b/src/OpenFOAM/primitives/ints/int32/int32IO.C
index f1b4f23fb0..926421ff2b 100644
--- a/src/OpenFOAM/primitives/ints/int32/int32IO.C
+++ b/src/OpenFOAM/primitives/ints/int32/int32IO.C
@@ -39,13 +39,13 @@ int32_t Foam::readInt32(const char* buf)
 
     const int32_t val = int32_t(parsed);
 
-    if (parsed < INT32_MIN || parsed > INT32_MAX)
-    {
-        // Range error
-        errno = ERANGE;
-    }
+    const parsing::errorType err =
+    (
+        (parsed < INT32_MIN || parsed > INT32_MAX)
+      ? parsing::errorType::RANGE
+      : parsing::checkConversion(buf, endptr)
+    );
 
-    const parsing::errorType err = parsing::checkConversion(buf, endptr);
     if (err != parsing::errorType::NONE)
     {
         FatalIOErrorInFunction("unknown")
@@ -65,24 +65,12 @@ bool Foam::readInt32(const char* buf, int32_t& val)
 
     val = int32_t(parsed);
 
-    if (parsed < INT32_MIN || parsed > INT32_MAX)
-    {
-        // Range error
-        errno = ERANGE;
-    }
-
-    const parsing::errorType err = parsing::checkConversion(buf, endptr);
-    if (err != parsing::errorType::NONE)
-    {
-        #ifdef FULLDEBUG
-        IOWarningInFunction("unknown")
-            << parsing::errorNames[err] << " '" << buf << "'"
-            << endl;
-        #endif
-        return false;
-    }
-
-    return true;
+    return
+    (
+        (parsed < INT32_MIN || parsed > INT32_MAX)
+      ? false
+      : (parsing::checkConversion(buf, endptr) == parsing::errorType::NONE)
+    );
 }
 
 
diff --git a/src/OpenFOAM/primitives/ints/int64/int64IO.C b/src/OpenFOAM/primitives/ints/int64/int64IO.C
index d16d979d6f..1a2ad639e3 100644
--- a/src/OpenFOAM/primitives/ints/int64/int64IO.C
+++ b/src/OpenFOAM/primitives/ints/int64/int64IO.C
@@ -39,13 +39,13 @@ int64_t Foam::readInt64(const char* buf)
 
     const int64_t val = int64_t(parsed);
 
-    if (parsed < INT64_MIN || parsed > INT64_MAX)
-    {
-        // Range error
-        errno = ERANGE;
-    }
+    const parsing::errorType err =
+    (
+        (parsed < INT64_MIN || parsed > INT64_MAX)
+      ? parsing::errorType::RANGE
+      : parsing::checkConversion(buf, endptr)
+    );
 
-    const parsing::errorType err = parsing::checkConversion(buf, endptr);
     if (err != parsing::errorType::NONE)
     {
         FatalIOErrorInFunction("unknown")
@@ -65,24 +65,12 @@ bool Foam::readInt64(const char* buf, int64_t& val)
 
     val = int64_t(parsed);
 
-    if (parsed < INT64_MIN || parsed > INT64_MAX)
-    {
-        // Range error
-        errno = ERANGE;
-    }
-
-    const parsing::errorType err = parsing::checkConversion(buf, endptr);
-    if (err != parsing::errorType::NONE)
-    {
-        #ifdef FULLDEBUG
-        IOWarningInFunction("unknown")
-            << parsing::errorNames[err] << " '" << buf << "'"
-            << endl;
-        #endif
-        return false;
-    }
-
-    return true;
+    return
+    (
+        (parsed < INT64_MIN || parsed > INT64_MAX)
+      ? false
+      : (parsing::checkConversion(buf, endptr) == parsing::errorType::NONE)
+    );
 }
 
 
diff --git a/src/OpenFOAM/primitives/ints/uint32/uint32IO.C b/src/OpenFOAM/primitives/ints/uint32/uint32IO.C
index bf4e6fb8a0..96fdc8c85b 100644
--- a/src/OpenFOAM/primitives/ints/uint32/uint32IO.C
+++ b/src/OpenFOAM/primitives/ints/uint32/uint32IO.C
@@ -38,13 +38,13 @@ uint32_t Foam::readUint32(const char* buf)
 
     const uint32_t val = uint32_t(parsed);
 
-    if (parsed > UINT32_MAX)
-    {
-        // Range error
-        errno = ERANGE;
-    }
+    const parsing::errorType err =
+    (
+        (parsed > UINT32_MAX)
+      ? parsing::errorType::RANGE
+      : parsing::checkConversion(buf, endptr)
+    );
 
-    const parsing::errorType err = parsing::checkConversion(buf, endptr);
     if (err != parsing::errorType::NONE)
     {
         FatalIOErrorInFunction("unknown")
@@ -64,24 +64,12 @@ bool Foam::readUint32(const char* buf, uint32_t& val)
 
     val = uint32_t(parsed);
 
-    if (parsed > UINT32_MAX)
-    {
-        // Range error
-        errno = ERANGE;
-    }
-
-    const parsing::errorType err = parsing::checkConversion(buf, endptr);
-    if (err != parsing::errorType::NONE)
-    {
-        #ifdef FULLDEBUG
-        IOWarningInFunction("unknown")
-            << parsing::errorNames[err] << " '" << buf << "'"
-            << endl;
-        #endif
-        return false;
-    }
-
-    return true;
+    return
+    (
+        (parsed > UINT32_MAX)
+      ? false
+      : (parsing::checkConversion(buf, endptr) == parsing::errorType::NONE)
+    );
 }
 
 
diff --git a/src/OpenFOAM/primitives/ints/uint64/uint64IO.C b/src/OpenFOAM/primitives/ints/uint64/uint64IO.C
index 2f23b89ddd..b73e90e771 100644
--- a/src/OpenFOAM/primitives/ints/uint64/uint64IO.C
+++ b/src/OpenFOAM/primitives/ints/uint64/uint64IO.C
@@ -38,13 +38,13 @@ uint64_t Foam::readUint64(const char* buf)
 
     const uint64_t val = uint64_t(parsed);
 
-    if (parsed > UINT64_MAX)
-    {
-        // Range error
-        errno = ERANGE;
-    }
+    const parsing::errorType err =
+    (
+        (parsed > UINT64_MAX)
+      ? parsing::errorType::RANGE
+      : parsing::checkConversion(buf, endptr)
+    );
 
-    const parsing::errorType err = parsing::checkConversion(buf, endptr);
     if (err != parsing::errorType::NONE)
     {
         FatalIOErrorInFunction("unknown")
@@ -64,24 +64,12 @@ bool Foam::readUint64(const char* buf, uint64_t& val)
 
     val = uint64_t(parsed);
 
-    if (parsed > UINT64_MAX)
-    {
-        // Range error
-        errno = ERANGE;
-    }
-
-    const parsing::errorType err = parsing::checkConversion(buf, endptr);
-    if (err != parsing::errorType::NONE)
-    {
-        #ifdef FULLDEBUG
-        IOWarningInFunction("unknown")
-            << parsing::errorNames[err] << " '" << buf << "'"
-            << endl;
-        #endif
-        return false;
-    }
-
-    return true;
+    return
+    (
+        (parsed > UINT64_MAX)
+      ? false
+      : (parsing::checkConversion(buf, endptr) == parsing::errorType::NONE)
+    );
 }
 
 
diff --git a/src/OpenFOAM/primitives/strings/parsing/parsing.C b/src/OpenFOAM/primitives/strings/parsing/parsing.C
index 2b81829528..bb3a38564b 100644
--- a/src/OpenFOAM/primitives/strings/parsing/parsing.C
+++ b/src/OpenFOAM/primitives/strings/parsing/parsing.C
@@ -30,7 +30,8 @@ License
 const Foam::Enum<Foam::parsing::errorType>
 Foam::parsing::errorNames
 {
-    { errorType::GENERAL,  "General error parsing" },
+    { errorType::GENERAL, "General error parsing" },
+    { errorType::RANGE, "Range error while parsing" },
     { errorType::TRAILING, "Trailing content found parsing" },
 };
 
diff --git a/src/OpenFOAM/primitives/strings/parsing/parsing.H b/src/OpenFOAM/primitives/strings/parsing/parsing.H
index bef3d34f9c..925de3ccdb 100644
--- a/src/OpenFOAM/primitives/strings/parsing/parsing.H
+++ b/src/OpenFOAM/primitives/strings/parsing/parsing.H
@@ -56,7 +56,8 @@ namespace parsing
     {
         NONE = 0,         //!< No error encountered
         GENERAL = 1,      //!< General parsing error
-        TRAILING = 2,     //!< Trailing content detected
+        RANGE = 2,        //!< Range error
+        TRAILING = 3,     //!< Trailing content detected
     };
 
 
diff --git a/src/OpenFOAM/primitives/strings/parsing/parsingI.H b/src/OpenFOAM/primitives/strings/parsing/parsingI.H
index 7f6a4087a1..63e4209ba1 100644
--- a/src/OpenFOAM/primitives/strings/parsing/parsingI.H
+++ b/src/OpenFOAM/primitives/strings/parsing/parsingI.H
@@ -32,7 +32,7 @@ inline Foam::parsing::errorType Foam::parsing::checkConversion
     if (errno || endptr == buf)
     {
         // Some type of error OR no conversion
-        return errorType::GENERAL;
+        return (errno == ERANGE ? errorType::RANGE : errorType::GENERAL);
     }
 
     // Trailing spaces are permitted
-- 
GitLab