ITstream.C 8.57 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
// * * * * * * * * * * * * * 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();

117
        if (nElem > n)
118
        {
119
120
            if (!n) n = 1;  // Avoid dead-lock when starting from zero-sized

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
            do
            {
                n *= 2;
            }
            while (nElem >= n);

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


138
139
140
141
142
143
144
145
146
147
148
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

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

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


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

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


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

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


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
201

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

    if (size())
    {
208
209
210
        os  << tokenList::first().lineNumber();

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

220
221
    os  << ", ";

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


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


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

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

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

281
        tok.reset();
Mark Olesen's avatar
Mark Olesen committed
282

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

    return *this;
}


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

Mark Olesen's avatar
Mark Olesen committed
303
304

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


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


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


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


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


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


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


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


void Foam::ITstream::seek(label pos)
{
361
    lineNumber_ = 0;
362
    tokenList& toks = *this;
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
    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();
    }
}


405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
void Foam::ITstream::append(const token& t, const bool lazy)
{
    reserveCapacity(tokenIndex_ + 1, lazy);
    tokenList& toks = *this;

    toks[tokenIndex_] = t;  // copy append
    ++tokenIndex_;
}


void Foam::ITstream::append(token&& t, const bool lazy)
{
    reserveCapacity(tokenIndex_ + 1, lazy);
    tokenList& toks = *this;

    toks[tokenIndex_] = std::move(t);  // move append
    ++tokenIndex_;
}


425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
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)
444
    {
445
446
        toks[tokenIndex_] = std::move(t);  // move append
        ++tokenIndex_;
447
448
    }

449
    newTokens.clear();
450
451
452
453
}


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