From 49e4c831a7a23e67af7a868b39f633169ea7d8ca Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Fri, 15 Sep 2017 10:43:11 +0200
Subject: [PATCH] BUG: parse issues for nastran long format (closes #589)

---
 src/fileFormats/nas/NASCore.C                 | 25 +++++++++++--------
 src/fileFormats/nas/NASCore.H                 |  4 +--
 .../triSurface/interfaces/NAS/readNAS.C       | 25 ++++---------------
 3 files changed, 22 insertions(+), 32 deletions(-)

diff --git a/src/fileFormats/nas/NASCore.C b/src/fileFormats/nas/NASCore.C
index 45edbd9691..176953cc67 100644
--- a/src/fileFormats/nas/NASCore.C
+++ b/src/fileFormats/nas/NASCore.C
@@ -34,28 +34,33 @@ Foam::fileFormats::NASCore::NASCore()
 
 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
 
-Foam::scalar Foam::fileFormats::NASCore::parseNASCoord
-(
-    const string& s
-)
+Foam::scalar Foam::fileFormats::NASCore::parseNASCoord(const string& s)
 {
-    size_t expSign = s.find_last_of("+-");
+    scalar value = 0;
 
-    if (expSign != string::npos && expSign > 0 && !isspace(s[expSign-1]))
+    const size_t expSign = s.find_last_of("+-");
+
+    if (expSign != std::string::npos && expSign > 0 && !isspace(s[expSign-1]))
     {
-        scalar mantissa = readScalar(IStringStream(s.substr(0, expSign))());
-        scalar exponent = readScalar(IStringStream(s.substr(expSign+1))());
+        scalar exponent = 0;
+
+        // Parse as per strtod/strtof - allowing trailing space or [Ee]
+        readScalar(s.substr(0, expSign).c_str(), value);  // mantissa
+        readScalar(s.substr(expSign+1).c_str(),  exponent);
 
         if (s[expSign] == '-')
         {
             exponent = -exponent;
         }
-        return mantissa * pow(10, exponent);
+
+        value *= ::pow(10, exponent);
     }
     else
     {
-        return readScalar(IStringStream(s)());
+        readScalar(s.c_str(), value);
     }
+
+    return value;
 }
 
 
diff --git a/src/fileFormats/nas/NASCore.H b/src/fileFormats/nas/NASCore.H
index e19a040569..ebe3dabb97 100644
--- a/src/fileFormats/nas/NASCore.H
+++ b/src/fileFormats/nas/NASCore.H
@@ -55,8 +55,8 @@ public:
 
     // Public Member Functions
 
-        //- Do weird things to extract number
-        static scalar parseNASCoord(const string&);
+        //- Extract numbers from things like "-2.358-8" (same as "-2.358e-8")
+        static scalar parseNASCoord(const string& s);
 
 
     // Constructors
diff --git a/src/triSurface/triSurface/interfaces/NAS/readNAS.C b/src/triSurface/triSurface/interfaces/NAS/readNAS.C
index c98b11f08c..d77cecf9e9 100644
--- a/src/triSurface/triSurface/interfaces/NAS/readNAS.C
+++ b/src/triSurface/triSurface/interfaces/NAS/readNAS.C
@@ -36,6 +36,7 @@ Description
 \*---------------------------------------------------------------------------*/
 
 #include "triSurface.H"
+#include "NASCore.H"
 #include "IFstream.H"
 #include "IStringStream.H"
 
@@ -47,25 +48,9 @@ namespace Foam
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
 // Do weird things to extract number
-static scalar parseNASCoord(const string& s)
+static inline scalar parseNASCoord(const string& s)
 {
-    size_t expSign = s.find_last_of("+-");
-
-    if (expSign != string::npos && expSign > 0 && !isspace(s[expSign-1]))
-    {
-        scalar mantissa = readScalar(IStringStream(s.substr(0, expSign))());
-        scalar exponent = readScalar(IStringStream(s.substr(expSign+1))());
-
-        if (s[expSign] == '-')
-        {
-            exponent = -exponent;
-        }
-        return mantissa*pow(10, exponent);
-    }
-    else
-    {
-        return readScalar(IStringStream(s)());
-    }
+    return fileFormats::NASCore::parseNASCoord(s);
 }
 
 
@@ -311,8 +296,8 @@ bool triSurface::readNAS(const fileName& fName)
             // GRID*      126   0 -5.55999875E+02 -5.68730474E+02
             // *         2.14897901E+02
             label index =
-                readLabel(IStringStream(readNASToken(line, 8, linei))());
-            readNASToken(line, 8, linei);
+                readLabel(IStringStream(readNASToken(line, 16, linei))());
+            readNASToken(line, 16, linei);
             scalar x = parseNASCoord(readNASToken(line, 16, linei));
             scalar y = parseNASCoord(readNASToken(line, 16, linei));
 
-- 
GitLab