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-2019 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 
43 (
44  const fileName& pathname,
45  IOstream::compressionType compression,
46  const bool append
47 )
48 :
49  allocatedPtr_(nullptr)
50 {
51  if (pathname.empty())
52  {
53  if (OFstream::debug)
54  {
55  InfoInFunction << "Cannot open null file " << endl;
56  }
57  }
58 
59  std::ios_base::openmode mode(std::ios_base::out|std::ios_base::binary);
60  if (append)
61  {
62  mode |= std::ios_base::app;
63  }
64 
65  if (compression == IOstream::COMPRESSED)
66  {
67  // Get identically named uncompressed version out of the way
68  fileName::Type pathType = Foam::type(pathname, false);
69  if (pathType == fileName::FILE || pathType == fileName::LINK)
70  {
71  rm(pathname);
72  }
73  fileName gzPathName(pathname + ".gz");
74 
75  if (!append && Foam::type(gzPathName) == fileName::LINK)
76  {
77  // Disallow writing into softlink to avoid any problems with
78  // e.g. softlinked initial fields
79  rm(gzPathName);
80  }
81 
82  allocatedPtr_ = new ogzstream(gzPathName.c_str(), mode);
83  }
84  else
85  {
86  // get identically named compressed version out of the way
87  fileName gzPathName(pathname + ".gz");
88  fileName::Type gzType = Foam::type(gzPathName, false);
89  if (gzType == fileName::FILE || gzType == fileName::LINK)
90  {
91  rm(gzPathName);
92  }
93  if (!append && Foam::type(pathname, false) == fileName::LINK)
94  {
95  // Disallow writing into softlink to avoid any problems with
96  // e.g. softlinked initial fields
97  rm(pathname);
98  }
99 
100  allocatedPtr_ = new std::ofstream(pathname, mode);
101  }
102 }
103 
104 
106 {
107  deallocate();
108 }
109 
110 
112 {
113  if (allocatedPtr_)
114  {
115  delete allocatedPtr_;
116  allocatedPtr_ = nullptr;
117  }
118 }
119 
120 
121 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
122 
124 (
125  const fileName& pathname,
128  compressionType compression,
129  const bool append
130 )
131 :
132  Detail::OFstreamAllocator(pathname, compression, append),
133  OSstream
134  (
135  *allocatedPtr_,
136  pathname,
137  format,
138  version,
139  compression
140  )
141 {
142  setClosed();
143  setState(allocatedPtr_->rdstate());
144 
145  if (!good())
146  {
147  if (debug)
148  {
150  << "Could not open file " << pathname
151  << " for output" << nl << info() << Foam::endl;
152  }
153 
154  setBad();
155  }
156  else
157  {
158  setOpened();
159  }
160 
161  lineNumber_ = 1;
162 }
163 
164 
165 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
166 
168 {
169  if (!allocatedPtr_)
170  {
172  << "No stream allocated." << abort(FatalError);
173  }
174  return *allocatedPtr_;
175 }
176 
177 
178 const std::ostream& Foam::OFstream::stdStream() const
179 {
180  if (!allocatedPtr_)
181  {
183  << "No stream allocated." << abort(FatalError);
184  }
185  return *allocatedPtr_;
186 }
187 
188 
190 {
191  os << "OFstream: ";
192  OSstream::print(os);
193 }
194 
195 
196 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:316
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:60
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:337
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::OFstream::print
void print(Ostream &os) const
Print description of IOstream to Ostream.
Definition: OFstream.C:189
format
word format(conversionProperties.get< word >("format"))
Foam::Detail::OFstreamAllocator::~OFstreamAllocator
~OFstreamAllocator()
Destructor.
Definition: OFstream.C:105
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:79
Foam::OFstream::OFstream
OFstream(const fileName &pathname, streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED, const bool append=false)
Construct from pathname.
Definition: OFstream.C:124
Foam::OSstream
Generic output stream.
Definition: OSstream.H:54
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:64
Foam::FatalError
error FatalError
Foam::Detail::OFstreamAllocator::deallocate
void deallocate()
Delete the stream pointer.
Definition: OFstream.C:111
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::foamVersion::version
const std::string version
OpenFOAM version (name or stringified number) as a std::string.
Foam::Detail::OFstreamAllocator::OFstreamAllocator
OFstreamAllocator(const fileName &pathname, IOstream::compressionType compression=IOstream::UNCOMPRESSED, const bool append=false)
Construct from pathname.
Definition: OFstream.C:43
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::OFstream::stdStream
virtual std::ostream & stdStream()
Access to underlying std::ostream.
Definition: OFstream.C:167
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 description of IOstream to Ostream.
Definition: SstreamsPrint.C:45
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:71
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::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)