ENH: add OFstream rewind() method.
- truncates output file, handles compressed streams
This commit is contained in:
parent
f5a931d4b5
commit
221c3c0200
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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();
|
||||
|
||||
|
||||
|
@ -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: ";
|
||||
|
@ -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
|
||||
|
||||
|
@ -167,6 +167,16 @@ class ofstreamPointer
|
||||
//- The stream pointer (ofstream | ogzstream | ocountstream)
|
||||
std::unique_ptr<std::ostream> 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
|
||||
|
@ -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<ogzstream*>(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<std::ofstream*>(ptr_.get());
|
||||
|
||||
if (file)
|
||||
{
|
||||
if (file->is_open())
|
||||
{
|
||||
file->close();
|
||||
}
|
||||
file->clear();
|
||||
file->open(pathname);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Foam::IOstreamOption::compressionType
|
||||
Foam::ifstreamPointer::whichCompression() const
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user