From 5bdcece3af51113232f2602703da625816c143d2 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Mon, 17 May 2010 12:56:30 +0200
Subject: [PATCH] ENH: allow retention of 'FoamFile' when reading dictionaries

- used in expandDictionary
---
 .../expandDictionary/expandDictionary.C       |  11 +-
 src/OpenFOAM/db/dictionary/dictionary.H       |  11 +-
 src/OpenFOAM/db/dictionary/dictionaryIO.C     | 111 +++++++++++-------
 3 files changed, 85 insertions(+), 48 deletions(-)

diff --git a/applications/utilities/miscellaneous/expandDictionary/expandDictionary.C b/applications/utilities/miscellaneous/expandDictionary/expandDictionary.C
index 08e8f29f24d..86b1caec440 100644
--- a/applications/utilities/miscellaneous/expandDictionary/expandDictionary.C
+++ b/applications/utilities/miscellaneous/expandDictionary/expandDictionary.C
@@ -42,6 +42,12 @@ using namespace Foam;
 
 int main(int argc, char *argv[])
 {
+    argList::addNote
+    (
+        "Read the specified dictionary file, expand the macros etc. and write\n"
+        "the resulting dictionary to standard output."
+    );
+
     argList::noBanner();
     argList::noParallel();
     argList::validArgs.append("inputDict");
@@ -49,9 +55,10 @@ int main(int argc, char *argv[])
 
     const string dictName = args[1];
 
-    Info<<"//\n// expansion of dictionary " << dictName << "\n//\n";
+    IOobject::writeBanner(Info)
+        <<"//\n// " << dictName << "\n//\n";
 
-    dictionary(IFstream(dictName)()).write(Info, false);
+    dictionary(IFstream(dictName)(), true).write(Info, false);
 
     IOobject::writeDivider(Info);
 
diff --git a/src/OpenFOAM/db/dictionary/dictionary.H b/src/OpenFOAM/db/dictionary/dictionary.H
index c483aed5570..3c3d5043539 100644
--- a/src/OpenFOAM/db/dictionary/dictionary.H
+++ b/src/OpenFOAM/db/dictionary/dictionary.H
@@ -191,10 +191,14 @@ public:
             Istream&
         );
 
-        //- Construct top-level dictionary from Istream, reading entries
-        //  until EOF
+        //- Construct top-level dictionary from Istream,
+        //  reading entries until EOF
         dictionary(Istream&);
 
+        //- Construct top-level dictionary from Istream,
+        //  reading entries until EOF, optionally keeping the header
+        dictionary(Istream&, const bool keepHeader);
+
         //- Construct as copy given the parent dictionary
         dictionary(const dictionary& parentDict, const dictionary&);
 
@@ -441,6 +445,9 @@ public:
             //- Read dictionary from Istream
             bool read(Istream&);
 
+            //- Read dictionary from Istream, optionally keeping the header
+            bool read(Istream&, const bool keepHeader);
+
 
         // Write
 
diff --git a/src/OpenFOAM/db/dictionary/dictionaryIO.C b/src/OpenFOAM/db/dictionary/dictionaryIO.C
index 1c452bafaf3..54d2877da2a 100644
--- a/src/OpenFOAM/db/dictionary/dictionaryIO.C
+++ b/src/OpenFOAM/db/dictionary/dictionaryIO.C
@@ -28,13 +28,61 @@ License
 #include "inputModeEntry.H"
 #include "regExp.H"
 
-// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
+// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
 
-bool Foam::dictionary::read(Istream& is)
+Foam::dictionary::dictionary
+(
+    const fileName& name,
+    const dictionary& parentDict,
+    Istream& is
+)
+:
+    dictionaryName(parentDict.name() + "::" + name),
+    parent_(parentDict)
+{
+    read(is);
+}
+
+
+Foam::dictionary::dictionary(Istream& is)
+:
+    dictionaryName(is.name()),
+    parent_(dictionary::null)
+{
+    // Reset input mode as this is a "top-level" dictionary
+    functionEntries::inputModeEntry::clear();
+
+    read(is);
+}
+
+
+Foam::dictionary::dictionary(Istream& is, const bool keepHeader)
+:
+    dictionaryName(is.name()),
+    parent_(dictionary::null)
+{
+    // Reset input mode as this is a "top-level" dictionary
+    functionEntries::inputModeEntry::clear();
+
+    read(is, keepHeader);
+}
+
+
+// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
+
+Foam::autoPtr<Foam::dictionary> Foam::dictionary::New(Istream& is)
+{
+    return autoPtr<dictionary>(new dictionary(is));
+}
+
+
+// * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
+
+bool Foam::dictionary::read(Istream& is, const bool keepHeader)
 {
     if (!is.good())
     {
-        FatalIOErrorIn("dictionary::read(Istream&, const word&)", is)
+        FatalIOErrorIn("dictionary::read(Istream&, bool)", is)
             << "Istream not OK for reading dictionary "
             << exit(FatalIOError);
 
@@ -50,12 +98,15 @@ bool Foam::dictionary::read(Istream& is)
     while (!is.eof() && entry::New(*this, is))
     {}
 
-    // Remove the FoamFile header entry if it exists
-    remove("FoamFile");
+    // normally remove the FoamFile header entry if it exists
+    if (!keepHeader)
+    {
+        remove("FoamFile");
+    }
 
     if (is.bad())
     {
-        Info<< "dictionary::read(Istream&, const word&) : "
+        Info<< "dictionary::read(Istream&, bool) : "
             << "Istream not OK after reading dictionary " << name()
             << endl;
 
@@ -66,6 +117,12 @@ bool Foam::dictionary::read(Istream& is)
 }
 
 
+bool Foam::dictionary::read(Istream& is)
+{
+    return this->read(is, false);
+}
+
+
 bool Foam::dictionary::substituteKeyword(const word& keyword)
 {
     word varName = keyword(1, keyword.size()-1);
@@ -90,40 +147,6 @@ bool Foam::dictionary::substituteKeyword(const word& keyword)
 }
 
 
-// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
-
-Foam::dictionary::dictionary
-(
-    const fileName& name,
-    const dictionary& parentDict,
-    Istream& is
-)
-:
-    dictionaryName(parentDict.name() + "::" + name),
-    parent_(parentDict)
-{
-    read(is);
-}
-
-
-Foam::dictionary::dictionary(Istream& is)
-:
-    dictionaryName(is.name()),
-    parent_(dictionary::null)
-{
-    // Reset input mode as this is a "top-level" dictionary
-    functionEntries::inputModeEntry::clear();
-
-    read(is);
-}
-
-
-Foam::autoPtr<Foam::dictionary> Foam::dictionary::New(Istream& is)
-{
-    return autoPtr<dictionary>(new dictionary(is));
-}
-
-
 // * * * * * * * * * * * * * * Istream Operator  * * * * * * * * * * * * * * //
 
 Foam::Istream& Foam::operator>>(Istream& is, dictionary& dict)
@@ -145,7 +168,7 @@ void Foam::dictionary::write(Ostream& os, bool subDict) const
 {
     if (subDict)
     {
-        os << nl << indent << token::BEGIN_BLOCK << incrIndent << nl;
+        os  << nl << indent << token::BEGIN_BLOCK << incrIndent << nl;
     }
 
     forAllConstIter(IDLList<entry>, *this, iter)
@@ -153,12 +176,12 @@ void Foam::dictionary::write(Ostream& os, bool subDict) const
         const entry& e = *iter;
 
         // Write entry
-        os << e;
+        os  << e;
 
         // Add extra new line between entries for "top-level" dictionaries
         if (!subDict && parent() == dictionary::null && e != *last())
         {
-            os << nl;
+            os  << nl;
         }
 
         // Check stream before going to next entry.
@@ -173,7 +196,7 @@ void Foam::dictionary::write(Ostream& os, bool subDict) const
 
     if (subDict)
     {
-        os << decrIndent << indent << token::END_BLOCK << endl;
+        os  << decrIndent << indent << token::END_BLOCK << endl;
     }
 }
 
-- 
GitLab