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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "IFstream.H"
30 #include "OSspecific.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeNameAndDebug(IFstream, 0);
37 }
38 
39 
40 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41 
43 (
44  const fileName& pathname,
45  IOstreamOption streamOpt
46 )
47 :
48  Foam::ifstreamPointer(pathname),
49  ISstream(*(ifstreamPointer::get()), pathname, streamOpt)
50 {
51  IOstream::compression(ifstreamPointer::whichCompression());
52 
53  setClosed();
54 
55  setState(ifstreamPointer::get()->rdstate());
56 
57  if (good())
58  {
59  setOpened();
60  }
61  else
62  {
63  setBad();
64  }
65 
66  lineNumber_ = 1;
67 
68  if (debug)
69  {
70  if (pathname.empty())
71  {
73  << "Cannot open empty file name"
74  << Foam::endl;
75  }
76  else if (IOstreamOption::COMPRESSED == IOstream::compression())
77  {
79  << "Decompressing " << (this->name() + ".gz") << Foam::endl;
80  }
81 
82  if (!opened())
83  {
85  << "Could not open file " << pathname
86  << " for input\n" << info() << Foam::endl;
87  }
88  }
89 }
90 
91 
92 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
93 
95 {
96  std::istream* ptr = ifstreamPointer::get();
97 
98  if (!ptr)
99  {
101  << "No stream allocated\n"
102  << abort(FatalError);
103  }
104 
105  return *ptr;
106 }
107 
108 
109 const std::istream& Foam::IFstream::stdStream() const
110 {
111  const std::istream* ptr = ifstreamPointer::get();
112 
113  if (!ptr)
114  {
116  << "No stream allocated\n"
117  << abort(FatalError);
118  }
119 
120  return *ptr;
121 }
122 
123 
125 {
126  lineNumber_ = 1; // Reset line number
127 
129  {
130  // Special treatment for compressed stream
131  ifstreamPointer::reopen_gz(this->name() + ".gz");
132 
133  setState(ifstreamPointer::get()->rdstate());
134  }
135  else
136  {
138  }
139 }
140 
141 
143 {
144  os << "IFstream: ";
145  ISstream::print(os);
146 }
147 
148 
149 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
150 
152 {
153  if (!good())
154  {
155  // Also checks .gz file
156  if (isFile(this->name(), true))
157  {
158  check(FUNCTION_NAME);
159  FatalIOError.exit();
160  }
161  else
162  {
164  << "file " << this->name() << " does not exist"
165  << exit(FatalIOError);
166  }
167  }
168 
169  return const_cast<IFstream&>(*this);
170 }
171 
172 
173 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::ISstream::print
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: SstreamsPrint.C:36
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:325
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::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:53
Foam::IFstream::print
virtual void print(Ostream &os) const
Print stream description.
Definition: IFstream.C:142
Foam::ISstream
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:55
Foam::IFstream::operator()
IFstream & operator()() const
Return a non-const reference to const IFstream.
Definition: IFstream.C:151
Foam::isFile
bool isFile(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist as a FILE in the file system?
Definition: MSwindows.C:658
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::ISstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ISstream.C:874
Foam::ifstreamPointer::whichCompression
IOstreamOption::compressionType whichCompression() const
Which compression type?
Definition: fstreamPointers.C:211
Foam::ifstreamPointer::reopen_gz
void reopen_gz(const std::string &pathname_gz)
Special 'rewind' method for compressed stream.
Definition: fstreamPointers.C:194
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
IFstream.H
Foam::FatalError
error FatalError
Foam::ifstreamPointer::get
std::istream * get() noexcept
The stream pointer (ifstream or igzstream)
Definition: fstreamPointer.H:123
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::IFstream::IFstream
IFstream(const fileName &pathname, IOstreamOption streamOpt=IOstreamOption())
Construct from pathname.
Definition: IFstream.C:43
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::IFstream::stdStream
virtual std::istream & stdStream()
Access to underlying std::istream.
Definition: IFstream.C:94
Foam::IOerror::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:249
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::IOstreamOption::COMPRESSED
compression = true
Definition: IOstreamOption.H:80
Foam::ifstreamPointer
A wrapped std::ifstream with possible compression handling (igzstream) that behaves much like a std::...
Definition: fstreamPointer.H:71
Foam::IFstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: IFstream.C:124
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)