All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OFstream.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "OFstream.H"
27 #include "OSspecific.H"
28 #include "gzstream.h"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34  defineTypeNameAndDebug(OFstream, 0);
35 }
36 
37 
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 
40 Foam::OFstreamAllocator::OFstreamAllocator
41 (
42  const fileName& filePath,
43  IOstream::compressionType compression,
44  const bool append
45 )
46 :
47  ofPtr_(nullptr)
48 {
49  if (filePath.empty())
50  {
51  if (OFstream::debug)
52  {
53  InfoInFunction << "Cannot open null file " << endl;
54  }
55  }
56  ofstream::openmode mode(ofstream::out);
57  if (append)
58  {
59  mode |= ofstream::app;
60  }
61 
62  if (compression == IOstream::COMPRESSED)
63  {
64  // Get identically named uncompressed version out of the way
65  fileType pathType = Foam::type(filePath, false, false);
66  if (pathType == fileType::file || pathType == fileType::link)
67  {
68  rm(filePath);
69  }
70  fileName gzfilePath(filePath + ".gz");
71 
72  if (!append && Foam::type(gzfilePath) == fileType::link)
73  {
74  // Disallow writing into softlink to avoid any problems with
75  // e.g. softlinked initial fields
76  rm(gzfilePath);
77  }
78 
79  ofPtr_ = new ogzstream(gzfilePath.c_str(), mode);
80  }
81  else
82  {
83  // get identically named compressed version out of the way
84  fileName gzfilePath(filePath + ".gz");
85  fileType gzType = Foam::type(gzfilePath, false, false);
86  if (gzType == fileType::file || gzType == fileType::link)
87  {
88  rm(gzfilePath);
89  }
90  if
91  (
92  !append
93  && Foam::type(filePath, false, false) == fileType::link
94  )
95  {
96  // Disallow writing into softlink to avoid any problems with
97  // e.g. softlinked initial fields
98  rm(filePath);
99  }
100 
101  ofPtr_ = new ofstream(filePath.c_str(), mode);
102  }
103 }
104 
105 
106 Foam::OFstreamAllocator::~OFstreamAllocator()
107 {
108  delete ofPtr_;
109 }
110 
111 
112 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
113 
115 (
116  const fileName& filePath,
117  streamFormat format,
118  versionNumber version,
119  compressionType compression,
120  const bool append
121 )
122 :
123  OFstreamAllocator(filePath, compression, append),
124  OSstream(*ofPtr_, "OFstream.sinkFile_", format, version, compression),
125  filePath_(filePath)
126 {
127  setClosed();
128  setState(ofPtr_->rdstate());
129 
130  if (!good())
131  {
132  if (debug)
133  {
135  << "Could not open file " << filePath
136  << "for input\n"
137  "in stream " << info() << Foam::endl;
138  }
139 
140  setBad();
141  }
142  else
143  {
144  setOpened();
145  }
146 
147  lineNumber_ = 1;
148 }
149 
150 
151 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
152 
154 {}
155 
156 
157 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
158 
160 {
161  if (!ofPtr_)
162  {
164  << "No stream allocated." << abort(FatalError);
165  }
166  return *ofPtr_;
167 }
168 
169 
170 const std::ostream& Foam::OFstream::stdStream() const
171 {
172  if (!ofPtr_)
173  {
175  << "No stream allocated." << abort(FatalError);
176  }
177  return *ofPtr_;
178 }
179 
180 
182 {
183  os << " OFstream: ";
184  OSstream::print(os);
185 }
186 
187 
188 // ************************************************************************* //
label lineNumber_
Definition: IOstream.H:228
mode_t mode(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file mode.
Definition: POSIX.C:461
A class for handling file names.
Definition: fileName.H:79
void setClosed()
Set stream closed.
Definition: IOstream.H:242
OFstream(const fileName &filePath, streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED, const bool append=false)
Construct from filePath.
Definition: OFstream.C:115
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
~OFstream()
Destructor.
Definition: OFstream.C:153
virtual void endl()
Add newline and flush stream.
Definition: OSstream.C:213
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
virtual ostream & stdStream()
Access to underlying std::ostream.
Definition: OFstream.C:159
OSstream(ostream &os, const string &name, streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Set stream status.
Definition: OSstreamI.H:31
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: OFstream.C:181
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
void setBad()
Set stream to be bad.
Definition: IOstream.H:484
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
fileType
Enumeration of file types.
Definition: fileName.H:66
errorManip< error > abort(error &err)
Definition: errorManip.H:131
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
defineTypeNameAndDebug(combustionModel, 0)
void setOpened()
Set stream opened.
Definition: IOstream.H:236
virtual void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: SstreamsPrint.C:43
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
Version number type.
Definition: IOstream.H:96
A std::ostream with ability to handle compressed files.
Definition: OFstream.H:56
void setState(ios_base::iostate state)
Set stream state.
Definition: IOstream.H:248
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:1017
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:528
Namespace for OpenFOAM.
#define InfoInFunction
Report an information message using Foam::Info.