OFstream.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2017 OpenFOAM Foundation
9 Copyright (C) 2017-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "OFstream.H"
30#include "OSspecific.H"
31
32// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33
34namespace Foam
35{
37}
38
39
40// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41
43(
44 std::nullptr_t
45)
46:
47 Foam::ofstreamPointer(nullptr),
48 OSstream(*(ofstreamPointer::get()), "/dev/null")
49{
50 setState(ofstreamPointer::get()->rdstate());
51 setOpened();
52
53 lineNumber_ = 1;
54}
55
56
58(
59 const fileName& pathname,
60 IOstreamOption streamOpt,
61 const bool append
62)
63:
64 Foam::ofstreamPointer(pathname, streamOpt.compression(), append),
65 OSstream(*(ofstreamPointer::get()), pathname, streamOpt)
66{
67 setClosed();
68 setState(ofstreamPointer::get()->rdstate());
69
70 if (good())
71 {
72 setOpened();
73 }
74 else
75 {
76 setBad();
77 }
78
79 lineNumber_ = 1;
80
81 if (debug)
82 {
83 if (pathname.empty())
84 {
86 << "Cannot open empty file name"
87 << Foam::endl;
88 }
89
90 if (!opened())
91 {
93 << "Could not open file " << pathname
94 << " for output\n" << info() << Foam::endl;
95 }
96 }
97}
98
99
100// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
101
103{
104 std::ostream* ptr = ofstreamPointer::get();
105
106 if (!ptr)
107 {
109 << "No stream allocated\n"
110 << abort(FatalError);
111 }
112
113 return *ptr;
114}
115
116
117const std::ostream& Foam::OFstream::stdStream() const
118{
119 const std::ostream* ptr = ofstreamPointer::get();
120
121 if (!ptr)
122 {
124 << "No stream allocated\n"
125 << abort(FatalError);
126 }
127
128 return *ptr;
129}
130
131
133{
135 {
136 ofstreamPointer::reopen_gz(this->name() + ".gz");
137 }
138 else
139 {
140 // Reopen (truncate)
142 }
143
144 // As per OSstream::rewind()
145
146 lineNumber_ = 1; // Reset line number
147 setState(ofstreamPointer::get()->rdstate());
148
149 if (good())
150 {
151 setOpened();
152 }
153 else
154 {
155 setClosed();
156 setBad();
157 }
158
159 stdStream().rdbuf()->pubseekpos(0, std::ios_base::out);
160}
161
162
164{
165 os << "OFstream: ";
167}
168
169
170// ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
The IOstreamOption is a simple container for options an IOstream can normally have.
@ COMPRESSED
compression = true
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
bool opened() const noexcept
True if stream has been opened.
Definition: IOstream.H:221
void setBad()
Set stream state to be 'bad'.
Definition: IOstream.H:369
label lineNumber_
The file line.
Definition: IOstream.H:121
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:413
void setClosed() noexcept
Set stream closed.
Definition: IOstream.H:135
void setState(std::ios_base::iostate state) noexcept
Set stream state.
Definition: IOstream.H:141
void setOpened() noexcept
Set stream opened.
Definition: IOstream.H:129
Output to file stream, using an OSstream.
Definition: OFstream.H:57
virtual std::ostream & stdStream()
Access to underlying std::ostream.
Definition: OFstream.C:102
virtual void rewind()
Definition: OFstream.C:132
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:57
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A class for handling file names.
Definition: fileName.H:76
scalar print()
Print to screen.
A wrapped std::ofstream with possible compression handling (ogzstream) that behaves much like a std::...
void reopen_gz(const std::string &pathname_gz)
Special 'rewind' method for compressed stream.
std::ostream * get() noexcept
The stream pointer (ofstream or ogzstream)
void reopen(const std::string &pathname)
General 'rewind' method (non-compressed)
IOstreamOption::compressionType whichCompression() const
Which compression type?
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
#define InfoInFunction
Report an information message using Foam::Info.
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
Namespace for OpenFOAM.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
errorManip< error > abort(error &err)
Definition: errorManip.H:144
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59