FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/OSspecific.H>
28 #include <OpenFOAM/gzstream.h>
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
33 
34 
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 
37 Foam::OFstreamAllocator::OFstreamAllocator
38 (
39  const fileName& pathname,
40  IOstream::compressionType compression
41 )
42 :
43  ofPtr_(NULL)
44 {
45  if (pathname.empty())
46  {
47  if (OFstream::debug)
48  {
49  Info<< "OFstreamAllocator::OFstreamAllocator(const fileName&) : "
50  "cannot open null file " << endl;
51  }
52  }
53 
54  if (compression == IOstream::COMPRESSED)
55  {
56  // get identically named uncompressed version out of the way
57  if (isFile(pathname, false))
58  {
59  rm(pathname);
60  }
61 
62  ofPtr_ = new ogzstream((pathname + ".gz").c_str());
63  }
64  else
65  {
66  // get identically named compressed version out of the way
67  if (isFile(pathname + ".gz", false))
68  {
69  rm(pathname + ".gz");
70  }
71 
72  ofPtr_ = new ofstream(pathname.c_str());
73  }
74 }
75 
76 
77 Foam::OFstreamAllocator::~OFstreamAllocator()
78 {
79  delete ofPtr_;
80 }
81 
82 
84 {
85  if (!ofPtr_)
86  {
87  FatalErrorIn("OFstreamAllocator::stdStream()")
88  << "No stream allocated." << abort(FatalError);
89  }
90  return *ofPtr_;
91 }
92 
93 
94 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
95 
97 (
98  const fileName& pathname,
100  versionNumber version,
101  compressionType compression
102 )
103 :
104  OFstreamAllocator(pathname, compression),
105  OSstream(*ofPtr_, "OFstream.sinkFile_", format, version, compression),
106  pathname_(pathname)
107 {
108  setClosed();
109  setState(ofPtr_->rdstate());
110 
111  if (!good())
112  {
113  if (debug)
114  {
115  Info<< "IFstream::IFstream(const fileName&,"
116  "streamFormat format=ASCII,"
117  "versionNumber version=currentVersion) : "
118  "could not open file for input\n"
119  "in stream " << info() << Foam::endl;
120  }
121 
122  setBad();
123  }
124  else
125  {
126  setOpened();
127  }
128 
129  lineNumber_ = 1;
130 }
131 
132 
133 // * * * * * * * * * * * * * * * * Destructors * * * * * * * * * * * * * * * //
134 
136 {}
137 
138 
139 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
140 
142 {
143  os << " OFstream: ";
144  OSstream::print(os);
145 }
146 
147 
148 // ************************ vim: set sw=4 sts=4 et: ************************ //