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 #include "gzstream.h"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineTypeNameAndDebug(OFstream, 0);
38 }
39 
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
44 (
45  const fileName& pathname,
47  const bool append
48 )
49 :
50  allocatedPtr_(nullptr)
51 {
52  if (pathname.empty())
53  {
54  if (OFstream::debug)
55  {
56  InfoInFunction << "Cannot open null file " << endl;
57  }
58  }
59 
60  std::ios_base::openmode mode(std::ios_base::out|std::ios_base::binary);
61  if (append)
62  {
63  mode |= std::ios_base::app;
64  }
65 
66  if (comp == IOstream::COMPRESSED)
67  {
68  // Get identically named uncompressed version out of the way
69  fileName gzPathName(pathname + ".gz");
70 
71  fileName::Type pathType = Foam::type(pathname, false);
72  if (pathType == fileName::FILE || pathType == fileName::LINK)
73  {
74  rm(pathname);
75  }
76 
77  if (!append && Foam::type(gzPathName) == fileName::LINK)
78  {
79  // Disallow writing into softlink to avoid any problems with
80  // e.g. softlinked initial fields
81  rm(gzPathName);
82  }
83 
84  allocatedPtr_.reset(new ogzstream(gzPathName.c_str(), mode));
85  }
86  else
87  {
88  // Get identically named compressed version out of the way
89  fileName gzPathName(pathname + ".gz");
90 
91  fileName::Type gzType = Foam::type(gzPathName, false);
92  if (gzType == fileName::FILE || gzType == fileName::LINK)
93  {
94  rm(gzPathName);
95  }
96 
97  if (!append && Foam::type(pathname, false) == fileName::LINK)
98  {
99  // Disallow writing into softlink to avoid any problems with
100  // e.g. softlinked initial fields
101  rm(pathname);
102  }
103 
104  allocatedPtr_.reset(new std::ofstream(pathname, mode));
105  }
106 }
107 
108 
109 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
110 
112 (
113  const fileName& pathname,
114  IOstreamOption streamOpt,
115  const bool append
116 )
117 :
118  Detail::OFstreamAllocator(pathname, streamOpt.compression(), append),
119  OSstream(*allocatedPtr_, pathname, streamOpt)
120 {
121  setClosed();
122  setState(allocatedPtr_->rdstate());
123 
124  if (!good())
125  {
126  if (debug)
127  {
129  << "Could not open file " << pathname
130  << " for output" << nl << info() << Foam::endl;
131  }
132 
133  setBad();
134  }
135  else
136  {
137  setOpened();
138  }
139 
140  lineNumber_ = 1;
141 }
142 
143 
144 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
145 
147 {
148  if (!allocatedPtr_)
149  {
151  << "No stream allocated." << abort(FatalError);
152  }
153  return *allocatedPtr_;
154 }
155 
156 
157 const std::ostream& Foam::OFstream::stdStream() const
158 {
159  if (!allocatedPtr_)
160  {
162  << "No stream allocated." << abort(FatalError);
163  }
164  return *allocatedPtr_;
165 }
166 
167 
169 {
170  os << "OFstream: ";
171  OSstream::print(os);
172 }
173 
174 
175 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:320
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::Detail::OFstreamAllocator
A std::ostream with the ability to handle compressed files.
Definition: OFstream.H:59
Foam::OFstream::OFstream
OFstream(const fileName &pathname, IOstreamOption streamOpt=IOstreamOption(), const bool append=false)
Construct from pathname.
Definition: OFstream.C:112
Foam::fileName::Type
Type
Enumerations to handle directory entry types.
Definition: fileName.H:76
Foam::rm
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: MSwindows.C:994
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
OFstream.H
Foam::mode
mode_t mode(const fileName &name, const bool followLink=true)
Return the file mode, normally following symbolic links.
Definition: MSwindows.C:564
Foam::Detail::OFstreamAllocator::OFstreamAllocator
OFstreamAllocator(const fileName &pathname, IOstream::compressionType comp=IOstream::UNCOMPRESSED, const bool append=false)
Construct from pathname.
Definition: OFstream.C:44
Foam::OFstream::print
void print(Ostream &os) const
Print stream description to Ostream.
Definition: OFstream.C:168
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:137
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:372
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::OFstream::stdStream
virtual std::ostream & stdStream()
Access to underlying std::ostream.
Definition: OFstream.C:146
Foam::Detail::OFstreamAllocator::allocatedPtr_
std::unique_ptr< std::ostream > allocatedPtr_
The allocated stream pointer (ofstream or ogzstream).
Definition: OFstream.H:66
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::OSstream::print
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: SstreamsPrint.C:45
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:77
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
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)