ENH: simplify/extend decomposedBlockData retrieve block information

- handle existence/non-existence of a FoamFile header automatically
- support an upper limit when getting the number of blocks and
  use that for a hasBlock(...) method, which will stop reading sooner.
This commit is contained in:
Mark Olesen 2023-06-30 16:23:05 +02:00
parent f8987e64ed
commit fc2760ab9c
2 changed files with 43 additions and 10 deletions

View File

@ -86,11 +86,11 @@ Foam::decomposedBlockData::decomposedBlockData
contentData_()
{
// Temporary warning
if (readOpt() == IOobject::MUST_READ_IF_MODIFIED)
if (readOpt() == IOobjectOption::READ_MODIFIED)
{
WarningInFunction
<< "decomposedBlockData " << name()
<< " constructed with IOobject::MUST_READ_IF_MODIFIED"
<< " constructed with READ_MODIFIED"
" but decomposedBlockData does not support automatic rereading."
<< endl;
}
@ -226,12 +226,13 @@ bool Foam::decomposedBlockData::skipBlockEntry(Istream& is)
Foam::label Foam::decomposedBlockData::getNumBlocks
(
Istream& is,
const bool readHeader
const label maxNumBlocks
)
{
label nBlocks = 0;
if (readHeader)
// Handle OpenFOAM header if it is the first entry
if (is.good())
{
token tok(is);
@ -243,9 +244,16 @@ Foam::label Foam::decomposedBlockData::getNumBlocks
{
is.version(tok);
}
is.format(headerDict.get<word>("format"));
word formatName;
if (headerDict.readIfPresent("format", formatName))
{
is.format(formatName);
}
//// Obtain number of blocks directly
// headerDict.readIfPresent("blocks", nBlocks);
/// This may not be reliable...
//if (headerDict.readIfPresent("blocks", nBlocks))
//{
// return nBlocks;
//}
@ -259,12 +267,27 @@ Foam::label Foam::decomposedBlockData::getNumBlocks
while (is.good() && skipBlockEntry(is))
{
++nBlocks;
if (maxNumBlocks == nBlocks)
{
break;
}
}
return nBlocks;
}
bool Foam::decomposedBlockData::hasBlock(Istream& is, const label blockNumber)
{
return
(
blockNumber >= 0
&& (blockNumber < getNumBlocks(is, blockNumber+1))
);
}
std::streamoff Foam::decomposedBlockData::writeBlockEntry
(
OSstream& os,

View File

@ -157,10 +157,6 @@ protected:
//- Helper: skip a block of (binary) character data
static bool skipBlockEntry(Istream& is);
//- Extract number of blocks from decomposedBlockData file stream
static label getNumBlocks(Istream& is, const bool readHeader);
public:
//- Declare type-name, virtual type (with debug switch)
@ -207,6 +203,20 @@ public:
//- True if object header class is a known collated type
static bool isCollatedType(const IOobject& io);
//- Extract number of decomposedBlockData block entries, optionally
//- with an upper limit.
//- The input stream should be in a rewound state
//- (or only have read the header) before calling.
static label getNumBlocks(Istream& is, const label maxNumBlocks = -1);
//- True if the given block number (starts at 0) has a corresponding
//- decomposedBlockData block entry.
//- The input stream should be in a rewound state
//- (or only have read the header) before calling.
// This will be faster than checking against getNumBlocks()
// since it can potentially exit without scanning the entire file.
static bool hasBlock(Istream& is, const label blockNumber);
//- Read header as per IOobject with additional handling of
//- decomposedBlockData
static bool readHeader(IOobject& io, Istream& is);