ITstream.C 8.09 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-2015 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

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

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

34
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
35

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

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

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

49
    tokens.resize(nTok);
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
99
    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;
100
101
102
}


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

void Foam::ITstream::reserveCapacity
(
    const label nElem,
    const bool lazy
)
{
    if (lazy)
    {
        // Reserve - leave excess capacity for further appends

        label n = tokenList::size();

        if (nElem >= n)
        {
            do
            {
                n *= 2;
            }
            while (nElem >= n);

            tokenList::resize(n);
        }
    }
    else
    {
        // Strict capacity
        tokenList::resize(nElem);
    }
}


136
137
138
139
140
141
142
143
144
145
146
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::ITstream::ITstream
(
    const string& name,
    const UList<char>& input,
    streamFormat format,
    versionNumber version
)
:
    Istream(format, version),
147
    tokenList(),
148
149
150
151
152
    name_(name),
    tokenIndex_(0)
{
    UIListStream is(input, format, version);

153
154
    parseStream(is, static_cast<tokenList&>(*this));
    ITstream::rewind();
155
156
157
158
159
160
161
162
163
164
165
166
}


Foam::ITstream::ITstream
(
    const string& name,
    const std::string& input,
    streamFormat format,
    versionNumber version
)
:
    Istream(format, version),
167
    tokenList(),
168
169
170
171
172
    name_(name),
    tokenIndex_(0)
{
    UIListStream is(input.data(), input.size(), format, version);

173
174
    parseStream(is, static_cast<tokenList&>(*this));
    ITstream::rewind();
175
176
177
178
179
180
181
182
183
184
185
186
}


Foam::ITstream::ITstream
(
    const string& name,
    const char* input,
    streamFormat format,
    versionNumber version
)
:
    Istream(format, version),
187
    tokenList(),
188
189
190
    name_(name),
    tokenIndex_(0)
{
191
    UIListStream is(input, strlen(input), format, version);
192

193
194
    parseStream(is, static_cast<tokenList&>(*this));
    ITstream::rewind();
195
196
197
198
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
199

Mark Olesen's avatar
Mark Olesen committed
200
void Foam::ITstream::print(Ostream& os) const
201
{
202
    os  << "ITstream : " << name_.c_str() << ", line ";
Mark Olesen's avatar
Mark Olesen committed
203
204
205

    if (size())
    {
206
207
208
        os  << tokenList::first().lineNumber();

        if (tokenList::first().lineNumber() < tokenList::last().lineNumber())
Mark Olesen's avatar
Mark Olesen committed
209
        {
210
            os  << '-' << tokenList::last().lineNumber();
Mark Olesen's avatar
Mark Olesen committed
211
212
213
214
        }
    }
    else
    {
215
        os  << lineNumber();
Mark Olesen's avatar
Mark Olesen committed
216
217
    }

218
219
    os  << ", ";

Mark Olesen's avatar
Mark Olesen committed
220
221
    IOstream::print(os);
}
222
223


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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();
}


246
Foam::Istream& Foam::ITstream::read(token& tok)
247
248
{
    // Return the put back token if it exists
249
    if (Istream::getBack(tok))
250
    {
251
        lineNumber_ = tok.lineNumber();
252
253
254
255
256
        return *this;
    }

    if (tokenIndex_ < size())
    {
257
258
        tok = operator[](tokenIndex_++);
        lineNumber_ = tok.lineNumber();
259
260
261
262
263
264
265
266
267
268

        if (tokenIndex_ == size())
        {
            setEof();
        }
    }
    else
    {
        if (eof())
        {
269
270
            FatalIOErrorInFunction(*this)
                << "attempt to read beyond EOF"
271
272
273
274
275
276
277
278
                << exit(FatalIOError);
            setBad();
        }
        else
        {
            setEof();
        }

279
        tok.reset();
Mark Olesen's avatar
Mark Olesen committed
280

281
282
        if (size())
        {
283
            tok.lineNumber() = tokenList::last().lineNumber();
284
285
286
        }
        else
        {
287
            tok.lineNumber() = lineNumber();
288
289
290
291
292
293
294
        }
    }

    return *this;
}


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

Mark Olesen's avatar
Mark Olesen committed
301
302

Foam::Istream& Foam::ITstream::read(word&)
303
{
304
    NotImplemented;
305
306
307
308
    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
309
Foam::Istream& Foam::ITstream::read(string&)
310
{
311
    NotImplemented;
312
313
314
315
    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
316
Foam::Istream& Foam::ITstream::read(label&)
317
{
318
    NotImplemented;
319
320
321
322
    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
323
Foam::Istream& Foam::ITstream::read(floatScalar&)
324
{
325
    NotImplemented;
326
327
328
329
    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
330
Foam::Istream& Foam::ITstream::read(doubleScalar&)
331
{
332
    NotImplemented;
333
334
335
336
    return *this;
}


337
338
339
340
341
342
343
Foam::Istream& Foam::ITstream::readRaw(char*, std::streamsize)
{
    NotImplemented;
    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
344
Foam::Istream& Foam::ITstream::read(char*, std::streamsize)
345
{
346
    NotImplemented;
347
348
349
350
    return *this;
}


351
void Foam::ITstream::rewind()
352
{
353
354
355
356
357
358
    seek(0);
}


void Foam::ITstream::seek(label pos)
{
359
    lineNumber_ = 0;
360
    tokenList& toks = *this;
361

362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
    if (!pos)
    {
        // Seek begin (rewind)
        tokenIndex_ = 0;

        if (!toks.empty())
        {
            lineNumber_ = toks.first().lineNumber();
        }

        setOpened();
        setGood();
    }
    else if (pos < 0 || pos >= toks.size())
    {
        // Seek end or seek is out of range
        tokenIndex_ = toks.size();

        if (!toks.empty())
        {
            lineNumber_ = toks.last().lineNumber();
        }

        setEof();
    }
    else
    {
        // Seek middle (from the beginning)
        tokenIndex_ = pos;

        if (!toks.empty())
        {
            lineNumber_ = toks[tokenIndex_].lineNumber();
        }

        setOpened();
        setGood();
    }
}


void Foam::ITstream::append(const tokenList& newTokens, const bool lazy)
{
    reserveCapacity(tokenIndex_ + newTokens.size(), lazy);
    tokenList& toks = *this;

    for (const token& t : newTokens)
    {
        toks[tokenIndex_] = t;  // copy append
        ++tokenIndex_;
    }
}


void Foam::ITstream::append(tokenList&& newTokens, const bool lazy)
{
    reserveCapacity(tokenIndex_ + newTokens.size(), lazy);
    tokenList& toks = *this;

    for (token& t : newTokens)
422
    {
423
424
        toks[tokenIndex_] = std::move(t);  // move append
        ++tokenIndex_;
425
426
    }

427
    newTokens.clear();
428
429
430
431
}


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