IFstream.H
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 OpenFOAM Foundation
9  Copyright (C) 2017-2024 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 Class
28  Foam::IFstream
29 
30 Description
31  Input from file stream as an ISstream, normally using \c std::ifstream
32  for the actual input.
33 
34 SourceFiles
35  IFstream.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef Foam_IFstream_H
40 #define Foam_IFstream_H
41 
42 #include "ISstream.H"
43 #include "className.H"
44 #include "fstreamPointer.H"
45 #include "DynamicList.H"
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 /*---------------------------------------------------------------------------*\
53  Class IFstream Declaration
54 \*---------------------------------------------------------------------------*/
55 
56 class IFstream
57 :
58  private Foam::ifstreamPointer,
59  public Foam::ISstream
60 {
61 public:
62 
63  //- Declare type-name (with debug switch)
64  ClassName("IFstream");
65 
66 
67  // Constructors
68 
69  //- Construct from pathname, default or specified stream options
70  explicit IFstream
71  (
72  const fileName& pathname,
73  IOstreamOption streamOpt = IOstreamOption()
74  );
75 
76  //- Construct from pathname and format
77  IFstream
78  (
79  const fileName& pathname,
81  )
82  :
83  IFstream(pathname, IOstreamOption(fmt))
84  {}
85 
86 
87  //- Destructor
88  ~IFstream() = default;
89 
90 
91  // Static Functions
92 
93  //- Get file contents from specified file (compressed/uncompressed).
94  //- Returns an empty list if the file cannot be opened.
95  static DynamicList<char> readContents(const fileName& pathname);
96 
97  //- Get file contents from IFstream (assumed to be rewound)
99 
100 
101  // Member Functions
102 
103  //- Get character(s)
104  using ISstream::get;
105 
106  //- Read/write access to the name of the stream
107  using ISstream::name;
108 
109  //- Return the size of the underlying file (-1 on error).
110  //- This corresponds to Foam::fileSize() but with extra handling of
111  //- compressed files.
112  // The return type is \c std::streamsize instead of \c off_t.
113  // \note Use sparingly since it involves a file stat()!
114  std::streamsize fileSize() const;
115 
116 
117  // STL stream
118 
119  //- Access to underlying std::istream
120  virtual std::istream& stdStream() override;
121 
122  //- Const access to underlying std::istream
123  virtual const std::istream& stdStream() const override;
124 
125  //- Rewind the stream so that it may be read again.
126  // Includes special handling for compressed streams.
127  virtual void rewind() override;
128 
129 
130  // Print
131 
132  //- Print stream description
133  virtual void print(Ostream& os) const override;
134 
135 
136  // Member Operators
137 
138  //- Return a non-const reference to const IFstream
139  // Needed for read-constructors where the stream argument is temporary:
140  // e.g. thing thisThing(IFstream("fileName")());
141  IFstream& operator()() const;
142 };
143 
144 
145 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
146 
147 } // End namespace Foam
148 
149 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
150 
151 #endif
152 
153 // ************************************************************************* //
A wrapped std::ifstream with possible compression handling (igzstream) that behaves much like a std::...
IFstream(const fileName &pathname, IOstreamOption streamOpt=IOstreamOption())
Construct from pathname, default or specified stream options.
Definition: IFstream.C:149
~IFstream()=default
Destructor.
A class for handling file names.
Definition: fileName.H:72
A simple container for options an IOstream can normally have.
ClassName("IFstream")
Declare type-name (with debug switch)
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
virtual void rewind() override
Rewind the stream so that it may be read again.
Definition: IFstream.C:269
virtual const fileName & name() const override
The name of the input serial stream. (eg, the name of the Fstream file name)
Definition: ISstream.H:147
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
virtual std::istream & stdStream() override
Access to underlying std::istream.
Definition: IFstream.C:239
OBJstream os(runTime.globalPath()/outputName)
Input from file stream as an ISstream, normally using std::ifstream for the actual input...
Definition: IFstream.H:51
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:200
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
Macro definitions for declaring ClassName(), NamespaceName(), etc.
static DynamicList< char > readContents(const fileName &pathname)
Get file contents from specified file (compressed/uncompressed). Returns an empty list if the file ca...
Definition: IFstream.C:130
IFstream & operator()() const
Return a non-const reference to const IFstream.
Definition: IFstream.C:293
streamFormat
Data format (ascii | binary)
virtual void print(Ostream &os) const override
Print stream description.
Definition: IFstream.C:284
ISstream & get(char &c)
Raw, low-level get character function.
Definition: ISstreamI.H:49
Namespace for OpenFOAM.