ITstream.C 6.1 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 "UIListStream.H"
31

32
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
33

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

38
39
    tokens.clear();
    tokens.setSize(64, token::undefinedToken);
40

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

47
    tokens.setSize(nTok);
48

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
    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;
98
99
100
101
102
103
104
105
106
107
108
109
110
111
}


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

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

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


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

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


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

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


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
164

Mark Olesen's avatar
Mark Olesen committed
165
void Foam::ITstream::print(Ostream& os) const
166
{
Mark Olesen's avatar
Mark Olesen committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    os  << "ITstream : " << name_.c_str();

    if (size())
    {
        if (begin()->lineNumber() == rbegin()->lineNumber())
        {
            os  << ", line " << begin()->lineNumber() << ", ";
        }
        else
        {
            os  << ", lines " << begin()->lineNumber()
                << '-' << rbegin()->lineNumber() << ", ";
        }
    }
    else
    {
        os  << ", line " << lineNumber() << ", ";
    }

    IOstream::print(os);
}
188
189


190
Foam::Istream& Foam::ITstream::read(token& tok)
191
192
{
    // Return the put back token if it exists
193
    if (Istream::getBack(tok))
194
    {
195
        lineNumber_ = tok.lineNumber();
196
197
198
199
200
        return *this;
    }

    if (tokenIndex_ < size())
    {
201
202
        tok = operator[](tokenIndex_++);
        lineNumber_ = tok.lineNumber();
203
204
205
206
207
208
209
210
211
212

        if (tokenIndex_ == size())
        {
            setEof();
        }
    }
    else
    {
        if (eof())
        {
213
214
            FatalIOErrorInFunction(*this)
                << "attempt to read beyond EOF"
215
216
217
218
219
220
221
222
                << exit(FatalIOError);
            setBad();
        }
        else
        {
            setEof();
        }

223
        tok = token::undefinedToken;
Mark Olesen's avatar
Mark Olesen committed
224

225
226
        if (size())
        {
227
            tok.lineNumber() = tokenList::last().lineNumber();
228
229
230
        }
        else
        {
231
            tok.lineNumber() = lineNumber();
232
233
234
235
236
237
238
        }
    }

    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
239
Foam::Istream& Foam::ITstream::read(char&)
240
{
241
    NotImplemented;
242
243
244
    return *this;
}

Mark Olesen's avatar
Mark Olesen committed
245
246

Foam::Istream& Foam::ITstream::read(word&)
247
{
248
    NotImplemented;
249
250
251
252
    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
253
Foam::Istream& Foam::ITstream::read(string&)
254
{
255
    NotImplemented;
256
257
258
259
    return *this;
}


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


Mark Olesen's avatar
Mark Olesen committed
267
Foam::Istream& Foam::ITstream::read(floatScalar&)
268
{
269
    NotImplemented;
270
271
272
273
    return *this;
}


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


281
282
283
284
285
286
287
Foam::Istream& Foam::ITstream::readRaw(char*, std::streamsize)
{
    NotImplemented;
    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
288
Foam::Istream& Foam::ITstream::read(char*, std::streamsize)
289
{
290
    NotImplemented;
291
292
293
294
    return *this;
}


295
void Foam::ITstream::rewind()
296
297
{
    tokenIndex_ = 0;
298
    lineNumber_ = 0;
299
300
301

    if (size())
    {
Mark Olesen's avatar
Mark Olesen committed
302
        lineNumber_ = tokenList::first().lineNumber();
303
304
    }

305
    setOpened();
306
307
308
309
310
    setGood();
}


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