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

13
14
15
16
    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.
17
18
19
20
21
22
23

    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
24
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
25
26
27
28
29

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

#include "error.H"
#include "ITstream.H"
30
#include "StringStream.H"
31
#include "UIListStream.H"
32

33
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
34

35
Foam::label Foam::ITstream::parseStream(ISstream& is, tokenList& tokens)
36
{
37
    label nTok = 0;
38

39
    tokens.clear();
40
    tokens.resize(64, token());
41

42
    token tok;
43
44
    while (!is.read(tok).bad() && tok.good())
    {
45
        tokens.newElmt(nTok++) = std::move(tok);
46
47
    }

48
    tokens.resize(nTok);
49

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    return nTok;
}


Foam::tokenList Foam::ITstream::parse
(
    const UList<char>& input,
    streamFormat format
)
{
    UIListStream is(input, format, IOstream::currentVersion);

    tokenList tokens;
    parseStream(is, tokens);
    return tokens;
}


Foam::tokenList Foam::ITstream::parse
(
    const std::string& input,
    streamFormat format
)
{
    UIListStream is
    (
        input.data(),
        input.size(),
        format,
        IOstream::currentVersion
    );

    tokenList tokens;
    parseStream(is, tokens);
    return tokens;
}


Foam::tokenList Foam::ITstream::parse
(
    const char* input,
    streamFormat format
)
{
    UIListStream is(input, strlen(input), format, IOstream::currentVersion);

    tokenList tokens;
    parseStream(is, tokens);
    return tokens;
99
100
101
102
103
104
105
106
107
108
109
110
111
112
}


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::ITstream::ITstream
(
    const string& name,
    const UList<char>& input,
    streamFormat format,
    versionNumber version
)
:
    Istream(format, version),
113
    tokenList(),
114
115
116
117
118
    name_(name),
    tokenIndex_(0)
{
    UIListStream is(input, format, version);

119
120
    parseStream(is, static_cast<tokenList&>(*this));
    ITstream::rewind();
121
122
123
124
125
126
127
128
129
130
131
132
}


Foam::ITstream::ITstream
(
    const string& name,
    const std::string& input,
    streamFormat format,
    versionNumber version
)
:
    Istream(format, version),
133
    tokenList(),
134
135
136
137
138
    name_(name),
    tokenIndex_(0)
{
    UIListStream is(input.data(), input.size(), format, version);

139
140
    parseStream(is, static_cast<tokenList&>(*this));
    ITstream::rewind();
141
142
143
144
145
146
147
148
149
150
151
152
}


Foam::ITstream::ITstream
(
    const string& name,
    const char* input,
    streamFormat format,
    versionNumber version
)
:
    Istream(format, version),
153
    tokenList(),
154
155
156
    name_(name),
    tokenIndex_(0)
{
157
    UIListStream is(input, strlen(input), format, version);
158

159
160
    parseStream(is, static_cast<tokenList&>(*this));
    ITstream::rewind();
161
162
163
164
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
165

Mark Olesen's avatar
Mark Olesen committed
166
void Foam::ITstream::print(Ostream& os) const
167
{
168
    os  << "ITstream : " << name_.c_str() << ", line ";
Mark Olesen's avatar
Mark Olesen committed
169
170
171

    if (size())
    {
172
173
174
        os  << tokenList::first().lineNumber();

        if (tokenList::first().lineNumber() < tokenList::last().lineNumber())
Mark Olesen's avatar
Mark Olesen committed
175
        {
176
            os  << '-' << tokenList::last().lineNumber();
Mark Olesen's avatar
Mark Olesen committed
177
178
179
180
        }
    }
    else
    {
181
        os  << lineNumber();
Mark Olesen's avatar
Mark Olesen committed
182
183
    }

184
185
    os  << ", ";

Mark Olesen's avatar
Mark Olesen committed
186
187
    IOstream::print(os);
}
188
189


190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
std::string Foam::ITstream::toString() const
{
    OStringStream buf;

    const tokenList& tokens = *this;

    label len = tokens.size();

    for (const token& tok : tokens)
    {
        buf << tok;

        if (--len)
        {
            buf << ' ';
        }
    }

    return buf.str();
}


212
Foam::Istream& Foam::ITstream::read(token& tok)
213
214
{
    // Return the put back token if it exists
215
    if (Istream::getBack(tok))
216
    {
217
        lineNumber_ = tok.lineNumber();
218
219
220
221
222
        return *this;
    }

    if (tokenIndex_ < size())
    {
223
224
        tok = operator[](tokenIndex_++);
        lineNumber_ = tok.lineNumber();
225
226
227
228
229
230
231
232
233
234

        if (tokenIndex_ == size())
        {
            setEof();
        }
    }
    else
    {
        if (eof())
        {
235
236
            FatalIOErrorInFunction(*this)
                << "attempt to read beyond EOF"
237
238
239
240
241
242
243
244
                << exit(FatalIOError);
            setBad();
        }
        else
        {
            setEof();
        }

245
        tok.reset();
Mark Olesen's avatar
Mark Olesen committed
246

247
248
        if (size())
        {
249
            tok.lineNumber() = tokenList::last().lineNumber();
250
251
252
        }
        else
        {
253
            tok.lineNumber() = lineNumber();
254
255
256
257
258
259
260
        }
    }

    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
261
Foam::Istream& Foam::ITstream::read(char&)
262
{
263
    NotImplemented;
264
265
266
    return *this;
}

Mark Olesen's avatar
Mark Olesen committed
267
268

Foam::Istream& Foam::ITstream::read(word&)
269
{
270
    NotImplemented;
271
272
273
274
    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
275
Foam::Istream& Foam::ITstream::read(string&)
276
{
277
    NotImplemented;
278
279
280
281
    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
282
Foam::Istream& Foam::ITstream::read(label&)
283
{
284
    NotImplemented;
285
286
287
288
    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
289
Foam::Istream& Foam::ITstream::read(floatScalar&)
290
{
291
    NotImplemented;
292
293
294
295
    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
296
Foam::Istream& Foam::ITstream::read(doubleScalar&)
297
{
298
    NotImplemented;
299
300
301
302
    return *this;
}


303
304
305
306
307
308
309
Foam::Istream& Foam::ITstream::readRaw(char*, std::streamsize)
{
    NotImplemented;
    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
310
Foam::Istream& Foam::ITstream::read(char*, std::streamsize)
311
{
312
    NotImplemented;
313
314
315
316
    return *this;
}


317
void Foam::ITstream::rewind()
318
319
{
    tokenIndex_ = 0;
320
    lineNumber_ = 0;
321
322
323

    if (size())
    {
Mark Olesen's avatar
Mark Olesen committed
324
        lineNumber_ = tokenList::first().lineNumber();
325
326
    }

327
    setOpened();
328
329
330
331
332
    setGood();
}


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