diff --git a/ReleaseNotes-1.6 b/ReleaseNotes-1.6 index 7227dac4fe..78a3f045a7 100644 --- a/ReleaseNotes-1.6 +++ b/ReleaseNotes-1.6 @@ -58,6 +58,8 @@ their keywords. When searching, an exact match has priority over a regular expression match. Multiple regular expressions are matched in reverse order. + + The *new* =#includeIfPresent= directive is similar to the =#include= + directive, but does not generate an error if the file does not exist. + The default =#inputMode= is now '=merge=', which corresponds to the most general usage. The =#inputMode warn= corresponds to the previous default behaviour. diff --git a/applications/test/dictionary/testDict b/applications/test/dictionary/testDict index 7709155fec..6a0863a454 100644 --- a/applications/test/dictionary/testDict +++ b/applications/test/dictionary/testDict @@ -78,6 +78,7 @@ boundaryField // NB: the inputMode has a global scope #inputMode merge #include "testDict2" +#includeIfPresent "SomeUnknownFile" foo { diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index 9d1daeeeef..8e2f04c359 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -132,6 +132,7 @@ $(dictionaryEntry)/dictionaryEntryIO.C functionEntries = $(dictionary)/functionEntries $(functionEntries)/functionEntry/functionEntry.C $(functionEntries)/includeEntry/includeEntry.C +$(functionEntries)/includeIfPresentEntry/includeIfPresentEntry.C $(functionEntries)/inputModeEntry/inputModeEntry.C $(functionEntries)/removeEntry/removeEntry.C diff --git a/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.C index 2d49418fae..66148e27f3 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.C @@ -62,7 +62,7 @@ namespace functionEntries } } -// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * // +// * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * // Foam::fileName Foam::functionEntries::includeEntry::includeFileName ( @@ -112,6 +112,7 @@ bool Foam::functionEntries::includeEntry::execute } } + bool Foam::functionEntries::includeEntry::execute ( const dictionary& parentDict, diff --git a/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.H b/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.H index 6fc94aa1ad..80dcaa75af 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.H +++ b/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.H @@ -67,15 +67,19 @@ class includeEntry { // Private Member Functions - //- Read the include fileName from Istream, expand and return - static fileName includeFileName(Istream&); - //- Disallow default bitwise copy construct includeEntry(const includeEntry&); //- Disallow default bitwise assignment void operator=(const includeEntry&); +protected: + + // Protected Member Functions + + //- Read the include fileName from Istream, expand and return + static fileName includeFileName(Istream&); + public: diff --git a/src/OpenFOAM/db/dictionary/functionEntries/includeIfPresentEntry/includeIfPresentEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/includeIfPresentEntry/includeIfPresentEntry.C new file mode 100644 index 0000000000..9fc1ea1727 --- /dev/null +++ b/src/OpenFOAM/db/dictionary/functionEntries/includeIfPresentEntry/includeIfPresentEntry.C @@ -0,0 +1,101 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2009 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 2 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, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +\*---------------------------------------------------------------------------*/ + +#include "includeIfPresentEntry.H" +#include "dictionary.H" +#include "IFstream.H" +#include "addToMemberFunctionSelectionTable.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +const Foam::word Foam::functionEntries::includeIfPresentEntry::typeName +( + Foam::functionEntries::includeIfPresentEntry::typeName_() +); + +// Don't lookup the debug switch here as the debug switch dictionary +// might include includeIfPresentEntry +int Foam::functionEntries::includeIfPresentEntry::debug(0); + +namespace Foam +{ +namespace functionEntries +{ + addToMemberFunctionSelectionTable + ( + functionEntry, + includeIfPresentEntry, + execute, + dictionaryIstream + ); + + addToMemberFunctionSelectionTable + ( + functionEntry, + includeIfPresentEntry, + execute, + primitiveEntryIstream + ); +} +} + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +bool Foam::functionEntries::includeIfPresentEntry::execute +( + dictionary& parentDict, + Istream& is +) +{ + IFstream ifs(includeFileName(is)); + + if (ifs) + { + parentDict.read(ifs); + } + + return true; +} + + +bool Foam::functionEntries::includeIfPresentEntry::execute +( + const dictionary& parentDict, + primitiveEntry& entry, + Istream& is +) +{ + IFstream ifs(includeFileName(is)); + + if (ifs) + { + entry.read(parentDict, ifs); + } + + return true; +} + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/dictionary/functionEntries/includeIfPresentEntry/includeIfPresentEntry.H b/src/OpenFOAM/db/dictionary/functionEntries/includeIfPresentEntry/includeIfPresentEntry.H new file mode 100644 index 0000000000..751de7dba2 --- /dev/null +++ b/src/OpenFOAM/db/dictionary/functionEntries/includeIfPresentEntry/includeIfPresentEntry.H @@ -0,0 +1,101 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2009 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 2 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, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Class + Foam::functionEntries::includeIfPresentEntry + +Description + Specify a file to include if it exists. Expects a single string to follow. + + The @c \#includeIfPresent directive is similar to the @c \#include + directive, but does not generate an error if the file does not exist. + +See Also + Foam::functionEntries::includeEntry + +SourceFiles + includeIfPresentEntry.C + +\*---------------------------------------------------------------------------*/ + +#ifndef includeIfPresentEntry_H +#define includeIfPresentEntry_H + +#include "includeEntry.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace functionEntries +{ + +/*---------------------------------------------------------------------------*\ + Class includeIfPresentEntry Declaration +\*---------------------------------------------------------------------------*/ + +class includeIfPresentEntry +: + public includeEntry +{ + // Private Member Functions + + //- Disallow default bitwise copy construct + includeIfPresentEntry(const includeIfPresentEntry&); + + //- Disallow default bitwise assignment + void operator=(const includeIfPresentEntry&); + +public: + + //- Runtime type information + ClassName("includeIfPresent"); + + + // Member Functions + + //- Execute the functionEntry in a sub-dict context + static bool execute(dictionary& parentDict, Istream&); + + //- Execute the functionEntry in a primitiveEntry context + static bool execute + ( + const dictionary& parentDict, + primitiveEntry&, + Istream& + ); + +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace functionEntries +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* //