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  IOstreamOption::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 == IOstreamOption::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 {
127  {
128  lineNumber_ = 1; // Reset line number
129  ifstreamPointer::reopen_gz(this->name() + ".gz");
130  setState(ifstreamPointer::get()->rdstate());
131  }
132  else
133  {
135  }
136 }
137 
138 
140 {
141  os << "IFstream: ";
143 }
144 
145 
146 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
147 
149 {
150  if (!good())
151  {
152  // Also checks .gz file
153  if (isFile(this->name(), true))
154  {
156  FatalIOError.exit();
157  }
158  else
159  {
161  << "file " << this->name() << " does not exist"
162  << exit(FatalIOError);
163  }
164  }
165 
166  return const_cast<IFstream&>(*this);
167 }
168 
169 
170 // ************************************************************************* //
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:350
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:73
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:139
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:148
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:369
Foam::ISstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ISstream.C:1064
Foam::ifstreamPointer::whichCompression
IOstreamOption::compressionType whichCompression() const
Which compression type?
Definition: fstreamPointers.C:243
Foam::check
static void check(const int retVal, const char *what)
Definition: ptscotchDecomp.C:80
Foam::ifstreamPointer::reopen_gz
void reopen_gz(const std::string &pathname_gz)
Special 'rewind' method for compressed stream.
Definition: fstreamPointers.C:194
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
os
OBJstream os(runTime.globalPath()/outputName)
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:243
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
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:295
Foam::PtrListOps::get
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
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)