From a4e6adcd54e72cbaa4077994301fa9aa7aa8463b Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Thu, 21 Feb 2019 20:02:13 +0000 Subject: [PATCH] foamDictionary: Added initial support for dictionaries containing binary data The dictionary read is set to binary if there is a "FoamFile" header present which specifies binary format. --- .../foamDictionary/foamDictionary.C | 80 +++++++++---------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C b/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C index 964e619e93..b0a37183ec 100644 --- a/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C +++ b/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2016-2018 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2016-2019 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -122,11 +122,40 @@ Usage #include "IFstream.H" #include "OFstream.H" #include "includeEntry.H" +#include "dictionaryEntry.H" using namespace Foam; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +//- Read dictionary from file and return +// Sets steam to binary mode if specified in the optional header +dictionary readDict(const fileName& dictFileName) +{ + IFstream dictFile(dictFileName); + if (!dictFile().good()) + { + FatalErrorInFunction + << "Cannot open file " << dictFileName + << exit(FatalError, 1); + } + + dictionary dict; + dictionaryEntry headerDict(dict, dictFile()); + + if (headerDict.keyword() == "FoamFile") + { + dictFile().format(headerDict.lookup("format")); + } + + dict.add(headerDict); + + dict.read(dictFile(), true); + + return dict; +} + + //- Converts old scope syntax to new syntax word scope(const fileName& entryName) { @@ -323,20 +352,8 @@ int main(int argc, char *argv[]) } - fileName dictFileName(args[1]); - - autoPtr dictFile(new IFstream(dictFileName)); - if (!dictFile().good()) - { - FatalErrorInFunction - << "Cannot open file " << dictFileName - << exit(FatalError, 1); - } - - // Read but preserve headers - dictionary dict; - dict.read(dictFile(), true); - + const fileName dictFileName(args[1]); + dictionary dict(readDict(dictFileName)); bool changed = false; @@ -356,21 +373,13 @@ int main(int argc, char *argv[]) // Second dictionary for -diff - dictionary diffDict; fileName diffFileName; - if (args.optionReadIfPresent("diff", diffFileName)) - { - autoPtr diffFile(new IFstream(diffFileName)); - if (!diffFile().good()) - { - FatalErrorInFunction - << "Cannot open file " << diffFileName - << exit(FatalError, 1); - } - - // Read but preserve headers - diffDict.read(diffFile(), true); - } + const dictionary diffDict + ( + args.optionReadIfPresent("diff", diffFileName) + ? readDict(diffFileName) + : dictionary::null + ); word entryName; @@ -397,15 +406,7 @@ int main(int argc, char *argv[]) if (args.optionFound("dict")) { const fileName fromDictFileName(newValue); - autoPtr fromDictFile(new IFstream(fromDictFileName)); - if (!fromDictFile().good()) - { - FatalErrorInFunction - << "Cannot open file " << fromDictFileName - << exit(FatalError, 1); - } - - dictionary fromDict(fromDictFile()); + const dictionary fromDict(readDict(fromDictFileName)); const entry* fePtr ( @@ -544,7 +545,7 @@ int main(int argc, char *argv[]) } else { - FatalIOErrorInFunction(dictFile) + FatalIOErrorInFunction(dict) << "Cannot find entry " << entryName << exit(FatalIOError, 2); } @@ -569,7 +570,6 @@ int main(int argc, char *argv[]) if (changed) { - dictFile.clear(); OFstream os(dictFileName); IOobject::writeBanner(os); dict.write(os, false);