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-2023 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" // For isFile(), fileSize()
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
37 }
38 
39 
40 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41 
43 (
44  const fileName& pathname,
45  IOstreamOption streamOpt
46 )
47 :
48  Foam::ifstreamPointer(pathname, streamOpt),
49  ISstream(*(ifstreamPointer::get()), pathname, streamOpt)
50 {
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  }
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 
94 std::streamsize Foam::IFstream::fileSize() const
95 {
96  const std::istream* ptr = ifstreamPointer::get();
97 
98  if (!ptr || this->name().empty())
99  {
100  return std::streamsize(-1);
101  }
102 
103  off_t fileLen = -1;
104 
106  {
107  fileLen = Foam::fileSize(this->name() + ".gz");
108  }
109  else
110  {
111  // TBD: special handing for wrapped icharstream
112  // if
113  // (
114  // const Foam::icharstream* charstr
115  // = dynamic_cast<const Foam::icharstream*>(ptr)>(ptr)
116  // )
117  // {
118  // return charstr->capacity();
119  // }
120 
121  fileLen = Foam::fileSize(this->name());
122  }
123 
124  if (fileLen >= 0)
125  {
126  return std::streamsize(fileLen);
127  }
128 
129  return std::streamsize(-1);
130 }
131 
132 
133 std::istream& Foam::IFstream::stdStream()
134 {
135  std::istream* ptr = ifstreamPointer::get();
136 
137  if (!ptr)
138  {
140  << "No stream allocated\n"
142  }
143 
144  return *ptr;
145 }
146 
147 
148 const std::istream& Foam::IFstream::stdStream() const
149 {
150  const std::istream* ptr = ifstreamPointer::get();
151 
152  if (!ptr)
153  {
155  << "No stream allocated\n"
157  }
158 
159  return *ptr;
160 }
161 
162 
164 {
166  {
167  lineNumber_ = 1; // Reset line number
169  setState(ifstreamPointer::get()->rdstate());
170  }
171  else
172  {
174  }
175 }
176 
177 
178 void Foam::IFstream::print(Ostream& os) const
179 {
180  os << "IFstream: ";
182 }
183 
184 
185 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
186 
188 {
189  if (!good())
190  {
191  // Also checks .gz file
192  if (Foam::isFile(this->name(), true))
193  {
195  FatalIOError.exit();
196  }
197  else
198  {
200  << "file " << this->name() << " does not exist"
201  << exit(FatalIOError);
202  }
203  }
204 
205  return const_cast<IFstream&>(*this);
206 }
207 
208 
209 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
label lineNumber_
The file line.
Definition: IOstream.H:140
A wrapped std::ifstream with possible compression handling (igzstream) that behaves much like a std::...
bool opened() const noexcept
True if stream has been opened.
Definition: IOstream.H:265
IFstream(const fileName &pathname, IOstreamOption streamOpt=IOstreamOption())
Construct from pathname, default or specified stream options.
Definition: IFstream.C:36
A class for handling file names.
Definition: fileName.H:72
virtual void rewind() override
Rewind the stream so that it may be read again.
Definition: ISstream.C:1078
off_t fileSize(const fileName &name, const bool followLink=true)
Return size of file or -1 on failure (normally follows symbolic links).
Definition: POSIX.C:905
void setClosed() noexcept
Set stream closed.
Definition: IOstream.H:158
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
void setState(std::ios_base::iostate state) noexcept
Set stream state.
Definition: IOstream.H:166
IOstreamOption::compressionType whichCompression() const
Which compression type?
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
InfoProxy< IOstream > info() const noexcept
Return info proxy, used to print IOstream information to a stream.
Definition: IOstream.H:517
void reopen_gz(const std::string &pathname)
Special &#39;rewind&#39; method for compressed stream.
A simple container for options an IOstream can normally have.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
void setBad()
Set stream state to be &#39;bad&#39;.
Definition: IOstream.H:459
virtual void rewind() override
Rewind the stream so that it may be read again.
Definition: IFstream.C:156
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
std::istream * get() noexcept
The stream pointer (ifstream or igzstream)
virtual const fileName & name() const override
The name of the input serial stream. (eg, the name of the Fstream file name)
Definition: ISstream.H:141
errorManip< error > abort(error &err)
Definition: errorManip.H:139
virtual std::istream & stdStream() override
Access to underlying std::istream.
Definition: IFstream.C:126
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:239
int debug
Static debugging option.
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
defineTypeNameAndDebug(combustionModel, 0)
compressionType compression() const noexcept
Get the stream compression.
Input from file stream, using an ISstream.
Definition: IFstream.H:49
static void check(const int retVal, const char *what)
std::streamsize fileSize() const
Return the size of the underlying file (-1 on error). This corresponds to Foam::fileSize() but with e...
Definition: IFstream.C:87
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: POSIX.C:877
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:281
IFstream & operator()() const
Return a non-const reference to const IFstream.
Definition: IFstream.C:180
void setOpened() noexcept
Set stream opened.
Definition: IOstream.H:150
virtual void print(Ostream &os) const override
Print stream description.
Definition: IFstream.C:171
virtual void print(Ostream &os) const override
Print stream description to Ostream.
Definition: SstreamsPrint.C:30
Namespace for OpenFOAM.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...
#define InfoInFunction
Report an information message using Foam::Info.