Loading...
Searching...
No Matches
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-2024 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
37}
38
39
40// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41
43(
44 std::nullptr_t
45)
46:
47 Foam::ofstreamPointer(nullptr),
48 OSstream(*(ofstreamPointer::get()), "/dev/null")
49{
51 setOpened();
52
53 lineNumber_ = 1;
54}
55
56
58(
60 const fileName& pathname,
61 IOstreamOption streamOpt,
63)
64:
66 (
67 pathname,
68 streamOpt,
69 append,
70 (IOstreamOption::atomicType::ATOMIC == atomic)
71 ),
72 OSstream(*(ofstreamPointer::get()), pathname, streamOpt)
73{
74 setClosed();
75 setState(ofstreamPointer::get()->rdstate());
76
77 if (good())
78 {
79 setOpened();
80 }
81 else
82 {
83 setBad();
84 }
85
86 lineNumber_ = 1;
87
88 if (debug)
89 {
90 if (pathname.empty())
91 {
93 << "Cannot open empty file name"
94 << Foam::endl;
95 }
96
97 if (!opened())
98 {
100 << "Could not open file " << pathname
101 << " for output\n" << info() << Foam::endl;
103 }
104}
105
106
107// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
108
112}
113
114
115// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
116
117std::ostream& Foam::OFstream::stdStream()
118{
119 std::ostream* ptr = ofstreamPointer::get();
120
121 if (!ptr)
122 {
124 << "No stream allocated\n"
126 }
127
128 return *ptr;
129}
130
131
132const std::ostream& Foam::OFstream::stdStream() const
133{
134 const std::ostream* ptr = ofstreamPointer::get();
135
136 if (!ptr)
137 {
139 << "No stream allocated\n"
141 }
142
143 return *ptr;
144}
145
146
148{
149 // Reopen (truncate) std::ostream
151
152 // As per OSstream::rewind()
153
154 lineNumber_ = 1; // Reset line number
155 setState(ofstreamPointer::get()->rdstate());
156
157 if (good())
158 {
159 setOpened();
160 }
161 else
162 {
163 setClosed();
165 }
166
167 stdStream().rdbuf()->pubseekpos(0, std::ios_base::out);
168}
169
170
171void Foam::OFstream::print(Ostream& os) const
172{
173 os << "OFstream: ";
175}
176
177
178// ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
A simple container for options an IOstream can normally have.
atomicType
Atomic operations (output).
appendType
File appending (NO_APPEND | APPEND_APP | APPEND_ATE).
void setBad() noexcept
Set stream state to be 'bad'.
Definition IOstream.H:488
bool good() const noexcept
True if next operation might succeed.
Definition IOstream.H:281
bool opened() const noexcept
True if stream has been opened.
Definition IOstream.H:265
label lineNumber_
The file line.
Definition IOstream.H:140
InfoProxy< IOstream > info() const noexcept
Return info proxy, used to print IOstream information to a stream.
Definition IOstream.H:551
void setClosed() noexcept
Set stream closed.
Definition IOstream.H:158
void setState(std::ios_base::iostate state) noexcept
Set stream state.
Definition IOstream.H:166
void setOpened() noexcept
Set stream opened.
Definition IOstream.H:150
Output to file stream as an OSstream, normally using std::ofstream for the actual output.
Definition OFstream.H:75
void print(Ostream &os) const override
Print stream description.
Definition OFstream.C:164
OFstream(std::nullptr_t)
Construct a null output file stream that behaves like /dev/null.
Definition OFstream.C:36
~OFstream()
Destructor. Possibly invokes an atomic rename (preference defined during construction).
Definition OFstream.C:102
virtual const std::ostream & stdStream() const override
Const access to underlying std::ostream.
Definition OFstream.C:125
virtual void rewind()
Rewind the stream so that it may be written again. Reopens the file (truncation).
Definition OFstream.C:140
Generic output stream using a standard (STL) stream.
Definition OSstream.H:53
OSstream(const OSstream &)=default
Copy construct.
virtual void print(Ostream &os) const override
Print stream description to Ostream.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A class for handling file names.
Definition fileName.H:75
A wrapped std::ofstream with possible compression handling (ogzstream) that behaves much like a std::...
ofstreamPointer(const ofstreamPointer &)=delete
No copy construct.
std::ostream * get() noexcept
The stream pointer (ofstream or ogzstream).
void reopen(const std::string &pathname)
Reopen for compressed/non-compressed. Discards append status.
void close(const std::string &pathname)
Close stream and rename file.
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
OBJstream os(runTime.globalPath()/outputName)
auto & name
#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 handling debugging switches.
Definition debug.C:45
Namespace for OpenFOAM.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
errorManip< error > abort(error &err)
Definition errorManip.H:139
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...