From b2c8377ab1fb1e9444d9ca316e806cd4f7adcde7 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Tue, 13 Aug 2013 14:33:37 +0100
Subject: [PATCH] ENH: primitives: abstract out number parsing

---
 .../Scalar/doubleScalar/doubleScalar.H          | 10 +++++++++-
 .../primitives/Scalar/floatScalar/floatScalar.H | 10 +++++++++-
 src/OpenFOAM/primitives/ints/int/int.H          |  3 ++-
 src/OpenFOAM/primitives/ints/int/intIO.C        | 11 ++++++++++-
 src/OpenFOAM/primitives/ints/label/label.H      | 17 ++++++++++++++++-
 src/OpenFOAM/primitives/ints/long/long.H        |  3 ++-
 src/OpenFOAM/primitives/ints/long/longIO.C      |  9 ++++++++-
 .../primitives/ints/longLong/longLong.H         |  3 ++-
 .../primitives/ints/longLong/longLongIO.C       | 10 +++++++++-
 9 files changed, 67 insertions(+), 9 deletions(-)

diff --git a/src/OpenFOAM/primitives/Scalar/doubleScalar/doubleScalar.H b/src/OpenFOAM/primitives/Scalar/doubleScalar/doubleScalar.H
index 3e8b2a3b057..13a70e475e6 100644
--- a/src/OpenFOAM/primitives/Scalar/doubleScalar/doubleScalar.H
+++ b/src/OpenFOAM/primitives/Scalar/doubleScalar/doubleScalar.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -58,6 +58,14 @@ static const doubleScalar doubleScalarSMALL = 1.0e-15;
 static const doubleScalar doubleScalarVSMALL = 1.0e-300;
 static const doubleScalar doubleScalarROOTVSMALL = 1.0e-150;
 
+//- Read whole of buf as a scalar. Return true if succesful.
+inline bool readScalar(const char* buf, doubleScalar& s)
+{
+    char* endPtr;
+    s = strtod(buf, &endPtr);
+
+    return (*endPtr == '\0');
+}
 
 #define Scalar doubleScalar
 #define ScalarVGREAT doubleScalarVGREAT
diff --git a/src/OpenFOAM/primitives/Scalar/floatScalar/floatScalar.H b/src/OpenFOAM/primitives/Scalar/floatScalar/floatScalar.H
index 3386d3630e9..5401bc79a6c 100644
--- a/src/OpenFOAM/primitives/Scalar/floatScalar/floatScalar.H
+++ b/src/OpenFOAM/primitives/Scalar/floatScalar/floatScalar.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -58,6 +58,14 @@ static const floatScalar floatScalarSMALL = 1.0e-6;
 static const floatScalar floatScalarVSMALL = 1.0e-37;
 static const floatScalar floatScalarROOTVSMALL = 1.0e-18;
 
+//- Read whole of buf as a scalar. Return true if succesful.
+inline bool readScalar(const char* buf, floatScalar& s)
+{
+    char* endPtr;
+    s = strtof(buf, &endPtr);
+
+    return (*endPtr == '\0');
+}
 
 #define Scalar floatScalar
 #define ScalarVGREAT floatScalarVGREAT
diff --git a/src/OpenFOAM/primitives/ints/int/int.H b/src/OpenFOAM/primitives/ints/int/int.H
index c734f3e5830..9f3a34c978d 100644
--- a/src/OpenFOAM/primitives/ints/int/int.H
+++ b/src/OpenFOAM/primitives/ints/int/int.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -54,6 +54,7 @@ word name(const int);
 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
 
 int readInt(Istream&);
+bool readInt(const char*, int&);
 Istream& operator>>(Istream&, int&);
 Ostream& operator<<(Ostream&, const int);
 
diff --git a/src/OpenFOAM/primitives/ints/int/intIO.C b/src/OpenFOAM/primitives/ints/int/intIO.C
index fb9ff8b0e31..bd02519897d 100644
--- a/src/OpenFOAM/primitives/ints/int/intIO.C
+++ b/src/OpenFOAM/primitives/ints/int/intIO.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -88,6 +88,15 @@ int Foam::readInt(Istream& is)
 }
 
 
+bool Foam::readInt(const char* buf, int& s)
+{
+    char *endptr = NULL;
+    long l = strtol(buf, &endptr, 10);
+    s = int(l);
+    return (*endptr == 0);
+}
+
+
 Foam::Ostream& Foam::operator<<(Ostream& os, const int i)
 {
     os.write(label(i));
diff --git a/src/OpenFOAM/primitives/ints/label/label.H b/src/OpenFOAM/primitives/ints/label/label.H
index 17911aed1f7..0e97dc7c576 100644
--- a/src/OpenFOAM/primitives/ints/label/label.H
+++ b/src/OpenFOAM/primitives/ints/label/label.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -72,6 +72,11 @@ namespace Foam
         return readInt(is);
     }
 
+    inline bool readLabel(const char* buf, label& s)
+    {
+        return readInt(buf, s);
+    }
+
 } // End namespace Foam
 
 
@@ -96,6 +101,11 @@ namespace Foam
         return readLong(is);
     }
 
+    inline bool readLabel(const char* buf, label& s)
+    {
+        return readLong(buf, s);
+    }
+
 } // End namespace Foam
 
 
@@ -122,6 +132,11 @@ namespace Foam
         return readLongLong(is);
     }
 
+    inline bool readLabel(const char* buf, label& s)
+    {
+        return readLongLong(buf, s);
+    }
+
 } // End namespace Foam
 
 #endif
diff --git a/src/OpenFOAM/primitives/ints/long/long.H b/src/OpenFOAM/primitives/ints/long/long.H
index 5fbf2fa95be..6e09fc2046b 100644
--- a/src/OpenFOAM/primitives/ints/long/long.H
+++ b/src/OpenFOAM/primitives/ints/long/long.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -53,6 +53,7 @@ word name(const long);
 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
 
 long readLong(Istream&);
+bool readLong(const char*, long&);
 Istream& operator>>(Istream&, long&);
 Ostream& operator<<(Ostream&, const long);
 
diff --git a/src/OpenFOAM/primitives/ints/long/longIO.C b/src/OpenFOAM/primitives/ints/long/longIO.C
index 3a9d8e3fe53..7e352bd9db3 100644
--- a/src/OpenFOAM/primitives/ints/long/longIO.C
+++ b/src/OpenFOAM/primitives/ints/long/longIO.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -85,6 +85,13 @@ long Foam::readLong(Istream& is)
     return val;
 }
 
+bool Foam::readLong(const char* buf, long& s)
+{
+    char *endptr = NULL;
+    s = strtol(buf, &endptr, 10);
+    return (*endptr == 0);
+}
+
 
 Foam::Ostream& Foam::operator<<(Ostream& os, const long l)
 {
diff --git a/src/OpenFOAM/primitives/ints/longLong/longLong.H b/src/OpenFOAM/primitives/ints/longLong/longLong.H
index 00d34b5d924..e1595e8462d 100644
--- a/src/OpenFOAM/primitives/ints/longLong/longLong.H
+++ b/src/OpenFOAM/primitives/ints/longLong/longLong.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -53,6 +53,7 @@ word name(long long);
 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
 
 long long readLongLong(Istream&);
+bool readLongLong(const char*, long long&);
 Istream& operator>>(Istream&, long long&);
 Ostream& operator<<(Ostream&, const long long);
 
diff --git a/src/OpenFOAM/primitives/ints/longLong/longLongIO.C b/src/OpenFOAM/primitives/ints/longLong/longLongIO.C
index 146bf011ea0..384fe1bf1f0 100644
--- a/src/OpenFOAM/primitives/ints/longLong/longLongIO.C
+++ b/src/OpenFOAM/primitives/ints/longLong/longLongIO.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -87,6 +87,14 @@ long long Foam::readLongLong(Istream& is)
 }
 
 
+bool Foam::readLongLong(const char* buf, long long& s)
+{
+    char *endptr = NULL;
+    s = strtoll(buf, &endptr, 10);
+    return (*endptr == 0);
+}
+
+
 Foam::Ostream& Foam::operator<<(Ostream& os, const long long l)
 {
     long long val = l;
-- 
GitLab