Skip to content
Permalink
Browse files

foamDictionary: Added support for writing dictionaries containing bin…

…ary entries
  • Loading branch information...
Henry Weller
Henry Weller committed Feb 22, 2019
1 parent 190dd33 commit a88405a1380206e90ac94e015f0c0d7562b70d13
Showing with 28 additions and 18 deletions.
  1. +28 −18 applications/utilities/miscellaneous/foamDictionary/foamDictionary.C
@@ -118,20 +118,22 @@ Usage
\*---------------------------------------------------------------------------*/

#include "argList.H"
#include "Time.H"
#include "IOobject.H"
#include "Pair.H"
#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)
IOstream::streamFormat readDict(dictionary& dict, const fileName& dictFileName)
{
IOstream::streamFormat dictFormat = IOstream::ASCII;

IFstream dictFile(dictFileName);
if (!dictFile().good())
{
@@ -140,19 +142,25 @@ dictionary readDict(const fileName& dictFileName)
<< exit(FatalError, 1);
}

dictionary dict;
dictionaryEntry headerDict(dict, dictFile());
// Read the first entry from the dictionary
autoPtr<entry> firstEntry(entry::New(dictFile()));

if (headerDict.keyword() == "FoamFile")
// If the first entry is the "FoamFile" header dictionary
// read and set the stream format
if (firstEntry->isDict() && firstEntry->keyword() == "FoamFile")
{
dictFile().format(headerDict.lookup("format"));
dictFormat = IOstream::formatEnum(firstEntry->dict().lookup("format"));
dictFile().format(dictFormat);
}

dict.add(headerDict);
// Add the first entry to the dictionary
dict.add(firstEntry);

// Read and add the rest of the dictionary entries
// preserving the "FoamFile" header dictionary if present
dict.read(dictFile(), true);

return dict;
return dictFormat;
}


@@ -353,7 +361,8 @@ int main(int argc, char *argv[])


const fileName dictFileName(args[1]);
dictionary dict(readDict(dictFileName));
dictionary dict;
IOstream::streamFormat dictFormat = readDict(dict, dictFileName);

bool changed = false;

@@ -374,12 +383,12 @@ int main(int argc, char *argv[])

// Second dictionary for -diff
fileName diffFileName;
const dictionary diffDict
(
args.optionReadIfPresent("diff", diffFileName)
? readDict(diffFileName)
: dictionary::null
);
dictionary diffDict;

if (args.optionReadIfPresent("diff", diffFileName))
{
readDict(diffDict, diffFileName);
}


word entryName;
@@ -406,7 +415,8 @@ int main(int argc, char *argv[])
if (args.optionFound("dict"))
{
const fileName fromDictFileName(newValue);
const dictionary fromDict(readDict(fromDictFileName));
dictionary fromDict;
readDict(fromDict, fromDictFileName);

const entry* fePtr
(
@@ -570,7 +580,7 @@ int main(int argc, char *argv[])

if (changed)
{
OFstream os(dictFileName);
OFstream os(dictFileName, dictFormat);
IOobject::writeBanner(os);
dict.write(os, false);
IOobject::writeEndDivider(os);

0 comments on commit a88405a

Please sign in to comment.
You can’t perform that action at this time.