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-2015 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 
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  // Use binary mode in case we write binary.
76  // Causes windows reading to fail if we don't
77  ofPtr_ = new ofstream(pathname.c_str(),
78  ios_base::out|ios_base::binary);
79  }
80 }
81 
82 
84 {
85  delete ofPtr_;
86 }
87 
88 
89 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
90 
92 (
93  const fileName& pathname,
95  versionNumber version,
96  compressionType compression
97 )
98 :
99  OFstreamAllocator(pathname, compression),
100  OSstream(*ofPtr_, "OFstream.sinkFile_", format, version, compression),
101  pathname_(pathname)
102 {
103  setClosed();
104  setState(ofPtr_->rdstate());
105 
106  if (!good())
107  {
108  if (debug)
109  {
110  Info<< "OFstream::OFstream(const fileName&,"
111  "streamFormat, versionNumber, compressionType) : "
112  "could not open file " << pathname
113  << "for input\n"
114  "in stream " << info() << Foam::endl;
115  }
116 
117  setBad();
118  }
119  else
120  {
121  setOpened();
122  }
123 
124  lineNumber_ = 1;
125 }
126 
127 
128 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
129 
131 {}
132 
133 
134 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
135 
137 {
138  if (!ofPtr_)
139  {
141  << "No stream allocated." << abort(FatalError);
142  }
143  return *ofPtr_;
144 }
145 
146 
147 const std::ostream& Foam::OFstream::stdStream() const
148 {
149  if (!ofPtr_)
150  {
152  << "No stream allocated." << abort(FatalError);
153  }
154  return *ofPtr_;
155 }
156 
157 
159 {
160  os << " OFstream: ";
161  OSstream::print(os);
162 }
163 
164 
165 // ************************************************************************* //
format
word format(conversionProperties.lookup("format"))
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::rm
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:954
Foam::IOstream::compressionType
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
Foam::OSstream::print
virtual void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: SstreamsPrint.C:43
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::OFstreamAllocator::OFstream
friend class OFstream
Definition: OFstream.H:58
OFstream.H
Foam::IOstream::versionNumber
Version number type.
Definition: IOstream.H:96
Foam::OFstreamAllocator::~OFstreamAllocator
~OFstreamAllocator()
Destructor.
Definition: OFstream.C:83
Foam::Info
messageStream Info
Foam::OSstream
Generic output stream.
Definition: OSstream.H:51
Foam::FatalError
error FatalError
Foam::OFstream::~OFstream
~OFstream()
Destructor.
Definition: OFstream.C:130
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::isFile
bool isFile(const fileName &, const bool checkGzip=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:622
Foam::OFstreamAllocator::ofPtr_
ostream * ofPtr_
Definition: OFstream.H:60
Foam::OFstream::stdStream
virtual ostream & stdStream()
Access to underlying std::ostream.
Definition: OFstream.C:136
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::OFstreamAllocator
A std::ostream with ability to handle compressed files.
Definition: OFstream.H:56
Foam::OFstream::print
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: OFstream.C:158
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::OFstreamAllocator::OFstreamAllocator
OFstreamAllocator(const fileName &pathname, IOstream::compressionType compression=IOstream::UNCOMPRESSED)
Construct from pathname.
Definition: OFstream.C:41
Foam::IOstream::streamFormat
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86