From f265b0484cc2217b21b76f37c4a2dc9fb0159b2c Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Tue, 17 Jan 2017 08:42:05 +0100
Subject: [PATCH] BUG: extra newline in foamVtkAppendBase64Formatter flush()

Enhancements

- introduce intermediate layer for base64 foamVtk formatting
- add encodedLength() method, which is useful for xml appended output
---
 .../db/IOstreams/hashes/base64Layer.C         |  18 ++-
 .../db/IOstreams/hashes/base64Layer.H         |  20 +--
 src/fileFormats/Make/files                    |   1 +
 .../vtk/format/foamVtkAppendBase64Formatter.C |  17 +--
 .../vtk/format/foamVtkAppendBase64Formatter.H |  12 +-
 .../vtk/format/foamVtkAppendRawFormatter.C    |  14 +--
 .../vtk/format/foamVtkAppendRawFormatter.H    |  14 ++-
 .../vtk/format/foamVtkAsciiFormatter.C        |  29 +++--
 .../vtk/format/foamVtkAsciiFormatter.H        |  29 +++--
 .../vtk/format/foamVtkBase64Formatter.C       |  65 ++--------
 .../vtk/format/foamVtkBase64Formatter.H       |  30 +----
 .../vtk/format/foamVtkBase64Layer.C           | 116 ++++++++++++++++++
 .../vtk/format/foamVtkBase64Layer.H           | 114 +++++++++++++++++
 src/fileFormats/vtk/format/foamVtkFormatter.C |  10 +-
 src/fileFormats/vtk/format/foamVtkFormatter.H |  54 ++++----
 .../vtk/format/foamVtkLegacyFormatter.C       |   6 +-
 .../vtk/format/foamVtkLegacyFormatter.H       |  28 +++--
 .../vtk/format/foamVtkOutputOptions.C         |  24 ++--
 .../vtk/format/foamVtkOutputOptions.H         |  12 +-
 19 files changed, 406 insertions(+), 207 deletions(-)
 create mode 100644 src/fileFormats/vtk/format/foamVtkBase64Layer.C
 create mode 100644 src/fileFormats/vtk/format/foamVtkBase64Layer.H

diff --git a/src/OpenFOAM/db/IOstreams/hashes/base64Layer.C b/src/OpenFOAM/db/IOstreams/hashes/base64Layer.C
index f1123f7519f..5e055c1ca1c 100644
--- a/src/OpenFOAM/db/IOstreams/hashes/base64Layer.C
+++ b/src/OpenFOAM/db/IOstreams/hashes/base64Layer.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2016 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -43,27 +43,35 @@ static const unsigned char base64Chars[64] =
 //! \endcond
 
 
+// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
+
+std::size_t Foam::base64Layer::encodedLength(std::size_t n)
+{
+    return 4 * ((n / 3) + (n % 3 ? 1 : 0));
+}
+
+
 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
 
-inline unsigned char Foam::base64Layer::encode0()
+inline unsigned char Foam::base64Layer::encode0() const
 {
     // Top 6 bits of char0
     return base64Chars[((group_[0] & 0xFC) >> 2)];
 }
 
-inline unsigned char Foam::base64Layer::encode1()
+inline unsigned char Foam::base64Layer::encode1() const
 {
     // Bottom 2 bits of char0, Top 4 bits of char1
     return base64Chars[((group_[0] & 0x03) << 4) | ((group_[1] & 0xF0) >> 4)];
 }
 
-inline unsigned char Foam::base64Layer::encode2()
+inline unsigned char Foam::base64Layer::encode2() const
 {
     // Bottom 4 bits of char1, Top 2 bits of char2
     return base64Chars[((group_[1] & 0x0F) << 2) | ((group_[2] & 0xC0) >> 6)];
 }
 
-inline unsigned char Foam::base64Layer::encode3()
+inline unsigned char Foam::base64Layer::encode3() const
 {
     // Bottom 6 bits of char2
     return base64Chars[(group_[2] & 0x3F)];
diff --git a/src/OpenFOAM/db/IOstreams/hashes/base64Layer.H b/src/OpenFOAM/db/IOstreams/hashes/base64Layer.H
index 9aa4bc8d796..4eaaf87e83e 100644
--- a/src/OpenFOAM/db/IOstreams/hashes/base64Layer.H
+++ b/src/OpenFOAM/db/IOstreams/hashes/base64Layer.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2016 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -30,7 +30,7 @@ Description
     Base64 encoding accoding to RFC 4648 specification
     (https://tools.ietf.org/html/rfc4648#page-5).
     It is the obligation of the caller to avoid using normal output
-    while the base-64 encoding layer is actively being used.
+    while the base-64 encoding layer is actively used.
 
 SourceFiles
     base64Layer.C
@@ -70,10 +70,10 @@ class base64Layer
 
     // Private Member Functions
 
-        inline unsigned char encode0();
-        inline unsigned char encode1();
-        inline unsigned char encode2();
-        inline unsigned char encode3();
+        inline unsigned char encode0() const;
+        inline unsigned char encode1() const;
+        inline unsigned char encode2() const;
+        inline unsigned char encode3() const;
 
         //- Disallow default bitwise copy construct
         base64Layer(const base64Layer&) = delete;
@@ -95,7 +95,7 @@ public:
     // Constructors
 
         //- Construct and attach to an output stream
-        base64Layer(std::ostream&);
+        base64Layer(std::ostream& os);
 
 
     //- Destructor
@@ -104,6 +104,10 @@ public:
 
     // Member Functions
 
+        //- The encoded length has 4 bytes out for every 3 bytes in.
+        static std::size_t encodedLength(std::size_t n);
+
+
         //- Encode the character sequence, writing when possible.
         void write(const char* s, std::streamsize n);
 
@@ -111,7 +115,7 @@ public:
         void reset();
 
         //- End the encoding sequence, padding the final characters with '='.
-        //  Return false if no encoding layer was actually used.
+        //  Return false if no encoding was actually performed.
         bool close();
 
 };
diff --git a/src/fileFormats/Make/files b/src/fileFormats/Make/files
index 87fd35ee22b..ab7fe26dc28 100644
--- a/src/fileFormats/Make/files
+++ b/src/fileFormats/Make/files
@@ -19,6 +19,7 @@ vtk/format/foamVtkAppendBase64Formatter.C
 vtk/format/foamVtkAppendRawFormatter.C
 vtk/format/foamVtkAsciiFormatter.C
 vtk/format/foamVtkBase64Formatter.C
+vtk/format/foamVtkBase64Layer.C
 vtk/format/foamVtkLegacyFormatter.C
 vtk/format/foamVtkFormatter.C
 vtk/format/foamVtkOutputOptions.C
diff --git a/src/fileFormats/vtk/format/foamVtkAppendBase64Formatter.C b/src/fileFormats/vtk/format/foamVtkAppendBase64Formatter.C
index ee8b84200f0..3805b61812e 100644
--- a/src/fileFormats/vtk/format/foamVtkAppendBase64Formatter.C
+++ b/src/fileFormats/vtk/format/foamVtkAppendBase64Formatter.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2016 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -27,8 +27,7 @@ License
 
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
 
-const char* Foam::foamVtkAppendBase64Formatter::name_     = "append";
-const char* Foam::foamVtkAppendBase64Formatter::encoding_ = "base64";
+const char* Foam::foamVtkAppendBase64Formatter::name_ = "append";
 
 
 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
@@ -38,14 +37,16 @@ Foam::foamVtkAppendBase64Formatter::foamVtkAppendBase64Formatter
     std::ostream& os
 )
 :
-    foamVtkBase64Formatter(os)
+    foamVtkBase64Layer(os)
 {}
 
 
 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
 
 Foam::foamVtkAppendBase64Formatter::~foamVtkAppendBase64Formatter()
-{}
+{
+    base64Layer::close();
+}
 
 
 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
@@ -56,10 +57,4 @@ const char* Foam::foamVtkAppendBase64Formatter::name() const
 }
 
 
-const char* Foam::foamVtkAppendBase64Formatter::encoding() const
-{
-    return encoding_;
-}
-
-
 // ************************************************************************* //
diff --git a/src/fileFormats/vtk/format/foamVtkAppendBase64Formatter.H b/src/fileFormats/vtk/format/foamVtkAppendBase64Formatter.H
index b7239ee7c66..db3e1d6694f 100644
--- a/src/fileFormats/vtk/format/foamVtkAppendBase64Formatter.H
+++ b/src/fileFormats/vtk/format/foamVtkAppendBase64Formatter.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2016 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -36,7 +36,7 @@ SourceFiles
 #ifndef foamVtkAppendBase64Formatter_H
 #define foamVtkAppendBase64Formatter_H
 
-#include "foamVtkBase64Formatter.H"
+#include "foamVtkBase64Layer.H"
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
@@ -49,12 +49,11 @@ namespace Foam
 
 class foamVtkAppendBase64Formatter
 :
-    public foamVtkBase64Formatter
+    public foamVtkBase64Layer
 {
     // Private Data Members
 
         static const char* name_;
-        static const char* encoding_;
 
 
     // Private Member Functions
@@ -71,7 +70,7 @@ public:
     // Constructors
 
         //- Construct and attach to an output stream
-        foamVtkAppendBase64Formatter(std::ostream&);
+        foamVtkAppendBase64Formatter(std::ostream& os);
 
 
     //- Destructor
@@ -83,9 +82,6 @@ public:
         //- Output name for XML type ("append")
         virtual const char* name() const;
 
-        //- Name for the XML append encoding ("base64").
-        virtual const char* encoding() const;
-
 };
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
diff --git a/src/fileFormats/vtk/format/foamVtkAppendRawFormatter.C b/src/fileFormats/vtk/format/foamVtkAppendRawFormatter.C
index 9933970495e..ae565323b85 100644
--- a/src/fileFormats/vtk/format/foamVtkAppendRawFormatter.C
+++ b/src/fileFormats/vtk/format/foamVtkAppendRawFormatter.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2016 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -71,9 +71,9 @@ const char* Foam::foamVtkAppendRawFormatter::encoding() const
 }
 
 
-void Foam::foamVtkAppendRawFormatter::writeSize(const uint64_t val)
+void Foam::foamVtkAppendRawFormatter::writeSize(const uint64_t nBytes)
 {
-    write(reinterpret_cast<const char*>(&val), sizeof(uint64_t));
+    write(reinterpret_cast<const char*>(&nBytes), sizeof(uint64_t));
 }
 
 
@@ -85,28 +85,28 @@ void Foam::foamVtkAppendRawFormatter::write(const uint8_t val)
 
 void Foam::foamVtkAppendRawFormatter::write(const label val)
 {
-    // std::cerr<<"label is:" << sizeof(val) << '\n';
+    // std::cerr<<"label:" << sizeof(val) << "=" << val << '\n';
     write(reinterpret_cast<const char*>(&val), sizeof(label));
 }
 
 
 void Foam::foamVtkAppendRawFormatter::write(const float val)
 {
-    // std::cerr<<"float is:" << sizeof(val) << '\n';
+    // std::cerr<<"float:" << sizeof(val) << "=" << val << '\n';
     write(reinterpret_cast<const char*>(&val), sizeof(float));
 }
 
 
 void Foam::foamVtkAppendRawFormatter::write(const double val)
 {
-    // std::cerr<<"write double as float:" << val << '\n';
+    // std::cerr<<"double as float=" << val << '\n';
     float copy(val);
     write(copy);
 }
 
 
 void Foam::foamVtkAppendRawFormatter::flush()
-{}
+{/*nop*/}
 
 
 // ************************************************************************* //
diff --git a/src/fileFormats/vtk/format/foamVtkAppendRawFormatter.H b/src/fileFormats/vtk/format/foamVtkAppendRawFormatter.H
index abc3db09293..1dcb9b8d55c 100644
--- a/src/fileFormats/vtk/format/foamVtkAppendRawFormatter.H
+++ b/src/fileFormats/vtk/format/foamVtkAppendRawFormatter.H
@@ -77,7 +77,7 @@ public:
     // Constructors
 
         //- Construct and attach to an output stream
-        foamVtkAppendRawFormatter(std::ostream&);
+        foamVtkAppendRawFormatter(std::ostream& os);
 
 
     //- Destructor
@@ -94,12 +94,14 @@ public:
 
 
         //- Write leading size for binary output
-        virtual void writeSize(const uint64_t);
+        virtual void writeSize(const uint64_t nBytes);
 
-        virtual void write(const uint8_t);
-        virtual void write(const label);
-        virtual void write(const float);
-        virtual void write(const double);
+        virtual void write(const uint8_t val);
+        virtual void write(const label val);
+        virtual void write(const float val);
+        virtual void write(const double val);
+
+        //- A no-op for this format
         virtual void flush();
 
 };
diff --git a/src/fileFormats/vtk/format/foamVtkAsciiFormatter.C b/src/fileFormats/vtk/format/foamVtkAsciiFormatter.C
index 6a78f73f53c..9ed22db2330 100644
--- a/src/fileFormats/vtk/format/foamVtkAsciiFormatter.C
+++ b/src/fileFormats/vtk/format/foamVtkAsciiFormatter.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2016 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -47,6 +47,16 @@ inline void Foam::foamVtkAsciiFormatter::next()
 }
 
 
+inline void Foam::foamVtkAsciiFormatter::done()
+{
+    if (pos_)
+    {
+        os()<< '\n';
+    }
+    pos_ = 0;
+}
+
+
 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
 
 Foam::foamVtkAsciiFormatter::foamVtkAsciiFormatter(std::ostream& os)
@@ -73,7 +83,7 @@ Foam::foamVtkAsciiFormatter::foamVtkAsciiFormatter
 
 Foam::foamVtkAsciiFormatter::~foamVtkAsciiFormatter()
 {
-    flush();
+    done();
 }
 
 
@@ -91,7 +101,7 @@ const char* Foam::foamVtkAsciiFormatter::encoding() const
 }
 
 
-void Foam::foamVtkAsciiFormatter::writeSize(const uint64_t)
+void Foam::foamVtkAsciiFormatter::writeSize(const uint64_t ignored)
 {/*nop*/}
 
 
@@ -125,11 +135,14 @@ void Foam::foamVtkAsciiFormatter::write(const double val)
 
 void Foam::foamVtkAsciiFormatter::flush()
 {
-    if (pos_)
-    {
-        os()<< '\n';
-    }
-    pos_ = 0;
+    done();
+}
+
+
+std::size_t
+Foam::foamVtkAsciiFormatter::encodedLength(std::size_t ignored) const
+{
+    return 0;
 }
 
 
diff --git a/src/fileFormats/vtk/format/foamVtkAsciiFormatter.H b/src/fileFormats/vtk/format/foamVtkAsciiFormatter.H
index 009d776f89f..8b8e06b2a22 100644
--- a/src/fileFormats/vtk/format/foamVtkAsciiFormatter.H
+++ b/src/fileFormats/vtk/format/foamVtkAsciiFormatter.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2016 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -62,9 +62,12 @@ class foamVtkAsciiFormatter
 
     // Private Member Functions
 
-        //- Advance to next position, adding space or newline as required
+        //- Advance to next position, adding space or newline as needed
         inline void next();
 
+        //- Finish an output line, adding newline as needed
+        inline void done();
+
 
         //- Disallow default bitwise copy construct
         foamVtkAsciiFormatter(const foamVtkAsciiFormatter&) = delete;
@@ -78,10 +81,10 @@ public:
     // Constructors
 
         //- Construct and attach to an output stream, use default precision
-        foamVtkAsciiFormatter(std::ostream&);
+        foamVtkAsciiFormatter(std::ostream& os);
 
         //- Construct and attach to an output stream, use specified precision
-        foamVtkAsciiFormatter(std::ostream&, unsigned precision);
+        foamVtkAsciiFormatter(std::ostream& os, unsigned precision);
 
 
     //- Destructor
@@ -95,18 +98,24 @@ public:
         virtual const char* name() const;
 
         //- Name for the XML append encoding - unused.
-        //  Currently simply "ASCII", but this should not be relied upon.
+        //  Currently identical to name(), but do not rely on this.
         virtual const char* encoding() const;
 
 
         //- Write leading size - this is a no-op for ascii output
-        virtual void writeSize(const uint64_t);
+        virtual void writeSize(const uint64_t ignored);
+
+        virtual void write(const uint8_t val);
+        virtual void write(const label val);
+        virtual void write(const float val);
+        virtual void write(const double val);
 
-        virtual void write(const uint8_t);
-        virtual void write(const label);
-        virtual void write(const float);
-        virtual void write(const double);
+        //- Write a newline if needed to finish a line of output.
         virtual void flush();
+
+        //- The encoded length for ascii output is not applicable.
+        virtual std::size_t encodedLength(std::size_t ignored) const;
+
 };
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
diff --git a/src/fileFormats/vtk/format/foamVtkBase64Formatter.C b/src/fileFormats/vtk/format/foamVtkBase64Formatter.C
index a11d97bd49e..f87efbb19f4 100644
--- a/src/fileFormats/vtk/format/foamVtkBase64Formatter.C
+++ b/src/fileFormats/vtk/format/foamVtkBase64Formatter.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2016 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -27,28 +27,14 @@ License
 
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
 
-const char* Foam::foamVtkBase64Formatter::name_     = "binary";
-const char* Foam::foamVtkBase64Formatter::encoding_ = "base64";
-
-
-// * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
-
-void Foam::foamVtkBase64Formatter::write
-(
-    const char* s,
-    std::streamsize n
-)
-{
-    base64Layer::write(s, n);
-}
+const char* Foam::foamVtkBase64Formatter::name_ = "binary";
 
 
 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
 
 Foam::foamVtkBase64Formatter::foamVtkBase64Formatter(std::ostream& os)
 :
-    foamVtkFormatter(os),
-    base64Layer(os)
+    foamVtkBase64Layer(os)
 {}
 
 
@@ -56,7 +42,10 @@ Foam::foamVtkBase64Formatter::foamVtkBase64Formatter(std::ostream& os)
 
 Foam::foamVtkBase64Formatter::~foamVtkBase64Formatter()
 {
-    flush();
+    if (base64Layer::close())
+    {
+        os().put('\n');
+    }
 }
 
 
@@ -68,46 +57,6 @@ const char* Foam::foamVtkBase64Formatter::name() const
 }
 
 
-const char* Foam::foamVtkBase64Formatter::encoding() const
-{
-    return encoding_;
-}
-
-
-void Foam::foamVtkBase64Formatter::writeSize(const uint64_t val)
-{
-    write(reinterpret_cast<const char*>(&val), sizeof(uint64_t));
-}
-
-
-void Foam::foamVtkBase64Formatter::write(const uint8_t val)
-{
-    base64Layer::add(val);
-}
-
-
-void Foam::foamVtkBase64Formatter::write(const label val)
-{
-    // std::cerr<<"label is:" << sizeof(val) << '\n';
-    write(reinterpret_cast<const char*>(&val), sizeof(label));
-}
-
-
-void Foam::foamVtkBase64Formatter::write(const float val)
-{
-    // std::cerr<<"float is:" << sizeof(val) << '\n';
-    write(reinterpret_cast<const char*>(&val), sizeof(float));
-}
-
-
-void Foam::foamVtkBase64Formatter::write(const double val)
-{
-    // std::cerr<<"write double as float:" << val << '\n';
-    float copy(val);
-    write(copy);
-}
-
-
 void Foam::foamVtkBase64Formatter::flush()
 {
     if (base64Layer::close())
diff --git a/src/fileFormats/vtk/format/foamVtkBase64Formatter.H b/src/fileFormats/vtk/format/foamVtkBase64Formatter.H
index f01bec3d9aa..e92fa4f9e37 100644
--- a/src/fileFormats/vtk/format/foamVtkBase64Formatter.H
+++ b/src/fileFormats/vtk/format/foamVtkBase64Formatter.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2016 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -33,8 +33,7 @@ Description
 #ifndef foamVtkBase64Formatter_H
 #define foamVtkBase64Formatter_H
 
-#include "foamVtkFormatter.H"
-#include "base64Layer.H"
+#include "foamVtkBase64Layer.H"
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
@@ -47,8 +46,7 @@ namespace Foam
 
 class foamVtkBase64Formatter
 :
-    public foamVtkFormatter,
-    private base64Layer
+    public foamVtkBase64Layer
 {
     // Private Data Members
 
@@ -64,20 +62,12 @@ class foamVtkBase64Formatter
         //- Disallow default bitwise assignment
         void operator=(const foamVtkBase64Formatter&) = delete;
 
-protected:
-
-    // Protected Member Functions
-
-        //- Write
-        void write(const char* s, std::streamsize n);
-
-
 public:
 
     // Constructors
 
         //- Construct and attach to an output stream
-        foamVtkBase64Formatter(std::ostream&);
+        foamVtkBase64Formatter(std::ostream& os);
 
 
     //- Destructor
@@ -90,17 +80,9 @@ public:
         //  The lowercase version of the Legacy output type.
         virtual const char* name() const;
 
-        //- Name for the XML append encoding.
-        virtual const char* encoding() const;
-
-
-        //- Write leading size for binary output
-        virtual void writeSize(const uint64_t);
 
-        virtual void write(const uint8_t);
-        virtual void write(const label);
-        virtual void write(const float);
-        virtual void write(const double);
+        //- End the encoding sequence (padding the final characters with '=')
+        //  and write a newline to the output if any encoding was done.
         virtual void flush();
 
 };
diff --git a/src/fileFormats/vtk/format/foamVtkBase64Layer.C b/src/fileFormats/vtk/format/foamVtkBase64Layer.C
new file mode 100644
index 00000000000..d66970d522b
--- /dev/null
+++ b/src/fileFormats/vtk/format/foamVtkBase64Layer.C
@@ -0,0 +1,116 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2017 OpenCFD Ltd.
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+License
+    This file is part of OpenFOAM.
+
+    OpenFOAM is free software: you can redistribute it and/or modify it
+    under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
+    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+    for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
+
+\*---------------------------------------------------------------------------*/
+
+#include "foamVtkBase64Layer.H"
+
+// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
+
+const char* Foam::foamVtkBase64Layer::encoding_ = "base64";
+
+
+// * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
+
+void Foam::foamVtkBase64Layer::write
+(
+    const char* s,
+    std::streamsize n
+)
+{
+    base64Layer::write(s, n);
+}
+
+
+// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
+
+Foam::foamVtkBase64Layer::foamVtkBase64Layer(std::ostream& os)
+:
+    foamVtkFormatter(os),
+    base64Layer(os)
+{}
+
+
+// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
+
+Foam::foamVtkBase64Layer::~foamVtkBase64Layer()
+{
+    base64Layer::close();
+}
+
+
+// * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
+
+const char* Foam::foamVtkBase64Layer::encoding() const
+{
+    return encoding_;
+}
+
+
+void Foam::foamVtkBase64Layer::writeSize(const uint64_t nBytes)
+{
+    write(reinterpret_cast<const char*>(&nBytes), sizeof(uint64_t));
+}
+
+
+void Foam::foamVtkBase64Layer::write(const uint8_t val)
+{
+    base64Layer::add(val);
+}
+
+
+void Foam::foamVtkBase64Layer::write(const label val)
+{
+    // std::cerr<<"label:" << sizeof(val) << "=" << val << '\n';
+    write(reinterpret_cast<const char*>(&val), sizeof(label));
+}
+
+
+void Foam::foamVtkBase64Layer::write(const float val)
+{
+    // std::cerr<<"float:" << sizeof(val) << "=" << val << '\n';
+    write(reinterpret_cast<const char*>(&val), sizeof(float));
+}
+
+
+void Foam::foamVtkBase64Layer::write(const double val)
+{
+    // std::cerr<<"double as float=" << val << '\n';
+    float copy(val);
+    write(copy);
+}
+
+
+void Foam::foamVtkBase64Layer::flush()
+{
+    base64Layer::close();
+}
+
+
+std::size_t Foam::foamVtkBase64Layer::encodedLength(std::size_t n) const
+{
+    return base64Layer::encodedLength(n);
+}
+
+
+// ************************************************************************* //
diff --git a/src/fileFormats/vtk/format/foamVtkBase64Layer.H b/src/fileFormats/vtk/format/foamVtkBase64Layer.H
new file mode 100644
index 00000000000..61527a11bc0
--- /dev/null
+++ b/src/fileFormats/vtk/format/foamVtkBase64Layer.H
@@ -0,0 +1,114 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2017 OpenCFD Ltd.
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+License
+    This file is part of OpenFOAM.
+
+    OpenFOAM is free software: you can redistribute it and/or modify it
+    under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
+    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+    for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
+
+Class
+    foamVtkBase64Layer
+
+Description
+    Base-64 encoded output.
+
+\*---------------------------------------------------------------------------*/
+
+#ifndef foamVtkBase64Layer_H
+#define foamVtkBase64Layer_H
+
+#include "foamVtkFormatter.H"
+#include "base64Layer.H"
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+namespace Foam
+{
+
+/*---------------------------------------------------------------------------*\
+                   Class foamVtkBase64Layer Declaration
+\*---------------------------------------------------------------------------*/
+
+class foamVtkBase64Layer
+:
+    public foamVtkFormatter,
+    protected base64Layer
+{
+    // Private Data Members
+
+        static const char* encoding_;
+
+
+    // Private Member Functions
+
+        //- Disallow default bitwise copy construct
+        foamVtkBase64Layer(const foamVtkBase64Layer&) = delete;
+
+        //- Disallow default bitwise assignment
+        void operator=(const foamVtkBase64Layer&) = delete;
+
+protected:
+
+    // Protected Member Functions
+
+        //- Write
+        void write(const char* s, std::streamsize n);
+
+
+    // Constructors
+
+        //- Construct and attach to an output stream
+        foamVtkBase64Layer(std::ostream& os);
+
+public:
+
+    //- Destructor
+    virtual ~foamVtkBase64Layer();
+
+
+    // Member Functions
+
+        //- Name for the XML append encoding ("base64").
+        virtual const char* encoding() const;
+
+
+        //- Write leading size for binary output
+        virtual void writeSize(const uint64_t nBytes);
+
+        virtual void write(const uint8_t val);
+        virtual void write(const label val);
+        virtual void write(const float val);
+        virtual void write(const double val);
+
+        //- End the encoding sequence (padding the final characters with '=')
+        virtual void flush();
+
+        //- The encoded length for base64 encoded output.
+        virtual std::size_t encodedLength(std::size_t n) const;
+
+};
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+} // End namespace Foam
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+#endif
+
+// ************************************************************************* //
diff --git a/src/fileFormats/vtk/format/foamVtkFormatter.C b/src/fileFormats/vtk/format/foamVtkFormatter.C
index 7271dd22f5e..2bc607a78a2 100644
--- a/src/fileFormats/vtk/format/foamVtkFormatter.C
+++ b/src/fileFormats/vtk/format/foamVtkFormatter.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2016 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -52,6 +52,12 @@ Foam::foamVtkFormatter::~foamVtkFormatter()
 
 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
 
+std::size_t Foam::foamVtkFormatter::encodedLength(std::size_t n) const
+{
+    return n;
+}
+
+
 void Foam::foamVtkFormatter::indent()
 {
     label n = xmlTags_.size() * 2;
@@ -149,7 +155,6 @@ Foam::foamVtkFormatter::tag(const word& tag)
 }
 
 
-
 Foam::foamVtkFormatter&
 Foam::foamVtkFormatter::endTag(const word& tag)
 {
@@ -181,7 +186,6 @@ Foam::foamVtkFormatter::endTag(const word& tag)
 }
 
 
-
 Foam::foamVtkFormatter&
 Foam::foamVtkFormatter::xmlAttr
 (
diff --git a/src/fileFormats/vtk/format/foamVtkFormatter.H b/src/fileFormats/vtk/format/foamVtkFormatter.H
index 65519b1955a..5ab0776d606 100644
--- a/src/fileFormats/vtk/format/foamVtkFormatter.H
+++ b/src/fileFormats/vtk/format/foamVtkFormatter.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2016 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -74,8 +74,8 @@ class foamVtkFormatter
         template<class Type>
         foamVtkFormatter& xmlAttribute
         (
-            const word&,
-            const Type&,
+            const word& k,
+            const Type& v,
             const char quote
         );
 
@@ -120,14 +120,20 @@ public:
 
 
         //- Write leading size for binary output
-        virtual void writeSize(const uint64_t) = 0;
+        virtual void writeSize(const uint64_t nBytes) = 0;
 
-        virtual void write(const uint8_t) = 0;
-        virtual void write(const label)   = 0;
-        virtual void write(const float)   = 0;
-        virtual void write(const double)  = 0;
+        virtual void write(const uint8_t val) = 0;
+        virtual void write(const label val)   = 0;
+        virtual void write(const float val)   = 0;
+        virtual void write(const double val)  = 0;
+
+        //- Flush encoding, write newline etc.
         virtual void flush() = 0;
 
+        //- The encoded length for binary output.
+        //  The default is pass-through.
+        virtual std::size_t encodedLength(std::size_t n) const;
+
 
     // Member Functions
 
@@ -138,7 +144,7 @@ public:
         foamVtkFormatter& xmlHeader();
 
         //- Write XML comment (at the current indentation level)
-        foamVtkFormatter& comment(const std::string&);
+        foamVtkFormatter& comment(const std::string& text);
 
 
         //- Open XML tag
@@ -177,40 +183,40 @@ public:
         //- Write XML attribute
         foamVtkFormatter& xmlAttr
         (
-            const word&,
-            const std::string&,
+            const word& k,
+            const std::string& v,
             const char quote = '\''
         );
 
         //- Write XML attribute
         foamVtkFormatter& xmlAttr
         (
-            const word&,
-            const int32_t,
+            const word& k,
+            const int32_t v,
             const char quote = '\''
         );
 
         //- Write XML attribute
         foamVtkFormatter& xmlAttr
         (
-            const word&,
-            const int64_t,
+            const word& k,
+            const int64_t v,
             const char quote = '\''
         );
 
         //- Write XML attribute
         foamVtkFormatter& xmlAttr
         (
-            const word&,
-            const uint64_t,
+            const word& k,
+            const uint64_t v,
             const char quote = '\''
         );
 
         //- Write XML attribute
         foamVtkFormatter& xmlAttr
         (
-            const word&,
-            const scalar,
+            const word& k,
+            const scalar v,
             const char quote = '\''
         );
 
@@ -219,19 +225,19 @@ public:
     // Member Operators
 
         //- Write XML attribute
-        foamVtkFormatter& operator()(const word&, const std::string&);
+        foamVtkFormatter& operator()(const word& k, const std::string& v);
 
         //- Write XML attribute
-        foamVtkFormatter& operator()(const word&, const int32_t);
+        foamVtkFormatter& operator()(const word& k, const int32_t v);
 
         //- Write XML attribute
-        foamVtkFormatter& operator()(const word&, const int64_t);
+        foamVtkFormatter& operator()(const word& k, const int64_t v);
 
         //- Write XML attribute
-        foamVtkFormatter& operator()(const word&, const uint64_t);
+        foamVtkFormatter& operator()(const word& k, const uint64_t v);
 
         //- Write XML attribute
-        foamVtkFormatter& operator()(const word&, const scalar);
+        foamVtkFormatter& operator()(const word& k, const scalar v);
 
 };
 
diff --git a/src/fileFormats/vtk/format/foamVtkLegacyFormatter.C b/src/fileFormats/vtk/format/foamVtkLegacyFormatter.C
index 89525ab0baa..a3e87264f82 100644
--- a/src/fileFormats/vtk/format/foamVtkLegacyFormatter.C
+++ b/src/fileFormats/vtk/format/foamVtkLegacyFormatter.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2016 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -71,8 +71,8 @@ const char* Foam::foamVtkLegacyFormatter::encoding() const
 }
 
 
-void Foam::foamVtkLegacyFormatter::writeSize(const uint64_t)
-{}
+void Foam::foamVtkLegacyFormatter::writeSize(const uint64_t ignored)
+{/*nop*/}
 
 
 void Foam::foamVtkLegacyFormatter::write(const uint8_t val)
diff --git a/src/fileFormats/vtk/format/foamVtkLegacyFormatter.H b/src/fileFormats/vtk/format/foamVtkLegacyFormatter.H
index fe2395add4d..6ec5ba7e8dd 100644
--- a/src/fileFormats/vtk/format/foamVtkLegacyFormatter.H
+++ b/src/fileFormats/vtk/format/foamVtkLegacyFormatter.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2016 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -25,11 +25,10 @@ Class
     foamVtkLegacyFormatter
 
 Description
-    Binary output for the VTK legacy format, always written as big-endian.
+    Binary output for the VTK legacy format, always written as big-endian
+    and with 32-bit integers.
 
-    The legacy files are always written as big endian.
-    Since integers in the legacy format are limited to 32-bit,
-    this format should not be used for OpenFOAM with 64-bit label sizes.
+    This format should never be used for OpenFOAM with 64-bit label sizes.
 
 SourceFiles
     foamVtkLegacyFormatter.C
@@ -81,7 +80,7 @@ public:
     // Constructors
 
         //- Construct and attach to an output stream
-        foamVtkLegacyFormatter(std::ostream&);
+        foamVtkLegacyFormatter(std::ostream& os);
 
 
     //- Destructor
@@ -90,22 +89,25 @@ public:
 
     // Member Functions
 
-        //- Name for the Legacy output type ("BINARY")
+        //- Name for the legacy binary output type ("BINARY")
         virtual const char* name() const;
 
         //- Name for the XML append encoding (unused)
-        //  Currently simply "BINARY", but this should not be relied upon.
+        //  Currently identical to name(), but do not rely on this.
         virtual const char* encoding() const;
 
 
         //- Write leading size - a no-op for legacy binary output
-        virtual void writeSize(const uint64_t);
+        virtual void writeSize(const uint64_t ignored);
 
-        virtual void write(const uint8_t);
-        virtual void write(const label);
-        virtual void write(const float);
-        virtual void write(const double);
+        virtual void write(const uint8_t val);
+        virtual void write(const label val);
+        virtual void write(const float val);
+        virtual void write(const double val);
+
+        //- Write a newline to the output
         virtual void flush();
+
 };
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
diff --git a/src/fileFormats/vtk/format/foamVtkOutputOptions.C b/src/fileFormats/vtk/format/foamVtkOutputOptions.C
index 39f11a432e8..c88d334594d 100644
--- a/src/fileFormats/vtk/format/foamVtkOutputOptions.C
+++ b/src/fileFormats/vtk/format/foamVtkOutputOptions.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2016 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -45,10 +45,8 @@ Foam::foamVtkOutputOptions::foamVtkOutputOptions()
 
 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
 
-Foam::autoPtr<Foam::foamVtkFormatter> Foam::foamVtkOutputOptions::newFormatter
-(
-    std::ostream& os
-) const
+Foam::autoPtr<Foam::foamVtkFormatter>
+Foam::foamVtkOutputOptions::newFormatter(std::ostream& os) const
 {
     switch (type_)
     {
@@ -87,9 +85,9 @@ Foam::autoPtr<Foam::foamVtkFormatter> Foam::foamVtkOutputOptions::newFormatter
 
 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
 
-void Foam::foamVtkOutputOptions::ascii(bool b)
+void Foam::foamVtkOutputOptions::ascii(bool on)
 {
-    if (b)
+    if (on)
     {
         // Force ASCII:
 
@@ -132,9 +130,9 @@ void Foam::foamVtkOutputOptions::ascii(bool b)
 }
 
 
-void Foam::foamVtkOutputOptions::append(bool b)
+void Foam::foamVtkOutputOptions::append(bool on)
 {
-    if (b)
+    if (on)
     {
         if (!(type_ & APPEND))
         {
@@ -153,9 +151,9 @@ void Foam::foamVtkOutputOptions::append(bool b)
 }
 
 
-void Foam::foamVtkOutputOptions::legacy(bool b)
+void Foam::foamVtkOutputOptions::legacy(bool on)
 {
-    if (b)
+    if (on)
     {
         if (type_ & APPEND)
         {
@@ -180,9 +178,9 @@ void Foam::foamVtkOutputOptions::legacy(bool b)
 }
 
 
-void Foam::foamVtkOutputOptions::precision(unsigned val) const
+void Foam::foamVtkOutputOptions::precision(unsigned prec) const
 {
-    precision_ = val;
+    precision_ = prec;
 }
 
 
diff --git a/src/fileFormats/vtk/format/foamVtkOutputOptions.H b/src/fileFormats/vtk/format/foamVtkOutputOptions.H
index a5f59de5144..7e0e89b24be 100644
--- a/src/fileFormats/vtk/format/foamVtkOutputOptions.H
+++ b/src/fileFormats/vtk/format/foamVtkOutputOptions.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2016 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2016-2017 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -88,7 +88,7 @@ public:
     // Selectors
 
         //- Return new data formatter based on the writer options
-        autoPtr<foamVtkFormatter> newFormatter(std::ostream&) const;
+        autoPtr<foamVtkFormatter> newFormatter(std::ostream& os) const;
 
 
     // Member Functions
@@ -117,16 +117,16 @@ public:
         //  In append mode, this switches between base64 and raw binary.
         //  In XML mode, this switches between ASCII and base64.
         //  In legacy mode, this switches between ASCII and binary.
-        void ascii(bool);
+        void ascii(bool on);
 
         //- Toggle append mode on/off.
-        void append(bool);
+        void append(bool on);
 
         //- Toggle legacy mode on/off.
-        void legacy(bool);
+        void legacy(bool on);
 
         //- Set the write precision to be used for new ASCII formatters
-        void precision(unsigned val) const;
+        void precision(unsigned prec) const;
 
 
     // Other
-- 
GitLab