ENH: foamDictionary now respects header format [ascii/binary] (#3329)

This commit is contained in:
Mark Olesen
2025-03-12 16:33:45 +01:00
parent 2d246cd5d1
commit d64c6371f1

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2016-2017 OpenFOAM Foundation Copyright (C) 2016-2017 OpenFOAM Foundation
Copyright (C) 2017-2023 OpenCFD Ltd. Copyright (C) 2017-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -140,6 +140,38 @@ using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Read dictionary from IFstream, setting the stream to ascii/binary mode
//- depending on the 'FoamFile' header content
dictionary readDictionary(Istream& is)
{
auto format = is.format();
// If the file starts with 'FoamFile { ... }'
token tok;
if
(
(tok.read(is) && tok.isWord("FoamFile"))
&& (tok.read(is) && tok.isPunctuation(token::BEGIN_BLOCK))
)
{
is.putBack(tok); // Put back '{'
// FoamFile sub-dictionary content
dictionary header(is);
// Get "format" if present
format = IOstreamOption::formatEnum("format", header, format);
}
// Start again. Probably does not work well with IPstream though
is.rewind();
is.format(format);
// Read, preserving headers
return dictionary(is, true);
}
//- Convert very old ':' scope syntax to less old '.' scope syntax, //- Convert very old ':' scope syntax to less old '.' scope syntax,
// but leave anything with '/' delimiters untouched // but leave anything with '/' delimiters untouched
bool upgradeScope(word& entryName) bool upgradeScope(word& entryName)
@ -266,6 +298,8 @@ void removeDict(dictionary& dict, const dictionary& dictToRemove)
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
argList::addNote argList::addNote
@ -273,7 +307,7 @@ int main(int argc, char *argv[])
"Interrogate and manipulate dictionaries" "Interrogate and manipulate dictionaries"
); );
argList::noBanner(); argList::noBanner(); // Essential if redirecting stdout
argList::noJobInfo(); argList::noJobInfo();
argList::addArgument("dict", "The dictionary file to process"); argList::addArgument("dict", "The dictionary file to process");
argList::addBoolOption("keywords", "List keywords"); argList::addBoolOption("keywords", "List keywords");
@ -369,7 +403,7 @@ int main(int argc, char *argv[])
const auto dictFileName = args.get<fileName>(1); const auto dictFileName = args.get<fileName>(1);
auto dictFile = autoPtr<IFstream>::New(dictFileName); auto dictFile = autoPtr<IFstream>::New(dictFileName);
if (!dictFile().good()) if (!dictFile || !dictFile().good())
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Cannot open file " << dictFileName << "Cannot open file " << dictFileName
@ -379,8 +413,13 @@ int main(int argc, char *argv[])
bool changed = false; bool changed = false;
// Read but preserve headers // Read, preserving headers
dictionary dict(dictFile(), true); //// dictionary dict(dictFile(), true);
dictionary dict = readDictionary(dictFile());
// The extracted dictionary format
const auto dictFormat = dictFile().format();
if (listIncludes) if (listIncludes)
{ {
@ -414,8 +453,10 @@ int main(int argc, char *argv[])
<< exit(FatalError, 1); << exit(FatalError, 1);
} }
// Read but preserve headers // Read, preserving headers
diffDict.read(diffFile, true); //// diffDict.read(diffFile, true);
diffDict = readDictionary(diffFile);
optDiff = true; optDiff = true;
} }
else if (args.readIfPresent("diff-etc", diffFileName)) else if (args.readIfPresent("diff-etc", diffFileName))
@ -436,8 +477,9 @@ int main(int argc, char *argv[])
<< exit(FatalError, 1); << exit(FatalError, 1);
} }
// Read but preserve headers // Read, preserving headers
diffDict.read(diffFile, true); //// diffDict.read(diffFile, true);
diffDict = readDictionary(diffFile);
optDiff = true; optDiff = true;
} }
} }
@ -592,10 +634,12 @@ int main(int argc, char *argv[])
dict.write(Info, false); dict.write(Info, false);
} }
// Close the input file
dictFile.reset();
if (changed) if (changed)
{ {
dictFile.clear(); OFstream os(dictFileName, dictFormat);
OFstream os(dictFileName);
IOobject::writeBanner(os); IOobject::writeBanner(os);
IOobject::writeDivider(os); IOobject::writeDivider(os);
dict.write(os, false); dict.write(os, false);