initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / db / IOstreams / Sstreams / ISread.C
blobffb516365a5dcc41eb903787a73d4ccc10df3d4a
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
26     Read token and binary block from ISstream
28 \*---------------------------------------------------------------------------*/
30 #include "ISstream.H"
31 #include "int.H"
32 #include "token.H"
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 namespace Foam
39 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
41 Istream& ISstream::read(char& c)
43     c = nextValid();
44     return *this;
48 Istream& ISstream::read(word& w)
50     static const int MAX_WORD = 1024;
51     static char wordBuffer[MAX_WORD];
53     register int i = 0;
54     register int bc = 0;
55     char c;
57     while (get(c) && word::valid(c))
58     {
59         if (fail())
60         {
61             if (i < MAX_WORD-1)
62             {
63                 wordBuffer[i] = '\0';
64             }
65             else
66             {
67                 wordBuffer[MAX_WORD-1] = '\0';
68             }
70             FatalIOErrorIn("ISstream::read(word&)", *this)
71                 << "problem while reading word '" << wordBuffer << '\''
72                 << exit(FatalIOError);
74             return *this;
75         }
77         if (i >= MAX_WORD)
78         {
79             wordBuffer[MAX_WORD-1] = '\0';
81             FatalIOErrorIn("ISstream::read(word&)", *this)
82                 << "word " << wordBuffer << " too long" << endl
83                 << "    maximum number of characters allowed = " << MAX_WORD
84                 << exit(FatalIOError);
86             return *this;
87         }
89         if (c == token::BEGIN_LIST)
90         {
91             bc++;
92         }
93         else if (c == token::END_LIST)
94         {
95             bc--;
97             if (bc == -1)
98             {
99                 break;
100             }
101         }
103         wordBuffer[i++] = c;
104     }
106     if (i == 0)
107     {
108         FatalIOErrorIn("ISstream::read(word&)", *this)
109             << "invalid first character found : " << c
110             << exit(FatalIOError);
111     }
113     wordBuffer[i] = '\0';        // Terminator.
114     w = wordBuffer;
115     putback(c);
117     return *this;
121 Istream& ISstream::read(string& s)
123     static const int MAX_STR = 1024;
124     static const int MAX_ERROR_STR = 80;
125     static char stringBuffer[MAX_STR];
127     char c;
129     if (!get(c))
130     {
131         stringBuffer[0] = '\0';
133         FatalIOErrorIn("ISstream::read(string& s)", *this)
134             << "cannot read start of string"
135             << exit(FatalIOError);
137         return *this;
138     }
140     if (c != token::BEGIN_STRING)
141     {
142         stringBuffer[0] = '\0';
144         FatalIOErrorIn("ISstream::read(string& s)", *this)
145             << "Incorrect start of string character"
146             << exit(FatalIOError);
148         return *this;
149     }
152     register int i = 0;
153     bool escape = false;
155     while (get(c))
156     {
157         if (c == token::END_STRING && !escape)
158         {
159             stringBuffer[i] = '\0';
160             s = stringBuffer;
161             return *this;
162         }
164         if (c == '\n' && !escape)
165         {
166             stringBuffer[i] = '\0';
167             stringBuffer[MAX_ERROR_STR] = '\0';
169             FatalIOErrorIn("ISstream::read(string& s)", *this)
170                 << "found a '\\n' while reading string \""
171                 << stringBuffer << '"'
172                 << exit(FatalIOError);
174             return *this;
175         }
177         if (c != '\\')
178         {
179             escape = false;
181             stringBuffer[i] = c;
183             if (i++ == MAX_STR)
184             {
185                 stringBuffer[MAX_STR - 1] = '\0';
187                 FatalIOErrorIn("ISstream::read(string& s)", *this)
188                     << " string " << stringBuffer << "is too long" << endl
189                     << "    maximum number of characters allowed = " << MAX_STR
190                     << exit(FatalIOError);
192                 return *this;
193             }
194         }
195         else
196         {
197             escape = true;
198         }
199     }
201     stringBuffer[i] = '\0';
202     stringBuffer[MAX_ERROR_STR] = '\0';
204     FatalIOErrorIn("ISstream::read(string& s)", *this)
205         << "problem while reading string \"" << stringBuffer << "...\""
206         << exit(FatalIOError);
208     return *this;
212 Istream& ISstream::read(label& l)
214     is_ >> l;
215     setState(is_.rdstate());
216     return *this;
220 Istream& ISstream::read(floatScalar& s)
222     is_ >> s;
223     setState(is_.rdstate());
224     return *this;
228 Istream& ISstream::read(doubleScalar& s)
230     is_ >> s;
231     setState(is_.rdstate());
232     return *this;
236 // read binary block
237 Istream& ISstream::read(char* buf, std::streamsize count)
239     if (format() != BINARY)
240     {
241         FatalIOErrorIn("ISstream::read(char*, std::streamsize)", *this)
242             << "stream format not binary"
243             << exit(FatalIOError);
244     }
246     readBegin("binaryBlock");
247     is_.read(buf, count);
248     readEnd("binaryBlock");
250     setState(is_.rdstate());
252     return *this;
256 //- Rewind the ISstream so that it may be read again
257 Istream& ISstream::rewind()
259     stream().rdbuf()->pubseekpos(0);
261     return *this;
265 // Set flags of output stream
266 ios_base::fmtflags ISstream::flags() const
268     return is_.flags();
272 // Set flags of given field of output stream
273 ios_base::fmtflags ISstream::flags(const ios_base::fmtflags f)
275     return is_.flags(f);
279 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
281 } // End namespace Foam
283 // ************************************************************************* //