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 -------------------------------------------------------------------------------
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 {
36  defineTypeNameAndDebug(OFstream, 0);
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 
117 const 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)
141  ofstreamPointer::reopen(this->name());
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 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::OFstream::rewind
virtual void rewind()
Definition: OFstream.C:132
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:350
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::OFstream::OFstream
OFstream(std::nullptr_t)
Construct a null output file stream.
Definition: OFstream.C:43
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
OFstream.H
Foam::ofstreamPointer::get
std::ostream * get() noexcept
The stream pointer (ofstream or ogzstream)
Definition: fstreamPointer.H:231
Foam::OFstream::print
void print(Ostream &os) const
Print stream description.
Definition: OFstream.C:163
Foam::ofstreamPointer::reopen
void reopen(const std::string &pathname)
General 'rewind' method (non-compressed)
Definition: fstreamPointers.C:226
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::OSstream
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:54
Foam::FatalError
error FatalError
Foam::ofstreamPointer::reopen_gz
void reopen_gz(const std::string &pathname_gz)
Special 'rewind' method for compressed stream.
Definition: fstreamPointers.C:210
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::IOstreamOption::COMPRESSED
compression = true
Definition: IOstreamOption.H:80
Foam::OFstream::stdStream
virtual std::ostream & stdStream()
Access to underlying std::ostream.
Definition: OFstream.C:102
Foam::ofstreamPointer
A wrapped std::ofstream with possible compression handling (ogzstream) that behaves much like a std::...
Definition: fstreamPointer.H:161
Foam::OSstream::print
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: SstreamsPrint.C:45
Foam::PtrListOps::get
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::IOstreamOption::compression
compressionType compression() const noexcept
Get the stream compression.
Definition: IOstreamOption.H:312
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::ofstreamPointer::whichCompression
IOstreamOption::compressionType whichCompression() const
Which compression type?
Definition: fstreamPointers.C:257