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 |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 OpenFOAM Foundation
Copyright (C) 2017-2023 OpenCFD Ltd.
Copyright (C) 2017-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
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,
// but leave anything with '/' delimiters untouched
bool upgradeScope(word& entryName)
@ -266,6 +298,8 @@ void removeDict(dictionary& dict, const dictionary& dictToRemove)
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
argList::addNote
@ -273,7 +307,7 @@ int main(int argc, char *argv[])
"Interrogate and manipulate dictionaries"
);
argList::noBanner();
argList::noBanner(); // Essential if redirecting stdout
argList::noJobInfo();
argList::addArgument("dict", "The dictionary file to process");
argList::addBoolOption("keywords", "List keywords");
@ -336,7 +370,7 @@ int main(int argc, char *argv[])
"disableFunctionEntries",
"Disable expansion of dictionary directives - #include, #codeStream etc"
);
profiling::disable(); // Disable profiling (and its output)
profiling::disable(); // Disable profiling (and its output)
argList args(argc, argv);
@ -369,7 +403,7 @@ int main(int argc, char *argv[])
const auto dictFileName = args.get<fileName>(1);
auto dictFile = autoPtr<IFstream>::New(dictFileName);
if (!dictFile().good())
if (!dictFile || !dictFile().good())
{
FatalErrorInFunction
<< "Cannot open file " << dictFileName
@ -379,8 +413,13 @@ int main(int argc, char *argv[])
bool changed = false;
// Read but preserve headers
dictionary dict(dictFile(), true);
// Read, preserving headers
//// dictionary dict(dictFile(), true);
dictionary dict = readDictionary(dictFile());
// The extracted dictionary format
const auto dictFormat = dictFile().format();
if (listIncludes)
{
@ -414,8 +453,10 @@ int main(int argc, char *argv[])
<< exit(FatalError, 1);
}
// Read but preserve headers
diffDict.read(diffFile, true);
// Read, preserving headers
//// diffDict.read(diffFile, true);
diffDict = readDictionary(diffFile);
optDiff = true;
}
else if (args.readIfPresent("diff-etc", diffFileName))
@ -436,8 +477,9 @@ int main(int argc, char *argv[])
<< exit(FatalError, 1);
}
// Read but preserve headers
diffDict.read(diffFile, true);
// Read, preserving headers
//// diffDict.read(diffFile, true);
diffDict = readDictionary(diffFile);
optDiff = true;
}
}
@ -592,10 +634,12 @@ int main(int argc, char *argv[])
dict.write(Info, false);
}
// Close the input file
dictFile.reset();
if (changed)
{
dictFile.clear();
OFstream os(dictFileName);
OFstream os(dictFileName, dictFormat);
IOobject::writeBanner(os);
IOobject::writeDivider(os);
dict.write(os, false);