diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Istream.H b/src/OpenFOAM/db/IOstreams/IOstreams/Istream.H index 36a9f9a665..6cd20ba048 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams/Istream.H +++ b/src/OpenFOAM/db/IOstreams/IOstreams/Istream.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2022 OpenCFD Ltd. + Copyright (C) 2017-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -161,8 +161,10 @@ public: //- Read a double virtual Istream& read(double&) = 0; - //- Read binary block - virtual Istream& read(char*, std::streamsize) = 0; + //- Read binary block (with any possible block delimiters). + //- Reading into a null pointer shall ideally behave like a seek + //- operation. + virtual Istream& read(char* data, std::streamsize count) = 0; //- Start of low-level raw binary read virtual bool beginRawRead() = 0; @@ -170,8 +172,10 @@ public: //- End of low-level raw binary read virtual bool endRawRead() = 0; - //- Low-level raw binary read - virtual Istream& readRaw(char*, std::streamsize) = 0; + //- Low-level raw binary read (without possible block delimiters). + //- Reading into a null pointer shall ideally behave like a seek + //- operation. + virtual Istream& readRaw(char* data, std::streamsize count) = 0; //- Rewind the stream so that it may be read again virtual void rewind() = 0; diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.H b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.H index 68e3f26bdc..9a61d58f7f 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.H +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.H @@ -75,6 +75,7 @@ class UIPstreamBase //- Read count bytes of data from the receive buffer. // Prior data alignment is done by prepareBuffer + // Reading into a null pointer behaves like a forward seek inline void readFromBuffer(void* data, const size_t count); //- Read string length and string content @@ -181,9 +182,13 @@ public: Istream& read(double& val); //- Read binary block with 8-byte alignment. + //- Reading into a null pointer behaves like a forward seek of + //- count characters. Istream& read(char* data, std::streamsize count); - //- Low-level raw binary read + //- Low-level raw binary read. + //- Reading into a null pointer behaves like a forward seek of + //- count characters. Istream& readRaw(char* data, std::streamsize count); //- Start of low-level raw binary read diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstreamBase.C b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstreamBase.C index 97685b3022..db04620b71 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstreamBase.C +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstreamBase.C @@ -106,12 +106,15 @@ inline void Foam::UIPstreamBase::readFromBuffer const size_t count ) { - const char* const __restrict__ buf = &recvBuf_[recvBufPos_]; - char* const __restrict__ output = reinterpret_cast(data); - - for (size_t i = 0; i < count; ++i) + if (data) { - output[i] = buf[i]; + const char* const __restrict__ buf = &recvBuf_[recvBufPos_]; + char* const __restrict__ output = reinterpret_cast(data); + + for (size_t i = 0; i < count; ++i) + { + output[i] = buf[i]; + } } recvBufPos_ += count; diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C index 6f0aa24fb3..646a1cedac 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C +++ b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2022 OpenCFD Ltd. + Copyright (C) 2017-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -1018,19 +1018,41 @@ Foam::Istream& Foam::ISstream::read(double& val) } -Foam::Istream& Foam::ISstream::read(char* buf, std::streamsize count) +Foam::Istream& Foam::ISstream::read(char* data, std::streamsize count) { beginRawRead(); - readRaw(buf, count); + readRaw(data, count); endRawRead(); return *this; } -Foam::Istream& Foam::ISstream::readRaw(char* buf, std::streamsize count) +Foam::Istream& Foam::ISstream::readRaw(char* data, std::streamsize count) { - is_.read(buf, count); + if (count) + { + if (data) + { + is_.read(data, count); + } + else + { + // Forward seek + is_.seekg(is_.tellg() + count); + + // Not sure if this is needed (as per rewind) + // some documentation indicates that ifstream needs + // seekg with values from a tellg + // + // stdStream().rdbuf()->pubseekpos + // ( + // count, + // std::ios_base::seekdir::cur, + // std::ios_base::in + // ); + } + } syncState(); return *this; } diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H index 7b3fcb3b3d..9e31ba9a84 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H +++ b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H @@ -222,10 +222,14 @@ public: //- Read a double virtual Istream& read(double& val); - //- Read binary block - virtual Istream& read(char* buf, std::streamsize count); + //- Read binary block (with any possible block delimiters). + //- Reading into a null pointer behaves like a forward seek of + //- count characters. + virtual Istream& read(char* data, std::streamsize count); - //- Low-level raw binary read + //- Low-level raw binary read (without possible block delimiters). + //- Reading into a null pointer behaves like a forward seek of + //- count characters. virtual Istream& readRaw(char* data, std::streamsize count); //- Start of low-level raw binary read