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-2020 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 {
134  os << "OFstream: ";
135  OSstream::print(os);
136 }
137 
138 
139 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:325
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:69
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:350
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:221
Foam::OFstream::print
void print(Ostream &os) const
Print stream description.
Definition: OFstream.C:132
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
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:381
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::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:315
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)