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-------------------------------------------------------------------------------
11License
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
42namespace 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::SYMLINK)
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::SYMLINK)
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
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
185 {
186 removeConflictingFiles(pathname_gz, append, pathname);
187 ptr_.reset(new std::ofstream(pathname, mode));
188 }
189}
190
191
192// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
193
194void 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
210void 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
226void 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 {
249 }
250 #endif /* HAVE_LIBZ */
251
253}
254
255
258{
259 #ifdef HAVE_LIBZ
260 if (dynamic_cast<const ogzstream*>(ptr_.get()))
261 {
263 }
264 #endif /* HAVE_LIBZ */
265
267}
268
269
270// ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
@ UNCOMPRESSED
compression = false
@ COMPRESSED
compression = true
A class for handling file names.
Definition: fileName.H:76
Type
Enumerations to handle directory entry types.
Definition: fileName.H:81
@ SYMLINK
A symbolic link.
Definition: fileName.H:85
@ FILE
A regular file.
Definition: fileName.H:83
void reopen_gz(const std::string &pathname_gz)
Special 'rewind' method for compressed stream.
IOstreamOption::compressionType whichCompression() const
Which compression type?
static bool supports_gz()
True if compiled with libz support.
ifstreamPointer() noexcept=default
Default construct (empty)
Trivial output stream for calculating byte counts.
Definition: OCountStream.H:153
void reopen_gz(const std::string &pathname_gz)
Special 'rewind' method for compressed stream.
void reopen(const std::string &pathname)
General 'rewind' method (non-compressed)
IOstreamOption::compressionType whichCompression() const
Which compression type?
static bool supports_gz()
True if compiled with libz support.
ofstreamPointer() noexcept=default
Default construct (empty)
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
Namespace for OpenFOAM.
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: MSwindows.C:1012
mode_t mode(const fileName &name, const bool followLink=true)
Return the file mode, normally following symbolic links.
Definition: MSwindows.C:572
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:598
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
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:666
error FatalError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
static void removeConflictingFiles(const fileName &otherName, const bool append, const fileName &targetName)
messageStream Warning
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53