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-2013 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  Info<< "OFstreamAllocator::OFstreamAllocator(const fileName&) : "
53  "cannot open null file " << endl;
54  }
55  }
56 
57  if (compression == IOstream::COMPRESSED)
58  {
59  // get identically named uncompressed version out of the way
60  if (isFile(pathname, false))
61  {
62  rm(pathname);
63  }
64 
65  ofPtr_ = new ogzstream((pathname + ".gz").c_str());
66  }
67  else
68  {
69  // get identically named compressed version out of the way
70  if (isFile(pathname + ".gz", false))
71  {
72  rm(pathname + ".gz");
73  }
74 
75  ofPtr_ = new ofstream(pathname.c_str());
76  }
77 }
78 
79 
80 Foam::OFstreamAllocator::~OFstreamAllocator()
81 {
82  delete ofPtr_;
83 }
84 
85 
86 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
87 
89 (
90  const fileName& pathname,
91  streamFormat format,
92  versionNumber version,
93  compressionType compression
94 )
95 :
96  OFstreamAllocator(pathname, compression),
97  OSstream(*ofPtr_, "OFstream.sinkFile_", format, version, compression),
98  pathname_(pathname)
99 {
100  setClosed();
101  setState(ofPtr_->rdstate());
102 
103  if (!good())
104  {
105  if (debug)
106  {
107  Info<< "OFstream::OFstream(const fileName&,"
108  "streamFormat, versionNumber, compressionType) : "
109  "could not open file " << pathname
110  << "for input\n"
111  "in stream " << info() << Foam::endl;
112  }
113 
114  setBad();
115  }
116  else
117  {
118  setOpened();
119  }
120 
121  lineNumber_ = 1;
122 }
123 
124 
125 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
126 
128 {}
129 
130 
131 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
132 
134 {
135  if (!ofPtr_)
136  {
137  FatalErrorIn("OFstream::stdStream()")
138  << "No stream allocated." << abort(FatalError);
139  }
140  return *ofPtr_;
141 }
142 
143 
144 const std::ostream& Foam::OFstream::stdStream() const
145 {
146  if (!ofPtr_)
147  {
148  FatalErrorIn("OFstreamAllocator::stdStream() const")
149  << "No stream allocated." << abort(FatalError);
150  }
151  return *ofPtr_;
152 }
153 
154 
156 {
157  os << " OFstream: ";
158  OSstream::print(os);
159 }
160 
161 
162 // ************************************************************************* //
OSstream(ostream &os, const string &name, streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Set stream status.
Definition: OSstreamI.H:31
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:531
void setState(ios_base::iostate state)
Set stream state.
Definition: IOstream.H:251
void setOpened()
Set stream opened.
Definition: IOstream.H:239
label lineNumber_
Definition: IOstream.H:231
A std::ostream with ability to handle compressed files.
Definition: OFstream.H:56
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
messageStream Info
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:955
Namespace for OpenFOAM.
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: OFstream.C:155
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
void setClosed()
Set stream closed.
Definition: IOstream.H:245
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
void setBad()
Set stream to be bad.
Definition: IOstream.H:487
errorManip< error > abort(error &err)
Definition: errorManip.H:131
virtual ostream & stdStream()
Access to underlying std::ostream.
Definition: OFstream.C:133
virtual void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: SstreamsPrint.C:43
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
OFstream(const fileName &pathname, streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Construct from pathname.
Definition: OFstream.C:89
error FatalError
A class for handling file names.
Definition: fileName.H:69
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
~OFstream()
Destructor.
Definition: OFstream.C:127
virtual void endl()
Add newline and flush stream.
Definition: OSstream.C:253
bool isFile(const fileName &, const bool checkGzip=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:623
Version number type.
Definition: IOstream.H:96
defineTypeNameAndDebug(combustionModel, 0)