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-2016 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 )
45 :
46  ofPtr_(NULL)
47 {
48  if (pathname.empty())
49  {
50  if (OFstream::debug)
51  {
52  InfoInFunction << "Cannot open null file " << endl;
53  }
54  }
55 
56  if (compression == IOstream::COMPRESSED)
57  {
58  // get identically named uncompressed version out of the way
59  if (isFile(pathname, false))
60  {
61  rm(pathname);
62  }
63 
64  ofPtr_ = new ogzstream((pathname + ".gz").c_str());
65  }
66  else
67  {
68  // get identically named compressed version out of the way
69  if (isFile(pathname + ".gz", false))
70  {
71  rm(pathname + ".gz");
72  }
73 
74  ofPtr_ = new ofstream(pathname.c_str());
75  }
76 }
77 
78 
79 Foam::OFstreamAllocator::~OFstreamAllocator()
80 {
81  delete ofPtr_;
82 }
83 
84 
85 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
86 
88 (
89  const fileName& pathname,
90  streamFormat format,
91  versionNumber version,
92  compressionType compression
93 )
94 :
95  OFstreamAllocator(pathname, compression),
96  OSstream(*ofPtr_, "OFstream.sinkFile_", format, version, compression),
97  pathname_(pathname)
98 {
99  setClosed();
100  setState(ofPtr_->rdstate());
101 
102  if (!good())
103  {
104  if (debug)
105  {
107  << "Could not open file " << pathname
108  << "for input\n"
109  "in stream " << info() << Foam::endl;
110  }
111 
112  setBad();
113  }
114  else
115  {
116  setOpened();
117  }
118 
119  lineNumber_ = 1;
120 }
121 
122 
123 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
124 
126 {}
127 
128 
129 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
130 
132 {
133  if (!ofPtr_)
134  {
136  << "No stream allocated." << abort(FatalError);
137  }
138  return *ofPtr_;
139 }
140 
141 
142 const std::ostream& Foam::OFstream::stdStream() const
143 {
144  if (!ofPtr_)
145  {
147  << "No stream allocated." << abort(FatalError);
148  }
149  return *ofPtr_;
150 }
151 
152 
154 {
155  os << " OFstream: ";
156  OSstream::print(os);
157 }
158 
159 
160 // ************************************************************************* //
label lineNumber_
Definition: IOstream.H:231
A class for handling file names.
Definition: fileName.H:69
void setClosed()
Set stream closed.
Definition: IOstream.H:245
error FatalError
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:531
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
~OFstream()
Destructor.
Definition: OFstream.C:125
virtual void endl()
Add newline and flush stream.
Definition: OSstream.C:252
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
bool isFile(const fileName &, const bool checkGzip=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:492
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
virtual ostream & stdStream()
Access to underlying std::ostream.
Definition: OFstream.C:131
OSstream(ostream &os, const string &name, streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Set stream status.
Definition: OSstreamI.H:31
OFstream(const fileName &pathname, streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Construct from pathname.
Definition: OFstream.C:88
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
virtual void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: SstreamsPrint.C:43
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
errorManip< error > abort(error &err)
Definition: errorManip.H:131
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: OFstream.C:153
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)
void setOpened()
Set stream opened.
Definition: IOstream.H:239
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:819
Namespace for OpenFOAM.
#define InfoInFunction
Report an information message using Foam::Info.