fstreamPointers.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 OpenFOAM Foundation
9  Copyright (C) 2018-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 "fstreamPointer.H"
30 #include "OCountStream.H"
31 #include "OSspecific.H"
32 
33 // HAVE_LIBZ defined externally
34 // #define HAVE_LIBZ
35 
36 #ifdef HAVE_LIBZ
37 #include "gzstream.h"
38 #endif /* HAVE_LIBZ */
39 
40 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
41 
42 namespace Foam
43 {
44  static inline void removeConflictingFiles
45  (
46  const fileName& otherName,
47  const bool append,
48  const fileName& targetName
49  )
50  {
51  // Remove other (compressed/uncompressed) version
52 
53  const fileName::Type pathType = Foam::type(otherName, false);
54 
55  if (pathType == fileName::FILE || pathType == fileName::LINK)
56  {
57  Foam::rm(otherName);
58  }
59 
60  // Disallow writing into symlinked files.
61  // Eg, avoid problems with symlinked initial fields
62 
63  if (!append && Foam::type(targetName, false) == fileName::LINK)
64  {
65  Foam::rm(targetName);
66  }
67  }
68 }
69 
70 
71 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
72 
74 {
75  #ifdef HAVE_LIBZ
76  return true;
77  #else
78  return false;
79  #endif
80 }
81 
82 
84 {
85  #ifdef HAVE_LIBZ
86  return true;
87  #else
88  return false;
89  #endif
90 }
91 
92 
93 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
94 
96 (
97  const fileName& pathname
98 )
99 :
100  ptr_(nullptr)
101 {
102  const std::ios_base::openmode mode
103  (
104  std::ios_base::in | std::ios_base::binary
105  );
106 
107  ptr_.reset(new std::ifstream(pathname, mode));
108 
109  if (!ptr_->good())
110  {
111  // Try compressed version instead
112 
113  const fileName pathname_gz(pathname + ".gz");
114 
115  if (isFile(pathname_gz, false))
116  {
117  #ifdef HAVE_LIBZ
118 
119  ptr_.reset(new igzstream(pathname_gz, mode));
120 
121  #else /* HAVE_LIBZ */
122 
123  FatalError
124  << "No read support for gz compressed files (libz)"
125  << " : could use 'gunzip' from the command-line" << nl
126  << "file: " << pathname_gz << endl
127  << exit(FatalError);
128 
129  #endif /* HAVE_LIBZ */
130  }
131  }
132 }
133 
134 
136 :
137  ptr_(new Foam::ocountstream)
138 {}
139 
140 
142 (
143  const fileName& pathname,
145  const bool append
146 )
147 :
148  ptr_(nullptr)
149 {
150  std::ios_base::openmode mode
151  (
152  std::ios_base::out | std::ios_base::binary
153  );
154 
155  if (append)
156  {
157  mode |= std::ios_base::app;
158  }
159 
160  const fileName pathname_gz(pathname + ".gz");
161 
162  if (IOstreamOption::COMPRESSED == comp)
163  {
164  // Output compression requested
165 
166  #ifdef HAVE_LIBZ
167 
168  removeConflictingFiles(pathname, append, pathname_gz);
169  ptr_.reset(new ogzstream(pathname_gz, mode));
170 
171  #else /* HAVE_LIBZ */
172 
174 
175  Warning
176  << nl
177  << "No write support for gz compressed files (libz)"
178  << " : downgraded to UNCOMPRESSED" << nl
179  << "file: " << pathname_gz << endl;
180 
181  #endif /* HAVE_LIBZ */
182  }
183 
184  if (IOstreamOption::UNCOMPRESSED == comp)
185  {
186  removeConflictingFiles(pathname_gz, append, pathname);
187  ptr_.reset(new std::ofstream(pathname, mode));
188  }
189 }
190 
191 
192 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
193 
194 void Foam::ifstreamPointer::reopen_gz(const std::string& pathname_gz)
195 {
196  #ifdef HAVE_LIBZ
197  igzstream* gz = dynamic_cast<igzstream*>(ptr_.get());
198 
199  if (gz)
200  {
201  // Special treatment for gzstream
202  gz->close();
203  gz->clear();
204  gz->open(pathname_gz);
205  }
206  #endif /* HAVE_LIBZ */
207 }
208 
209 
210 void Foam::ofstreamPointer::reopen_gz(const std::string& pathname_gz)
211 {
212  #ifdef HAVE_LIBZ
213  ogzstream* gz = dynamic_cast<ogzstream*>(ptr_.get());
214 
215  if (gz)
216  {
217  // Special treatment for gzstream
218  gz->close();
219  gz->clear();
220  gz->open(pathname_gz);
221  }
222  #endif /* HAVE_LIBZ */
223 }
224 
225 
226 void Foam::ofstreamPointer::reopen(const std::string& pathname)
227 {
228  std::ofstream* file = dynamic_cast<std::ofstream*>(ptr_.get());
229 
230  if (file)
231  {
232  if (file->is_open())
233  {
234  file->close();
235  }
236  file->clear();
237  file->open(pathname);
238  }
239 }
240 
241 
244 {
245  #ifdef HAVE_LIBZ
246  if (dynamic_cast<const igzstream*>(ptr_.get()))
247  {
248  return IOstreamOption::compressionType::COMPRESSED;
249  }
250  #endif /* HAVE_LIBZ */
251 
252  return IOstreamOption::compressionType::UNCOMPRESSED;
253 }
254 
255 
258 {
259  #ifdef HAVE_LIBZ
260  if (dynamic_cast<const ogzstream*>(ptr_.get()))
261  {
262  return IOstreamOption::compressionType::COMPRESSED;
263  }
264  #endif /* HAVE_LIBZ */
265 
266  return IOstreamOption::compressionType::UNCOMPRESSED;
267 }
268 
269 
270 // ************************************************************************* //
Foam::IOstreamOption::UNCOMPRESSED
compression = false
Definition: IOstreamOption.H:79
Foam::ifstreamPointer::supports_gz
static bool supports_gz()
True if compiled with libz support.
Definition: fstreamPointers.C:73
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::ofstreamPointer::supports_gz
static bool supports_gz()
True if compiled with libz support.
Definition: fstreamPointers.C:83
Foam::fileName::FILE
A file.
Definition: fileName.H:83
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::Warning
messageStream Warning
Foam::fileName::Type
Type
Enumerations to handle directory entry types.
Definition: fileName.H:80
Foam::isFile
bool isFile(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist as a FILE in the file system?
Definition: MSwindows.C:658
Foam::rm
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: MSwindows.C:1004
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))))
Foam::ifstreamPointer::whichCompression
IOstreamOption::compressionType whichCompression() const
Which compression type?
Definition: fstreamPointers.C:243
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::ifstreamPointer::reopen_gz
void reopen_gz(const std::string &pathname_gz)
Special 'rewind' method for compressed stream.
Definition: fstreamPointers.C:194
Foam::ofstreamPointer::reopen
void reopen(const std::string &pathname)
General 'rewind' method (non-compressed)
Definition: fstreamPointers.C:226
fstreamPointer.H
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
OCountStream.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::removeConflictingFiles
static void removeConflictingFiles(const fileName &otherName, const bool append, const fileName &targetName)
Definition: fstreamPointers.C:45
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::ofstreamPointer::ofstreamPointer
ofstreamPointer() noexcept=default
Default construct (empty)
Foam::ocountstream
Trivial output stream for calculating byte counts.
Definition: OCountStream.H:148
Foam::IOstreamOption::COMPRESSED
compression = true
Definition: IOstreamOption.H:80
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::fileName::LINK
A symlink.
Definition: fileName.H:85
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::ifstreamPointer::ifstreamPointer
ifstreamPointer() noexcept=default
Default construct (empty)
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:77
Foam::ofstreamPointer::whichCompression
IOstreamOption::compressionType whichCompression() const
Which compression type?
Definition: fstreamPointers.C:257