Commit e3f681fa authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: support use of IOstreamOption for IFstream/OFstream

- can be convenient to bundle IO options as a single parameter
parent f3106ec1
......@@ -80,20 +80,14 @@ Foam::Detail::IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
Foam::IFstream::IFstream
(
const fileName& pathname,
streamFormat format,
versionNumber version
IOstreamOption streamOpt
)
:
Detail::IFstreamAllocator(pathname),
ISstream
(
*allocatedPtr_,
pathname,
format,
version,
IFstreamAllocator::detectedCompression_
)
ISstream(*allocatedPtr_, pathname, streamOpt)
{
IOstream::compression(IFstreamAllocator::detectedCompression_);
setClosed();
setState(allocatedPtr_->rdstate());
......
......@@ -100,10 +100,20 @@ public:
explicit IFstream
(
const fileName& pathname,
streamFormat format=ASCII,
versionNumber version=currentVersion
IOstreamOption streamOpt = IOstreamOption()
);
//- Construct from pathname, format (version)
IFstream
(
const fileName& pathname,
streamFormat fmt,
versionNumber ver = currentVersion
)
:
IFstream(pathname, IOstreamOption(fmt, ver))
{}
//- Destructor
~IFstream() = default;
......
......@@ -111,21 +111,12 @@ Foam::Detail::OFstreamAllocator::OFstreamAllocator
Foam::OFstream::OFstream
(
const fileName& pathname,
streamFormat format,
versionNumber version,
compressionType compression,
IOstreamOption streamOpt,
const bool append
)
:
Detail::OFstreamAllocator(pathname, compression, append),
OSstream
(
*allocatedPtr_,
pathname,
format,
version,
compression
)
Detail::OFstreamAllocator(pathname, streamOpt.compression(), append),
OSstream(*allocatedPtr_, pathname, streamOpt)
{
setClosed();
setState(allocatedPtr_->rdstate());
......
......@@ -102,12 +102,23 @@ public:
explicit OFstream
(
const fileName& pathname,
streamFormat format=ASCII,
versionNumber version=currentVersion,
compressionType compression=UNCOMPRESSED,
IOstreamOption streamOpt = IOstreamOption(),
const bool append = false
);
//- Construct from pathname, format (version, compression)
OFstream
(
const fileName& pathname,
streamFormat fmt,
versionNumber ver = currentVersion,
compressionType comp = compressionType::UNCOMPRESSED,
const bool append = false
)
:
OFstream(pathname, IOstreamOption(fmt, comp, ver), append)
{}
//- Destructor
~OFstream() = default;
......
......@@ -184,16 +184,14 @@ void Foam::masterOFstream::commit()
Foam::masterOFstream::masterOFstream
(
const fileName& pathName,
streamFormat format,
versionNumber version,
compressionType compression,
IOstreamOption streamOpt,
const bool append,
const bool valid
)
:
OStringStream(format, version),
OStringStream(streamOpt.format(), streamOpt.version()),
pathName_(pathName),
compression_(compression),
compression_(streamOpt.compression()),
append_(append),
valid_(valid)
{}
......
......@@ -86,17 +86,35 @@ public:
// Constructors
//- Construct and set stream status
//- Construct from pathname and set stream status
explicit masterOFstream
(
const fileName& pathname,
streamFormat format=ASCII,
versionNumber version=currentVersion,
compressionType compression=UNCOMPRESSED,
IOstreamOption streamOpt = IOstreamOption(),
const bool append = false,
const bool valid = true
);
//- Construct from pathname, version and set stream status
masterOFstream
(
const fileName& pathname,
streamFormat fmt,
versionNumber ver = currentVersion,
compressionType comp = compressionType::UNCOMPRESSED,
const bool append = false,
const bool valid = true
)
:
masterOFstream
(
pathname,
IOstreamOption(fmt, comp, ver),
append,
valid
)
{}
//- Destructor - commits buffered information to file
~masterOFstream();
......
......@@ -28,6 +28,7 @@ Class
Description
A simple output token stream that can be used to build token lists.
Always UNCOMPRESSED.
Note
Appending single characters to token list is fragile.
......@@ -63,13 +64,23 @@ public:
// Constructors
//- Default construct, set stream status
explicit OTstream(IOstreamOption streamOpt = IOstreamOption())
:
Ostream(streamOpt.format(), streamOpt.version()),
DynamicList<token>()
{
setOpened();
setGood();
}
//- Construct with format, version
explicit OTstream
(
streamFormat format=ASCII,
versionNumber version=currentVersion
streamFormat fmt,
versionNumber ver = currentVersion
)
:
Ostream(format, version),
Ostream(fmt, ver),
DynamicList<token>()
{
setOpened();
......
......@@ -199,10 +199,8 @@ bool Foam::fileOperations::collatedFileOperation::appendObject
OFstream os
(
pathName,
IOstream::BINARY,
ver,
IOstream::UNCOMPRESSED, // no compression
!isMaster
IOstreamOption(IOstream::BINARY, ver), // UNCOMPRESSED
!isMaster // append slaves
);
if (!os.good())
......@@ -488,10 +486,8 @@ bool Foam::fileOperations::collatedFileOperation::writeObject
masterOFstream os
(
pathName,
fmt,
ver,
cmp,
false,
IOstreamOption(fmt, ver, cmp),
false, // append=false
valid
);
......@@ -534,10 +530,8 @@ bool Foam::fileOperations::collatedFileOperation::writeObject
masterOFstream os
(
pathName,
fmt,
ver,
cmp,
false,
IOstreamOption(fmt, ver, cmp),
false, // append=false
valid
);
......@@ -596,9 +590,7 @@ bool Foam::fileOperations::collatedFileOperation::writeObject
(
writer_,
pathName,
fmt,
ver,
cmp,
IOstreamOption(fmt, ver, cmp),
useThread
);
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2018 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -35,16 +36,14 @@ Foam::threadedCollatedOFstream::threadedCollatedOFstream
(
OFstreamCollator& writer,
const fileName& pathName,
streamFormat format,
versionNumber version,
compressionType compression,
IOstreamOption streamOpt,
const bool useThread
)
:
OStringStream(format, version),
OStringStream(streamOpt.format(), streamOpt.version()),
writer_(writer),
pathName_(pathName),
compression_(compression),
compression_(streamOpt.compression()),
useThread_(useThread)
{}
......@@ -61,7 +60,7 @@ Foam::threadedCollatedOFstream::~threadedCollatedOFstream()
IOstream::BINARY,
version(),
compression_,
false, // append
false, // append=false
useThread_
);
}
......
......@@ -72,14 +72,32 @@ public:
//- Construct and set stream status
threadedCollatedOFstream
(
OFstreamCollator&,
OFstreamCollator& writer,
const fileName& pathname,
streamFormat format=ASCII,
versionNumber version=currentVersion,
compressionType compression=UNCOMPRESSED,
IOstreamOption streamOpt = IOstreamOption(),
const bool useThread = true
);
//- Construct and set stream status
threadedCollatedOFstream
(
OFstreamCollator& writer,
const fileName& pathname,
streamFormat fmt,
versionNumber ver = currentVersion,
compressionType comp = compressionType::UNCOMPRESSED,
const bool useThread = true
)
:
threadedCollatedOFstream
(
writer,
pathname,
IOstreamOption(fmt, ver, comp),
useThread
)
{}
//- Destructor
~threadedCollatedOFstream();
......
......@@ -466,7 +466,7 @@ bool Foam::fileOperation::writeObject
const regIOobject& io,
IOstream::streamFormat fmt,
IOstream::versionNumber ver,
IOstream::compressionType cmp,
IOstream::compressionType comp,
const bool valid
) const
{
......@@ -478,21 +478,15 @@ bool Foam::fileOperation::writeObject
autoPtr<OSstream> osPtr
(
NewOFstream
(
pathName,
fmt,
ver,
cmp
)
NewOFstream(pathName, IOstreamOption(fmt, ver, comp))
);
if (!osPtr.valid())
if (!osPtr)
{
return false;
}
Ostream& os = osPtr();
OSstream& os = osPtr();
// If any of these fail, return (leave error handling to Ostream class)
if (!os.good())
......
......@@ -409,10 +409,10 @@ public:
// suppress empty local lagrangian data)
virtual bool writeObject
(
const regIOobject&,
IOstream::streamFormat format=IOstream::ASCII,
IOstream::versionNumber version=IOstream::currentVersion,
IOstream::compressionType compression=IOstream::UNCOMPRESSED,
const regIOobject& io,
IOstream::streamFormat fmt = IOstream::ASCII,
IOstream::versionNumber ver = IOstream::currentVersion,
IOstream::compressionType comp = IOstream::UNCOMPRESSED,
const bool valid = true
) const;
......@@ -430,9 +430,7 @@ public:
virtual autoPtr<OSstream> NewOFstream
(
const fileName& pathname,
IOstream::streamFormat format=IOstream::ASCII,
IOstream::versionNumber version=IOstream::currentVersion,
IOstream::compressionType compression=IOstream::UNCOMPRESSED,
IOstreamOption streamOpt = IOstreamOption(),
const bool valid = true
) const = 0;
......
......@@ -2246,7 +2246,7 @@ bool Foam::fileOperations::masterUncollatedFileOperation::writeObject
const regIOobject& io,
IOstream::streamFormat fmt,
IOstream::versionNumber ver,
IOstream::compressionType cmp,
IOstream::compressionType comp,
const bool valid
) const
{
......@@ -2266,13 +2266,11 @@ bool Foam::fileOperations::masterUncollatedFileOperation::writeObject
NewOFstream
(
pathName,
fmt,
ver,
cmp,
IOstreamOption(fmt, ver, comp),
valid
)
);
Ostream& os = osPtr();
OSstream& os = osPtr();
// If any of these fail, return (leave error handling to Ostream class)
if (!os.good())
......@@ -2549,9 +2547,7 @@ Foam::autoPtr<Foam::OSstream>
Foam::fileOperations::masterUncollatedFileOperation::NewOFstream
(
const fileName& pathName,
IOstream::streamFormat fmt,
IOstream::versionNumber ver,
IOstream::compressionType cmp,
IOstreamOption streamOpt,
const bool valid
) const
{
......@@ -2560,10 +2556,8 @@ Foam::fileOperations::masterUncollatedFileOperation::NewOFstream
new masterOFstream
(
pathName,
fmt,
ver,
cmp,
false, // append
streamOpt,
false, // append=false
valid
)
);
......
......@@ -487,7 +487,7 @@ public:
TypeName("masterUncollated");
// Static data
// Static Data
//- Max size of parallel communications. Switches from non-blocking
// to scheduled when reading/writing files. Read as float to enable
......@@ -696,7 +696,7 @@ public:
// Returns success state.
virtual bool writeObject
(
const regIOobject&,
const regIOobject& io,
IOstream::streamFormat format=IOstream::ASCII,
IOstream::versionNumber version=IOstream::currentVersion,
IOstream::compressionType compression=IOstream::UNCOMPRESSED,
......@@ -710,9 +710,7 @@ public:
virtual autoPtr<OSstream> NewOFstream
(
const fileName& pathname,
IOstream::streamFormat format=IOstream::ASCII,
IOstream::versionNumber version=IOstream::currentVersion,
IOstream::compressionType compression=IOstream::UNCOMPRESSED,
IOstreamOption streamOpt = IOstreamOption(),
const bool valid = true
) const;
......
......@@ -724,13 +724,11 @@ Foam::autoPtr<Foam::OSstream>
Foam::fileOperations::uncollatedFileOperation::NewOFstream
(
const fileName& pathName,
IOstream::streamFormat fmt,
IOstream::versionNumber ver,
IOstream::compressionType cmp,
IOstreamOption streamOpt,
const bool valid
) const
{
return autoPtr<OSstream>(new OFstream(pathName, fmt, ver, cmp));
return autoPtr<OSstream>(new OFstream(pathName, streamOpt));
}
......
......@@ -282,9 +282,7 @@ public:
virtual autoPtr<OSstream> NewOFstream
(
const fileName& pathname,
IOstream::streamFormat format=IOstream::ASCII,
IOstream::versionNumber version=IOstream::currentVersion,
IOstream::compressionType compression=IOstream::UNCOMPRESSED,
IOstreamOption streamOpt = IOstreamOption(),
const bool valid = true
) const;
};
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2016 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -63,12 +64,10 @@ void Foam::OBJstream::writeAndCheck(const char c)
Foam::OBJstream::OBJstream
(
const fileName& pathname,
streamFormat format,
versionNumber version,
compressionType compression
IOstreamOption streamOpt
)
:
OFstream(pathname, format, version, compression),
OFstream(pathname, streamOpt),
startOfLine_(true),
nVertices_(0)
{}
......
......@@ -83,11 +83,21 @@ public:
explicit OBJstream
(
const fileName& pathname,
streamFormat format=ASCII,
versionNumber version=currentVersion,
compressionType compression=UNCOMPRESSED
IOstreamOption streamOpt = IOstreamOption()
);
//- Construct from pathname
OBJstream
(
const fileName& pathname,
streamFormat fmt,
versionNumber ver = currentVersion,
compressionType comp = compressionType::UNCOMPRESSED
)
:
OBJstream(pathname, IOstreamOption(fmt, ver, comp))
{}
//- Destructor
~OBJstream() = default;
......
......@@ -142,13 +142,7 @@ void Foam::MeshedSurfaceProxy<Face>::write
)
);
OFstream os
(
objectDir/io.name(),
t.writeFormat(),
IOstream::currentVersion,
t.writeCompression()
);
OFstream os(objectDir/io.name(), t.writeStreamOption());
io.writeHeader(os);
......@@ -174,13 +168,8 @@ void Foam::MeshedSurfaceProxy<Face>::write
)
);
OFstream os
(
objectDir/io.name(),
t.writeFormat(),
IOstream::currentVersion,
t.writeCompression()
);
OFstream os(objectDir/io.name(), t.writeStreamOption());
io.writeHeader(os);
if (this->useFaceMap())
......@@ -212,8 +201,9 @@ void Foam::MeshedSurfaceProxy<Face>::write
)
);
// write as ascii
// Write as ASCII-only
OFstream os(objectDir/io.name());
io.writeHeader(os);
os << this->surfZones();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment