mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-12-28 03:37:59 +00:00
ENH: support OFstream "/dev/null" equivalent directly
- uses ocountstream for the output, which swallows all output. Improves portability ENH: improved efficiency in countstreambuf - xsputn() instead of overflow - more consistent seek* methods
This commit is contained in:
@ -39,6 +39,21 @@ namespace Foam
|
||||
|
||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||
|
||||
Foam::OFstream::OFstream
|
||||
(
|
||||
std::nullptr_t
|
||||
)
|
||||
:
|
||||
Foam::ofstreamPointer(nullptr),
|
||||
OSstream(*(ofstreamPointer::get()), "/dev/null")
|
||||
{
|
||||
setState(ofstreamPointer::get()->rdstate());
|
||||
setOpened();
|
||||
|
||||
lineNumber_ = 1;
|
||||
}
|
||||
|
||||
|
||||
Foam::OFstream::OFstream
|
||||
(
|
||||
const fileName& pathname,
|
||||
|
||||
@ -64,6 +64,10 @@ public:
|
||||
|
||||
// Constructors
|
||||
|
||||
//- Construct a null output file stream.
|
||||
// Behaves like \c /dev/null and is named accordingly
|
||||
explicit OFstream(std::nullptr_t);
|
||||
|
||||
//- Construct from pathname
|
||||
explicit OFstream
|
||||
(
|
||||
|
||||
@ -82,7 +82,7 @@ protected:
|
||||
// Protected Member Functions
|
||||
|
||||
//- Special 'rewind' method for compressed stream
|
||||
void reopen_gz(const fileName& pathname_gz);
|
||||
void reopen_gz(const std::string& pathname_gz);
|
||||
|
||||
public:
|
||||
|
||||
@ -164,7 +164,7 @@ class ofstreamPointer
|
||||
{
|
||||
// Private Data
|
||||
|
||||
//- The stream pointer (ofstream or ogzstream)
|
||||
//- The stream pointer (ofstream | ogzstream | ocountstream)
|
||||
std::unique_ptr<std::ostream> ptr_;
|
||||
|
||||
public:
|
||||
@ -192,6 +192,9 @@ public:
|
||||
|
||||
// Constructors
|
||||
|
||||
//- Construct as null output stream using Foam::ocountstream
|
||||
explicit ofstreamPointer(std::nullptr_t);
|
||||
|
||||
//- Construct from pathname, with specified append option
|
||||
ofstreamPointer(const fileName& pathname, const bool append)
|
||||
:
|
||||
|
||||
@ -27,6 +27,7 @@ License
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "fstreamPointer.H"
|
||||
#include "OCountStream.H"
|
||||
#include "OSspecific.H"
|
||||
|
||||
// HAVE_LIBZ defined externally
|
||||
@ -131,6 +132,12 @@ Foam::ifstreamPointer::ifstreamPointer
|
||||
}
|
||||
|
||||
|
||||
Foam::ofstreamPointer::ofstreamPointer(std::nullptr_t)
|
||||
:
|
||||
ptr_(new Foam::ocountstream)
|
||||
{}
|
||||
|
||||
|
||||
Foam::ofstreamPointer::ofstreamPointer
|
||||
(
|
||||
const fileName& pathname,
|
||||
@ -184,7 +191,7 @@ Foam::ofstreamPointer::ofstreamPointer
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
void Foam::ifstreamPointer::reopen_gz(const fileName& pathname_gz)
|
||||
void Foam::ifstreamPointer::reopen_gz(const std::string& pathname_gz)
|
||||
{
|
||||
#ifdef HAVE_LIBZ
|
||||
igzstream* gz = dynamic_cast<igzstream*>(ptr_.get());
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011 OpenFOAM Foundation
|
||||
Copyright (C) 2018 OpenCFD Ltd.
|
||||
Copyright (C) 2018-2020 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -49,7 +49,7 @@ unsigned int Foam::IOstream::precision_
|
||||
Foam::ISstream Foam::Sin(std::cin, "Sin");
|
||||
Foam::OSstream Foam::Sout(std::cout, "Sout");
|
||||
Foam::OSstream Foam::Serr(std::cerr, "Serr");
|
||||
Foam::OFstream Foam::Snull("/dev/null");
|
||||
Foam::OFstream Foam::Snull(nullptr); // A "/dev/null" equivalent
|
||||
|
||||
Foam::prefixOSstream Foam::Pout(std::cout, "Pout");
|
||||
Foam::prefixOSstream Foam::Perr(std::cerr, "Perr");
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011 OpenFOAM Foundation
|
||||
Copyright (C) 2020 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -49,19 +50,19 @@ Description
|
||||
|
||||
namespace Foam
|
||||
{
|
||||
//- An Istream wrapper for std::cin
|
||||
//- ISstream wrapped stdin (std::cin)
|
||||
extern ISstream Sin;
|
||||
|
||||
//- An Ostream wrapper for std::cout
|
||||
//- OSstream wrapped stdout (std::cout)
|
||||
extern OSstream Sout;
|
||||
|
||||
//- An Ostream wrapper for std::cerr
|
||||
//- OSstream wrapped stderr (std::cerr)
|
||||
extern OSstream Serr;
|
||||
|
||||
//- An Ostream wrapper for parallel output to std::cout
|
||||
//- OSstream wrapped stdout (std::cout) with parallel prefix
|
||||
extern prefixOSstream Pout;
|
||||
|
||||
//- An Ostream wrapper for parallel output to std::cerr
|
||||
//- OSstream wrapped stderr (std::cerr) with parallel prefix
|
||||
extern prefixOSstream Perr;
|
||||
}
|
||||
|
||||
|
||||
@ -56,6 +56,48 @@ class countstreambuf
|
||||
|
||||
protected:
|
||||
|
||||
//- Set position pointer to relative position
|
||||
virtual std::streampos seekoff
|
||||
(
|
||||
std::streamoff off,
|
||||
std::ios_base::seekdir way,
|
||||
std::ios_base::openmode which = std::ios_base::in|std::ios_base::out
|
||||
)
|
||||
{
|
||||
if (which & std::ios_base::out)
|
||||
{
|
||||
if (way == std::ios_base::beg)
|
||||
{
|
||||
size_ = off;
|
||||
}
|
||||
else if (way == std::ios_base::cur)
|
||||
{
|
||||
size_ += off;
|
||||
}
|
||||
else if (way == std::ios_base::end)
|
||||
{
|
||||
// not really possible
|
||||
}
|
||||
|
||||
return size_; // tellp()
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
//- Set position pointer to absolute position
|
||||
// For the counter, adjust the count accordingly.
|
||||
virtual std::streampos seekpos
|
||||
(
|
||||
std::streampos pos,
|
||||
std::ios_base::openmode which = std::ios_base::in|std::ios_base::out
|
||||
)
|
||||
{
|
||||
return seekoff(pos, std::ios_base::beg, which);
|
||||
}
|
||||
|
||||
|
||||
//- Handle output counting via overflow
|
||||
virtual int overflow(int c = EOF)
|
||||
{
|
||||
@ -66,35 +108,31 @@ protected:
|
||||
return c;
|
||||
}
|
||||
|
||||
//- Set position pointer to absolute position
|
||||
// For the counter, any positioning is ignored and it always acts like
|
||||
// seekpos(0), which resets the count.
|
||||
virtual std::streampos seekpos
|
||||
(
|
||||
std::streampos,
|
||||
std::ios_base::openmode which = std::ios_base::in|std::ios_base::out
|
||||
)
|
||||
|
||||
//- Put sequence of characters
|
||||
virtual std::streamsize xsputn(const char* s, std::streamsize n)
|
||||
{
|
||||
size_ = 0;
|
||||
return 0;
|
||||
size_ += n;
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
public:
|
||||
|
||||
//- Default construct, or with precount size
|
||||
countstreambuf(std::streamsize precount=0)
|
||||
explicit countstreambuf(std::streamsize precount=0)
|
||||
:
|
||||
size_(precount)
|
||||
{}
|
||||
|
||||
//- \return The number of bytes counted
|
||||
std::streamsize size() const
|
||||
//- \return The buffer put position == number of bytes counted.
|
||||
std::streamsize tellp() const
|
||||
{
|
||||
return size_;
|
||||
}
|
||||
|
||||
//- Some information about the number of bytes counted
|
||||
inline void printBufInfo(Ostream& os) const
|
||||
void printBufInfo(Ostream& os) const
|
||||
{
|
||||
os << "count=" << size_;
|
||||
}
|
||||
@ -124,8 +162,14 @@ public:
|
||||
{}
|
||||
|
||||
|
||||
//- \return The buffer put position == number of bytes counted.
|
||||
using countstreambuf::tellp;
|
||||
|
||||
//- \return The number of bytes counted
|
||||
using countstreambuf::size;
|
||||
std::streamsize size() const
|
||||
{
|
||||
return countstreambuf::tellp();
|
||||
}
|
||||
|
||||
//- Rewind the stream, reset the count
|
||||
void rewind()
|
||||
@ -183,7 +227,7 @@ public:
|
||||
//- The number of bytes counted
|
||||
std::streamsize size() const
|
||||
{
|
||||
return buf_.size();
|
||||
return buf_.tellp();
|
||||
}
|
||||
|
||||
//- Rewind the stream, reset the count
|
||||
|
||||
Reference in New Issue
Block a user