From 221c3c02004a8efb97c63f0b855cc7c39c3188b1 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 22 Mar 2021 16:41:15 +0100 Subject: [PATCH] ENH: add OFstream rewind() method. - truncates output file, handles compressed streams --- .../test/OCountStream/Test-OCountStream.C | 12 +++++++ src/OpenFOAM/db/IOstreams/Fstreams/IFstream.C | 5 +-- src/OpenFOAM/db/IOstreams/Fstreams/IFstream.H | 3 +- src/OpenFOAM/db/IOstreams/Fstreams/OFstream.C | 33 ++++++++++++++++++- src/OpenFOAM/db/IOstreams/Fstreams/OFstream.H | 4 +++ .../db/IOstreams/Fstreams/fstreamPointer.H | 10 ++++++ .../db/IOstreams/Fstreams/fstreamPointers.C | 32 ++++++++++++++++++ 7 files changed, 93 insertions(+), 6 deletions(-) diff --git a/applications/test/OCountStream/Test-OCountStream.C b/applications/test/OCountStream/Test-OCountStream.C index 02594c2be0..1af8aa4255 100644 --- a/applications/test/OCountStream/Test-OCountStream.C +++ b/applications/test/OCountStream/Test-OCountStream.C @@ -105,6 +105,18 @@ int main(int argc, char *argv[]) printInfo(os1); printInfo(os2); printInfo(os3); + + // Rewind and test single output + os1.rewind(); generateOutput(os1); + os2.rewind(); generateOutput(os2); + os3.rewind(); generateOutput(os3); + + Info<< nl + << "single output" << nl; + + printInfo(os1); + printInfo(os2); + printInfo(os3); } Info<< "\nEnd\n" << endl; diff --git a/src/OpenFOAM/db/IOstreams/Fstreams/IFstream.C b/src/OpenFOAM/db/IOstreams/Fstreams/IFstream.C index 6e2916411b..8829cbc877 100644 --- a/src/OpenFOAM/db/IOstreams/Fstreams/IFstream.C +++ b/src/OpenFOAM/db/IOstreams/Fstreams/IFstream.C @@ -123,13 +123,10 @@ const std::istream& Foam::IFstream::stdStream() const void Foam::IFstream::rewind() { - lineNumber_ = 1; // Reset line number - if (IOstreamOption::COMPRESSED == ifstreamPointer::whichCompression()) { - // Special treatment for compressed stream + lineNumber_ = 1; // Reset line number ifstreamPointer::reopen_gz(this->name() + ".gz"); - setState(ifstreamPointer::get()->rdstate()); } else diff --git a/src/OpenFOAM/db/IOstreams/Fstreams/IFstream.H b/src/OpenFOAM/db/IOstreams/Fstreams/IFstream.H index d28d09d030..86a4b9432a 100644 --- a/src/OpenFOAM/db/IOstreams/Fstreams/IFstream.H +++ b/src/OpenFOAM/db/IOstreams/Fstreams/IFstream.H @@ -104,7 +104,8 @@ public: //- Const access to underlying std::istream virtual const std::istream& stdStream() const; - //- Rewind the stream so that it may be read again + //- Rewind the stream so that it may be read again. + // Includes special handling for compressed streams. virtual void rewind(); diff --git a/src/OpenFOAM/db/IOstreams/Fstreams/OFstream.C b/src/OpenFOAM/db/IOstreams/Fstreams/OFstream.C index d218df92ed..1c875c4e96 100644 --- a/src/OpenFOAM/db/IOstreams/Fstreams/OFstream.C +++ b/src/OpenFOAM/db/IOstreams/Fstreams/OFstream.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2017-2020 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -129,6 +129,37 @@ const std::ostream& Foam::OFstream::stdStream() const } +void Foam::OFstream::rewind() +{ + if (IOstreamOption::COMPRESSED == ofstreamPointer::whichCompression()) + { + ofstreamPointer::reopen_gz(this->name() + ".gz"); + } + else + { + // Reopen (truncate) + ofstreamPointer::reopen(this->name()); + } + + // As per OSstream::rewind() + + lineNumber_ = 1; // Reset line number + setState(ofstreamPointer::get()->rdstate()); + + if (good()) + { + setOpened(); + } + else + { + setClosed(); + setBad(); + } + + stdStream().rdbuf()->pubseekpos(0, std::ios_base::out); +} + + void Foam::OFstream::print(Ostream& os) const { os << "OFstream: "; diff --git a/src/OpenFOAM/db/IOstreams/Fstreams/OFstream.H b/src/OpenFOAM/db/IOstreams/Fstreams/OFstream.H index 53b9689b45..9ee99bc9c8 100644 --- a/src/OpenFOAM/db/IOstreams/Fstreams/OFstream.H +++ b/src/OpenFOAM/db/IOstreams/Fstreams/OFstream.H @@ -108,6 +108,10 @@ public: //- Const access to underlying std::ostream virtual const std::ostream& stdStream() const; + //- Rewind the stream so that it may be written again. + //- Reopens the file (truncation) + virtual void rewind(); + // Print diff --git a/src/OpenFOAM/db/IOstreams/Fstreams/fstreamPointer.H b/src/OpenFOAM/db/IOstreams/Fstreams/fstreamPointer.H index 02cd3eb47c..ed62c35b12 100644 --- a/src/OpenFOAM/db/IOstreams/Fstreams/fstreamPointer.H +++ b/src/OpenFOAM/db/IOstreams/Fstreams/fstreamPointer.H @@ -167,6 +167,16 @@ class ofstreamPointer //- The stream pointer (ofstream | ogzstream | ocountstream) std::unique_ptr ptr_; +protected: + + // Protected Member Functions + + //- Special 'rewind' method for compressed stream + void reopen_gz(const std::string& pathname_gz); + + //- General 'rewind' method (non-compressed) + void reopen(const std::string& pathname); + public: // Generated Methods diff --git a/src/OpenFOAM/db/IOstreams/Fstreams/fstreamPointers.C b/src/OpenFOAM/db/IOstreams/Fstreams/fstreamPointers.C index 8fd7021828..31c8500dca 100644 --- a/src/OpenFOAM/db/IOstreams/Fstreams/fstreamPointers.C +++ b/src/OpenFOAM/db/IOstreams/Fstreams/fstreamPointers.C @@ -207,6 +207,38 @@ void Foam::ifstreamPointer::reopen_gz(const std::string& pathname_gz) } +void Foam::ofstreamPointer::reopen_gz(const std::string& pathname_gz) +{ + #ifdef HAVE_LIBZ + ogzstream* gz = dynamic_cast(ptr_.get()); + + if (gz) + { + // Special treatment for gzstream + gz->close(); + gz->clear(); + gz->open(pathname_gz); + } + #endif /* HAVE_LIBZ */ +} + + +void Foam::ofstreamPointer::reopen(const std::string& pathname) +{ + std::ofstream* file = dynamic_cast(ptr_.get()); + + if (file) + { + if (file->is_open()) + { + file->close(); + } + file->clear(); + file->open(pathname); + } +} + + Foam::IOstreamOption::compressionType Foam::ifstreamPointer::whichCompression() const {