ITstream.C 6.39 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
{
Mark Olesen's avatar
Mark Olesen committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    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);
}
189
190


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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();
}


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

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

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

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

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

    return *this;
}


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

Mark Olesen's avatar
Mark Olesen committed
268
269

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


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


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


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


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


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


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


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

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

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


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