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

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

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

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

#include "error.H"
#include "ITstream.H"
28
#include "UIListStream.H"
29

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
98
99
100
101
102
103
104
105
106
107
108
109
110
// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

void Foam::ITstream::toTokenList(ISstream& is)
{
    tokenIndex_ = 0;

    token tok;

    while (!is.read(tok).bad() && tok.good())
    {
        newElmt(tokenIndex()++) = std::move(tok);
    }

    tokenList::setSize(tokenIndex());

    setOpened();
    ITstream::rewind();
}


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

Foam::ITstream::ITstream
(
    const string& name,
    const UList<char>& input,
    streamFormat format,
    versionNumber version
)
:
    Istream(format, version),
    tokenList(16, token::undefinedToken),
    name_(name),
    tokenIndex_(0)
{
    UIListStream is(input, format, version);

    toTokenList(is);
}


Foam::ITstream::ITstream
(
    const string& name,
    const std::string& input,
    streamFormat format,
    versionNumber version
)
:
    Istream(format, version),
    tokenList(16, token::undefinedToken),
    name_(name),
    tokenIndex_(0)
{
    UIListStream is(input.data(), input.size(), format, version);

    toTokenList(is);
}


Foam::ITstream::ITstream
(
    const string& name,
    const char* input,
    streamFormat format,
    versionNumber version
)
:
    Istream(format, version),
    tokenList(16, token::undefinedToken),
    name_(name),
    tokenIndex_(0)
{
    const size_t len = strlen(input);
    UIListStream is(input, len, format, version);

    toTokenList(is);
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
111

Mark Olesen's avatar
Mark Olesen committed
112
void Foam::ITstream::print(Ostream& os) const
113
{
Mark Olesen's avatar
Mark Olesen committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    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);
}
135
136


137
Foam::Istream& Foam::ITstream::read(token& tok)
138
139
{
    // Return the put back token if it exists
140
    if (Istream::getBack(tok))
141
    {
142
        lineNumber_ = tok.lineNumber();
143
144
145
146
147
        return *this;
    }

    if (tokenIndex_ < size())
    {
148
149
        tok = operator[](tokenIndex_++);
        lineNumber_ = tok.lineNumber();
150
151
152
153
154
155
156
157
158
159

        if (tokenIndex_ == size())
        {
            setEof();
        }
    }
    else
    {
        if (eof())
        {
160
            FatalIOErrorInFunction
161
162
163
164
165
166
167
168
169
170
171
172
            (
                *this
            )   << "attempt to read beyond EOF"
                << exit(FatalIOError);

            setBad();
        }
        else
        {
            setEof();
        }

173
        tok = token::undefinedToken;
Mark Olesen's avatar
Mark Olesen committed
174

175
176
        if (size())
        {
177
            tok.lineNumber() = tokenList::last().lineNumber();
178
179
180
        }
        else
        {
181
            tok.lineNumber() = lineNumber();
182
183
184
185
186
187
188
        }
    }

    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
189
Foam::Istream& Foam::ITstream::read(char&)
190
{
191
    NotImplemented;
192
193
194
    return *this;
}

Mark Olesen's avatar
Mark Olesen committed
195
196

Foam::Istream& Foam::ITstream::read(word&)
197
{
198
    NotImplemented;
199
200
201
202
    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
203
Foam::Istream& Foam::ITstream::read(string&)
204
{
205
    NotImplemented;
206
207
208
209
    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
210
Foam::Istream& Foam::ITstream::read(label&)
211
{
212
    NotImplemented;
213
214
215
216
    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
217
Foam::Istream& Foam::ITstream::read(floatScalar&)
218
{
219
    NotImplemented;
220
221
222
223
    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
224
Foam::Istream& Foam::ITstream::read(doubleScalar&)
225
{
226
    NotImplemented;
227
228
229
230
    return *this;
}


Mark Olesen's avatar
Mark Olesen committed
231
Foam::Istream& Foam::ITstream::read(char*, std::streamsize)
232
{
233
    NotImplemented;
234
235
236
237
    return *this;
}


238
void Foam::ITstream::rewind()
239
240
{
    tokenIndex_ = 0;
241
    lineNumber_ = 0;
242
243
244

    if (size())
    {
Mark Olesen's avatar
Mark Olesen committed
245
        lineNumber_ = tokenList::first().lineNumber();
246
247
248
249
250
251
252
    }

    setGood();
}


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