From d64c6371f13cfe76cbacc973163330804539bca4 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 12 Mar 2025 16:33:45 +0100 Subject: [PATCH] ENH: foamDictionary now respects header format [ascii/binary] (#3329) --- .../foamDictionary/foamDictionary.C | 68 +++++++++++++++---- 1 file changed, 56 insertions(+), 12 deletions(-) diff --git a/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C b/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C index 35392b2ef5..09dec6e4d1 100644 --- a/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C +++ b/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C @@ -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(1); auto dictFile = autoPtr::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);