ITstream.H 9.76 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
OpenFOAM bot's avatar
OpenFOAM bot committed
6
7
     \\/     M anipulation  |
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8
    Copyright (C) 2011-2016 OpenFOAM Foundation
9
    Copyright (C) 2017-2019 OpenCFD Ltd.
10
11
12
13
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

14
15
16
17
    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
18
19
20
21
22
23
24

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    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
30

Class
    Foam::ITstream

Description
31
    An input stream of tokens.
32
33

SourceFiles
Mark Olesen's avatar
Mark Olesen committed
34
    ITstream.C
35
36
37
38
39
40
41
42
43
44
45
46
47
48

\*---------------------------------------------------------------------------*/

#ifndef ITstream_H
#define ITstream_H

#include "Istream.H"
#include "tokenList.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{

49
50
51
// Forward declaration
class ISstream;

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*---------------------------------------------------------------------------*\
                           Class ITstream Declaration
\*---------------------------------------------------------------------------*/

class ITstream
:
    public Istream,
    public tokenList
{
    // Private data

        //- Name of ITstream
        fileName name_;

        //- Index of token currently being read
        label tokenIndex_;


70
71
    // Private Member Functions

72
73
74
        //- Convert input sequence into a list of tokens.
        //  \return the number of tokens in the resulting list.
        static label parseStream(ISstream& input, tokenList& tokens);
75

76
77
78
79
80
81
82
83
84
        //- An ad hoc combination of reserve and setCapacity somewhat
        //- similar to DynamicList.
        //
        //  In lazy mode, increase list size if needed, but leave any
        //  excess capacity - works like reserve.
        //
        //  In non-lazy mode, set exact capacity
        void reserveCapacity(const label nElem, const bool lazy);

85

86
87
88
89
90
91
92
93
public:

    // Constructors

        //- Construct from components
        ITstream
        (
            const string& name,
Mark Olesen's avatar
Mark Olesen committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
            const UList<token>& tokens,
            streamFormat format=ASCII,
            versionNumber version=currentVersion
        )
        :
            Istream(format, version),
            tokenList(tokens),
            name_(name),
            tokenIndex_(0)
        {
            setOpened();
            setGood();
        }


        //- Construct from components, transferring the tokens
        ITstream
        (
            const string& name,
113
            List<token>&& tokens,
114
115
116
117
118
            streamFormat format=ASCII,
            versionNumber version=currentVersion
        )
        :
            Istream(format, version),
119
            tokenList(std::move(tokens)),
120
121
122
123
124
125
126
127
            name_(name),
            tokenIndex_(0)
        {
            setOpened();
            setGood();
        }


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
        //- Construct token list by parsing the input character sequence
        //  Uses UIListStream internally.
        ITstream
        (
            const string& name,
            const UList<char>& input,
            streamFormat format=ASCII,
            versionNumber version=currentVersion
        );


        //- Construct token list by parsing the input string
        //  Uses UIListStream internally.
        ITstream
        (
            const string& name,
            const std::string& input,
            streamFormat format=ASCII,
            versionNumber version=currentVersion
        );


        //- Construct token list by parsing the input character sequence
        //  Uses UIListStream internally.
        ITstream
        (
            const string& name,
            const char* input,
            streamFormat format=ASCII,
            versionNumber version=currentVersion
        );


161
        //- Copy construct
162
        ITstream(const ITstream& is)
163
164
        :
            Istream(ASCII, currentVersion),
165
166
            tokenList(is),
            name_(is.name_),
167
168
169
170
171
172
173
            tokenIndex_(0)
        {
            setOpened();
            setGood();
        }


Mark Olesen's avatar
Mark Olesen committed
174
        //- Destructor
175
        virtual ~ITstream() = default;
henry's avatar
henry committed
176
177


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    // Static Functions

        //- Create token list by parsing the input character sequence until
        //- no good tokens remain.
        static tokenList parse
        (
            const UList<char>& input,
            streamFormat format=ASCII
        );

        //- Create token list by parsing the input string until
        //- no good tokens remain.
        static tokenList parse
        (
            const std::string& input,
            streamFormat format=ASCII
        );

        //- Create token list by parsing the input character sequence until
        //- no good tokens remain.
        static tokenList parse
        (
            const char* input,
            streamFormat format=ASCII
        );


205
    // Member Functions
206
207
208
209

        // Inquiry

            //- Return the name of the stream
210
            virtual const fileName& name() const
211
212
213
214
215
            {
                return name_;
            }

            //- Return non-const access to the name of the stream
216
            virtual fileName& name()
217
218
219
220
            {
                return name_;
            }

221
            //- The current token index when reading, or the insertion point.
222
223
224
225
226
            label tokenIndex() const
            {
                return tokenIndex_;
            }

227
            //- Non-const access to the current token index
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
            label& tokenIndex()
            {
                return tokenIndex_;
            }

            //- Return the number of remaining tokens
            label nRemainingTokens() const
            {
                return size() - tokenIndex_;
            }

            //- Return flags of output stream
            ios_base::fmtflags flags() const
            {
                return ios_base::fmtflags(0);
            }


246
        // Read Functions
247
248

            //- Return next token from stream
249
            virtual Istream& read(token& tok);
250
251

            //- Read a character
henry's avatar
henry committed
252
            virtual Istream& read(char&);
253
254

            //- Read a word
henry's avatar
henry committed
255
            virtual Istream& read(word&);
256
257

            // Read a string (including enclosing double-quotes)
henry's avatar
henry committed
258
            virtual Istream& read(string&);
259
260

            //- Read a label
henry's avatar
henry committed
261
            virtual Istream& read(label&);
262
263

            //- Read a floatScalar
henry's avatar
henry committed
264
            virtual Istream& read(floatScalar&);
265
266

            //- Read a doubleScalar
henry's avatar
henry committed
267
            virtual Istream& read(doubleScalar&);
268
269

            //- Read binary block
270
271
272
273
274
275
276
277
            //  \note Not implemented
            virtual Istream& read(char* data, std::streamsize);

            //- Low-level raw binary read
            //  \note Not implemented
            virtual Istream& readRaw(char* data, std::streamsize count);

            //- Start of low-level raw binary read
278
            virtual bool beginRawRead()
279
280
281
282
283
            {
                return false;
            }

            //- End of low-level raw binary read
284
            virtual bool endRawRead()
285
286
287
            {
                return false;
            }
288

289
290
            //- Rewind the stream so that it may be read again
            virtual void rewind();
291

292
293
294
295
296
            //- Move the tokenIndex to the specified position.
            //  Using seek(0) is identical to rewind.
            //  Using seek(-1) moves to the end.
            void seek(label pos);

297
298
299

        // Edit

300
301
302
303
304
305
306
307
            //- Copy append a token at the current tokenIndex,
            //- incrementing the index.
            void append(const token& t, const bool lazy);

            //- Move append a token at the current tokenIndex,
            //- incrementing the index.
            void append(token&& t, const bool lazy);

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
            //- Copy append a tokenList at the current tokenIndex,
            //- incrementing the index.
            //
            //  \param newTokens the list of tokens to copy append
            //  \param lazy leaves any excess capacity for further appends.
            //      The caller will be responsible for resizing later.
            void append(const tokenList& newTokens, const bool lazy);

            //- Move append a tokenList at the current tokenIndex,
            //- incrementing the index.
            //
            //  \param newTokens the list of tokens to move append
            //  \param lazy leaves any excess capacity for further appends.
            //      The caller will be responsible for resizing later.
            void append(tokenList&& newTokens, const bool lazy);

324
325
326
327
328
329
330
            //- Set flags of stream
            ios_base::fmtflags flags(const ios_base::fmtflags)
            {
                return ios_base::fmtflags(0);
            }


331
        // Output
332

333
334
            //- Print description of stream to Ostream
            void print(Ostream& os) const;
335
336
337
338

            //- Concatenate tokens into a space-separated std::string.
            //- The resulting string may contain quote characters.
            std::string toString() const;
339
340
341
342
343
344
345
346
347
348
349
350
};


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#endif

// ************************************************************************* //