FreeFOAM The Cross-Platform CFD Toolkit
IFstream.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 "IFstream.H"
27 #include <OpenFOAM/OSspecific.H>
28 #include <OpenFOAM/gzstream.h>
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
33 
34 
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 
37 Foam::IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
38 :
39  ifPtr_(NULL),
40  compression_(IOstream::UNCOMPRESSED)
41 {
42  if (pathname.empty())
43  {
44  if (IFstream::debug)
45  {
46  Info<< "IFstreamAllocator::IFstreamAllocator(const fileName&) : "
47  "cannot open null file " << endl;
48  }
49  }
50 
51  ifPtr_ = new ifstream(pathname.c_str());
52 
53  // If the file is compressed, decompress it before reading.
54  if (!ifPtr_->good() && isFile(pathname + ".gz", false))
55  {
56  if (IFstream::debug)
57  {
58  Info<< "IFstreamAllocator::IFstreamAllocator(const fileName&) : "
59  "decompressing " << pathname + ".gz" << endl;
60  }
61 
62  delete ifPtr_;
63 
64  ifPtr_ = new igzstream((pathname + ".gz").c_str());
65 
66  if (ifPtr_->good())
67  {
68  compression_ = IOstream::COMPRESSED;
69  }
70  }
71 }
72 
73 
74 Foam::IFstreamAllocator::~IFstreamAllocator()
75 {
76  delete ifPtr_;
77 }
78 
79 
81 {
82  if (!ifPtr_)
83  {
84  FatalErrorIn("IFstreamAllocator::stdStream()")
85  << "No stream allocated" << abort(FatalError);
86  }
87  return *ifPtr_;
88 }
89 
90 
91 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
92 
94 (
95  const fileName& pathname,
97  versionNumber version
98 )
99 :
100  IFstreamAllocator(pathname),
101  ISstream
102  (
103  *ifPtr_,
104  "IFstream.sourceFile_",
105  format,
106  version,
107  IFstreamAllocator::compression_
108  ),
109  pathname_(pathname)
110 {
111  setClosed();
112 
113  setState(ifPtr_->rdstate());
114 
115  if (!good())
116  {
117  if (debug)
118  {
119  Info<< "IFstream::IFstream(const fileName&,"
120  "streamFormat=ASCII,"
121  "versionNumber=currentVersion) : "
122  "could not open file for input"
123  << endl << info() << endl;
124  }
125 
126  setBad();
127  }
128  else
129  {
130  setOpened();
131  }
132 
133  lineNumber_ = 1;
134 }
135 
136 
137 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
138 
140 {}
141 
142 
143 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
144 
146 {
147  // Print File data
148  os << "IFstream: ";
149  ISstream::print(os);
150 }
151 
152 
153 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
154 
156 {
157  if (!good())
158  {
159  // also checks .gz file
160  if (isFile(pathname_, true))
161  {
162  check("IFstream::operator()");
163  FatalIOError.exit();
164  }
165  else
166  {
167  FatalIOErrorIn("IFstream::operator()", *this)
168  << "file " << pathname_ << " does not exist"
169  << exit(FatalIOError);
170  }
171  }
172 
173  return const_cast<IFstream&>(*this);
174 }
175 
176 
177 // ************************ vim: set sw=4 sts=4 et: ************************ //