From 69fde6b831e9fc70173cfba0a56d9de84958182e Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Fri, 15 Apr 2016 11:04:13 +0200
Subject: [PATCH] ENH: provide Ostream virtual methods beginBlock, endBlock
 (fixes #112)

- eases creation of dictionary-like output.

- make keyWord() method virtual to allow adaptation for alternative
  outputs
---
 src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C | 35 +++++++++++++++++--
 src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H | 25 ++++++++++---
 src/OpenFOAM/db/dictionary/dictionaryIO.C     |  7 ++--
 .../GeometricField/GeometricBoundaryField.C   | 11 +++---
 .../polyBoundaryMesh/polyBoundaryMesh.C       |  7 ++--
 .../meshes/primitiveShapes/plane/plane.C      | 17 +++++----
 .../primitives/functions/Function1/CSV/CSV.C  | 19 +++++-----
 .../functions/Function1/Sine/Sine.C           | 10 +++---
 .../functions/Function1/Square/Square.C       | 10 +++---
 .../functions/Function1/TableFile/TableFile.C | 11 +++---
 10 files changed, 103 insertions(+), 49 deletions(-)

diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C
index ab95c8c19f5..4131da7af45 100644
--- a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C
+++ b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -40,7 +40,7 @@ void Foam::Ostream::decrIndent()
     }
     else
     {
-        indentLevel_--;
+        --indentLevel_;
     }
 }
 
@@ -79,4 +79,35 @@ Foam::Ostream& Foam::Ostream::writeKeyword(const keyType& kw)
 }
 
 
+Foam::Ostream& Foam::Ostream::beginBlock(const word& keyword)
+{
+    indent();
+    write(keyword);
+    endl();
+    beginBlock();
+
+    return *this;
+}
+
+
+Foam::Ostream& Foam::Ostream::beginBlock()
+{
+    indent();
+    write(char(token::BEGIN_BLOCK));
+    incrIndent();
+
+    return *this;
+}
+
+
+Foam::Ostream& Foam::Ostream::endBlock()
+{
+    decrIndent();
+    indent();
+    write(char(token::END_BLOCK));
+
+    return *this;
+}
+
+
 // ************************************************************************* //
diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H
index aada0b4f9a1..0bd02a5b553 100644
--- a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H
+++ b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -109,8 +109,8 @@ public:
             virtual Ostream& write(const word&) = 0;
 
             //- Write keyType
-            //  write regular expression as quoted string
-            //  write plain word as word (unquoted)
+            //  A plain word is written unquoted.
+            //  A regular expression is written as a quoted string.
             virtual Ostream& write(const keyType&);
 
             //- Write string
@@ -157,14 +157,29 @@ public:
             //- Incrememt the indent level
             void incrIndent()
             {
-                indentLevel_++;
+                ++indentLevel_;
             }
 
             //- Decrememt the indent level
             void decrIndent();
 
             //- Write the keyword followed by an appropriate indentation
-            Ostream& writeKeyword(const keyType&);
+            virtual Ostream& writeKeyword(const keyType&);
+
+            //- Write begin block group with the given name
+            //  Uses the appropriate indentation,
+            //  does not include a trailing newline.
+            virtual Ostream& beginBlock(const word&);
+
+            //- Write begin block group without a name
+            //  Uses the appropriate indentation,
+            //  does not include a trailing newline.
+            virtual Ostream& beginBlock();
+
+            //- Write end block group
+            //  Uses the appropriate indentation,
+            //  does not include a trailing newline.
+            virtual Ostream& endBlock();
 
 
         // Stream state functions
diff --git a/src/OpenFOAM/db/dictionary/dictionaryIO.C b/src/OpenFOAM/db/dictionary/dictionaryIO.C
index f27d326d8c6..3cef23692f1 100644
--- a/src/OpenFOAM/db/dictionary/dictionaryIO.C
+++ b/src/OpenFOAM/db/dictionary/dictionaryIO.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -174,7 +174,8 @@ void Foam::dictionary::write(Ostream& os, bool subDict) const
 {
     if (subDict)
     {
-        os  << nl << indent << token::BEGIN_BLOCK << incrIndent << nl;
+        os  << nl;
+        os.beginBlock() << nl;
     }
 
     forAllConstIter(IDLList<entry>, *this, iter)
@@ -202,7 +203,7 @@ void Foam::dictionary::write(Ostream& os, bool subDict) const
 
     if (subDict)
     {
-        os  << decrIndent << indent << token::END_BLOCK << endl;
+        os.endBlock() << endl;
     }
 }
 
diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C
index 717f7d609f3..de5ec76d2fc 100644
--- a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C
+++ b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C
@@ -569,17 +569,16 @@ template<class Type, template<class> class PatchField, class GeoMesh>
 void Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricBoundaryField::
 writeEntry(const word& keyword, Ostream& os) const
 {
-    os  << keyword << nl << token::BEGIN_BLOCK << incrIndent << nl;
+    os.beginBlock(keyword) << nl;
 
     forAll(*this, patchi)
     {
-        os  << indent << this->operator[](patchi).patch().name() << nl
-            << indent << token::BEGIN_BLOCK << nl
-            << incrIndent << this->operator[](patchi) << decrIndent
-            << indent << token::END_BLOCK << endl;
+        os.beginBlock(this->operator[](patchi).patch().name()) << nl;
+        os  << this->operator[](patchi);
+        os.endBlock() << endl;
     }
 
-    os  << decrIndent << token::END_BLOCK << endl;
+    os.endBlock() << endl;
 
     // Check state of IOstream
     os.check
diff --git a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C
index e15618b552c..6b8403a454b 100644
--- a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C
+++ b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C
@@ -1116,10 +1116,9 @@ bool Foam::polyBoundaryMesh::writeData(Ostream& os) const
 
     forAll(patches, patchI)
     {
-        os  << indent << patches[patchI].name() << nl
-            << indent << token::BEGIN_BLOCK << nl
-            << incrIndent << patches[patchI] << decrIndent
-            << indent << token::END_BLOCK << endl;
+        os.beginBlock(patches[patchI].name()) << nl;
+        os  << patches[patchI];
+        os.endBlock() << endl;
     }
 
     os  << decrIndent << token::END_LIST;
diff --git a/src/OpenFOAM/meshes/primitiveShapes/plane/plane.C b/src/OpenFOAM/meshes/primitiveShapes/plane/plane.C
index 482142bb2ae..a6d05b7f736 100644
--- a/src/OpenFOAM/meshes/primitiveShapes/plane/plane.C
+++ b/src/OpenFOAM/meshes/primitiveShapes/plane/plane.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -437,12 +437,15 @@ void Foam::plane::writeDict(Ostream& os) const
 {
     os.writeKeyword("planeType") << "pointAndNormal"
         << token::END_STATEMENT << nl;
-    os  << indent << "pointAndNormalDict" << nl
-        << indent << token::BEGIN_BLOCK << incrIndent << nl;
-    os.writeKeyword("basePoint") << basePoint_ << token::END_STATEMENT << nl;
-    os.writeKeyword("normalVector") << unitVector_ << token::END_STATEMENT
-        << nl;
-    os << decrIndent << indent << token::END_BLOCK << endl;
+
+    os.beginBlock("pointAndNormalDict") << nl;
+
+    os.writeKeyword("basePoint") << basePoint_
+        << token::END_STATEMENT << nl;
+    os.writeKeyword("normalVector") << unitVector_
+        << token::END_STATEMENT << nl;
+
+    os.endBlock() << endl;
 }
 
 
diff --git a/src/OpenFOAM/primitives/functions/Function1/CSV/CSV.C b/src/OpenFOAM/primitives/functions/Function1/CSV/CSV.C
index 513298f8c76..a929f868ee5 100644
--- a/src/OpenFOAM/primitives/functions/Function1/CSV/CSV.C
+++ b/src/OpenFOAM/primitives/functions/Function1/CSV/CSV.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -264,16 +264,17 @@ void Foam::Function1Types::CSV<Type>::writeData(Ostream& os) const
 {
     Function1<Type>::writeData(os);
     os  << token::END_STATEMENT << nl;
-    os  << indent << word(this->name() + "Coeffs") << nl;
-    os  << indent << token::BEGIN_BLOCK << incrIndent << nl;
+
+    os.beginBlock(word(this->name() + "Coeffs")) << nl;
 
     // Note: for TableBase write the dictionary entries it needs but not
     // the values themselves
     TableBase<Type>::writeEntries(os);
 
-    os.writeKeyword("nHeaderLine") << nHeaderLine_ << token::END_STATEMENT
-        << nl;
-    os.writeKeyword("refColumn") << refColumn_ << token::END_STATEMENT << nl;
+    os.writeKeyword("nHeaderLine") << nHeaderLine_
+        << token::END_STATEMENT << nl;
+    os.writeKeyword("refColumn") << refColumn_
+        << token::END_STATEMENT << nl;
 
     // Force writing labelList in ascii
     os.writeKeyword("componentColumns");
@@ -293,8 +294,10 @@ void Foam::Function1Types::CSV<Type>::writeData(Ostream& os) const
         << token::END_STATEMENT << nl;
     os.writeKeyword("mergeSeparators") << mergeSeparators_
         << token::END_STATEMENT << nl;
-    os.writeKeyword("fileName") << fName_ << token::END_STATEMENT << nl;
-    os  << decrIndent << indent << token::END_BLOCK << endl;
+    os.writeKeyword("fileName") << fName_
+        << token::END_STATEMENT << nl;
+
+    os.endBlock() << endl;
 }
 
 
diff --git a/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.C b/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.C
index 60310966438..85a7aefe042 100644
--- a/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.C
+++ b/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -90,14 +90,16 @@ void Foam::Function1Types::Sine<Type>::writeData(Ostream& os) const
 {
     Function1<Type>::writeData(os);
     os  << token::END_STATEMENT << nl;
-    os  << indent << word(this->name() + "Coeffs") << nl;
-    os  << indent << token::BEGIN_BLOCK << incrIndent << nl;
+
+    os.beginBlock(word(this->name() + "Coeffs")) << nl;
+
     os.writeKeyword("t0") << t0_ << token::END_STATEMENT << nl;
     amplitude_->writeData(os);
     frequency_->writeData(os);
     scale_->writeData(os);
     level_->writeData(os);
-    os  << decrIndent << indent << token::END_BLOCK << endl;
+
+    os.endBlock() << endl;
 }
 
 
diff --git a/src/OpenFOAM/primitives/functions/Function1/Square/Square.C b/src/OpenFOAM/primitives/functions/Function1/Square/Square.C
index 213e7683213..9c55e8800e6 100644
--- a/src/OpenFOAM/primitives/functions/Function1/Square/Square.C
+++ b/src/OpenFOAM/primitives/functions/Function1/Square/Square.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -103,15 +103,17 @@ void Foam::Function1Types::Square<Type>::writeData(Ostream& os) const
 {
     Function1<Type>::writeData(os);
     os  << token::END_STATEMENT << nl;
-    os  << indent << word(this->name() + "Coeffs") << nl;
-    os  << indent << token::BEGIN_BLOCK << incrIndent << nl;
+
+    os.beginBlock(word(this->name() + "Coeffs")) << nl;
+
     os.writeKeyword("t0") << t0_ << token::END_STATEMENT << nl;
     os.writeKeyword("markSpace") << markSpace_ << token::END_STATEMENT << nl;
     amplitude_->writeData(os);
     frequency_->writeData(os);
     scale_->writeData(os);
     level_->writeData(os);
-    os  << decrIndent << indent << token::END_BLOCK << endl;
+
+    os.endBlock() << endl;
 }
 
 
diff --git a/src/OpenFOAM/primitives/functions/Function1/TableFile/TableFile.C b/src/OpenFOAM/primitives/functions/Function1/TableFile/TableFile.C
index 8551d1a84a6..414e6160772 100644
--- a/src/OpenFOAM/primitives/functions/Function1/TableFile/TableFile.C
+++ b/src/OpenFOAM/primitives/functions/Function1/TableFile/TableFile.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -78,17 +78,16 @@ template<class Type>
 void Foam::Function1Types::TableFile<Type>::writeData(Ostream& os) const
 {
     Function1<Type>::writeData(os);
+    os  << token::END_STATEMENT << nl;
 
-    os  << token::END_STATEMENT << nl
-        << indent << word(this->name() + "Coeffs") << nl
-        << indent << token::BEGIN_BLOCK << nl << incrIndent;
+    os.beginBlock(word(this->name() + "Coeffs")) << nl;
 
     // Note: for TableBase write the dictionary entries it needs but not
     // the values themselves
     TableBase<Type>::writeEntries(os);
-
     os.writeKeyword("fileName")<< fName_ << token::END_STATEMENT << nl;
-    os  << decrIndent << indent << token::END_BLOCK << endl;
+
+    os.endBlock() << endl;
 }
 
 
-- 
GitLab