From c20efb0923910709ecc0fe805113abfba3b9e6f6 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Fri, 15 Apr 2011 13:34:25 +0200
Subject: [PATCH] ENH: add 'report' to trace
 #includeEntry/#includeIfPresentEntry

- used in "expandDictionary -list" to find which files are included by
  any particular dictionary
---
 .../expandDictionary/expandDictionary.C       | 47 +++++++++++++++++--
 .../includeEntry/includeEntry.C               | 19 ++++++--
 .../includeEntry/includeEntry.H               |  6 +++
 .../includeIfPresentEntry.C                   | 16 +++++--
 4 files changed, 77 insertions(+), 11 deletions(-)

diff --git a/applications/utilities/miscellaneous/expandDictionary/expandDictionary.C b/applications/utilities/miscellaneous/expandDictionary/expandDictionary.C
index b28d9768322..c8beecc3616 100644
--- a/applications/utilities/miscellaneous/expandDictionary/expandDictionary.C
+++ b/applications/utilities/miscellaneous/expandDictionary/expandDictionary.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2004-2010 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2008-2011 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -28,12 +28,33 @@ Description
     Read the dictionary provided as an argument, expand the macros etc. and
     write the resulting dictionary to standard output.
 
+Usage
+    - expandDictionary inputDict [OPTION]
+
+    \param -list \n
+    Report the #include/#includeIfPresent to stdout only.
+
+Note
+    The \c -list option can be useful when determining which files
+    are actually included by a directory. It can also be used to
+    determine which files may need to be copied when transferring
+    simulation to another environment. The following code snippet
+    could be a useful basis for such cases:
+
+    \verbatim
+        for i in . 0 constant system
+        do
+            find $i -maxdepth 1 -type f -exec expandDictionary -list '{}' \;
+        done | sed -ne '/^"\//!{ s/^"//; s/"$//; p }' | sort | uniq
+    \endverbatim
+
 \*---------------------------------------------------------------------------*/
 
 #include "argList.H"
 #include "IFstream.H"
 #include "IOobject.H"
 #include "dictionary.H"
+#include "includeEntry.H"
 
 using namespace Foam;
 
@@ -48,6 +69,12 @@ int main(int argc, char *argv[])
         "the resulting dictionary to standard output."
     );
 
+    argList::addBoolOption
+    (
+        "list",
+        "Report the #include/#includeIfPresent to stdout only"
+    );
+
     argList::noBanner();
     argList::noParallel();
     argList::validArgs.append("inputDict");
@@ -55,12 +82,22 @@ int main(int argc, char *argv[])
 
     const string dictName = args[1];
 
-    IOobject::writeBanner(Info)
-        <<"//\n// " << dictName << "\n//\n";
+    const bool listOpt = args.optionFound("list");
+
+    if (listOpt)
+    {
+        Foam::functionEntries::includeEntry::report = true;
+    }
 
-    dictionary(IFstream(dictName)(), true).write(Info, false);
+    dictionary dict(IFstream(dictName)(), true);
 
-    IOobject::writeDivider(Info);
+    if (!listOpt)
+    {
+        IOobject::writeBanner(Info)
+            <<"//\n// " << dictName << "\n//\n";
+        dict.write(Info, false);
+        IOobject::writeDivider(Info);
+    }
 
     return 0;
 }
diff --git a/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.C
index ee20e33191c..c45ef4f2913 100644
--- a/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.C
+++ b/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2004-2010 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2004-2011 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -39,6 +39,9 @@ const Foam::word Foam::functionEntries::includeEntry::typeName
 // might include includeEntry
 int Foam::functionEntries::includeEntry::debug(0);
 
+bool Foam::functionEntries::includeEntry::report(false);
+
+
 namespace Foam
 {
 namespace functionEntries
@@ -89,10 +92,15 @@ bool Foam::functionEntries::includeEntry::execute
     Istream& is
 )
 {
-    IFstream ifs(includeFileName(is));
+    const fileName fName(includeFileName(is));
+    IFstream ifs(fName);
 
     if (ifs)
     {
+        if (Foam::functionEntries::includeEntry::report)
+        {
+            Info<< fName << endl;
+        }
         parentDict.read(ifs);
         return true;
     }
@@ -119,10 +127,15 @@ bool Foam::functionEntries::includeEntry::execute
     Istream& is
 )
 {
-    IFstream ifs(includeFileName(is));
+    const fileName fName(includeFileName(is));
+    IFstream ifs(fName);
 
     if (ifs)
     {
+        if (Foam::functionEntries::includeEntry::report)
+        {
+            Info<< fName << endl;
+        }
         entry.read(parentDict, ifs);
         return true;
     }
diff --git a/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.H b/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.H
index 5145b742ae6..be99bf588af 100644
--- a/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.H
+++ b/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.H
@@ -82,6 +82,12 @@ protected:
 
 public:
 
+    // Static data members
+
+        //- Report which file is included to stdout
+        static bool report;
+
+
     //- Runtime type information
     ClassName("include");
 
diff --git a/src/OpenFOAM/db/dictionary/functionEntries/includeIfPresentEntry/includeIfPresentEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/includeIfPresentEntry/includeIfPresentEntry.C
index 86f16181637..8e1bc9b7f11 100644
--- a/src/OpenFOAM/db/dictionary/functionEntries/includeIfPresentEntry/includeIfPresentEntry.C
+++ b/src/OpenFOAM/db/dictionary/functionEntries/includeIfPresentEntry/includeIfPresentEntry.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2004-2010 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2004-2011 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -69,10 +69,15 @@ bool Foam::functionEntries::includeIfPresentEntry::execute
     Istream& is
 )
 {
-    IFstream ifs(includeFileName(is));
+    const fileName fName(includeFileName(is));
+    IFstream ifs(fName);
 
     if (ifs)
     {
+        if (Foam::functionEntries::includeEntry::report)
+        {
+            Info<< fName << endl;
+        }
         parentDict.read(ifs);
     }
 
@@ -87,10 +92,15 @@ bool Foam::functionEntries::includeIfPresentEntry::execute
     Istream& is
 )
 {
-    IFstream ifs(includeFileName(is));
+    const fileName fName(includeFileName(is));
+    IFstream ifs(fName);
 
     if (ifs)
     {
+        if (Foam::functionEntries::includeEntry::report)
+        {
+            Info<< fName << endl;
+        }
         entry.read(parentDict, ifs);
     }
 
-- 
GitLab