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-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 
34 namespace 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());
52 
53  lineNumber_ = 1;
54 }
55 
56 
58 (
60  const fileName& pathname,
61  IOstreamOption streamOpt,
63 )
64 :
66  (
67  pathname,
68  streamOpt,
69  (IOstreamOption::appendType::APPEND == 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;
102  }
103  }
104 }
105 
106 
107 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
108 
110 {
111  ofstreamPointer::close(this->name());
112 }
113 
114 
115 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
116 
117 std::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 
132 const 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
150  ofstreamPointer::reopen(this->name());
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();
164  setBad();
165  }
166 
167  stdStream().rdbuf()->pubseekpos(0, std::ios_base::out);
168 }
169 
170 
171 void Foam::OFstream::print(Ostream& os) const
172 {
173  os << "OFstream: ";
175 }
176 
177 
178 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
label lineNumber_
The file line.
Definition: IOstream.H:140
bool opened() const noexcept
True if stream has been opened.
Definition: IOstream.H:265
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
A wrapped std::ofstream with possible compression handling (ogzstream) that behaves much like a std::...
A class for handling file names.
Definition: fileName.H:72
void setClosed() noexcept
Set stream closed.
Definition: IOstream.H:158
std::ostream * get() noexcept
The stream pointer (ofstream or ogzstream)
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
void setState(std::ios_base::iostate state) noexcept
Set stream state.
Definition: IOstream.H:166
~OFstream()
Destructor. Possibly invokes an atomic rename (preference defined during construction) ...
Definition: OFstream.C:102
Output to file stream, using an OSstream.
Definition: OFstream.H:49
void print(Ostream &os) const override
Print stream description.
Definition: OFstream.C:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
InfoProxy< IOstream > info() const noexcept
Return info proxy, used to print IOstream information to a stream.
Definition: IOstream.H:517
A simple container for options an IOstream can normally have.
Generate append-data.
Definition: foamVtkCore.H:57
virtual const std::ostream & stdStream() const override
Const access to underlying std::ostream.
Definition: OFstream.C:125
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
void setBad()
Set stream state to be &#39;bad&#39;.
Definition: IOstream.H:459
atomicType
Atomic operations (output)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
virtual void rewind()
Rewind the stream so that it may be written again. Reopens the file (truncation)
Definition: OFstream.C:140
errorManip< error > abort(error &err)
Definition: errorManip.H:139
void reopen(const std::string &pathname)
Reopen for compressed/non-compressed.
int debug
Static debugging option.
OBJstream os(runTime.globalPath()/outputName)
defineTypeNameAndDebug(combustionModel, 0)
appendType
File appending (NON_APPEND | APPEND)
OFstream(std::nullptr_t)
Construct a null output file stream.
Definition: OFstream.C:36
void close(const std::string &pathname)
Close stream and rename file.
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:281
void setOpened() noexcept
Set stream opened.
Definition: IOstream.H:150
virtual void print(Ostream &os) const override
Print stream description to Ostream.
Definition: SstreamsPrint.C:39
Namespace for OpenFOAM.
#define InfoInFunction
Report an information message using Foam::Info.