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 | Copyright (C) 2011-2017 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& pathname,
43  IOstream::compressionType compression,
44  const bool append
45 )
46 :
47  ofPtr_(nullptr)
48 {
49  if (pathname.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  fileName::Type pathType = Foam::type(pathname, false);
66  if (pathType == fileName::FILE || pathType == fileName::LINK)
67  {
68  rm(pathname);
69  }
70  fileName gzPathName(pathname + ".gz");
71 
72  if (!append && Foam::type(gzPathName) == fileName::LINK)
73  {
74  // Disallow writing into softlink to avoid any problems with
75  // e.g. softlinked initial fields
76  rm(gzPathName);
77  }
78 
79  ofPtr_ = new ogzstream(gzPathName.c_str(), mode);
80  }
81  else
82  {
83  // get identically named compressed version out of the way
84  fileName gzPathName(pathname + ".gz");
85  fileName::Type gzType = Foam::type(gzPathName, false);
86  if (gzType == fileName::FILE || gzType == fileName::LINK)
87  {
88  rm(gzPathName);
89  }
90  if (!append && Foam::type(pathname, false) == fileName::LINK)
91  {
92  // Disallow writing into softlink to avoid any problems with
93  // e.g. softlinked initial fields
94  rm(pathname);
95  }
96 
97  ofPtr_ = new ofstream(pathname.c_str(), mode);
98  }
99 }
100 
101 
102 Foam::OFstreamAllocator::~OFstreamAllocator()
103 {
104  delete ofPtr_;
105 }
106 
107 
108 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
109 
111 (
112  const fileName& pathname,
113  streamFormat format,
114  versionNumber version,
115  compressionType compression,
116  const bool append
117 )
118 :
119  OFstreamAllocator(pathname, compression, append),
120  OSstream(*ofPtr_, "OFstream.sinkFile_", format, version, compression),
121  pathname_(pathname)
122 {
123  setClosed();
124  setState(ofPtr_->rdstate());
125 
126  if (!good())
127  {
128  if (debug)
129  {
131  << "Could not open file " << pathname
132  << "for input\n"
133  "in stream " << info() << Foam::endl;
134  }
135 
136  setBad();
137  }
138  else
139  {
140  setOpened();
141  }
142 
143  lineNumber_ = 1;
144 }
145 
146 
147 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
148 
150 {}
151 
152 
153 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
154 
156 {
157  if (!ofPtr_)
158  {
160  << "No stream allocated." << abort(FatalError);
161  }
162  return *ofPtr_;
163 }
164 
165 
166 const std::ostream& Foam::OFstream::stdStream() const
167 {
168  if (!ofPtr_)
169  {
171  << "No stream allocated." << abort(FatalError);
172  }
173  return *ofPtr_;
174 }
175 
176 
178 {
179  os << " OFstream: ";
180  OSstream::print(os);
181 }
182 
183 
184 // ************************************************************************* //
label lineNumber_
Definition: IOstream.H:231
OFstream(const fileName &pathname, streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED, const bool append=false)
Construct from pathname.
Definition: OFstream.C:111
A class for handling file names.
Definition: fileName.H:69
void setClosed()
Set stream closed.
Definition: IOstream.H:245
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
~OFstream()
Destructor.
Definition: OFstream.C:149
virtual void endl()
Add newline and flush stream.
Definition: OSstream.C:252
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
virtual ostream & stdStream()
Access to underlying std::ostream.
Definition: OFstream.C:155
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:333
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: OFstream.C:177
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:487
mode_t mode(const fileName &, const bool followLink=true)
Return the file mode.
Definition: POSIX.C:467
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
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:53
defineTypeNameAndDebug(combustionModel, 0)
fileName::Type type(const fileName &, const bool followLink=true)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:485
void setOpened()
Set stream opened.
Definition: IOstream.H:239
virtual void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: SstreamsPrint.C:43
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:251
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:984
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:531
Type
Enumerations to handle file types and modes.
Definition: fileName.H:82
Namespace for OpenFOAM.
#define InfoInFunction
Report an information message using Foam::Info.